<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , absolute_import <EOL> __all__ = [ ] <EOL> import numpy as np <EOL> from . base import spmatrix , _ufuncs_with_fixed_point_at_zero <EOL> from . sputils import isscalarlike <EOL> class _data_matrix ( spmatrix ) : <EOL> def __init__ ( self ) : <EOL> spmatrix . __init__ ( self ) <EOL> def _get_dtype ( self ) : <EOL> return self . data . dtype <EOL> def _set_dtype ( self , newtype ) : <EOL> self . data . dtype = newtype <EOL> dtype = property ( fget = _get_dtype , fset = _set_dtype ) <EOL> def _deduped_data ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . sum_duplicates ( ) <EOL> return self . data <EOL> def __abs__ ( self ) : <EOL> return self . _with_data ( abs ( self . _deduped_data ( ) ) ) <EOL> def _real ( self ) : <EOL> return self . _with_data ( self . data . real ) <EOL> def _imag ( self ) : <EOL> return self . _with_data ( self . data . imag ) <EOL> def __neg__ ( self ) : <EOL> return self . _with_data ( - self . data ) <EOL> def __imul__ ( self , other ) : <EOL> if isscalarlike ( other ) : <EOL> self . data *= other <EOL> return self <EOL> else : <EOL> return NotImplemented <EOL> def __itruediv__ ( self , other ) : <EOL> if isscalarlike ( other ) : <EOL> recip = <NUM_LIT:1.0> / other <EOL> self . data *= recip <EOL> return self <EOL> else : <EOL> return NotImplemented <EOL> def astype ( self , t ) : <EOL> return self . _with_data ( self . _deduped_data ( ) . astype ( t ) ) <EOL> def conj ( self ) : <EOL> return self . _with_data ( self . data . conj ( ) ) <EOL> def copy ( self ) : <EOL> return self . _with_data ( self . data . copy ( ) , copy = True ) <EOL> def count_nonzero ( self ) : <EOL> return np . count_nonzero ( self . _deduped_data ( ) ) <EOL> count_nonzero . __doc__ = spmatrix . count_nonzero . __doc__ <EOL> def power ( self , n , dtype = None ) : <EOL> """<STR_LIT>""" <EOL> if not isscalarlike ( n ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> data = self . _deduped_data ( ) <EOL> if dtype is not None : <EOL> data = data . astype ( dtype ) <EOL> return self . _with_data ( data ** n ) <EOL> def _mul_scalar ( self , other ) : <EOL> return self . _with_data ( self . data * other ) <EOL> for npfunc in _ufuncs_with_fixed_point_at_zero : <EOL> name = npfunc . __name__ <EOL> def _create_method ( op ) : <EOL> def method ( self ) : <EOL> result = op ( self . data ) <EOL> x = self . _with_data ( result , copy = True ) <EOL> return x <EOL> method . __doc__ = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , name ) ) <EOL> method . __name__ = name <EOL> return method <EOL> setattr ( _data_matrix , name , _create_method ( npfunc ) ) <EOL> class _minmax_mixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def _min_or_max_axis ( self , axis , min_or_max ) : <EOL> N = self . shape [ axis ] <EOL> if N == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> M = self . shape [ <NUM_LIT:1> - axis ] <EOL> mat = self . tocsc ( ) if axis == <NUM_LIT:0> else self . tocsr ( ) <EOL> mat . sum_duplicates ( ) <EOL> major_index , value = mat . _minor_reduce ( min_or_max ) <EOL> not_full = np . diff ( mat . indptr ) [ major_index ] < N <EOL> value [ not_full ] = min_or_max ( value [ not_full ] , <NUM_LIT:0> ) <EOL> mask = value != <NUM_LIT:0> <EOL> major_index = np . compress ( mask , major_index ) <EOL> value = np . compress ( mask , value ) <EOL> from . import coo_matrix <EOL> if axis == <NUM_LIT:0> : <EOL> return coo_matrix ( ( value , ( np . zeros ( len ( value ) ) , major_index ) ) , <EOL> dtype = self . dtype , shape = ( <NUM_LIT:1> , M ) ) <EOL> else : <EOL> return coo_matrix ( ( value , ( major_index , np . zeros ( len ( value ) ) ) ) , <EOL> dtype = self . dtype , shape = ( M , <NUM_LIT:1> ) ) <EOL> def _min_or_max ( self , axis , min_or_max ) : <EOL> if axis is None : <EOL> if <NUM_LIT:0> in self . shape : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> zero = self . dtype . type ( <NUM_LIT:0> ) <EOL> if self . nnz == <NUM_LIT:0> : <EOL> return zero <EOL> m = min_or_max . reduce ( self . _deduped_data ( ) . ravel ( ) ) <EOL> if self . nnz != np . product ( self . shape ) : <EOL> m = min_or_max ( zero , m ) <EOL> return m <EOL> if axis < <NUM_LIT:0> : <EOL> axis += <NUM_LIT:2> <EOL> if ( axis == <NUM_LIT:0> ) or ( axis == <NUM_LIT:1> ) : <EOL> return self . _min_or_max_axis ( axis , min_or_max ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def max ( self , axis = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _min_or_max ( axis , np . maximum ) <EOL> def min ( self , axis = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _min_or_max ( axis , np . minimum ) </s>
