Module 2-Sage Matrix Operations
Module 2-Sage Matrix Operations
Module 2-Sage Matrix Operations
SageMath
Module 2
Objectives
Output
[1 2]
[3 4]
• Matrix([3,2,[1,2,3,4,5,6])
Output
[1 2]
[3 4]
[5 6]
Matrix(QQ,3,2,[1,2,(1/3),4,(5/6),6])
Output
[ 1 2]
[1/3 4]
[5/6 6]
Output
Matrix(RR,3,2,[1,2,(1/3),4,(5/6),6])
[ 1.00000000000000 2.00000000000000]
[0.333333333333333 4.00000000000000]
[0.833333333333333 6.00000000000000]
Matrix(CC,3,2,[1,2,4*i,4,2+2*i,6])
Output:
[1.00000000000000 2.00000000000000]
[4.00000000000000 + 3.00000000000000*I 4.00000000000000]
[2.00000000000000 + 2.00000000000000*I 6.00000000000000]
I = identity_matrix(3)
Output
[1 0 0]
[0 1 0]
[0 0 1]
Output
[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
Ones_matrix(3,2)
Output
[1 1]
[1 1]
[1 1]
Identity_matrix(4)
Output
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
Matrix Representation-
1. A=matrix(QQ,[[1,2,3],[4,5,6],[-1,-2,-3]])
show("A="+ latex(A))
# Concatenates the string "A=" with the LaTeX representation of A.
Output
2. B=matrix(QQ,3,3,[1..9]);show("B="+ latex(B))
Output
Commands:
Transpose of a Matrix
The transpose of a matrix is a new matrix obtained by flipping A over its diagonal.
This means the rows of A become the columns of the transposed matrix, and
Example
show("Transpose of B="+latex(B.T))
Output
Inverse
Command: A.inverse()
Example:
A=matrix(QQ,[[1,10,11],[4,5,6],[-1,-2,-3]])
show("A="+latex(A))
B=A.inverse()
show(B)
Output
In SageMath, both print and show are used to display output, but they have
different purposes and presentation styles.
Example:
A = Matrix([[1, 2], [3, 4]])
print("Matrix A:")
print(A)
Output
Matrix A
[1 2]
[3 4]
Show
Example
A = Matrix([[1, 2], [3, 4]])
show("Matrix A:")
show(A)
Output
# Print results
print("Matrix A:")
print(A)
print("\nMatrix B:")
print(B)
print("\nMatrix C (A + B):")
print(C)
print("\nMatrix D (A * B):")
print(D)
print("\nTranspose of A (A^T):")
print(A_T)
print("\nInverse of A (A^-1):")
print(A_inv)
Here \n within the string ensures that "Matrix B:" is printed on a new
line.
Output:
Matrix A:
[1 2]
[3 4]
Matrix B:
[2 0]
[1 3]
Matrix C (A + B):
[3 2]
[4 7]
Matrix D (A * B):
[ 4 6]
[10 12]
Transpose of A (A^T):
[1 3]
[2 4]
Inverse of A (A^-1):
[ -2 1]
[ 3/2 -1/2]
Code:
from sage.all import *
A = Matrix(QQ, [[2, 1], [1, 3]]) # Define the matrix A
Output
Solution X:
(1, 2)
Explanation: