|
52 | 52 |
|
53 | 53 | import numpy as np
|
54 | 54 | from numpy import all, angle, any, array, asarray, concatenate, cos, delete, \
|
55 |
| - dot, empty, exp, eye, matrix, ones, pi, poly, poly1d, roots, shape, sin, \ |
| 55 | + dot, empty, exp, eye, ones, pi, poly, poly1d, roots, shape, sin, \ |
56 | 56 | zeros, squeeze
|
57 | 57 | from numpy.random import rand, randn
|
58 | 58 | from numpy.linalg import solve, eigvals, matrix_rank
|
|
66 | 66 |
|
67 | 67 | __all__ = ['StateSpace', 'ss', 'rss', 'drss', 'tf2ss', 'ssdata']
|
68 | 68 |
|
| 69 | + |
| 70 | +def _matrix(a): |
| 71 | + """_matrix(a) -> numpy.matrix |
| 72 | + a - passed to numpy.matrix |
| 73 | + Wrapper around numpy.matrix; unlike that function, _matrix([]) will be 0x0 |
| 74 | + """ |
| 75 | + from numpy import matrix |
| 76 | + am = matrix(a) |
| 77 | + if (1,0) == am.shape: |
| 78 | + am.shape = (0,0) |
| 79 | + return am |
| 80 | + |
| 81 | + |
69 | 82 | class StateSpace(LTI):
|
70 | 83 | """A class for representing state-space models
|
71 | 84 |
|
@@ -122,7 +135,7 @@ def __init__(self, *args):
|
122 | 135 | else:
|
123 | 136 | raise ValueError("Needs 1 or 4 arguments; received %i." % len(args))
|
124 | 137 |
|
125 |
| - A, B, C, D = [matrix(M) for M in (A, B, C, D)] |
| 138 | + A, B, C, D = [_matrix(M) for M in (A, B, C, D)] |
126 | 139 |
|
127 | 140 | # TODO: use super here?
|
128 | 141 | LTI.__init__(self, inputs=D.shape[1], outputs=D.shape[0], dt=dt)
|
@@ -327,8 +340,9 @@ def __rmul__(self, other):
|
327 | 340 | return _convertToStateSpace(other) * self
|
328 | 341 |
|
329 | 342 | # try to treat this as a matrix
|
| 343 | + # TODO: doesn't _convertToStateSpace do this anyway? |
330 | 344 | try:
|
331 |
| - X = matrix(other) |
| 345 | + X = _matrix(other) |
332 | 346 | C = X * self.C
|
333 | 347 | D = X * self.D
|
334 | 348 | return StateSpace(self.A, self.B, C, D, self.dt)
|
@@ -686,11 +700,9 @@ def _convertToStateSpace(sys, **kw):
|
686 | 700 |
|
687 | 701 | # If this is a matrix, try to create a constant feedthrough
|
688 | 702 | try:
|
689 |
| - D = matrix(sys) |
690 |
| - outputs, inputs = D.shape |
691 |
| - |
692 |
| - return StateSpace(0., zeros((1, inputs)), zeros((outputs, 1)), D) |
693 |
| - except Exception(e): |
| 703 | + D = _matrix(sys) |
| 704 | + return StateSpace([], [], [], D) |
| 705 | + except Exception as e: |
694 | 706 | print("Failure to assume argument is matrix-like in" \
|
695 | 707 | " _convertToStateSpace, result %s" % e)
|
696 | 708 |
|
|
0 commit comments