[Scipy-svn] r6365 - in trunk: doc/release doc/source scipy/linalg scipy/linalg/tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Sat May 1 18:26:34 EDT 2010
Author: warren.weckesser
Date: 2010-05-01 17:26:34 -0500 (Sat, 01 May 2010)
New Revision: 6365
Modified:
trunk/doc/release/0.8.0-notes.rst
trunk/doc/source/linalg.rst
trunk/scipy/linalg/info.py
trunk/scipy/linalg/special_matrices.py
trunk/scipy/linalg/tests/test_special_matrices.py
Log:
ENH: linalg: Added a function to create a Leslie matrix.
Modified: trunk/doc/release/0.8.0-notes.rst
===================================================================
--- trunk/doc/release/0.8.0-notes.rst 2010-04-30 16:38:37 UTC (rev 6364)
+++ trunk/doc/release/0.8.0-notes.rst 2010-05-01 22:26:34 UTC (rev 6365)
@@ -108,8 +108,8 @@
New functions and other changes in scipy.linalg
-----------------------------------------------
-The functions `circulant`, `hadamard` and `cho_solve_banded` were added
-to `scipy.linalg`.
+The functions `cho_solve_banded`, `circulant`, `hadamard` and `leslie`
+were added to `scipy.linalg`.
The function `block_diag` was enhanced to accept scalar and 1D arguments,
along with the usual 2D arguments.
Modified: trunk/doc/source/linalg.rst
===================================================================
--- trunk/doc/source/linalg.rst 2010-04-30 16:38:37 UTC (rev 6364)
+++ trunk/doc/source/linalg.rst 2010-05-01 22:26:34 UTC (rev 6365)
@@ -87,6 +87,7 @@
hadamard
hankel
kron
+ leslie
toeplitz
tri
tril
Modified: trunk/scipy/linalg/info.py
===================================================================
--- trunk/scipy/linalg/info.py 2010-04-30 16:38:37 UTC (rev 6364)
+++ trunk/scipy/linalg/info.py 2010-05-01 22:26:34 UTC (rev 6365)
@@ -65,6 +65,7 @@
hadamard --- Hadamard matrix of order 2^n
hankel --- Hankel matrix
kron --- Kronecker product of two arrays.
+ leslie --- Leslie matrix
toeplitz --- Toeplitz matrix
tri --- Construct a matrix filled with ones at and below a given diagonal.
tril --- Construct a lower-triangular matrix from a given matrix.
Modified: trunk/scipy/linalg/special_matrices.py
===================================================================
--- trunk/scipy/linalg/special_matrices.py 2010-04-30 16:38:37 UTC (rev 6364)
+++ trunk/scipy/linalg/special_matrices.py 2010-05-01 22:26:34 UTC (rev 6365)
@@ -320,6 +320,54 @@
return H
+
+def leslie(f, s):
+ """Create a Leslie matrix.
+
+ Parameters
+ ----------
+ f : array-like, 1D
+ The "fecundity" coefficients.
+ s : array-like, 1D
+ The "survival" coefficients. The length of `s` must be one less
+ than the length of `f`, and it must be at least 1.
+
+ Returns
+ -------
+ L : ndarray, 2D
+ Returns a 2D numpy ndarray with shape `(n,n)`, where `n` is the
+ length of `f`. The array is zero except for the first row,
+ which is `f`, and the first subdiagonal, which is `s`.
+ The data type of the array will be the data type of `f[0]+s[0]`.
+
+ Examples
+ --------
+ >>> leslie([0.1, 2.0, 1.0, 0.1], [0.2, 0.8, 0.7])
+ array([[ 0.1, 2. , 1. , 0.1],
+ [ 0.2, 0. , 0. , 0. ],
+ [ 0. , 0.8, 0. , 0. ],
+ [ 0. , 0. , 0.7, 0. ]])
+ """
+ f = np.atleast_1d(f)
+ s = np.atleast_1d(s)
+ if f.ndim != 1:
+ raise ValueError("Incorrect shape for f. f must be one-dimensional")
+ if s.ndim != 1:
+ raise ValueError("Incorrect shape for s. s must be one-dimensional")
+ if f.size != s.size + 1:
+ raise ValueError("Incorrect lengths for f and s. The length"
+ " of s must be one less than the length of f.")
+ if s.size == 0:
+ raise ValueError("The length of s must be at least 1.")
+
+ tmp = f[0] + s[0]
+ n = f.size
+ a = np.zeros((n,n), dtype=tmp.dtype)
+ a[0] = f
+ a[range(1,n), range(0,n-1)] = s
+ return a
+
+
def all_mat(*args):
return map(np.matrix,args)
Modified: trunk/scipy/linalg/tests/test_special_matrices.py
===================================================================
--- trunk/scipy/linalg/tests/test_special_matrices.py 2010-04-30 16:38:37 UTC (rev 6364)
+++ trunk/scipy/linalg/tests/test_special_matrices.py 2010-05-01 22:26:34 UTC (rev 6365)
@@ -3,8 +3,8 @@
from numpy import arange, add, array, eye, all, copy
from numpy.testing import *
-from scipy.linalg import toeplitz, hankel, circulant, hadamard, tri, triu, tril, \
- kron, block_diag
+from scipy.linalg import toeplitz, hankel, circulant, hadamard, leslie, \
+ tri, triu, tril, kron, block_diag
def get_mat(n):
@@ -173,6 +173,23 @@
assert_raises(ValueError, hadamard, 5)
+class TestLeslie(TestCase):
+
+ def test_bad_shapes(self):
+ assert_raises(ValueError, leslie, [[1,1],[2,2]], [3,4,5])
+ assert_raises(ValueError, leslie, [3,4,5], [[1,1],[2,2]])
+ assert_raises(ValueError, leslie, [1,2], [1,2])
+ assert_raises(ValueError, leslie, [1], [])
+
+ def test_basic(self):
+ a = leslie([1, 2, 3], [0.25, 0.5])
+ expected = array([
+ [1.0, 2.0, 3.0],
+ [0.25, 0.0, 0.0],
+ [0.0, 0.5, 0.0]])
+ assert_array_equal(a, expected)
+
+
class TestBlockDiag:
def test_basic(self):
x = block_diag(eye(2), [[1,2], [3,4], [5,6]], [[1, 2, 3]])
More information about the Scipy-svn
mailing list