summaryrefslogtreecommitdiff
path: root/numpy/linalg/linalg.py
diff options
context:
space:
mode:
authorPauli Virtanen <pav@iki.fi>2009-11-06 09:59:50 +0000
committerPauli Virtanen <pav@iki.fi>2009-11-06 09:59:50 +0000
commit1dba64cb3f7102bffc43c06cc4822ec8f8484bc1 (patch)
tree6ed20aac3b944255836d5fb3afe7c89cede65126 /numpy/linalg/linalg.py
parent9ea7dc1cfe1a75686f1c2a2724b91a69df256be0 (diff)
downloadnumpy-1dba64cb3f7102bffc43c06cc4822ec8f8484bc1.tar.gz
linalg: support '0-norm' (scipy ticket #1037)
Diffstat (limited to 'numpy/linalg/linalg.py')
-rw-r--r--numpy/linalg/linalg.py7
1 files changed, 5 insertions, 2 deletions
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py
index 287b38d9b..0b7aa132e 100644
--- a/numpy/linalg/linalg.py
+++ b/numpy/linalg/linalg.py
@@ -1674,7 +1674,7 @@ def norm(x, ord=None):
Notes
-----
- For values of ``ord < 0``, the result is, strictly speaking, not a
+ For values of ``ord <= 0``, the result is, strictly speaking, not a
mathematical 'norm', but it may still be useful for various numerical
purposes.
@@ -1687,6 +1687,7 @@ def norm(x, ord=None):
'fro' Frobenius norm --
inf max(sum(abs(x), axis=1)) max(abs(x))
-inf min(sum(abs(x), axis=1)) min(abs(x))
+ 0 -- sum(x != 0)
1 max(sum(abs(x), axis=0)) as below
-1 min(sum(abs(x), axis=0)) as below
2 2-norm (largest sing. value) as below
@@ -1754,15 +1755,17 @@ def norm(x, ord=None):
"""
x = asarray(x)
- nd = len(x.shape)
if ord is None: # check the default case first and handle it immediately
return sqrt(add.reduce((x.conj() * x).ravel().real))
+ nd = x.ndim
if nd == 1:
if ord == Inf:
return abs(x).max()
elif ord == -Inf:
return abs(x).min()
+ elif ord == 0:
+ return (x != 0).sum() # Zero norm
elif ord == 1:
return abs(x).sum() # special case for speedup
elif ord == 2: