PAQ=LU pivoting factorization python programming

My code:

import scipy.linalg
import numpy as np
(p_mtrx, q_mtrx, l_mtrx, u_mtrx) = scipy.linalg.lu( a_mtrx )

np.set_printoptions(threshold=3)
np.set_printoptions(precision=3)

print('l_mtrx=\n',l_mtrx)
print('q_mtrx=\n',q_mtrx)
print('u_mtrx=\n',u_mtrx)
print('p_mtrx=\n',p_mtrx)
print('verification\n',p_mtrx@l_mtrx @ u_mtrx-a_mtrx)

and I got error:

not enough values to unpack(expexted 4 got 3)

Topic programming python

Category Data Science


scipy.linalg.lu only returns 2 or 3 items, and you are unpacking it into four - p, q, l and u. Not sure where you got that from or what you think q is. See docs here:

https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.lu.html

Here's an example. First make a 3x3 array:

>>> a_mtrx = np.array([[3,4,1],[6,1,2],[7,5,6]])

Do what you did - get your error (python 3):

>>> (p_mtrx, q_mtrx, l_mtrx, u_mtrx) = scipy.linalg.lu( a_mtrx )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 4, got 3)

Unpack the three values and no error:

>>> (p_mtrx, l_mtrx, u_mtrx) = scipy.linalg.lu( a_mtrx )

Check that the three values are a decomposition of the original. Multiply them:

>>> np.dot(np.dot(p_mtrx, l_mtrx), u_mtrx)
array([[3., 4., 1.],
       [6., 1., 2.],
       [7., 5., 6.]])

looks like:

>>> a_mtrx
array([[3, 4, 1],
       [6, 1, 2],
       [7, 5, 6]])

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.