Example #1
0
 def __truediv__(self, other):
     """Point-wise division by scalar"""
     if util.isscalarlike(other):
         if self.dtype == numpy.complex64:
             # Note: This is a work-around to make the output dtype the same
             # as SciPy. It might be SciPy version dependent.
             dtype = numpy.float32
         else:
             if cupy.isscalar(other):
                 dtype = numpy.float64
             else:
                 dtype = numpy.promote_types(numpy.float64, other.dtype)
         d = cupy.array(1. / other, dtype=dtype)
         return multiply_by_scalar(self, d)
     # TODO(anaruse): Implement divide by dense or sparse matrix
     raise NotImplementedError
Example #2
0
    def __pow__(self, other):
        """Calculates n-th power of the matrix.

        This method calculates n-th power of a given matrix. The matrix must
        be a squared matrix, and a given exponent must be an integer.

        Args:
            other (int): Exponent.

        Returns:
            cupyx.scipy.sparse.spmatrix: A sparse matrix representing n-th
                power of this matrix.

        """
        m, n = self.shape
        if m != n:
            raise TypeError('matrix is not square')

        if util.isintlike(other):
            other = int(other)
            if other < 0:
                raise ValueError('exponent must be >= 0')

            if other == 0:
                import cupyx.scipy.sparse
                return cupyx.scipy.sparse.identity(m,
                                                   dtype=self.dtype,
                                                   format='csr')
            elif other == 1:
                return self.copy()
            else:
                tmp = self.__pow__(other // 2)
                if other % 2:
                    return self * tmp * tmp
                else:
                    return tmp * tmp
        elif util.isscalarlike(other):
            raise ValueError('exponent must be an integer')
        else:
            return NotImplemented