5353
5454"""
5555
56- import scipy as sp
5756import numpy as np
5857from . import xferfcn as tf
5958from . import statesp as ss
6059from . import frdata as frd
6160
6261__all__ = ['series' , 'parallel' , 'negate' , 'feedback' , 'append' , 'connect' ]
6362
63+
6464def series (sys1 , * sysn ):
6565 """Return the series connection (... \* sys3 \*) sys2 \* sys1
6666
6767 Parameters
6868 ----------
69- sys1: scalar, StateSpace, TransferFunction, or FRD
70- sysn: other scalars, StateSpaces, TransferFunctions, or FRDs
69+ sys1 : scalar, StateSpace, TransferFunction, or FRD
70+ sysn : other scalars, StateSpaces, TransferFunctions, or FRDs
7171
7272 Returns
7373 -------
74- out: scalar, StateSpace, or TransferFunction
74+ out : scalar, StateSpace, or TransferFunction
7575
7676 Raises
7777 ------
@@ -105,18 +105,19 @@ def series(sys1, *sysn):
105105 from functools import reduce
106106 return reduce (lambda x , y :y * x , sysn , sys1 )
107107
108+
108109def parallel (sys1 , * sysn ):
109110 """
110111 Return the parallel connection sys1 + sys2 (+ sys3 + ...)
111112
112113 Parameters
113114 ----------
114- sys1: scalar, StateSpace, TransferFunction, or FRD
115- *sysn: other scalars, StateSpaces, TransferFunctions, or FRDs
115+ sys1 : scalar, StateSpace, TransferFunction, or FRD
116+ *sysn : other scalars, StateSpaces, TransferFunctions, or FRDs
116117
117118 Returns
118119 -------
119- out: scalar, StateSpace, or TransferFunction
120+ out : scalar, StateSpace, or TransferFunction
120121
121122 Raises
122123 ------
@@ -150,17 +151,18 @@ def parallel(sys1, *sysn):
150151 from functools import reduce
151152 return reduce (lambda x , y :x + y , sysn , sys1 )
152153
154+
153155def negate (sys ):
154156 """
155157 Return the negative of a system.
156158
157159 Parameters
158160 ----------
159- sys: StateSpace, TransferFunction or FRD
161+ sys : StateSpace, TransferFunction or FRD
160162
161163 Returns
162164 -------
163- out: StateSpace or TransferFunction
165+ out : StateSpace or TransferFunction
164166
165167 Notes
166168 -----
@@ -177,7 +179,6 @@ def negate(sys):
177179 >>> sys2 = negate(sys1) # Same as sys2 = -sys1.
178180
179181 """
180-
181182 return - sys ;
182183
183184#! TODO: expand to allow sys2 default to work in MIMO case?
@@ -187,18 +188,18 @@ def feedback(sys1, sys2=1, sign=-1):
187188
188189 Parameters
189190 ----------
190- sys1: scalar, StateSpace, TransferFunction, FRD
191- The primary plant .
192- sys2: scalar, StateSpace, TransferFunction, FRD
193- The feedback plant (often a feedback controller).
191+ sys1 : scalar, StateSpace, TransferFunction, FRD
192+ The primary process .
193+ sys2 : scalar, StateSpace, TransferFunction, FRD
194+ The feedback process (often a feedback controller).
194195 sign: scalar
195196 The sign of feedback. `sign` = -1 indicates negative feedback, and
196197 `sign` = 1 indicates positive feedback. `sign` is an optional
197198 argument; it assumes a value of -1 if not specified.
198199
199200 Returns
200201 -------
201- out: StateSpace or TransferFunction
202+ out : StateSpace or TransferFunction
202203
203204 Raises
204205 ------
@@ -256,7 +257,7 @@ def feedback(sys1, sys2=1, sign=-1):
256257 return sys1 .feedback (sys2 , sign )
257258
258259def append (* sys ):
259- ''' append(sys1, sys2, ..., sysn)
260+ """ append(sys1, sys2, ..., sysn)
260261
261262 Group models by appending their inputs and outputs
262263
@@ -279,42 +280,40 @@ def append(*sys):
279280
280281 Examples
281282 --------
282- >>> sys1 = ss("1. -2; 3. -4", "5.; 7 ", "6. 8", "9." )
283- >>> sys2 = ss(" -1.", "1.", "1.", "0." )
283+ >>> sys1 = ss([[1., -2], [3., -4]], [[5.], [7]] ", [[6., 8]], [[9.]] )
284+ >>> sys2 = ss([[ -1.]], [[1.]], [[1.]], [[0.]] )
284285 >>> sys = append(sys1, sys2)
285286
286- .. todo::
287- also implement for transfer function, zpk, etc.
288- '''
287+ """
289288 s1 = sys [0 ]
290289 for s in sys [1 :]:
291290 s1 = s1 .append (s )
292291 return s1
293292
294293def connect (sys , Q , inputv , outputv ):
295- '''
296- Index-base interconnection of system
294+ """Index-based interconnection of an LTI system.
297295
298- The system sys is a system typically constructed with append, with
299- multiple inputs and outputs. The inputs and outputs are connected
300- according to the interconnection matrix Q , and then the final
301- inputs and outputs are trimmed according to the inputs and outputs
302- listed in inputv and outputv.
296+ The system ` sys` is a system typically constructed with ` append` , with
297+ multiple inputs and outputs. The inputs and outputs are connected
298+ according to the interconnection matrix `Q` , and then the final inputs and
299+ outputs are trimmed according to the inputs and outputs listed in `inputv`
300+ and ` outputv` .
303301
304- Note: to have this work, inputs and outputs start counting at 1!!!!
302+ NOTE: Inputs and outputs are indexed starting at 1 and negative values
303+ correspond to a negative feedback interconnection.
305304
306305 Parameters
307306 ----------
308- sys: StateSpace Transferfunction
307+ sys : StateSpace Transferfunction
309308 System to be connected
310- Q: 2d array
309+ Q : 2D array
311310 Interconnection matrix. First column gives the input to be connected
312- second column gives the output to be fed into this input. Negative
311+ second column gives the output to be fed into this input. Negative
313312 values for the second column mean the feedback is negative, 0 means
314- no connection is made
315- inputv: 1d array
313+ no connection is made. Inputs and outputs are indexed starting at 1.
314+ inputv : 1D array
316315 list of final external inputs
317- outputv: 1d array
316+ outputv : 1D array
318317 list of final external outputs
319318
320319 Returns
@@ -324,28 +323,30 @@ def connect(sys, Q, inputv, outputv):
324323
325324 Examples
326325 --------
327- >>> sys1 = ss("1. -2; 3. -4", "5.; 7", " 6, 8", "9." )
328- >>> sys2 = ss(" -1.", "1.", "1.", "0." )
326+ >>> sys1 = ss([[1., -2], [3., -4]], [[5.], [7]], [[ 6, 8]], [[9.]] )
327+ >>> sys2 = ss([[ -1.]], [[1.]], [[1.]], [[0.]] )
329328 >>> sys = append(sys1, sys2)
330- >>> Q = sp.mat([ [ 1, 2], [2, -1] ]) # basically feedback, output 2 in 1
329+ >>> Q = [[ 1, 2], [2, -1]] # negative feedback interconnection
331330 >>> sysc = connect(sys, Q, [2], [1, 2])
332- '''
331+
332+ """
333333 # first connect
334- K = sp .zeros ( (sys .inputs , sys .outputs ) )
335- for r in sp .array (Q ).astype (int ):
334+ K = np .zeros ((sys .inputs , sys .outputs ))
335+ for r in np .array (Q ).astype (int ):
336336 inp = r [0 ]- 1
337337 for outp in r [1 :]:
338338 if outp > 0 and outp <= sys .outputs :
339339 K [inp ,outp - 1 ] = 1.
340340 elif outp < 0 and - outp >= - sys .outputs :
341341 K [inp ,- outp - 1 ] = - 1.
342- sys = sys .feedback (sp . matrix (K ), sign = 1 )
342+ sys = sys .feedback (np . array (K ), sign = 1 )
343343
344344 # now trim
345- Ytrim = sp .zeros ( (len (outputv ), sys .outputs ) )
346- Utrim = sp .zeros ( (sys .inputs , len (inputv )) )
345+ Ytrim = np .zeros ((len (outputv ), sys .outputs ))
346+ Utrim = np .zeros ((sys .inputs , len (inputv )))
347347 for i ,u in enumerate (inputv ):
348348 Utrim [u - 1 ,i ] = 1.
349349 for i ,y in enumerate (outputv ):
350350 Ytrim [i ,y - 1 ] = 1.
351- return sp .matrix (Ytrim )* sys * sp .matrix (Utrim )
351+
352+ return Ytrim * sys * Utrim
0 commit comments