diff --git a/scipy/sparse/compressed.py b/scipy/sparse/compressed.py index 971e765c1153..5fa0785afdbd 100644 --- a/scipy/sparse/compressed.py +++ b/scipy/sparse/compressed.py @@ -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') diff --git a/scipy/sparse/coo.py b/scipy/sparse/coo.py index 060eafc17d05..b88477d03710 100644 --- a/scipy/sparse/coo.py +++ b/scipy/sparse/coo.py @@ -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 diff --git a/scipy/sparse/dia.py b/scipy/sparse/dia.py index 0389ebb7f008..ed6a193545e7 100644 --- a/scipy/sparse/dia.py +++ b/scipy/sparse/dia.py @@ -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] diff --git a/scipy/sparse/sputils.py b/scipy/sparse/sputils.py index 982b385d4a46..e6cc39f626df 100644 --- a/scipy/sparse/sputils.py +++ b/scipy/sparse/sputils.py @@ -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'))