sagemath matrix

Matrix plays an important role in mathematics, and we can create, perform different operations, and also solve linear systems of equations via matrices in sagemath.

In Sagemath the matrix() command is used to create matrices and perform different operations such as transpose, determinant, adjoint, inverse, etc. Sagemath can also solve a system of linear equations via matrices. 

Matrices Construction In Sagemath

We can create matrices of any size over any field of integer, rational, real, or complex. We can also create special kinds of matrices in sagemath. 

Generalised Matrix 

The general command is matrix(field, nrows, ncolns, [[1st row], [2nd row], …, [nth row]]).

If we don’t mention the field and size of matrices. It will take an integer ring, and the size of the matrix will be determined from the entries. The parent() command gives both size and field.

Find the dimension and underlying ring for the matrix \(A=\left[\begin{array}{rr}
1 & 2 \\
4 & 5
\end{array}\right]\)

Input: A = matrix([[1,2],[4,5]]);A
Output:
[1 2]
[4 5]
Input: A.parent()
Output: Full MatrixSpace of 2 by 2 dense matrices over Integer Ring

We can change the field to rational, real, or complex by using the command matrix.change_ring(), and QQ, RR, and CC are used for rational, real, and complex fields respectively.

Input:
A = matrix([[1,2],[4,5]]);
B = A.change_ring(QQ); 
B.base_ring()
Output: Rational Field

We can also create a matrix with the help of the size and range() function of Python. Given below example is 2-by-2 matrix over the rational field whose entries go from 0 to 3.

Input: B = matrix(QQ, 2,2, range(4));B
Output:
[0 1]
[2 3]

Special Matrices in Sagemath

In sagemath, we can create many kinds of special matrices, which we discuss below. 

Identity Matrix

An identity matrix can be made with a command matrix.identity(size) or identity_matrix(size)

If we write only a single number for size then it will create a square identity matrix of that size. 

If we need a rectangular identity matrix, we have to explicitly mention the matrix size with the command matrix.identity(nrows,ncolns).

Input: matrix.identity(3)
Output:
[1 0 0]
[0 1 0]
[0 0 1]
Input: identity_matrix(5,6)
Output:
[1 0 0 0 0]
[0 1 0 0 0]
[0 0 1 0 0]
[0 0 0 1 0]
[0 0 0 0 1]

Matrix of Zeros (Null)

zero_matrix(size) or matrix.zero(size) is used for the construction of a null matrix. A single digit as an argument gives a square matrix, while two digits separated by a comma as an argument gives a rectangular matrix. 

Input: matrix.zero(3)
Output:
[0 0 0]
[0 0 0]
[0 0 0]
Input: zero_matrix(3,4)
Output:
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]

Matrix of Ones

ones_matrix(size) or matrix.ones(size) is used for the construction of matrix of ones. The single-digit size will generate a square matrix, while double digits separated by a comma generate a rectangular matrix. 

Input: matrix.ones(4)
Output:
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
[1 1 1 1]
Input: ones_matrix(4,5)
Output:
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]

Diagonal Matrix

The command matrix.diagonal(size, [diagnoal entries]) or diagonal_matrix(size, [entris at diagonal position]) creates a diagonal matrix.

Input: matrix.diagonal(3, [2,4,5])
Output:
[2 0 0]
[0 4 0]
[0 0 5]

We can also create a diagonal matrix with the command diagonal_matrix([diagnoal entris]) without mentioning the size, and the size of the matrix will be determined from the diagonal entries.

Input: diagonal_matrix([1,1,1])
Output:
[1 0 0]
[0 1 0]
[0 0 1]

If we give fewer entries for diagonal entries than the size of the matrix, then other diagonal entries will be zero. 

Input: matrix.diagonal(4,[3,2])
Output:
[3 0 0 0]
[0 2 0 0]
[0 0 0 0]
[0 0 0 0]

Block Matrix

We create a block matrix with a command matrix.block(field, size, [[1st matrix], [2nd matrix],..[nth row]]). In Below Example A is a 2-by-2 matrix over a rational field. We created a block matrix using the A matrix by doing different operations on A.

Input: 
A = matrix(QQ, 2,2,[3,4,5,6]);
matrix.block([[A, -A], [56*A, ~A]])
Output:
[   3    4|  -3   -4]
[   5    6|  -5   -6]
[---------+---------]
[ 168  224|  -3    2]
[ 280  336| 5/2 -3/2]

If we input a single number instead of the matrix in the block matrix, it will insert a scalar matrix of that number.

