I am new to Python 3 and currently learning about LU Decomposition and how to express its functions in Python. We were given as a class a question that involves writing a code in order to find the LU decomposition of a matrix A (using scipy) as well as the norm of 'L+U' within that decomposition. I understand how to get P L and U with scipy.linalg.lu but I am having trouble understanding how to add L and U together after getting that answer. this is my code so far...
from numpy import array, sqrt
import scipy as sp
from scipy.linalg import norm
def scipy_LU(A):
Q = sp.linalg.lu(A)
print(Q)
elements_of_A = list(map(float, input(). split(' ')))
order = int(sqrt(len(elements_of_A)))
A = array(elements_of_A).reshape(order, order)
print(round(norm(L+U, 1), 4))
The return of scipy.linalg.lu is three separate matrices: P, L, and U. Rather than assigning them as a tuple to the variable Q, you can assign it to 3 variables, compute the addition, and then compute the norm:
def norm_LU(A):
P, L, U = sp.linalg.lu(A)
L_plus_U = L + U
return norm(L_plus_U)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With