diff options
author | Jim <jim.verheijde@hotmail.com> | 2020-03-12 19:05:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-12 14:05:22 -0400 |
commit | 33351f646e0b523139fbba4986eba54a3bfe55a7 (patch) | |
tree | 39631903f6f388c2b89e151594dab45eb1d22b77 /numpy/linalg/linalg.py | |
parent | 32f382c86f384a02eeb87f67fea806e6b9961b5c (diff) | |
download | numpy-33351f646e0b523139fbba4986eba54a3bfe55a7.tar.gz |
MAINT: Add better error handling in linalg.norm for vectors and clarify it in documentation (#15740)
* Clarify `fro` and `nuc` usage in linalg.norm documentation
(see #15533).
* Add improved error handling when getting Frobenius norm from
a vector (see #15533).
* Fix comment in linalg norm test.
Closes gh-15533.
Diffstat (limited to 'numpy/linalg/linalg.py')
-rw-r--r-- | numpy/linalg/linalg.py | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 85f714ebf..eb03932e1 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -2434,6 +2434,9 @@ def norm(x, ord=None, axis=None, keepdims=False): The nuclear norm is the sum of the singular values. + Both the Frobenius and nuclear norm orders are only defined for + matrices and raise a ValueError when ``x.ndim != 2``. + References ---------- .. [1] G. H. Golub and C. F. Van Loan, *Matrix Computations*, @@ -2556,11 +2559,11 @@ def norm(x, ord=None, axis=None, keepdims=False): # special case for speedup s = (x.conj() * x).real return sqrt(add.reduce(s, axis=axis, keepdims=keepdims)) + # None of the str-type keywords for ord ('fro', 'nuc') + # are valid for vectors + elif isinstance(ord, str): + raise ValueError(f"Invalid norm order '{ord}' for vectors") else: - try: - ord + 1 - except TypeError: - raise ValueError("Invalid norm order for vectors.") absx = abs(x) absx **= ord ret = add.reduce(absx, axis=axis, keepdims=keepdims) |