From: <kk...@us...> - 2011-02-08 22:19:50
|
Revision: 120 http://python-control.svn.sourceforge.net/python-control/?rev=120&view=rev Author: kkchen Date: 2011-02-08 22:19:44 +0000 (Tue, 08 Feb 2011) Log Message: ----------- Created copy constructors for StateSpace and TransferFunction. The copy() methods have been removed, since copy constructors should be used. Also, calling ss on a StateSpace object and tf on a TransferFunction object now return deep copies. Kevin K. Chen <kk...@pr...> Modified Paths: -------------- branches/control-0.4a/src/matlab.py branches/control-0.4a/src/statesp.py branches/control-0.4a/src/xferfcn.py Modified: branches/control-0.4a/src/matlab.py =================================================================== --- branches/control-0.4a/src/matlab.py 2011-02-08 22:19:39 UTC (rev 119) +++ branches/control-0.4a/src/matlab.py 2011-02-08 22:19:44 UTC (rev 120) @@ -59,6 +59,7 @@ import scipy as sp # SciPy library (used all over) import numpy as np # NumPy library import scipy.signal as signal # Signal processing library +from copy import deepcopy # Import MATLAB-like functions that are defined in other packages from scipy.signal import zpk2ss, ss2zpk, tf2zpk, zpk2tf @@ -305,7 +306,7 @@ elif len(args) == 1: sys = args[0] if isinstance(sys, StateSpace): - return sys + return deepcopy(sys) elif isinstance(sys, TransferFunction): return tf2ss(sys) else: @@ -366,7 +367,7 @@ if isinstance(sys, StateSpace): return ss2tf(sys) elif isinstance(sys, TransferFunction): - return sys + return deepcopy(sys) else: raise TypeError("tf(sys): sys must be a StateSpace or \ TransferFunction object. It is %s." % type(sys)) Modified: branches/control-0.4a/src/statesp.py =================================================================== --- branches/control-0.4a/src/statesp.py 2011-02-08 22:19:39 UTC (rev 119) +++ branches/control-0.4a/src/statesp.py 2011-02-08 22:19:44 UTC (rev 120) @@ -78,7 +78,6 @@ from numpy.linalg import inv, det, solve from numpy.linalg.linalg import LinAlgError from scipy.signal import lti -from copy import deepcopy from slycot import td04ad from lti import Lti import xferfcn @@ -96,9 +95,30 @@ """ - def __init__(self, A=0, B=0, C=0, D=1): - """Construct a state space object. The default is unit static gain.""" + def __init__(self, *args): + """Construct a state space object. + The default constructor is StateSpace(A, B, C, D), where A, B, C, D are + matrices or equivalent objects. To call the copy constructor, call + StateSpace(sys), where sys is a StateSpace object. + + """ + + if len(args) == 4: + # The user provided A, B, C, and D matrices. + (A, B, C, D) = args + elif len(args) == 1: + # Use the copy constructor. + if not isinstance(args[0], StateSpace): + raise TypeError("The one-argument constructor can only take in \ +a StateSpace object. Recived %s." % type(args[0])) + A = args[0].A + B = args[0].B + C = args[0].C + D = args[0].D + else: + raise ValueError("Needs 1 or 4 arguments; received %i." % len(args)) + # Here we're going to convert inputs to matrices, if the user gave a # non-matrix type. matrices = [A, B, C, D] @@ -171,11 +191,6 @@ self.inputs = self.B.shape[1] self.outputs = self.C.shape[0] - def copy(self): - """Return a deep copy of the instance.""" - - return deepcopy(self) - def __str__(self): """String representation of the state space.""" Modified: branches/control-0.4a/src/xferfcn.py =================================================================== --- branches/control-0.4a/src/xferfcn.py 2011-02-08 22:19:39 UTC (rev 119) +++ branches/control-0.4a/src/xferfcn.py 2011-02-08 22:19:44 UTC (rev 120) @@ -101,9 +101,29 @@ """ - def __init__(self, num=1, den=1): - """Construct a transfer function. The default is unit static gain.""" + def __init__(self, *args): + """Construct a transfer function. + + The default constructor is TransferFunction(num, den), where num and den + are lists of lists of arrays containing polynomial coefficients. To + call the copy constructor, call TransferFunction(sys), where sys is a + TransferFunction object. + """ + + if len(args) == 2: + # The user provided a numerator and a denominator. + (num, den) = args + elif len(args) == 1: + # Use the copy constructor. + if not isinstance(args[0], TransferFunction): + raise TypeError("The one-argument constructor can only take in \ +a TransferFunction object. Received %s." % type(args[0])) + num = args[0].num + den = args[0].den + else: + raise ValueError("Needs 1 or 2 arguments; receivd %i." % len(args)) + # Make num and den into lists of lists of arrays, if necessary. Beware: # this is a shallow copy! This should be okay, but be careful. data = [num, den] @@ -211,11 +231,6 @@ data[p][i][j] = data[p][i][j][nonzero:] [self.num, self.den] = data - def copy(self): - """Return a deep copy of the instance.""" - - return deepcopy(self) - def __str__(self): """String representation of the transfer function.""" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |