KEMBAR78
Lab 2 | PDF | Mathematical Physics | Algorithms
0% found this document useful (0 votes)
5 views4 pages

Lab 2

euifyiuiucheuled ed liadhae eui kadj w3 ewi

Uploaded by

mahendiayashu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lab 2

euifyiuiucheuled ed liadhae eui kadj w3 ewi

Uploaded by

mahendiayashu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

que 1

import numpy as np

def lot(space):
thr = space.shape[0]
her = np.eye(thr)
tur = np.zeros_like(space)
for var in range(thr):
for m in range(var, thr):
tur[var, m] = space[var, m] - np.dot(her[var, :var], tur[:var, m])

for o in range(var+1, thr):


her[o, var] = (space[o, var] - np.dot(her[o, :var], tur[:var, var])) /
tur[var, var]

return her, tur

def bc_subs(tur, plu):


thr_len = len(plu)
result = np.zeros(thr_len)
for var in range(thr_len-1, -1, -1):
result[var] = (plu[var] - np.dot(tur[var, var+1:], result[var+1:])) /
tur[var, var]

return result

def fwsubs(her, plu):


thr_len = len(plu)
result = np.zeros(thr_len)
for var in range(thr_len):
result[var] = (plu[var] - np.dot(her[var, :var], result[:var])) / her[var,
var]
return result

for var in range(4):


thr = 2
space = np.ones((thr, thr))
if var == 0:
space[0, 0] = 1e-17
elif var == 1:
space[0, 0] = 1e-10
elif var == 2:
space[0, 0] = 1e-15
else:
space[0, 0] = 1e-16

plu = np.array([1, 0])


her, tur = lot(space)
result_fwsubs = fwsubs(her, plu)
result_bcsubs = bc_subs(tur, plu)

print(f" var={var}:\n")
print("Original numpy result:", np.linalg.solve(space, plu))
print("forward substitution:", result_fwsubs)
print("backward substitution:", result_bcsubs)
print("="*40)

que2
import numpy as np
def fxone (A):
n = A.shape[0]
P = np.eye(n)

for k in range(n-1):
pivot_row = np.argmax(np.abs(A[k:, k])) + k
A[[k, pivot_row]] = A[[pivot_row, k]]
P[[k, pivot_row]] = P[[pivot_row, k]]

for i in range(k+1, n):


factor = A[i, k] / A[k, k]
A[i, k:] -= factor * A[k, k:]
A[i, k] = factor

L = np.eye(n) + np.tril(A, k=-1)


U = np.triu(A)

return P, L, U

A = np.ones((2, 2)) # Correct array initialization


P, L, U = fxone (A)

print("Permutation matrix (P):")


print(P)
print("\nLower triangular matrix (L):")
print(L)
print("\nUpper triangular matrix (U):")
print(U)

que3
import numpy as np
from scipy.linalg import lu, norm as linalg_norm

def my_lu_decomposition_with_pivot(A):
n = A.shape[0]
P_matrix = np.identity(n)
L_matrix = np.zeros((n, n))
U_matrix = A.copy()

for k in range(n - 1):


pivot_index = np.argmax(np.abs(U_matrix[k:, k])) + k

if pivot_index != k:
# Swap rows in U_matrix
U_matrix[[k, pivot_index]] = U_matrix[[pivot_index, k]]
# Swap rows in P_matrix
P_matrix[[k, pivot_index]] = P_matrix[[pivot_index, k]]

L_matrix[k, k] = 1.0
L_matrix[k+1:, k] = U_matrix[k+1:, k] / U_matrix[k, k]
U_matrix[k+1:, k:] -= np.outer(L_matrix[k+1:, k], U_matrix[k, k:])

L_matrix[-1, -1] = 1.0

return P_matrix, L_matrix, U_matrix

# Function to calculate the norm of P*A - LU


def norm_P_A_minus_LU(P_matrix, A, L_matrix, U_matrix):
return linalg_norm(np.dot(P_matrix, A) - np.dot(L_matrix, U_matrix))

# Set the values of n


n_values = [20, 40, 60, 100]

for n in n_values:
# Generate a random matrix A and modify the first element
A_custom = np.random.rand(n, n)
A_custom[0, 0] = 1e-20

# LU decomposition with partial pivoting


P_matrix_w, L_matrix_w, U_matrix_w = my_lu_decomposition_with_pivot(A_custom)

# LU decomposition without partial pivoting using NumPy's lu function


P_matrix_builtin, L_matrix_builtin, U_matrix_builtin = lu(A_custom)

# Calculate norms
norm_P_A_minus_LU_w = norm_P_A_minus_LU(P_matrix_w, A_custom, L_matrix_w,
U_matrix_w)
norm_P_A_minus_LU_builtin = norm_P_A_minus_LU(P_matrix_builtin, A_custom,
L_matrix_builtin, U_matrix_builtin)

# Display results
print(f"\nFor n = {n}:")
print("Norm of P*A - LU (with partial pivoting):", norm_P_A_minus_LU_w)
print("Norm of P*A - LU (using built-in LU):", norm_P_A_minus_LU_builtin)

que4
import numpy as np
from scipy.linalg import rref as scipy_rref

def with_pivot (A):


m, n = A.shape
Rref = A.copy()

for k in range(min(m, n)):


pivot_row = np.argmax(np.abs(Rref[k:, k])) + k
Rref[[k, pivot_row]] = Rref[[pivot_row, k]]

Rref[k, :] = Rref[k, :] / Rref[k, k]

for i in range(m):
if i != k:
factor = Rref[i, k] / Rref[k, k]
Rref[i, :] = Rref[i, :] - factor * Rref[k, :]

return Rref

A1_custom = np.random.rand(3, 4)
A2_custom = np.random.rand(4, 3)
A3_custom = np.random.rand(2, 2)
A4_custom = np.random.rand(5, 5)
A5_custom = np.random.rand(3, 2)

RREF1_custom = with_pivot (A1_custom)


RREF2_custom = with_pivot (A2_custom)
RREF3_custom = with_pivot (A3_custom)
RREF4_custom = with_pivot (A4_custom)
RREF5_custom = with_pivot (A5_custom)

RREF_builtin1, _ = scipy_rref(A1_custom)
RREF_builtin2, _ = scipy_rref(A2_custom)
RREF_builtin3, _ = scipy_rref(A3_custom)
RREF_builtin4, _ = scipy_rref(A4_custom)
RREF_builtin5, _ = scipy_rref(A5_custom)

You might also like