Пример #1
0
 def _add(self, other, lhs_negative, rhs_negative):
     if cupy.isscalar(other):
         if other == 0:
             if lhs_negative:
                 return -self
             else:
                 return self.copy()
         else:
             raise NotImplementedError(
                 'adding a nonzero scalar to a sparse matrix is not '
                 'supported')
     elif base.isspmatrix(other):
         alpha = -1 if lhs_negative else 1
         beta = -1 if rhs_negative else 1
         return self._add_sparse(other, alpha, beta)
     elif base.isdense(other):
         if lhs_negative:
             if rhs_negative:
                 return -self.todense() - other
             else:
                 return other - self.todense()
         else:
             if rhs_negative:
                 return self.todense() - other
             else:
                 return self.todense() + other
     else:
         return NotImplemented
Пример #2
0
 def __mul__(self, other):
     if cupy.isscalar(other):
         self.sum_duplicates()
         return self._with_data(self.data * other)
     elif isspmatrix_csr(other):
         self.sum_duplicates()
         other.sum_duplicates()
         return cusparse.csrgemm(self, other)
     elif csc.isspmatrix_csc(other):
         self.sum_duplicates()
         other.sum_duplicates()
         return cusparse.csrgemm(self, other.T, transb=True)
     elif base.isspmatrix(other):
         return self * other.tocsr()
     elif base.isdense(other):
         if other.ndim == 0:
             self.sum_duplicates()
             return self._with_data(self.data * other)
         elif other.ndim == 1:
             self.sum_duplicates()
             return cusparse.csrmv(self, cupy.asfortranarray(other))
         elif other.ndim == 2:
             self.sum_duplicates()
             return cusparse.csrmm2(self, cupy.asfortranarray(other))
         else:
             raise ValueError('could not interpret dimensions')
     else:
         return NotImplemented