Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
r""" Base class for dense matrices
TESTS::
sage: R.<a,b> = QQ[] sage: m = matrix(R,2,[0,a,b,b^2]) sage: TestSuite(m).run() """ from __future__ import absolute_import from __future__ import print_function
cimport sage.matrix.matrix as matrix
from sage.structure.element cimport Element, RingElement from sage.structure.richcmp cimport richcmp_not_equal, rich_to_bool import sage.matrix.matrix_space import sage.structure.sequence
cdef class Matrix_dense(matrix.Matrix): cdef bint is_sparse_c(self):
cdef bint is_dense_c(self):
def __copy__(self): """ Return a copy of this matrix. Changing the entries of the copy will not change the entries of this matrix. """ A.subdivide(*self.subdivisions())
cdef set_unsafe_int(self, Py_ssize_t i, Py_ssize_t j, int value): self[i][j] = value
def _pickle(self):
def _unpickle_generic(self, data, int version): cdef Py_ssize_t i, j, k # data is a *list* of the entries of the matrix. # TODO: Change the data[k] below to use the fast list access macros from the Python/C API else: raise RuntimeError("unknown matrix version (=%s)" % version)
cpdef _richcmp_(self, right, int op): """ EXAMPLES::
sage: P.<x> = QQ[] sage: m = matrix([[x,x+1],[1,x]]) sage: n = matrix([[x+1,x],[1,x]]) sage: o = matrix([[x,x],[1,x]]) sage: m < n True sage: m == m True sage: n > m True sage: m <= o False """ cdef Py_ssize_t i, j
def transpose(self): """ Returns the transpose of self, without changing self.
EXAMPLES: We create a matrix, compute its transpose, and note that the original matrix is not changed.
::
sage: M = MatrixSpace(QQ, 2) sage: A = M([1,2,3,4]) sage: B = A.transpose() sage: print(B) [1 3] [2 4] sage: print(A) [1 2] [3 4]
``.T`` is a convenient shortcut for the transpose::
sage: A.T [1 3] [2 4]
::
sage: A.subdivide(None, 1); A [1|2] [3|4] sage: A.transpose() [1 3] [---] [2 4] """ cdef Matrix_dense trans
cdef Py_ssize_t i, j
def antitranspose(self): """ Returns the antitranspose of self, without changing self.
EXAMPLES::
sage: A = matrix(2,3,range(6)); A [0 1 2] [3 4 5] sage: A.antitranspose() [5 2] [4 1] [3 0]
::
sage: A.subdivide(1,2); A [0 1|2] [---+-] [3 4|5] sage: A.antitranspose() [5|2] [-+-] [4|1] [3|0] """ (nc, nr) = (self.ncols(), self.nrows()) cdef Matrix_dense atrans atrans = self.new_matrix(nrows = nc, ncols = nr, copy=False, coerce=False) cdef Py_ssize_t i,j cdef Py_ssize_t ri,rj # reversed i and j rj = nc for j from 0 <= j < nc: ri = nr rj = rj-1 for i from 0 <= i < nr: ri = ri-1 atrans.set_unsafe(j , i, self.get_unsafe(ri,rj))
if self._subdivisions is not None: row_divs, col_divs = self.subdivisions() atrans.subdivide([nc - t for t in reversed(col_divs)], [nr - t for t in reversed(row_divs)]) return atrans
def _reverse_unsafe(self): r""" TESTS::
sage: m = matrix(QQ, 2, 3, range(6)) sage: m._reverse_unsafe() sage: m [5 4 3] [2 1 0] """ cdef Py_ssize_t i, j
def _elementwise_product(self, right): r""" Returns the elementwise product of two dense matrices with identical base rings.
This routine assumes that ``self`` and ``right`` are both matrices, both dense, with identical sizes and with identical base rings. It is "unsafe" in the sense that these conditions are not checked and no sensible errors are raised.
This routine is meant to be called from the :meth:`~sage.matrix.matrix2.Matrix.elementwise_product` method, which will ensure that this routine receives proper input. More thorough documentation is provided there.
EXAMPLES::
sage: A = matrix(ZZ, 2, range(6), sparse=False) sage: B = matrix(ZZ, 2, [1,0,2,0,3,0], sparse=False) sage: A._elementwise_product(B) [ 0 0 4] [ 0 12 0]
AUTHOR:
- Rob Beezer (2009-07-14) """ cdef Py_ssize_t r, c cdef Matrix_dense other, prod
def _derivative(self, var=None, R=None): """ Differentiate with respect to var by differentiating each element with respect to var.
.. SEEALSO::
:meth:`derivative`
EXAMPLES::
sage: m = matrix(2, [x^i for i in range(4)]) sage: m._derivative(x) [ 0 1] [ 2*x 3*x^2] """ # We would just use apply_map, except that Cython doesn't # allow lambda functions
return self.__copy__() image.subdivide(*self.subdivisions())
def _multiply_classical(left, matrix.Matrix right): """ Multiply the matrices left and right using the classical `O(n^3)` algorithm.
This method will almost always be overridden either by the implementation in :class:`~sage.matrix.Matrix_generic_dense`) or by more specialized versions, but having it here makes it possible to implement specialized dense matrix types with their own data structure without necessarily implementing ``_multiply_classical``, as described in :mod:`sage.matrix.docs`.
TESTS::
sage: from sage.matrix.matrix_dense import Matrix_dense sage: mats = [ ....: matrix(2, 2, [1, 2, 3, 4]), ....: matrix(2, 1, [1, 2]), ....: matrix(3, 2, [1, 2, 3, 4, 5, 6]), ....: matrix(ZZ, 0, 2), ....: matrix(ZZ, 2, 0) ....: ] sage: all(Matrix_dense._multiply_classical(a, b) == a*b ....: for a in mats for b in mats if a.ncols() == b.nrows()) True sage: Matrix_dense._multiply_classical(matrix(2, 1), matrix(2, 0)) Traceback (most recent call last): ... ArithmeticError: number of columns of left must equal number of rows of right """ cdef Py_ssize_t i, j |