Skip to content

Commit f9fed5a

Browse files
author
Kevin Chen
committed
Added a copy() deepcopy routine to Lti subclasses.
Also renamed rss_generate to _rss_generate and corrected some comments. Kevin K. Chen <kkchen@princeton.edu>
1 parent 58f9396 commit f9fed5a

5 files changed

Lines changed: 42 additions & 19 deletions

File tree

src/bdalg.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
negate
1010
feedback
1111
12-
Copyright (c) 2010 by California Institute of Technology
12+
"""
13+
14+
"""Copyright (c) 2010 by California Institute of Technology
1315
All rights reserved.
1416
1517
Redistribution and use in source and binary forms, with or without

src/lti.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Lti:
2121
"virtual" functions. These are:
2222
2323
__init__
24+
copy
2425
__str__
2526
__neg__
2627
__add__

src/matlab.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
have the same names as their MATLAB equivalents are automatically
1414
imported here.
1515
16-
Copyright (c) 2009 by California Institute of Technology
16+
"""
17+
18+
"""Copyright (c) 2009 by California Institute of Technology
1719
All rights reserved.
1820
1921
Redistribution and use in source and binary forms, with or without
@@ -65,7 +67,7 @@
6567
# Control system library
6668
import ctrlutil
6769
import freqplot
68-
from statesp import StateSpace, rss_generate, convertToStateSpace
70+
from statesp import StateSpace, _rss_generate, convertToStateSpace
6971
from xferfcn import TransferFunction, convertToTransferFunction
7072
from exception import ControlArgument
7173

@@ -511,7 +513,7 @@ def rss(states=1, inputs=1, outputs=1):
511513
512514
"""
513515

514-
return rss_generate(states, inputs, outputs, 'c')
516+
return _rss_generate(states, inputs, outputs, 'c')
515517

516518
def drss(states=1, inputs=1, outputs=1):
517519
"""
@@ -544,7 +546,7 @@ def drss(states=1, inputs=1, outputs=1):
544546
545547
"""
546548

547-
return rss_generate(states, inputs, outputs, 'd')
549+
return _rss_generate(states, inputs, outputs, 'd')
548550

549551
def pole(sys):
550552
"""

src/statesp.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
Routines in this module:
1010
1111
StateSpace.__init__
12+
StateSpace._remove_useless_states
13+
StateSpace.copy
1214
StateSpace.__str__
1315
StateSpace.__neg__
1416
StateSpace.__add__
@@ -26,9 +28,11 @@
2628
StateSpace.feedback
2729
StateSpace.returnScipySignalLti
2830
convertToStateSpace
29-
rss_generate
31+
_rss_generate
3032
31-
Copyright (c) 2010 by California Institute of Technology
33+
"""
34+
35+
"""Copyright (c) 2010 by California Institute of Technology
3236
All rights reserved.
3337
3438
Redistribution and use in source and binary forms, with or without
@@ -74,6 +78,7 @@
7478
from numpy.linalg import inv, det, solve
7579
from numpy.linalg.linalg import LinAlgError
7680
from scipy.signal import lti
81+
from copy import deepcopy
7782
from slycot import td04ad
7883
from lti import Lti
7984
import xferfcn
@@ -162,6 +167,11 @@ def _remove_useless_states(self):
162167
self.B = delete(self.B, useless, 0)
163168
self.C = delete(self.C, useless, 1)
164169

170+
def copy(self):
171+
"""Return a deep copy of the instance."""
172+
173+
return deepcopy(self)
174+
165175
def __str__(self):
166176
"""String representation of the state space."""
167177

@@ -207,9 +217,9 @@ def __add__(self, other):
207217

208218
return StateSpace(A, B, C, D)
209219

210-
# Reverse addition - just switch the arguments
220+
# Right addition - just switch the arguments
211221
def __radd__(self, other):
212-
"""Reverse add two LTI systems (parallel connection)."""
222+
"""Right add two LTI systems (parallel connection)."""
213223

214224
return self + other
215225

