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)
OUTPUT
var=0:
Original numpy result: [-1. 1.]
forward substitution: [ 1.e+00 -1.e+17]
backward substitution: [ 1.e+17 -0.e+00]
========================================
var=1:
Original numpy result: [-1. 1.]
forward substitution: [ 1.e+00 -1.e+10]
backward substitution: [ 1.e+10 -0.e+00]
========================================
var=2:
Original numpy result: [-1. 1.]
forward substitution: [ 1.e+00 -1.e+15]
backward substitution: [ 1.e+15 -0.e+00]
========================================
var=3:
Original numpy result: [-1. 1.]
forward substitution: [ 1.e+00 -1.e+16]
backward substitution: [ 1.e+16 -0.e+00]
========================================
que 2
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)
OUTPUT
Permutation matrix (P):
[[1. 0.]
[0. 1.]]
Lower triangular matrix (L):
[[1. 0.]
[1. 1.]]
Upper triangular matrix (U):
[[1. 1.]
[0. 0.]]
que 3
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)
output
For n = 20:
Norm of P*A - LU (with partial pivoting): 10.396618854146048
Norm of P*A - LU (using built-in LU): 7.411045294008804
For n = 40:
Norm of P*A - LU (with partial pivoting): 33.459489785968785
Norm of P*A - LU (using built-in LU): 16.121354452604585
For n = 60:
Norm of P*A - LU (with partial pivoting): 50.52685681554835
Norm of P*A - LU (using built-in LU): 24.2857593444543
For n = 100:
Norm of P*A - LU (with partial pivoting): 106.86776222146354
Norm of P*A - LU (using built-in LU): 41.053891037031946
que 4
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)
output