[Scipy-svn] r2036 - trunk/Lib/sparse

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Jul 5 10:23:41 EDT 2006


Author: edschofield
Date: 2006-07-05 09:23:38 -0500 (Wed, 05 Jul 2006)
New Revision: 2036

Modified:
   trunk/Lib/sparse/sparse.py
Log:
Better handling of 0-d arrays in sparse matrix operations


Modified: trunk/Lib/sparse/sparse.py
===================================================================
--- trunk/Lib/sparse/sparse.py	2006-07-04 16:43:35 UTC (rev 2035)
+++ trunk/Lib/sparse/sparse.py	2006-07-05 14:23:38 UTC (rev 2036)
@@ -196,14 +196,14 @@
         return csc * other
 
     def __truediv__(self, other):
-        if isscalar(other):
+        if isscalarlike(other):
             return self * (1./other)
         else:
             raise NotImplementedError, "sparse matrix division not yet supported"
 
     def __div__(self, other):
         # Always do true division
-        if isscalar(other):
+        if isscalarlike(other):
             return self * (1./other)
         else:
             raise NotImplementedError, "sparse matrix division not yet supported"
@@ -634,7 +634,7 @@
     def __radd__(self, other):
         """ Function supporting the operation: self + other.
         """
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             raise NotImplementedError, 'adding a scalar to a CSC matrix is ' \
                     'not yet supported'
         elif isspmatrix(other):
@@ -659,7 +659,7 @@
             raise TypeError, "unsupported type for sparse matrix addition"
 
     def __add__(self, other):
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             raise NotImplementedError, 'adding a scalar to a CSC matrix is ' \
                     'not yet supported'
         elif isspmatrix(other):
@@ -686,7 +686,7 @@
     def __mul__(self, other):
         """ Scalar, vector, or matrix multiplication
         """
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             new.data *= other
             new.dtype = new.data.dtype
@@ -696,7 +696,7 @@
             return self.dot(other)
 
     def __rmul__(self, other):  # other * self
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             new.data = other * new.data
             new.dtype = new.data.dtype
@@ -719,7 +719,7 @@
         """ Element-by-element power (unless other is a scalar, in which
         case return the matrix power.)
         """
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             new.data = new.data ** other
             new.dtype = new.data.dtype
@@ -1218,7 +1218,7 @@
     
     def __add__(self, other):
         # First check if argument is a scalar
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             # Now we would add this scalar to every element.
             raise NotImplementedError, 'adding a scalar to a CSR matrix ' \
                     'is not yet supported'
@@ -1244,7 +1244,7 @@
     def __mul__(self, other):
         """ Scalar, vector, or matrix multiplication
         """
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             new.data = other * new.data         # allows type conversion
             new.dtype = new.data.dtype
@@ -1254,7 +1254,7 @@
             return self.dot(other)
 
     def __rmul__(self, other):  # other * self
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             new.data = other * new.data         # allows type conversion
             new.dtype = new.data.dtype
@@ -1277,7 +1277,7 @@
         """ Element-by-element power (unless other is a scalar, in which
         case return the matrix power.)
         """
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             new.data = new.data ** other
             new.dtype = new.data.dtype
@@ -1913,7 +1913,7 @@
 
     def __add__(self, other):
         # First check if argument is a scalar
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = dok_matrix(self.shape, dtype=self.dtype)
             # Add this scalar to every element.
             M, N = self.shape
@@ -1943,7 +1943,7 @@
 
     def __radd__(self, other):
         # First check if argument is a scalar
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = dok_matrix(self.shape, dtype=self.dtype)
             # Add this scalar to every element.
             M, N = self.shape
@@ -1975,7 +1975,7 @@
         return new
 
     def __mul__(self, other):           # self * other
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = dok_matrix(self.shape, dtype=self.dtype)
             # Multiply this scalar by every element.
             for (key, val) in self.iteritems():
@@ -1986,7 +1986,7 @@
             return self.dot(other)
 
     def __rmul__(self, other):          # other * self
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = dok_matrix(self.shape, dtype=self.dtype)
             # Multiply this scalar by every element.
             for (key, val) in self.iteritems():
@@ -2549,7 +2549,7 @@
             else:
                 raise IndexError, "invalid index"
 
-            if isscalar(x):
+            if isscalar(x):             # does this work with 0-dim arrays too?
                 if len(row) == 0:
                     row[:] = seq
                     self.data[i] = [x for item in seq]  # make copies 
@@ -2612,7 +2612,7 @@
                 
 
     def __mul__(self, other):           # self * other
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             new = self.copy()
             if other == 0:
                 # Multiply by zero: return the zero matrix
@@ -2632,7 +2632,7 @@
     
     
     def __rmul__(self, other):          # other * self
-        if isscalar(other) or (isdense(other) and rank(other)==0):
+        if isscalarlike(other):
             # Multiplication by a scalar is symmetric
             return self.__mul__(other)
         else:
@@ -2723,6 +2723,10 @@
 def isdense(x):
     return _isinstance(x, ndarray)
 
+def isscalarlike(x):
+    """Is x either a scalar, an array scalar, or a 0-dim array?"""
+    return isscalar(x) or (isdense(x) and x.ndim == 0)
+
 def isshape(x):
     """Is x a valid 2-tuple of dimensions?
     """




More information about the Scipy-svn mailing list