Skip to content

Vectorize np.linalg.qr #11741

@oleg-kachan

Description

@oleg-kachan

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)]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions