Yes, I'd considered something like that. Given that I don't think this is
code that needs to be run at high frequency, I've tried to minimise
obfuscation...


On 29 January 2014 07:38, Jaime <notifications@github.com> wrote:

>  In scipy/sparse/data.py:
>
> > -            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()
> > +
> > +        zero = self.dtype.type(0)
> > +        out_mat = lil_matrix((1, len(mat.indptr) - 1), dtype=self.dtype)
> > +        row = out_mat.rows[0]
> > +        data = out_mat.data[0]
> > +
> > +        for i, (start, stop) in enumerate(izip(mat.indptr, mat.indptr[1:])):
>
> See what you think of the following code, it computes the min or max over
> rows of a CSR matrix, using boolean indexing to minimize calculations. It
> may be possible to improve it by not considering rows with a single item,
> but I doubt there is much to improve there:
>
> def row_minmax(csr_mat, minmax='min'):
>     if minmax == 'min':
>         ufunc = np.minimum
>         compare = np.greater_equal
>     elif minmax == 'max':
>         ufunc = np.maximum
>         compare = np.less_equal
>     else:
>         raise ValueError('not \'min\' or \'max\'')
>
>     mask_indptr = np.diff(csr_mat.indptr) > 0 # rows not all zeros
>     new_data = ufunc.reduceat(csr_mat.data,
>                               csr_mat.indptr[mask_indptr]) # min (max) of non-zeros in rows not all zeros
>     mask_data = compare(new_data, 0) # wrong entries in new_data, should be zero
>     new_data = new_data[~mask_data] # remove them
>     mask_indptr[mask_indptr] ^= mask_data # set to False those rows in mask_indptr
>     return sps.csc_matrix((new_data,
>                            np.nonzero(mask_indptr)[0],
>                            [0, len(new_data)]), shape=(csr_mat.shape[0], 1))
>
> --
> Reply to this email directly or view it on GitHub<https://github.com/scipy/scipy/pull/3248/files#r9243435>
> .
>