summaryrefslogtreecommitdiff
path: root/numpy/linalg
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
parent9ea7dc1cfe1a75686f1c2a2724b91a69df256be0 (diff)
downloadnumpy-1dba64cb3f7102bffc43c06cc4822ec8f8484bc1.tar.gz
linalg: support '0-norm' (scipy ticket #1037)
Diffstat (limited to 'numpy/linalg')
-rw-r--r--numpy/linalg/linalg.py7
-rw-r--r--numpy/linalg/tests/test_linalg.py2
2 files changed, 7 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:
diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py
index 849f30a50..01e96d6a2 100644
--- a/numpy/linalg/tests/test_linalg.py
+++ b/numpy/linalg/tests/test_linalg.py
@@ -282,6 +282,7 @@ class _TestNorm(TestCase):
decimal=self.dec)
np.testing.assert_almost_equal(norm(v,-2), ((205./144)**-0.5),
decimal=self.dec)
+ np.testing.assert_almost_equal(norm(v,0), 4, decimal=self.dec)
for v in (a, b, c,):
_test(v)
@@ -304,6 +305,7 @@ class _TestNorm(TestCase):
self.assertRaises(ValueError, norm, A, 'nofro')
self.assertRaises(ValueError, norm, A, -3)
+ self.assertRaises(ValueError, norm, A, 0)
class TestNormDouble(_TestNorm):
dt = np.double