NAME:R.
RAJAKUMARAN
REG.NO:2403917710421071
CSE-A(II-SEM)
MATHS PYTHON ASSIGNMENT
1. Check Linear Independence
import numpy as np
from sympy import Matrix
# Example: Replace with your actual vectors
vectors = [[1, 2, 3], [2, 4, 6], [1, 0, 1]]
A = Matrix(vectors).T
print("Linearly Independent:" if A.rank() ==len(vectors) else "Linearly Dependent")
2. Check if Vectors Span R³
# Same idea, check if rank is 3
vectors = [[1, 0, 2], [0, 1, 1], [1, 1, 3]]
A = Matrix(vectors).T
print("Spans R3:" if A.rank() == 3 else "Does NOT Span R3")
3. Reduced Row Echelon Form and Basis
A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rref, pivots = A.rref()
print("RREF:\n", rref)
print("Row Space Basis:", A.rowspace())
print("Column Space Basis:", A.columnspace())
print("Null Space Basis:", A.nullspace())
4. Angle Between Vectors
import numpy as np
from numpy.linalg import norm
from math import acos, degrees
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])
cos_theta = np.dot(v1, v2) / (norm(v1) * norm(v2))
angle = degrees(acos(cos_theta))
print("Angle in degrees:", angle)
5. Orthonormal Basis using Gram-Schmidt
def gram_schmidt(vectors):
ortho = []
for v in vectors:
for u in ortho:
v -= np.dot(v, u) * u
if np.linalg.norm(v) > 1e-10:
ortho.append(v / np.linalg.norm(v))
return ortho
vectors = [np.array([1, 1, 0]), np.array([1, 0, 1])]
orthonormal = gram_schmidt(vectors)
print("Orthonormal Basis:", orthonormal)
6. Matrix of Linear Transformation w.r.t. Basis
You'll need to define the transformation and bases. Here's a skeleton:
B = [Matrix([2, 1]), Matrix([-1, 1])]
U = [Matrix([1, 1, 1]), Matrix([1, 1, -1]), Matrix([1, 0, 1])]
def transform(x):
x1, x2 = x
return Matrix([x1 - 2 * x2, 2 * x1 + x2, x1 + x2])
matrix = []
for b in B:
matrix.append(transform(b))
print("Transformation Matrix:\n", Matrix.hstack(*matrix))
7. Singular Value Decomposition (SVD)
from numpy.linalg import svd
A = np.array([[1, 2], [3, 4], [5, 6]])
U, S, VT = svd(A)
print("U:\n", U)
print("S:\n", S)
print("VT:\n", VT)
8. Diagonalize Matrix
from sympy import Matrix
A = Matrix([[1.5, 0.5, -2.5], [0.5, 2.5, -0.5], [-2.5, -0.5, 5.5]])
P, D = A.diagonalize()
print("Diagonal Matrix D:\n", D)
print("Matrix P (eigenvectors):\n", P)
9. Solve System using Gaussian Elimination
A = Matrix([[1, -1, 3, 0], [2, -1, 4, 3], [1, 1, 1, 3]])
rref, pivots = A.rref()
print("Solution from RREF:\n", rref)
10. Inverse using Gauss-Jordan
A = Matrix([[1.5, 0.5, -2.5], [0.5, 2.5, -0.5], [-2.5, -0.5, 5.5]])
A_inv = A.inv(method='GE')
print("Inverse Matrix:\n", A_inv)