Permalink
Browse files

ENH: sparse: specialize upcast to upcast_char taking dtype.char as in…

…put (faster)
  • Loading branch information...
1 parent 06cd6e5 commit fa266263a2c5d5049abb77f280c43c90aae602d0 @pv pv committed Oct 1, 2011
Showing with 20 additions and 8 deletions.
  1. +6 −4 scipy/sparse/compressed.py
  2. +3 −2 scipy/sparse/coo.py
  3. +3 −2 scipy/sparse/dia.py
  4. +8 −0 scipy/sparse/sputils.py
View
@@ -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):
@@ -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')
@@ -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')
View
@@ -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):
"""
@@ -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
View
@@ -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):
@@ -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]
View
@@ -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'))

0 comments on commit fa26626

Please sign in to comment.