0% found this document useful (0 votes)
10 views7 pages

Maths

The document presents a report on Diagonalization and LU Factorization in the context of Digital Electronics and Communication. It includes Python code for diagonalizing a matrix and performing LU factorization, along with methods for solving linear equations using these techniques. The report is submitted by Kamalesh B Maharshi from Ramaiah Institute of Technology.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Maths

The document presents a report on Diagonalization and LU Factorization in the context of Digital Electronics and Communication. It includes Python code for diagonalizing a matrix and performing LU factorization, along with methods for solving linear equations using these techniques. The report is submitted by Kamalesh B Maharshi from Ramaiah Institute of Technology.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MASTER OF TECHNOLOGY

In

DIGITAL ELECTRONICS AND COMMUNICATION

Report On:
DIAGONALIZATION & LU FACTORIZATION

Submitted by:
KAMALESH B MAHARSHI

Bearing USN: 1MS24LEC014-T

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)

Dept of ECE (Digital Electronics & Communication) 2 Ramaiah institute of technology


Diagonalization & LU Factorization

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:

Dept of ECE (Digital Electronics & Communication) 3 Ramaiah institute of technology


Diagonalization & LU Factorization

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):

Dept of ECE (Digital Electronics & Communication) 4 Ramaiah institute of technology


Diagonalization & LU Factorization

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: "))

Dept of ECE (Digital Electronics & Communication) 5 Ramaiah institute of technology


Diagonalization & LU Factorization

cols = int(input("Enter the number of columns: "))


if rows != cols:
print("LU decomposition requires a square matrix.")
else:
print("Enter the matrix elements row-wise:")
A = np.zeros((rows, cols))
for i in range(rows):
A[i] = list(map(float, input().split()))
print("Enter the right-hand side vector B:")
B = np.array(list(map(float, input().split())))
try:
L, U, Y, X = solve_lu(A, B)
np.set_printoptions(precision=4, suppress=True) # Formatting for cleaner output
print("\nLower Triangular Matrix L:")
print(L)
print("\nUpper Triangular Matrix U:")
print(U)
print("\nIntermediate Solution Vector Y (LY = B):")
print(Y)
print("\nFinal Solution Vector X (UX = Y):")
print(X)
except ValueError as e:
print(f"Error: {e}")

Dept of ECE (Digital Electronics & Communication) 6 Ramaiah institute of technology


Diagonalization & LU Factorization

Example1:

Example2:

Dept of ECE (Digital Electronics & Communication) 7 Ramaiah institute of technology

You might also like