# [Numpy-svn] r4069 - in trunk/numpy/linalg: . tests

numpy-svn@scip... numpy-svn@scip...
Fri Sep 21 00:24:45 CDT 2007

```Author: oliphant
Date: 2007-09-21 00:23:46 -0500 (Fri, 21 Sep 2007)
New Revision: 4069

Modified:
trunk/numpy/linalg/linalg.py
trunk/numpy/linalg/tests/test_linalg.py
Log:
Apply patch to fix ticket #557 (pinv causing error with empty arrays)

Modified: trunk/numpy/linalg/linalg.py
===================================================================
--- trunk/numpy/linalg/linalg.py	2007-09-21 04:25:10 UTC (rev 4068)
+++ trunk/numpy/linalg/linalg.py	2007-09-21 05:23:46 UTC (rev 4069)
@@ -20,7 +20,7 @@
intc, single, double, csingle, cdouble, inexact, complexfloating, \
newaxis, ravel, all, Inf, dot, add, multiply, identity, sqrt, \
maximum, flatnonzero, diagonal, arange, fastCopyAndTranspose, sum, \
-        isfinite
+        isfinite, size
from numpy.lib import triu
from numpy.linalg import lapack_lite

@@ -126,6 +126,11 @@
if not (isfinite(a).all()):
raise LinAlgError, "Array must not contain infs or NaNs"

+def _assertNonEmpty(*arrays):
+    for a in arrays:
+        if size(a) == 0:
+            raise LinAlgError("Arrays cannot be empty")
+
# Linear equations

def tensorsolve(a, b, axes=None):
@@ -718,6 +723,7 @@
"""
a, wrap = _makearray(a)
_assertRank2(a)
+    _assertNonEmpty(a)
m, n = a.shape
t, result_t = _commonType(a)
real_t = _linalgRealType(t)
@@ -783,6 +789,7 @@
rcond of the largest.
"""
a, wrap = _makearray(a)
+    _assertNonEmpty(a)
a = a.conjugate()
u, s, vt = svd(a, 0)
m = u.shape[0]

Modified: trunk/numpy/linalg/tests/test_linalg.py
===================================================================
--- trunk/numpy/linalg/tests/test_linalg.py	2007-09-21 04:25:10 UTC (rev 4068)
+++ trunk/numpy/linalg/tests/test_linalg.py	2007-09-21 05:23:46 UTC (rev 4069)
@@ -4,7 +4,7 @@
from numpy.testing import *
set_package_path()
from numpy import array, single, double, csingle, cdouble, dot, identity, \
-        multiply
+        multiply, atleast_2d
from numpy import linalg
restore_path()

@@ -37,6 +37,15 @@
b = array([2.+1j, 1.+2j], dtype=cdouble)
self.do(a, b)

+    def check_empty(self):
+        a = atleast_2d(array([], dtype = double))
+        b = atleast_2d(array([], dtype = double))
+        try:
+            self.do(a, b)
+            raise AssertionError("%s should fail with empty matrices", self.__name__[5:])
+        except linalg.LinAlgError, e:
+            pass
+
class test_solve(LinalgTestCase):
def do(self, a, b):
x = linalg.solve(a, b)

```