Workshop on Graph Theory and Linear Algebra using
SageMath
GTN ARTS College
22.01.2025
Getting Started with SageMath for Linear Algebra
Module 2
Title: Matrix Theory with SageMath
Objectives
• Learn matrix basics using SageMath.
• Perform matrix computations and operations.
• Explore advanced concepts: eigenvalues, decompositions, and sparse
matrices.
• Hands-on activities for practical understanding.
Why Use SageMath for Matrix Theory?
• Extensive matrix manipulation tools
• Symbolic computation and visualization capabilities
• Supports diverse applications in science, engineering, and data analysis
Creating Matrices in SageMath
Basic matrix
• M = Matrix([[1, 2], [3, 4]])
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]
Zero and identity matrices
zero_matrix(3, 3)
Output
[0 0 0]
[0 0 0]
[0 0 0]
I = identity_matrix(3)
Output
[1 0 0]
[0 1 0]
[0 0 1]
To print the Matrix
(Use print())
Example:
A=matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(A)
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:
• show(B)------Also show the matrix B
Transpose of a Matrix
In SageMath, A.T is used to obtain the transpose of a matrix A.
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
the columns of A become its rows
Example
B=matrix(QQ,3,3,[1..9]); show("B="+ latex(B))
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
Difference between print and show
In SageMath, both print and show are used to display output, but they have
different purposes and presentation styles.
print
• Purpose: Displays plain text output
• Usage: Primarily for debugging, logging, or when you don't require
mathematical formatting
• Output Style: Displays content as plain text without any additional
formatting
Example:
A = Matrix([[1, 2], [3, 4]])
print("Matrix A:")
print(A)
Output
Matrix A
[1 2]
[3 4]
Show
• Purpose: Displays output with rich formatting, especially useful for
mathematical expressions
• Usage: Ideal for presenting mathematical content in a human-readable,
nicely formatted style (e.g., LaTeX-style rendering)
• Output Style: Displays matrices, equations, and other mathematical
content in a visually appealing way, often rendered with LaTeX in Jupyter
Notebooks or SageMath's GUI
Example
A = Matrix([[1, 2], [3, 4]])
show("Matrix A:")
show(A)
Output
Basic Matrix Operations
# Basic Matrix Operations
A = Matrix([[1, 2], [3, 4]])
B = Matrix([[2, 0], [1, 3]])
# Addition and multiplication
C=A+B
D=A*B
# Transpose and inverse
A_T = A.transpose()
A_inv = A.inverse()
# 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]
Solving Linear Equations
Code:
from sage.all import *
A = Matrix(QQ, [[2, 1], [1, 3]]) # Define the matrix A
B = vector(QQ, [4, 7]) # Define the vector B
X = A.solve_right(B) # Solve for X
print("Solution X:") # Print the solution
print(X)
Output
Solution X:
(1, 2)
Explanation:
1. Import necessary library:
o from sage.all import *: This line imports all the necessary functions
and classes from the SageMath library, which is a powerful open-
source mathematics software system.
2. Define the matrix A:
o A = Matrix(QQ, [[2, 1], [1, 3]]): This creates a 2x2 matrix A with
rational number entries (QQ represents the field of rational
numbers).
3. Define the vector B:
o B = vector(QQ, [4, 7]): This creates a column vector B with two
rational number components.
4. Solve for X:
o X = A.solve_right(B): This line uses the solve_right() method of the
matrix A to solve the linear equation AX = B for the unknown vector
X. The solve_right() method finds the solution by calculating the
right inverse of matrix A and multiplying it with vector B.
5. Print the solution:
o print("Solution X:") and print(X): These lines print the calculated
solution vector X to the console.
# Define matrices in SageMath
A = Matrix([[1, 2], [3, 4]])
B = Matrix([[5, 6], [7, 8]])
# Construct the block matrix
C = block_matrix([[A, B], [B, A]])
# Display the result
print(C)
show(C)
Output