question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vectorize np.linalg.qr

See original GitHub issue

QR decomposition is not vectorized, i.e. expects only 2-dimensional arrays as input, while you can pass 3-dimensional arrays (a number of matrices along 0-axis) to SVD:

Reproducing code example:

This gives error:

import numpy as np

A = np.random.normal(size=(3,2,2))
[Q, R] = np.linalg.qr(A)

while SVD returns proper arrays U, S and V

[U, S, V] = np.linalg.svd(A)

Error message:

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
<ipython-input-237-7b6d808ca118> in <module>()
----> 1 np.linalg.qr(A)

~/lib/python3.6/site-packages/numpy/linalg/linalg.py in qr(a, mode)
    858 
    859     a, wrap = _makearray(a)
--> 860     _assertRank2(a)
    861     _assertNoEmpty2d(a)
    862     m, n = a.shape

~/lib/python3.6/site-packages/numpy/linalg/linalg.py in _assertRank2(*arrays)
    196         if a.ndim != 2:
    197             raise LinAlgError('%d-dimensional array given. Array must be '
--> 198                     'two-dimensional' % a.ndim)
    199 
    200 def _assertRankAtLeast2(*arrays):

LinAlgError: 3-dimensional array given. Array must be two-dimensional

Numpy/Python version information:

1.15.0 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 17:02:57) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
eric-wiesercommented, Jun 1, 2021

Yes, that’s the right approach I think.

1reaction
czgdp1807commented, Jun 1, 2021

Hi @eric-wieser Thanks for the direction. I just started today with numpy and made some changes (at Python level) for allowing computation of results for matrices with higher than two dimensions. I will undo those changes and follow the pattern similar to lstsq. Could you please confirm if I should add qr in numpy/linalg/umath_linalg.c.src (see below for the location in the file I am referring too) as is the case with other linalg functions? Thanks.

https://github.com/numpy/numpy/blob/6790873334b143117f4e8d1f515def8c7fdeb9fb/numpy/linalg/umath_linalg.c.src#L3166

and other places where lstsq, svd are referred in the same file?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vectorize QR in Numpy Python - Stack Overflow
vecQR = np.vectorize(np.linalg.qr, signature='(m,n)->(m,p),(p,n)') ... How about just map np.linalg.qr to the 1st axis of the arr?:
Read more >
numpy.linalg.qr — NumPy v1.24 Manual
Compute the qr factorization of a matrix. Factor the matrix a as qr, where q is orthonormal and r is upper-triangular. Parameters: ......
Read more >
QR Decomposition with Python and NumPy - QuantStart
Essentially, we use this method because we want to create an upper triangular matrix, R . The householder reflection is able to carry...
Read more >
scipy.linalg.qr — SciPy v1.9.3 Manual
Compute QR decomposition of a matrix. Calculate the decomposition A = Q R where Q is unitary/orthogonal and R upper triangular.
Read more >
numpy, linear algebra, vectorization
qr or scipy.linalg.qr. We can use both in the same session: >>> from numpy.linalg import qr as npqr. > ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found