|
54 | 54 | import math |
55 | 55 | import numpy as np |
56 | 56 | from numpy import all, angle, any, array, asarray, concatenate, cos, delete, \ |
57 | | - dot, empty, exp, eye, matrix, ones, poly, poly1d, roots, shape, sin, \ |
| 57 | + dot, empty, exp, eye, matrix, ones, pi, poly, poly1d, roots, shape, sin, \ |
58 | 58 | zeros, squeeze |
59 | 59 | from numpy.random import rand, randn |
60 | 60 | from numpy.linalg import solve, eigvals, matrix_rank |
|
69 | 69 |
|
70 | 70 | __all__ = ['StateSpace', 'ss', 'rss', 'drss', 'tf2ss', 'ssdata'] |
71 | 71 |
|
| 72 | + |
| 73 | +def _matrix(a): |
| 74 | + """_matrix(a) -> numpy.matrix |
| 75 | + a - passed to numpy.matrix |
| 76 | + Wrapper around numpy.matrix; unlike that function, _matrix([]) will be 0x0 |
| 77 | + """ |
| 78 | + from numpy import matrix |
| 79 | + am = matrix(a) |
| 80 | + if (1,0) == am.shape: |
| 81 | + am.shape = (0,0) |
| 82 | + return am |
| 83 | + |
| 84 | + |
72 | 85 | class StateSpace(LTI): |
73 | 86 | """StateSpace(A, B, C, D[, dt]) |
74 | 87 |
|
@@ -130,7 +143,7 @@ def __init__(self, *args): |
130 | 143 | else: |
131 | 144 | raise ValueError("Needs 1 or 4 arguments; received %i." % len(args)) |
132 | 145 |
|
133 | | - A, B, C, D = map(matrix, [A, B, C, D]) |
| 146 | + A, B, C, D = [_matrix(M) for M in (A, B, C, D)] |
134 | 147 |
|
135 | 148 | # TODO: use super here? |
136 | 149 | LTI.__init__(self, inputs=D.shape[1], outputs=D.shape[0], dt=dt) |
@@ -336,7 +349,7 @@ def __rmul__(self, other): |
336 | 349 |
|
337 | 350 | # try to treat this as a matrix |
338 | 351 | try: |
339 | | - X = matrix(other) |
| 352 | + X = _matrix(other) |
340 | 353 | C = X * self.C |
341 | 354 | D = X * self.D |
342 | 355 | return StateSpace(self.A, self.B, C, D, self.dt) |
@@ -727,11 +740,9 @@ def _convertToStateSpace(sys, **kw): |
727 | 740 |
|
728 | 741 | # If this is a matrix, try to create a constant feedthrough |
729 | 742 | try: |
730 | | - D = matrix(sys) |
731 | | - outputs, inputs = D.shape |
732 | | - |
733 | | - return StateSpace(0., zeros((1, inputs)), zeros((outputs, 1)), D) |
734 | | - except Exception(e): |
| 743 | + D = _matrix(sys) |
| 744 | + return StateSpace([], [], [], D) |
| 745 | + except Exception as e: |
735 | 746 | print("Failure to assume argument is matrix-like in" \ |
736 | 747 | " _convertToStateSpace, result %s" % e) |
737 | 748 |
|
|
0 commit comments