[Scipy-svn] r5292 - in trunk/scipy/sparse: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Sat Dec 27 16:44:25 EST 2008
Author: wnbell
Date: 2008-12-27 15:44:20 -0600 (Sat, 27 Dec 2008)
New Revision: 5292
Modified:
trunk/scipy/sparse/construct.py
trunk/scipy/sparse/tests/test_construct.py
Log:
fixed sparse.eye() with k != 0
resolves ticket #825
Modified: trunk/scipy/sparse/construct.py
===================================================================
--- trunk/scipy/sparse/construct.py 2008-12-27 11:31:13 UTC (rev 5291)
+++ trunk/scipy/sparse/construct.py 2008-12-27 21:44:20 UTC (rev 5292)
@@ -99,14 +99,16 @@
else:
return identity(n, dtype=dtype, format='csr').asformat(format)
+
def eye(m, n, k=0, dtype='d', format=None):
"""eye(m, n) returns a sparse (m x n) matrix where the k-th diagonal
is all ones and everything else is zeros.
"""
m,n = int(m),int(n)
- diags = np.ones((1, min(m,n)), dtype=dtype)
+ diags = np.ones((1, min(m + k, n)), dtype=dtype)
return spdiags(diags, k, m, n).asformat(format)
+
def kron(A, B, format=None):
"""kronecker product of sparse matrices A and B
Modified: trunk/scipy/sparse/tests/test_construct.py
===================================================================
--- trunk/scipy/sparse/tests/test_construct.py 2008-12-27 11:31:13 UTC (rev 5291)
+++ trunk/scipy/sparse/tests/test_construct.py 2008-12-27 21:44:20 UTC (rev 5292)
@@ -1,6 +1,6 @@
"""test sparse matrix construction functions"""
-import numpy
+import numpy as np
from numpy import array, matrix
from numpy.testing import *
@@ -83,6 +83,16 @@
assert_equal(eye(3,3,dtype='int16').dtype, 'int16')
+ assert_equal(eye(3, 4, -4).toarray(), np.eye(3, 4, -4))
+ assert_equal(eye(3, 4, -3).toarray(), np.eye(3, 4, -3))
+ assert_equal(eye(3, 4, -2).toarray(), np.eye(3, 4, -2))
+ assert_equal(eye(3, 4, -1).toarray(), np.eye(3, 4, -1))
+ assert_equal(eye(3, 4, 0).toarray(), np.eye(3, 4, 0))
+ assert_equal(eye(3, 4, 1).toarray(), np.eye(3, 4, 1))
+ assert_equal(eye(3, 4, 2).toarray(), np.eye(3, 4, 2))
+ assert_equal(eye(3, 4, 3).toarray(), np.eye(3, 4, 3))
+ assert_equal(eye(3, 4, 4).toarray(), np.eye(3, 4, 4))
+
def test_kron(self):
cases = []
@@ -103,7 +113,7 @@
for a in cases:
for b in cases:
result = kron(csr_matrix(a),csr_matrix(b)).todense()
- expected = numpy.kron(a,b)
+ expected = np.kron(a,b)
assert_array_equal(result,expected)
def test_kronsum(self):
@@ -121,8 +131,8 @@
for a in cases:
for b in cases:
result = kronsum(csr_matrix(a),csr_matrix(b)).todense()
- expected = numpy.kron(numpy.eye(len(b)), a) + \
- numpy.kron(b, numpy.eye(len(a)))
+ expected = np.kron(np.eye(len(b)), a) + \
+ np.kron(b, np.eye(len(a)))
assert_array_equal(result,expected)
def test_vstack(self):
More information about the Scipy-svn
mailing list