Exemplo n.º 1
0
 def copyin_matrix(self, key, value):
     # include this here because it's not part of BaseMatrix
     rlo, rhi, clo, chi = self.key2bounds(key)
     shape = value.shape
     dr, dc = rhi - rlo, chi - clo
     if shape != (dr, dc):
         raise ShapeError(
             "The Matrix `value` doesn't have the same dimensions "
             "as the in sub-Matrix given by `key`.")
     if not isinstance(value, SparseMatrix):
         for i in range(value.rows):
             for j in range(value.cols):
                 self[i + rlo, j + clo] = value[i, j]
     else:
         if (rhi - rlo) * (chi - clo) < len(self):
             for i in range(rlo, rhi):
                 for j in range(clo, chi):
                     self._smat.pop((i, j), None)
         else:
             for i, j, v in self.row_list():
                 if rlo <= i < rhi and clo <= j < chi:
                     self._smat.pop((i, j), None)
         for k, v in value._smat.iteritems():
             i, j = k
             self[i + rlo, j + clo] = value[i, j]
Exemplo n.º 2
0
    def col_join(self, other):
        """Returns B augmented beneath A (row-wise joining)::

            [A]
            [B]

        Examples
        ========

        >>> from sympy import SparseMatrix, Matrix, ones
        >>> A = SparseMatrix(ones(3))
        >>> A
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]])
        >>> B = SparseMatrix.eye(3)
        >>> B
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
        >>> C = A.col_join(B); C
        Matrix([
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
        >>> C == A.col_join(Matrix(B))
        True

        Joining along columns is the same as appending rows at the end
        of the matrix:

        >>> C == A.row_insert(A.rows, Matrix(B))
        True
        """
        A, B = self, other
        if not A.cols == B.cols:
            raise ShapeError()
        A = A.copy()
        if not isinstance(B, SparseMatrix):
            k = 0
            b = B._mat
            for i in range(B.rows):
                for j in range(B.cols):
                    v = b[k]
                    if v:
                        A._smat[(i + A.rows, j)] = v
                    k += 1
        else:
            for (i, j), v in B._smat.iteritems():
                A._smat[i + A.rows, j] = v
        A.rows += B.rows
        return A
Exemplo n.º 3
0
    def row_join(self, other):
        """Returns B appended after A (column-wise augmenting)::

            [A B]

        Examples
        ========

        >>> from sympy import SparseMatrix, Matrix
        >>> A = SparseMatrix(((1, 0, 1), (0, 1, 0), (1, 1, 0)))
        >>> A
        Matrix([
        [1, 0, 1],
        [0, 1, 0],
        [1, 1, 0]])
        >>> B = SparseMatrix(((1, 0, 0), (0, 1, 0), (0, 0, 1)))
        >>> B
        Matrix([
        [1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])
        >>> C = A.row_join(B); C
        Matrix([
        [1, 0, 1, 1, 0, 0],
        [0, 1, 0, 0, 1, 0],
        [1, 1, 0, 0, 0, 1]])
        >>> C == A.row_join(Matrix(B))
        True

        Joining at row ends is the same as appending columns at the end
        of the matrix:

        >>> C == A.col_insert(A.cols, B)
        True
        """
        A, B = self, other
        if not A.rows == B.rows:
            raise ShapeError()
        A = A.copy()
        if not isinstance(B, SparseMatrix):
            k = 0
            b = B._mat
            for i in range(B.rows):
                for j in range(B.cols):
                    v = b[k]
                    if v:
                        A._smat[(i, j + A.cols)] = v
                    k += 1
        else:
            for (i, j), v in B._smat.iteritems():
                A._smat[(i, j + A.cols)] = v
        A.cols += B.cols
        return A
Exemplo n.º 4
0
 def cross(self, b):
     if not is_sequence(b):
         raise TypeError(
             "`b` must be an ordered iterable or Matrix, not %s." % type(b))
     if not (self.rows == 1 and self.cols == 3 or
             self.rows == 3 and self.cols == 1 ) and \
             (b.rows == 1 and b.cols == 3 or
             b.rows == 3 and b.cols == 1):
         raise ShapeError("Dimensions incorrect for cross product")
     else:
         return MutableSparseMatrix(1, 3,
                                    ((self[1] * b[2] - self[2] * b[1]),
                                     (self[2] * b[0] - self[0] * b[2]),
                                     (self[0] * b[1] - self[1] * b[0])))
Exemplo n.º 5
0
    def add(self, other):
        """Add two sparse matrices with dictionary representation.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix, eye, ones
        >>> SparseMatrix(eye(3)).add(SparseMatrix(ones(3)))
        Matrix([
        [2, 1, 1],
        [1, 2, 1],
        [1, 1, 2]])
        >>> SparseMatrix(eye(3)).add(-SparseMatrix(eye(3)))
        Matrix([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

        Only the non-zero elements are stored, so the resulting dictionary
        that is used to represent the sparse matrix is empty:
        >>> _._smat
        {}

        See Also
        ========

        multiply
        """
        if not isinstance(other, SparseMatrix):
            raise ValueError(
                'only use add with %s, not %s' %
                tuple([c.__class__.__name__ for c in (self, other)]))
        if self.shape != other.shape:
            raise ShapeError()
        M = self.copy()
        for i, v in other._smat.iteritems():
            v = M[i] + v
            if v:
                M._smat[i] = v
            else:
                M._smat.pop(i, None)
        return M