@@ -220,7 +230,7 @@ def __sub__(self, other):
220230
return self + (-other)
221231

222232
def __rsub__(self, other):
223-
"""Reverse subtract two LTI systems."""
233+
"""Right subtract two LTI systems."""
224234

225235
return other + (-self)
226236

@@ -253,10 +263,10 @@ def __mul__(self, other):
253263

254264
return StateSpace(A, B, C, D)
255265

256-
# Reverse multiplication of two transfer functions (series interconnection)
266+
# Right multiplication of two transfer functions (series interconnection)
257267
# Just need to convert LH argument to a state space object
258268
def __rmul__(self, other):
259-
"""Reverse multiply two LTI objects (serial connection)."""
269+
"""Right multiply two LTI objects (serial connection)."""
260270

261271
# Check for a couple of special cases
262272
if isinstance(other, (int, long, float, complex)):
@@ -276,7 +286,7 @@ def __div__(self, other):
276286
raise NotImplementedError("StateSpace.__div__ is not implemented yet.")
277287

278288
def __rdiv__(self, other):
279-
"""Reverse divide two LTI systems."""
289+
"""Right divide two LTI systems."""
280290

281291
raise NotImplementedError("StateSpace.__rdiv__ is not implemented yet.")
282292

@@ -465,7 +475,7 @@ def convertToStateSpace(sys, **kw):
465475
else:
466476
raise TypeError("Can't convert given type to StateSpace system.")
467477

468-
def rss_generate(states, inputs, outputs, type):
478+
def _rss_generate(states, inputs, outputs, type):
469479
"""Generate a random state space.
470480
471481
This does the actual random state space generation expected from rss and

src/xferfcn.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
TransferFunction.__init__
1212
TransferFunction._truncatecoeff
13+
TransferFunction.copy
1314
TransferFunction.__str__
1415
TransferFunction.__neg__
1516
TransferFunction.__add__
@@ -31,7 +32,9 @@
3132
_addSISO
3233
convertToTransferFunction
3334
34-
Copyright (c) 2010 by California Institute of Technology
35+
"""
36+
37+
"""Copyright (c) 2010 by California Institute of Technology
3538
All rights reserved.
3639
3740
Redistribution and use in source and binary forms, with or without
@@ -208,6 +211,11 @@ def _truncatecoeff(self):
208211
data[p][i][j] = data[p][i][j][nonzero:]
209212
[self.num, self.den] = data
210213

214+
def copy(self):
215+
"""Return a deep copy of the instance."""
216+
217+
return deepcopy(self)
218+
211219
def __str__(self):
212220
"""String representation of the transfer function."""
213221

@@ -276,7 +284,7 @@ def __add__(self, other):
276284
return TransferFunction(num, den)
277285

278286
def __radd__(self, other):
279-
"""Reverse add two LTI objects (parallel connection)."""
287+
"""Right add two LTI objects (parallel connection)."""
280288

281289
return self + other;
282290

@@ -286,7 +294,7 @@ def __sub__(self, other):
286294
return self + (-other)
287295

288296
def __rsub__(self, other):
289-
"""Reverse subtract two LTI objects."""
297+
"""Right subtract two LTI objects."""
290298

291299
return other + (-self)
292300

@@ -328,7 +336,7 @@ def __mul__(self, other):
328336
return TransferFunction(num, den)
329337

330338
def __rmul__(self, other):
331-
"""Reverse multiply two LTI objects (serial connection)."""
339+
"""Right multiply two LTI objects (serial connection)."""
332340

333341
return self * other
334342

@@ -351,7 +359,7 @@ def __div__(self, other):
351359

352360
# TODO: Division of MIMO transfer function objects is not written yet.
353361
def __rdiv__(self, other):
354-
"""Reverse divide two LTI objects."""
362+
"""Right divide two LTI objects."""
355363

356364
if (self.inputs > 1 or self.outputs > 1 or
357365
other.inputs > 1 or other.outputs > 1):

0 commit comments

Comments
 (0)