But half of the pain is putting it into a sparse result (which itself is
weird, because sum returns a dense matrix), since we can't access
sp.csr_matrix directly without importing them locally in the function, due
to dependency cycles.

On 29 January 2014 10:24, Joel Nothman <jnothman@student.usyd.edu.au> wrote:

> Firstly, I don't think using `out=out[mask]` could possibly work, since
> the masking is a copying operation.
>
> Secondly, I am trying something very similar that, but I'd like to
> encapsulate part of it in a function on csr and csc called
> "reduce_minor(ufunc)". This can also be used in `sum` where axis is minor,
> which gives gains over matrix multiplication for a large minor axis.
>
>
> On 29 January 2014 09:56, Jaime <notifications@github.com> wrote:
>
>>  In scipy/sparse/data.py:
>>
>> > -        elif axis == 1:
>> > -            mat = self.tocsc()
>> > -            if not mat.has_sorted_indices:
>> > -                mat.sort_indices()
>> > +        N = self.shape[axis]
>> > +        if N == 0:
>> > +            raise ValueError("zero-size array to reduction operation")
>> > +
>> > +        mat = self.tocsc() if axis == 0 else self.tocsr()
>> > +        mat.sum_duplicates()
>> > +
>> > +        indptr = mat.indptr
>> > +        zero = self.dtype.type(0)
>> > +        out = np.zeros(len(indptr) - 1, dtype=self.dtype)
>> > +        # can't use indices > data length with reduceat`
>> > +        trunc = np.searchsorted(indptr, indptr[-1])
>>
>> Are you sure you don't want to use boolean indexing before doing
>> .reduceat? Lines 118 through 125 can be rewritten more compactly,
>> probably also faster, and I think more readable, as:
>>
>> nnz = np.diff(indptr)
>> # look only at rows with non-zero entries
>> mask = nnz > 0
>> # compute min_or_max of non-zero entries in each row
>> min_or_max.reduceat(mat.data, indptr[mask], 0, None, out[mask])
>> # the min_or_max of non-full rows may be zero
>> mask &= nnz < N
>> out[mask] = min_or_max(out[mask], zero)
>>
>> --
>> Reply to this email directly or view it on GitHub<https://github.com/scipy/scipy/pull/3248/files#r9249448>
>> .
>>
>
>