Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the norm for 'L + U' in LU decomposition

Tags:

python

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))
like image 813
villematicc Avatar asked Dec 07 '25 08:12

villematicc


1 Answers

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)
like image 184
James Avatar answered Dec 09 '25 22:12

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!