Dense matrices using a NumPy backend¶
AUTHORS:
Jason Grout, Sep 2008: switch to NumPy backend, factored out the Matrix_double_dense class
Josh Kantor
William Stein: many bug fixes and touch ups.
- class sage.matrix.matrix_numpy_dense.Matrix_numpy_dense[source]¶
Bases:
Matrix_denseFill the matrix with entries.
The numpy matrix must have already been allocated.
INPUT:
parent– a matrix space overRDFentries– seematrix()copy– ignored (for backwards compatibility)coerce– ifTrue(the default), convert elements to the base ring before passing them to NumPy. IfFalse, pass the elements to NumPy as given.
EXAMPLES:
sage: matrix(RDF,3,range(9)) [0.0 1.0 2.0] [3.0 4.0 5.0] [6.0 7.0 8.0] sage: matrix(CDF,3,3,2) [2.0 0.0 0.0] [0.0 2.0 0.0] [0.0 0.0 2.0]
>>> from sage.all import * >>> matrix(RDF,Integer(3),range(Integer(9))) [0.0 1.0 2.0] [3.0 4.0 5.0] [6.0 7.0 8.0] >>> matrix(CDF,Integer(3),Integer(3),Integer(2)) [2.0 0.0 0.0] [0.0 2.0 0.0] [0.0 0.0 2.0]
matrix(RDF,3,range(9)) matrix(CDF,3,3,2)
- is_symmetric(tol=1e-12)[source]¶
Return whether this matrix is symmetric, to the given tolerance.
EXAMPLES:
sage: m = matrix(RDF,2,2,range(4)); m [0.0 1.0] [2.0 3.0] sage: m.is_symmetric() False sage: m[1,0] = 1.1; m [0.0 1.0] [1.1 3.0] sage: m.is_symmetric() False
>>> from sage.all import * >>> m = matrix(RDF,Integer(2),Integer(2),range(Integer(4))); m [0.0 1.0] [2.0 3.0] >>> m.is_symmetric() False >>> m[Integer(1),Integer(0)] = RealNumber('1.1'); m [0.0 1.0] [1.1 3.0] >>> m.is_symmetric() False
m = matrix(RDF,2,2,range(4)); m m.is_symmetric() m[1,0] = 1.1; m m.is_symmetric()
The tolerance inequality is strict:
sage: m.is_symmetric(tol=0.1) False sage: m.is_symmetric(tol=0.11) True
>>> from sage.all import * >>> m.is_symmetric(tol=RealNumber('0.1')) False >>> m.is_symmetric(tol=RealNumber('0.11')) True
m.is_symmetric(tol=0.1) m.is_symmetric(tol=0.11)
- numpy(dtype=None)[source]¶
Return a copy of the matrix as a numpy array.
It uses the numpy C/api so is very fast.
INPUT:
dtype– the desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence.
EXAMPLES:
sage: m = matrix(RDF,[[1,2],[3,4]]) sage: n = m.numpy() sage: import numpy sage: tuple(numpy.linalg.eig(n)) (array([-0.37228132..., 5.37228132...]), array([[-0.82456484..., -0.41597356...], [ 0.56576746..., -0.90937671...]])) sage: m = matrix(RDF, 2, range(6)); m [0.0 1.0 2.0] [3.0 4.0 5.0] sage: m.numpy() array([[0., 1., 2.], [3., 4., 5.]])
>>> from sage.all import * >>> m = matrix(RDF,[[Integer(1),Integer(2)],[Integer(3),Integer(4)]]) >>> n = m.numpy() >>> import numpy >>> tuple(numpy.linalg.eig(n)) (array([-0.37228132..., 5.37228132...]), array([[-0.82456484..., -0.41597356...], [ 0.56576746..., -0.90937671...]])) >>> m = matrix(RDF, Integer(2), range(Integer(6))); m [0.0 1.0 2.0] [3.0 4.0 5.0] >>> m.numpy() array([[0., 1., 2.], [3., 4., 5.]])
m = matrix(RDF,[[1,2],[3,4]]) n = m.numpy() import numpy tuple(numpy.linalg.eig(n)) m = matrix(RDF, 2, range(6)); m m.numpy()
Alternatively, numpy automatically calls this function (via the magic
__array__method) to convert Sage matrices to numpy arrays:sage: import numpy sage: m = matrix(RDF, 2, range(6)); m [0.0 1.0 2.0] [3.0 4.0 5.0] sage: numpy.array(m) array([[0., 1., 2.], [3., 4., 5.]]) sage: numpy.array(m).dtype dtype('float64') sage: m = matrix(CDF, 2, range(6)); m [0.0 1.0 2.0] [3.0 4.0 5.0] sage: numpy.array(m) array([[0.+0.j, 1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j, 5.+0.j]]) sage: numpy.array(m).dtype dtype('complex128')
>>> from sage.all import * >>> import numpy >>> m = matrix(RDF, Integer(2), range(Integer(6))); m [0.0 1.0 2.0] [3.0 4.0 5.0] >>> numpy.array(m) array([[0., 1., 2.], [3., 4., 5.]]) >>> numpy.array(m).dtype dtype('float64') >>> m = matrix(CDF, Integer(2), range(Integer(6))); m [0.0 1.0 2.0] [3.0 4.0 5.0] >>> numpy.array(m) array([[0.+0.j, 1.+0.j, 2.+0.j], [3.+0.j, 4.+0.j, 5.+0.j]]) >>> numpy.array(m).dtype dtype('complex128')
import numpy m = matrix(RDF, 2, range(6)); m numpy.array(m) numpy.array(m).dtype m = matrix(CDF, 2, range(6)); m numpy.array(m) numpy.array(m).dtype
- transpose()[source]¶
Return the transpose of this matrix, without changing
self.EXAMPLES:
sage: m = matrix(RDF,2,3,range(6)); m [0.0 1.0 2.0] [3.0 4.0 5.0] sage: m2 = m.transpose() sage: m[0,0] = 2 sage: m2 #note that m2 hasn't changed [0.0 3.0] [1.0 4.0] [2.0 5.0]
>>> from sage.all import * >>> m = matrix(RDF,Integer(2),Integer(3),range(Integer(6))); m [0.0 1.0 2.0] [3.0 4.0 5.0] >>> m2 = m.transpose() >>> m[Integer(0),Integer(0)] = Integer(2) >>> m2 #note that m2 hasn't changed [0.0 3.0] [1.0 4.0] [2.0 5.0]
m = matrix(RDF,2,3,range(6)); m m2 = m.transpose() m[0,0] = 2 m2 #note that m2 hasn't changed
.Tis a convenient shortcut for the transpose:sage: m.T [2.0 3.0] [1.0 4.0] [2.0 5.0] sage: m = matrix(RDF,0,3); m [] sage: m.transpose() [] sage: m.transpose().parent() Full MatrixSpace of 3 by 0 dense matrices over Real Double Field
>>> from sage.all import * >>> m.T [2.0 3.0] [1.0 4.0] [2.0 5.0] >>> m = matrix(RDF,Integer(0),Integer(3)); m [] >>> m.transpose() [] >>> m.transpose().parent() Full MatrixSpace of 3 by 0 dense matrices over Real Double Field
m.T m = matrix(RDF,0,3); m m.transpose() m.transpose().parent()