The VectorSpace command creates a vector space class,
from which one can create a subspace. Note the basis computed
by
SAGE is ``row reduced''.
sage: V = VectorSpace(GF(2),8)
sage: S = V.subspace([V([1,1,0,0,0,0,0,0]),V([1,0,0,0,0,1,1,0])])
sage: S.basis()
[
(1, 0, 0, 0, 0, 1, 1, 0),
(0, 1, 0, 0, 0, 1, 1, 0)
]
sage: S.dimension()
2
sage: M = MatrixSpace(IntegerRing(),4,2)(range(8)) sage: M.kernel() Free module of degree 4 and rank 2 over Integer Ring Echelon basis matrix: [ 1 0 -3 2] [ 0 1 -2 1]
A kernel of dimension one over
:
sage: A = MatrixSpace(RationalField(),3)(range(9)) sage: A.kernel() Vector space of degree 3 and dimension 1 over Rational Field Basis matrix: [ 1 -2 1]
A trivial kernel:
sage: A = MatrixSpace(RationalField(),2)([1,2,3,4]) sage: A.kernel() Vector space of degree 2 and dimension 0 over Rational Field Basis matrix: [] sage: M = MatrixSpace(RationalField(),0,2)(0) sage: M [] sage: M.kernel() Vector space of degree 0 and dimension 0 over Rational Field Basis matrix: [] sage: M = MatrixSpace(RationalField(),2,0)(0) sage: M.kernel() Vector space of dimension 2 over Rational Field
Kernel of a zero matrix:
sage: A = MatrixSpace(RationalField(),2)(0) sage: A.kernel() Vector space of degree 2 and dimension 2 over Rational Field Basis matrix: [1 0] [0 1]
Kernel of a non-square matrix:
sage: A = MatrixSpace(RationalField(),3,2)(range(6)) sage: A.kernel() Vector space of degree 3 and dimension 1 over Rational Field Basis matrix: [ 1 -2 1]
The 2-dimensional kernel of a matrix over a cyclotomic field:
sage: K = CyclotomicField(12); a = K.gen() sage: M = MatrixSpace(K,4,2)([1,-1, 0,-2, 0,-a^2-1, 0,a^2-1]) sage: M [ 1 -1] [ 0 -2] [ 0 -zeta12^2 - 1] [ 0 zeta12^2 - 1] sage: M.kernel() Vector space of degree 4 and dimension 2 over Cyclotomic Field of order 12 and degree 4 Basis matrix: [ 0 1 0 -2*zeta12^2] [ 0 0 1 -2*zeta12^2 + 1]
A nontrivial kernel over a complicated base field.
sage: K = FractionField(MPolynomialRing(RationalField(),2)) sage: M = MatrixSpace(K, 2)([[K.gen(1),K.gen(0)], [K.gen(1), K.gen(0)]]) sage: M [x1 x0] [x1 x0] sage: M.kernel() Vector space of degree 2 and dimension 1 over Fraction Field of Polynomial Ring in x0, x1 over Rational Field Basis matrix: [ 1 -1]
Other methods for integer matrices are
elementary_divisors, smith_form (for the Smith normal form),
echelon (a method for integer matrices) for the Hermite normal form,
frobenius for the Frobenius normal form (rational canonical form).
There a many methods for matrices over a field such as
or a finite field:
row_span, nullity, transpose, swap_rows,
matrix_from_columns, matrix_from_rows, among many others.
See the file matrix.py for further details.
The best approach, for now, is to use the interface with maxima:
sage: A = maxima("matrix ([1, -4], [1, -1])")
sage: eig = A.eigenvectors()
sage: eig
[[[ - sqrt(3)*%i,sqrt(3)*%i],[1,1]],[1,(sqrt(3)*%i + 1)/4],[1, - (sqrt(3)*%i - 1)/4]]
SAGE can also do this:
sage: M = MatrixSpace(RationalField(),2,2) sage: A = M([1,-4,1, -1]) sage: A.eigenvectors() [(1, a - 1)]
Here are two more examples:
sage: A = maxima("A: matrix ([11, 0, 0], [1, 11, 0], [1, 3, 2])")
sage: A.eigenvectors()
[[[2,11],[1,2]],[0,0,1],[0,1,1/3]]
sage: A = maxima("A: matrix ([-1, 0, 0], [1, -1, 0], [1, 3, 2])")
sage: A.eigenvectors()
[[[ - 1,2],[2,1]],[0,1, - 1],[0,0,1]]
sage: M = MatrixSpace(RationalField(),2,3) sage: A = M([1,2,3, 4,5,6]) sage: A [1 2 3] [4 5 6] sage: A.parent() Full MatrixSpace of 2 by 3 dense matrices over Rational Field sage: A[0,2] = 389 sage: A [ 1 2 389] [ 4 5 6] sage: A.echelon_form() [ 1 0 -1933/3] [ 0 1 1550/3]
sage: A = MatrixSpace(IntegerRing(),2)( [[1,2], [3,4]] ) sage: f = A.charpoly() sage: f x^2 - 5*x - 2 sage: f.parent() Univariate Polynomial Ring in x over Integer Ring
We compute the characteristic polynomial of a matrix over
the polynomial ring
:
sage: R = PolynomialRing(IntegerRing(),'a'); a = R.gen() sage: M = MatrixSpace(R,2)([[a,1], [a,a+1]]) sage: M [ a 1] [ a a + 1] sage: f = M.charpoly() sage: f x^2 + (-2*a - 1)*x + a^2 sage: f.parent() Univariate Polynomial Ring in x over Univariate Polynomial Ring in a over Integer Ring sage: M.trace() 2*a + 1 sage: M.determinant() a^2
We compute the characteristic polynomial of a matrix over the
multi-variate polynomial ring
:
sage: R.<u,v> = PolynomialRing(ZZ,2) sage: A = MatrixSpace(R,2)([u,v,u^2,v^2]) sage: f = A.charpoly() sage: f x^2 + (-1*v^2 - u)*x + u*v^2 - u^2*v
It's a little difficult to distinguish the variables. To fix this, we rename the indeterminate "Z":
sage: f.parent().assign_names("Z")
sage: f
Z^2 + (-1*v^2 - u)*Z + u*v^2 - u^2*v
On a related matter, to compute the eigenvalues and eigenvectors of a matrix, you can use SAGE's GAP interface:
sage: gap.eval("A := [[1,2,3],[4,5,6],[7,8,9]]")
'[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]'
sage: gap.eval("v := Eigenvectors( Rationals,A)")
'[ [ 1, -2, 1 ] ]'
sage: gap.eval("lambda := Eigenvalues( Rationals,A)")
'[ 0 ]'
Using maxima, you can easily solve linear equations:
sage: eqn = maxima(['a+b*c=1', 'b-a*c=0', 'a+b=5'])
sage: s = eqn.solve('[a,b,c]'); s
[[a = (25*sqrt(79)*%i + 25)/(6*sqrt(79)*%i - 34),b = (5*sqrt(79)*%i +
5)/(sqrt(79)*%i + 11),c = (sqrt(79)*%i + 1)/10],[a = (25*sqrt(79)*%i -
25)/(6*sqrt(79)*%i + 34),b = (5*sqrt(79)*%i - 5)/(sqrt(79)*%i - 11),c = -
(sqrt(79)*%i - 1)/10]]
You can even nicely typeset the solution in latex:
sage: print latex(s)
\left[ \left[ a=\frac{25 \sqrt{79} i+25}{6 \sqrt{79} i-34} ,
b= \frac{5 \sqrt{79} i+5}{\sqrt{79} i+11} , c=\frac{\sqrt{79} i+1}{10}
\right] , \left[ a=\frac{25 \sqrt{79} i-25}{6 \sqrt{79} i+34} ,
b= \frac{5 \sqrt{79} i-5}{\sqrt{79} i-11} , c=-\frac{\sqrt{79} i-1}{10}
\right] \right]
You can also solve linear equations symbolically using
the solve_linear command:
sage: eqns = ["x + z = y","2*a*x - y = 2*a^2","y - 2*z = 2"] sage: vars = ["x","y","z"] sage: maxima.solve_linear(eqns, vars) [x = a + 1,y = 2*a,z = a - 1]
To solve a system numerically, you can use SAGE's octave interface:
sage: M33 = MatrixSpace(QQ,3,3) sage: A = M33([1,2,3,4,5,6,7,8,0]) sage: V3 = VectorSpace(QQ,3) sage: b = V3([1,2,3]) sage: octave.solve_linear_system(A,b) # requires optional octave [-0.33333299999999999, 0.66666700000000001, -3.5236600000000002e-18]
See About this document... for information on suggesting changes.