Sharedsage_worksheets / ADS_matrix.sagewsOpen in CoCalc
Author: Ken Levasseur
Description: Worksheets related to Applied Discrete Structures
%md <H1>Row Reduction</H1>

Row Reduction

Here is a matrix over the rational numbers and its reduced echelon form.

C = Matrix(QQ,[[1,1,1,1],[5,1,2,2]]);C print '--------------------' C.echelon_form()
[1 1 1 1] [5 1 2 2] -------------------- [ 1 0 1/4 1/4] [ 0 1 3/4 3/4]

Here is a matrix over the integers mod 2 and its reduced echelon form.

H=Matrix(Integers(2),[[1,1,0,0,1,1],[1,0,1,0,1,0],[1,0,1,1,0,1],[0,1,1,1,0,0]]);H print '--------------------' H.echelon_form()
[1 1 0 0 1 1] [1 0 1 0 1 0] [1 0 1 1 0 1] [0 1 1 1 0 0] -------------------- [1 0 1 0 0 0] [0 1 1 0 0 1] [0 0 0 1 0 1] [0 0 0 0 1 0]
H=Matrix(Integers(2),[[1,1,0,1,0,0,1],[1,0,1,0,1,0,0],[0,1,1,0,0,1,0]]);H print '*****************' H.echelon_form()
***************** [1 0 1 0 1 0 0] [0 1 1 0 0 1 0] [0 0 0 1 1 1 1]
H2=Matrix(Integers(2),[[1,1,0,1,1,1],[1,0,1,1,0,0],[0,1,1,0,1,0]]);H2 print '*****************' H2.echelon_form()
[1 1 0 1 1 1] [1 0 1 1 0 0] [0 1 1 0 1 0] ***************** [1 0 1 1 0 0] [0 1 1 0 1 0] [0 0 0 0 0 1]
A=[[2,1,0,4],[-1,2,1,0],[0,-1,2,1]] A
[1 1 0 1 0 0] [1 0 1 0 1 0] [0 1 1 0 0 1] [[2, 1, 0, 4], [-1, 2, 1, 0], [0, -1, 2, 1]]
matrix(A)
[ 2 1 0 4] [-1 2 1 0] [ 0 -1 2 1]
Edit text...
var (' D, P') A = Matrix (QQ, [[4, 1, 0], [1, 5, 1], [0, 1, 4]]);A
(D, P) [4 1 0] [1 5 1] [0 1 4]
(D,P)=A.right_eigenmatrix();(D,P)
([6 0 0] [0 4 0] [0 0 3], [ 1 1 1] [ 2 0 -1] [ 1 -1 1])
P.inverse()*A*P
[6 0 0] [0 4 0] [0 0 3]
A2=Matrix(QQ,[[8,1,0],[1,5,1],[0,1,7]])
P=A2.right_eigenmatrix()[1] P.numerical_approx(digits=3) print '------------------' D=(P.inverse()*A2*P);D.numerical_approx(digits=3)
[ 1.00 1.00 1.00] [ -3.65 -0.726 0.377] [ 1.38 -2.65 0.274] ------------------ [ 4.35 0.000 0.000] [0.000 7.27 0.000] [0.000 0.000 8.38]
A=Matrix(QQ,[[1, 0, 0],[0,2,1],[1,-1,4]]) A.right_eigenmatrix()
([1 0 0] [0 3 0] [0 0 3], [ 1 0 0] [ 1/4 1 0] [-1/4 1 0])
A=Matrix(QQ,[[2,1],[2,3]]) A.exp()
[1/3*e^4 + 2/3*e 1/3*e^4 - 1/3*e] [2/3*e^4 - 2/3*e 2/3*e^4 + 1/3*e]
Matrix?
File: /ext/sage/sage-8.1/src/sage/matrix/constructor.pyx Signature : Matrix(self, ring=None, nrows=None, ncols=None, sparse=None, *args) Docstring : Create a matrix. This implements the "matrix" constructor: sage: matrix([[1,2],[3,4]]) [1 2] [3 4] It also contains methods to create special types of matrices, see "matrix.[tab]" for more options. For example: sage: matrix.identity(2) [1 0] [0 1] INPUT: The matrix command takes the entries of a matrix, optionally preceded by a ring and the dimensions of the matrix, and returns a matrix. The entries of a matrix can be specified as a flat list of elements, a list of lists (i.e., a list of rows), a list of Sage vectors, a callable object, or a dictionary having positions as keys and matrix entries as values (see the examples). If you pass in a callable object, then you must specify the number of rows and columns. You can create a matrix of zeros by passing an empty list or the integer zero for the entries. To construct a multiple of the identity (cI), you can specify square dimensions and pass in c. Calling matrix() with a Sage object may return something that makes sense. Calling matrix() with a NumPy array will convert the array to a matrix. The ring, number of rows, and number of columns of the matrix can be specified by setting the "ring", "nrows", or "ncols" keyword parameters or by passing them as the first arguments to the function in specified order. The ring defaults to "ZZ" if it is not specified and cannot be determined from the entries. If the number of rows and columns are not specified and cannot be determined, then an empty 0x0 matrix is returned. INPUT: * "ring" -- the base ring for the entries of the matrix. * "nrows" -- the number of rows in the matrix. * "ncols" -- the number of columns in the matrix. * "sparse" -- create a sparse matrix. This defaults to "True" when the entries are given as a dictionary, otherwise defaults to "False". * "entries" -- see examples below. OUTPUT: a matrix EXAMPLES: sage: m = matrix(2); m; m.parent() [0 0] [0 0] Full MatrixSpace of 2 by 2 dense matrices over Integer Ring sage: m = matrix(2,3); m; m.parent() [0 0 0] [0 0 0] Full MatrixSpace of 2 by 3 dense matrices over Integer Ring sage: m = matrix(QQ,[[1,2,3],[4,5,6]]); m; m.parent() [1 2 3] [4 5 6] Full MatrixSpace of 2 by 3 dense matrices over Rational Field sage: m = matrix(QQ, 3, 3, lambda i, j: i+j); m [0 1 2] [1 2 3] [2 3 4] sage: m = matrix(3, lambda i,j: i-j); m [ 0 -1 -2] [ 1 0 -1] [ 2 1 0] sage: matrix(QQ, 2, 3, lambda x, y: x+y) [0 1 2] [1 2 3] sage: matrix(QQ, 5, 5, lambda x, y: (x+1) / (y+1)) [ 1 1/2 1/3 1/4 1/5] [ 2 1 2/3 1/2 2/5] [ 3 3/2 1 3/4 3/5] [ 4 2 4/3 1 4/5] [ 5 5/2 5/3 5/4 1] sage: v1=vector((1,2,3)) sage: v2=vector((4,5,6)) sage: m = matrix([v1,v2]); m; m.parent() [1 2 3] [4 5 6] Full MatrixSpace of 2 by 3 dense matrices over Integer Ring sage: m = matrix(QQ,2,[1,2,3,4,5,6]); m; m.parent() [1 2 3] [4 5 6] Full MatrixSpace of 2 by 3 dense matrices over Rational Field sage: m = matrix(QQ,2,3,[1,2,3,4,5,6]); m; m.parent() [1 2 3] [4 5 6] Full MatrixSpace of 2 by 3 dense matrices over Rational Field sage: m = matrix({(0,1): 2, (1,1):2/5}); m; m.parent() [ 0 2] [ 0 2/5] Full MatrixSpace of 2 by 2 sparse matrices over Rational Field sage: m = matrix(QQ,2,3,{(1,1): 2}); m; m.parent() [0 0 0] [0 2 0] Full MatrixSpace of 2 by 3 sparse matrices over Rational Field sage: import numpy sage: n = numpy.array([[1,2],[3,4]],float) sage: m = matrix(n); m; m.parent() [1.0 2.0] [3.0 4.0] Full MatrixSpace of 2 by 2 dense matrices over Real Double Field sage: v = vector(ZZ, [1, 10, 100]) sage: m = matrix(v); m; m.parent() [ 1 10 100] Full MatrixSpace of 1 by 3 dense matrices over Integer Ring sage: m = matrix(GF(7), v); m; m.parent() [1 3 2] Full MatrixSpace of 1 by 3 dense matrices over Finite Field of size 7 sage: g = graphs.PetersenGraph() sage: m = matrix(g); m; m.parent() [0 1 0 0 1 1 0 0 0 0] [1 0 1 0 0 0 1 0 0 0] [0 1 0 1 0 0 0 1 0 0] [0 0 1 0 1 0 0 0 1 0] [1 0 0 1 0 0 0 0 0 1] [1 0 0 0 0 0 0 1 1 0] [0 1 0 0 0 0 0 0 1 1] [0 0 1 0 0 1 0 0 0 1] [0 0 0 1 0 1 1 0 0 0] [0 0 0 0 1 0 1 1 0 0] Full MatrixSpace of 10 by 10 dense matrices over Integer Ring sage: matrix(ZZ, 10, 10, range(100), sparse=True).parent() Full MatrixSpace of 10 by 10 sparse matrices over Integer Ring sage: R = PolynomialRing(QQ, 9, 'x') sage: A = matrix(R, 3, 3, R.gens()); A [x0 x1 x2] [x3 x4 x5] [x6 x7 x8] sage: det(A) -x2*x4*x6 + x1*x5*x6 + x2*x3*x7 - x0*x5*x7 - x1*x3*x8 + x0*x4*x8 AUTHORS: * William Stein: Initial implementation * Jason Grout (2008-03): almost a complete rewrite, with bits and pieces from the original implementation * Jeroen Demeyer (2016-02-05): major clean up, see https://trac.sagemath.org/20015 and https://trac.sagemath.org/20016