Skip to content

Commit

Permalink
ENH: sparse: specialize upcast to upcast_char taking dtype.char as in…
Browse files Browse the repository at this point in the history
…put (faster)
  • Loading branch information
pv committed Oct 1, 2011
1 parent 06cd6e5 commit fa26626
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
10 changes: 6 additions & 4 deletions scipy/sparse/compressed.py
Expand Up @@ -10,8 +10,8 @@
from base import spmatrix, isspmatrix, SparseEfficiencyWarning
from data import _data_matrix
import sparsetools
from sputils import upcast, to_native, isdense, isshape, getdtype, \
isscalarlike, isintlike
from sputils import upcast, upcast_char, to_native, isdense, isshape, \
getdtype, isscalarlike, isintlike


class _cs_matrix(_data_matrix):
Expand Down Expand Up @@ -251,7 +251,8 @@ def _mul_vector(self, other):
M,N = self.shape

# output array
result = np.zeros(M, dtype=upcast(self.dtype, other.dtype))
result = np.zeros(M, dtype=upcast_char(self.dtype.char,
other.dtype.char))

# csr_matvec or csc_matvec
fn = getattr(sparsetools,self.format + '_matvec')
Expand All @@ -264,7 +265,8 @@ def _mul_multivector(self, other):
M,N = self.shape
n_vecs = other.shape[1] #number of column vectors

result = np.zeros((M,n_vecs), dtype=upcast(self.dtype,other.dtype) )
result = np.zeros((M,n_vecs), dtype=upcast_char(self.dtype.char,
other.dtype.char))

# csr_matvecs or csc_matvecs
fn = getattr(sparsetools,self.format + '_matvecs')
Expand Down
5 changes: 3 additions & 2 deletions scipy/sparse/coo.py
Expand Up @@ -11,7 +11,7 @@
from sparsetools import coo_tocsr, coo_todense, coo_matvec
from base import isspmatrix
from data import _data_matrix
from sputils import upcast, to_native, isshape, getdtype, isintlike
from sputils import upcast, upcast_char, to_native, isshape, getdtype, isintlike

class coo_matrix(_data_matrix):
"""
Expand Down Expand Up @@ -369,7 +369,8 @@ def _with_data(self,data,copy=True):

def _mul_vector(self, other):
#output array
result = np.zeros( self.shape[0], dtype=upcast(self.dtype,other.dtype) )
result = np.zeros( self.shape[0], dtype=upcast_char(self.dtype.char,
other.dtype.char) )
coo_matvec(self.nnz, self.row, self.col, self.data, other, result)
return result

Expand Down
5 changes: 3 additions & 2 deletions scipy/sparse/dia.py
Expand Up @@ -8,7 +8,7 @@

from base import isspmatrix, _formats
from data import _data_matrix
from sputils import isshape, upcast, getdtype
from sputils import isshape, upcast, upcast_char, getdtype
from sparsetools import dia_matvec

class dia_matrix(_data_matrix):
Expand Down Expand Up @@ -165,7 +165,8 @@ def getnnz(self):
def _mul_vector(self, other):
x = other

y = np.zeros( self.shape[0], dtype=upcast(self.dtype,x.dtype))
y = np.zeros( self.shape[0], dtype=upcast_char(self.dtype.char,
x.dtype.char))

L = self.data.shape[1]

Expand Down
8 changes: 8 additions & 0 deletions scipy/sparse/sputils.py
Expand Up @@ -50,6 +50,14 @@ def upcast(*args):

raise TypeError('no supported conversion for types: %s' % args)

def upcast_char(*args):
"""Same as `upcast` but taking dtype.char as input (faster)."""
t = _upcast_memo.get(args)
if t is not None:
return t
t = upcast(*map(np.dtype, args))
_upcast_memo[args] = t
return t

def to_native(A):
return np.asarray(A,dtype=A.dtype.newbyteorder('native'))
Expand Down

0 comments on commit fa26626

Please sign in to comment.