Input: 
A = matrix(QQ, 2,2,[3,4,5,6]);
matrix.block([[7,2],[A,4]])
Output:
[7 0|2 0]
[0 7|0 2]
[---+---]
[3 4|4 0]
[5 6|0 4]

Block Diagonal Matrix

We can also create diagonal block matrices with command matrix.block_diagonal([matrices at diagonal position]) in which off-diagonal matrices will be zeros matrices. 

Input: 
A = matrix([[1,2],[3,4]]);
matrix.block_diagonal([A,A])
Output:
[1 2|0 0]
[3 4|0 0]
[---+---]
[0 0|1 2]
[0 0|3 4]

Matrix Creation with lambda Function

We can also create a matrix with the lambda function of Python and the code will be a “matrix(base ring, size, lambda i, j: function of ij)”, where i is ith row and j is jth column, and it is to be noted that index will start from 0.

Input: tt = matrix(ZZ, 2,3, lambda i, j: i+1*j+1); tt
Output:
[1 2 3]
[2 3 4]

Dimesnion of Matrices

The parent() function will give both dimension and base ring of the matrix. dimensions() function gives only the size of the matrix, and base_ring() will tell us about underlying field.

Input: 
A = matrix([[2,5,6],[5,7,8],[9,8,2]]);
A.parent()
Output: Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
Input: A.dimensions()
Output: (3, 3)
Input: A.base_ring()
Output: Integer Ring

Operations On matrices in sagemath

We can do simple arithmetic as well as special operations associated with matrices in sagemath. 

Arithmetic Operations

Simple arithmetics can be done with symbols +, -, * plus, minus, and multiplication respectively. 

In sagemath, we can add two, or more two matrices of the same order by simply putting a + sign between matrices, and a similarly – sign for subtraction.

Input: 
A = matrix([[1,2],[4,5]]);
B = matrix([[3,4],[7,8]])
Input: A+B
Output: 
[ 4  6]
[11 13]
Input: A-B
Output: 
[-2 -2]
[-3 -3]

We can multiply two matrices if the columns of the first matrix and rows of the second matrix are equal. Simply putting * between two matrices will do the job of multiplication of two matrices.

Input: 
h1 = matrix([[1,2]]);
h2 = matrix([[3,2],[5,6]]);
h1*h2
Output: 
[13 14]
Input: h2*h1
Output: Traceback (most recent call last)
...
TypeError: unsupported operand parent(s) for *: 'Full MatrixSpace of 2 by 2 dense matrices over Integer Ring' and 'Full MatrixSpace of 1 by 2 dense matrices over Integer Ring'

A scalar number can also be multiplied with matrices.

