Maths
Maths
In
Report On:
DIAGONALIZATION & LU FACTORIZATION
Submitted by:
KAMALESH B MAHARSHI
Department of Mathematics
RAMAIAH INSTITUTE OF TECHNOLOGY
(Autonomous Institute, Affiliated to VTU)
Accredited by National Board of Accreditation & NAAC with
‘A” Grade MSR Nagar, MSRIT Post, Bangalore – 560054
www.msrit.edu
Diagonalization & LU Factorization
[1]. Diagonalization
Python code:
import numpy as np
def diagonalize_and_power():
# Take matrix size as input
n = int(input("Enter the size of the square matrix (n x n): "))
# Initialize an empty matrix
matrix = []
print(f"Enter the {n}x{n} matrix row by row (space-separated values):")
for i in range(n):
row = list(map(float, input().split()))
if len(row) != n:
print("Invalid input! Please enter exactly", n, "values.")
return
matrix.append(row)
# Convert list to NumPy array
A = np.array(matrix)
# Compute eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
# Construct diagonal matrix from eigenvalues
D = np.diag(eigenvalues)
# Compute inverse of eigenvectors matrix
try:
P_inv = np.linalg.inv(eigenvectors)
except np.linalg.LinAlgError:
print("The matrix is not diagonalizable (P is singular).")
return
# Take power input
p = int(input("Enter the power to raise the matrix to: "))
# Compute D^p (power of diagonal matrix)
D_power = np.diag(eigenvalues ** p)
# Compute A^p using A^p = P * D^p * P_inv
A_power = eigenvectors @ D_power @ P_inv
print("\nOriginal Matrix (A):")
print(A)
print("\nEigenvalues (Diagonal Matrix D):")
print(D)
print("\nEigenvectors (P):")
print(eigenvectors)
print("\nP Inverse:")
print(P_inv)
print(f"\nMatrix A raised to power {p} (A^{p}):")
print(A_power)
# Run the function
diagonalize_and_power()
Example1:
Example2:
[2]. LU FACTORIZATION
Python code:
import numpy as np
def lu_factorization(A):
"""Performs LU factorization using Doolittle’s method (Rout's approach)."""
n = A.shape[0]
L = np.zeros((n, n))
U = np.zeros((n, n))
for i in range(n):
# Upper Triangular Matrix U
for k in range(i, n):
sum_ = sum(L[i][j] * U[j][k] for j in range(i))
U[i][k] = A[i][k] - sum_
# Lower Triangular Matrix L
for k in range(i, n):
if i == k:
L[i][i] = 1 # Diagonal as 1
else:
sum_ = sum(L[k][j] * U[j][i] for j in range(i))
if U[i][i] == 0:
raise ValueError("Matrix is singular or near singular, cannot perform LU decomposition.")
L[k][i] = (A[k][i] - sum_) / U[i][i]
return L, U
def forward_substitution(L, B):
"""Solves LY = B using forward substitution."""
n = L.shape[0]
Y = np.zeros(n)
for i in range(n):
Y[i] = (B[i] - np.dot(L[i, :i], Y[:i])) / L[i, i]
return Y
def backward_substitution(U, Y):
"""Solves UX = Y using backward substitution."""
n = U.shape[0]
X = np.zeros(n)
for i in range(n-1, -1, -1):
X[i] = (Y[i] - np.dot(U[i, i+1:], X[i+1:])) / U[i, i]
return X
def solve_lu(A, B):
"""Solves AX = B using LU decomposition."""
L, U = lu_factorization(A)
# Solve LY = B (forward substitution)
Y = forward_substitution(L, B)
# Solve UX = Y (backward substitution)
X = backward_substitution(U, Y)
return L, U, Y, X
# User Input
rows = int(input("Enter the number of rows: "))
Example1:
Example2: