|
From: <kk...@us...> - 2011-02-08 22:13:28
|
Revision: 44
http://python-control.svn.sourceforge.net/python-control/?rev=44&view=rev
Author: kkchen
Date: 2011-02-08 22:13:22 +0000 (Tue, 08 Feb 2011)
Log Message:
-----------
Added tests for valid inputs to StateSpace in statesp.py.
Kevin K. Chen <kk...@pr...>
Modified Paths:
--------------
branches/control-0.4a/src/statesp.py
Modified: branches/control-0.4a/src/statesp.py
===================================================================
--- branches/control-0.4a/src/statesp.py 2011-02-08 22:13:17 UTC (rev 43)
+++ branches/control-0.4a/src/statesp.py 2011-02-08 22:13:22 UTC (rev 44)
@@ -60,6 +60,28 @@
"""
def __init__(self, A, B, C, D):
+ # Here we're going to convert inputs to matrices, if the user gave a non
+ # 2-D array or matrix type.
+ matrices = [A, B, C, D]
+ for i in range(len(matrices)):
+ if (isinstance(matrices[i], (int, long, float, complex))):
+ # Convert scalars to matrices, if necessary.
+ matrices[i] = sp.matrix(matrices[i])
+ elif isinstance(matrices[i], sp.ndarray):
+ # Convert 0- or 1-D arrays to matrices, if necessary.
+ if len(matrices[i].shape) < 2:
+ matrices[i] = sp.matrix(matrices[i])
+ elif len(matrices[i].shape) == 2:
+ # If we're already a 2-D array or a matrix, then perfect!
+ pass
+ else:
+ raise ValueError("A, B, C, and D cannot have > 2 \
+ dimensions.")
+ else:
+ # If the user gave us a non-numeric type.
+ raise ValueError("A, B, C, and D must be arrays or matrices.")
+ [A, B, C, D] = matrices
+
self.A = A
self.B = B
self.C = C
@@ -69,11 +91,6 @@
self.inputs = B.shape[1]
self.outputs = C.shape[0]
- # Check that the inputs are arrays or matrices.
- if not (isinstance(A, sp.ndarray) and isinstance(B, sp.ndarray) and
- isinstance(C, sp.ndarray) and isinstance(D, sp.ndarray)):
- raise ValueError("A, B, C, and D must be arrays or matrices.")
-
# Check that the matrix sizes are consistent.
if self.states != A.shape[1]:
raise ValueError("A must be square.")
@@ -389,4 +406,4 @@
C = C * Cmask
D = D * Dmask
- return StateSpace(A, B, C, D)
\ No newline at end of file
+ return StateSpace(A, B, C, D)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|