Input: 
A = matrix([[1,2],[3,4]]); 
2*A
Output: 
[[2 4]
[6 8]

Operation Related to Matrices

We can do transposition, determinant, adjoint, inverse, echelon, reduced echelon etc. operations on matrices in sagemath.

Transpose of a Matrix

Transpose of the matrix can be determined with the command matrix.transpose(), matrix.column(), or matrix.T.

Find transpose of \(A=\left[\begin{array}{rr}
1 & 2 \\
\end{array}\right]\)

Input: A = matrix([1,2]);
Input: A.T
Output: 
[1]
[2]
Input: A.transpose()
Output: 
[1]
[2]
Input: matrix.column(A)
Output: 
[1]
[2]

Determinant of a Matrix

det(matrix()) or matrix().determinant() is used for the determinant operation.

Find determinant of \(A=\left[\begin{array}{rr}
0 & 1 \\
2 & 3
\end{array}\right]\)

Input: B = matrix(QQ, 2,2, range(4));
Input: det(B)
Output: 
-2

Adjoint Of a Matrix

“matrix.adjugate()” command is used for the determination of the adjoint of a matrix or cofactors of that matrix.

Find Adjoint of \(A=\left[\begin{array}{rr}
2 & 0 & 0 \\
0 & 4 & 0 \\
0 & 0 & 5
\end{array}\right]\)

Input: 
A = matrix.diagonal(3, [2,4,5]);
A.adjugate()
Output: 
[20  0  0]
[ 0 10  0]
[ 0  0  8]

The Inverse of a matrix

The inverse of the matrix in sagemath can be obtained with command matrix.inverse() or ~matrix().

Find inverse of \(A=\left[\begin{array}{rr}
1 & 2 \\
3 & 4
\end{array}\right]\)

Input: 
A = matrix([[1,2],[3,4]])
~A
Output: 
[  -2    1]
[ 3/2 -1/2]
Input: A.inverse()
Output: 
[  -2    1]
[ 3/2 -1/2]

Trace of a Matrix

The trace of the matrix in sagemath can be obtained with command matrix.trace().

Find trace of \(A=\left[\begin{array}{rr}
1 & 2 \\
3 & 4
\end{array}\right]\)

Input: 
A = matrix([[1,2],[3,4]])
A.trace()
Output: 
5

The Rank of a Matrix

The rank of any matrix can be determined with command rank(matrix()).

Find rank of \(A=\left[\begin{array}{rr}
1 & 2 \\
3 & 4
\end{array}\right]\)

Input: 
A = matrix([[1,2],[3,4]])
rank(A)
Output: 
2

Characteristic Polynomial

The characteristic polynomial of any matrix can be obtained with matrix().charpoly() command. 

Find characteristic polynomial of \(A=\left[\begin{array}{rr}
1 & 2 \\
3 & 4
\end{array}\right]\)

Input: 
A = matrix([[1,2],[3,4]])
A.charpoly()
Output: 
x^2 - 5*x - 2

Echelon form of a Matrix

The echelon form of a matrix can also be made in sage math with matrix.echelon_form() command.

Make echelon form of \(\left[\begin{array}{rrrr}
0 & 1 & 2 & 3 \\
4 & 5 & 6 & 7 \\
8 & 9 & 10 & 11
\end{array}\right]\)

Input: 
A = matrix(QQ, 3,4, range(12));
A.echelon_form()
Output: 
[ 1  0 -1 -2]
[ 0  1  2  3]
[ 0  0  0  0]

Reduced Echelon form of a Matrix 

For the reduced echelon form of a matrix, we will use matrix.rref().

Make Reduce Echelon form of \(\left[\begin{array}{rrr}
2 & 5 & 6 \\
5 & 7 & 8 \\
9 & 8 & 2
\end{array}\right]\)

Input: 
A = matrix(QQ, 3,3,[[2,5,6],[5,7,8],[9,8,2]]);
A.rref()
Output: 
[1 0 0]
[0 1 0]
[0 0 1]

Matrix with Complex Numbers

Matrix with complex numbers in sagemath can be created in a similar way but we have to change the base ring to a complex field. 

We can add, subtract, multiply, and take transpose, determinant, inverse etc. of complex matrices.

Example: Create \(A=\left[\begin{array}{rr}
1 & 2+i \\
3-i & 4
\end{array}\right]\; i=\sqrt{-1}\) in sagemath.

Input: matrix(CC, [[1,2+i],[3-i, 4]])
Output: 
[                     1.00000000000000 2.00000000000000 + 1.00000000000000*I]
[3.00000000000000 - 1.00000000000000*I                      4.00000000000000]

Solving System of Linear Equations

We can solve a system of linear equations in sagemath with the help of matrices, first, we have to create A and b matrices and then we can use A.solve_right(b) for a solution.

Example: Solve \(x_1+2x_2+x_3=0; \;2x_1+4x_2+x_3=0; \; 5x_1+4x_2+2x_3=0\)

Input: 
A = matrix([[1,2,1],[2,4,1],[5,4,2]]);
b = vector([1,1,4]);
x = A.solve_right(b);
x
Output: 
(2/3, -1/3, 1)

Instead of A.solve_right(b), A\b could also be used for the solution.

Solution by Inversion Method

We can also find the solution of the equation by the inversion method and the formula is \( X=A^{-1}b\).

Input: 
A = matrix([[1,2,1],[2,4,1],[5,4,2]]);
b = vector([1,1,4]);
x = ~A*b
x
Output: 
(2/3, -1/3, 1)

Solution by Crammer’s Method

In crammer’s method we have, \(x_1=\frac{|A_1|}{|A|}\), \(x_2=\frac{|A_2|}{|A|}\), and \(x_3=\frac{|A_3|}{|A|}\).

Input: 
A = matrix([[1,2,1],[2,4,1],[5,4,2]]);
b = vector([1,1,4]);
A1 = copy(A); A1.set_column(0,b);
A2 = copy(A); A2.set_column(1,b);
A3 = copy(A); A3.set_column(2,b);
x1,x2,x3 = [det(A1)/det(A),det(A2)/det(A),det(A3)/det(A)];
x1,x2,x3
Output: 
(2/3, -1/3, 1)

1 thought on “sagemath matrix”

Leave a comment