From: <kk...@us...> - 2011-02-08 22:14:00
|
Revision: 51 http://python-control.svn.sourceforge.net/python-control/?rev=51&view=rev Author: kkchen Date: 2011-02-08 22:13:53 +0000 (Tue, 08 Feb 2011) Log Message: ----------- *Class structure change* Added Lti2 superclass in lti2.py. Started the transfer function subclass in xferfcn.py, currently implemented as xTrasferFunction. Edits to the StateSpace class in statesp.py. Kevin K. Chen <kk...@pr...> Modified Paths: -------------- branches/control-0.4a/src/statesp.py branches/control-0.4a/src/xferfcn.py Added Paths: ----------- branches/control-0.4a/src/lti2.py Added: branches/control-0.4a/src/lti2.py =================================================================== --- branches/control-0.4a/src/lti2.py (rev 0) +++ branches/control-0.4a/src/lti2.py 2011-02-08 22:13:53 UTC (rev 51) @@ -0,0 +1,9 @@ +class Lti2: + """The Lti2 is a parent class to the StateSpace and TransferFunction child + classes. It only contains the number of inputs and outputs, but this can be + expanded in the future.""" + + def __init__(self, inputs=1, outputs=1): + # Data members common to StateSpace and TransferFunction. + self.inputs = inputs + self.outputs = outputs \ No newline at end of file Modified: branches/control-0.4a/src/statesp.py =================================================================== --- branches/control-0.4a/src/statesp.py 2011-02-08 22:13:47 UTC (rev 50) +++ branches/control-0.4a/src/statesp.py 2011-02-08 22:13:53 UTC (rev 51) @@ -42,46 +42,25 @@ # $Id: statesp.py 21 2010-06-06 17:29:42Z murrayrm $ import scipy as sp -import scipy.signal as signal -import xferfcn from scipy import concatenate, zeros +import xferfcn +from lti2 import Lti2 -class Lti2: - """The Lti2 is a parent class to the StateSpace and TransferFunction child - classes. It only contains the number of inputs and outputs, but this can be - expanded in the future.""" - - def __init__(self, inputs=1, outputs=1): - # Data members common to StateSpace and TransferFunction. - self.inputs = inputs - self.outputs = outputs - class StateSpace(Lti2): """The StateSpace class is used throughout the python-control library to represent systems in state space form. This class is derived from the Lti2 base class.""" - def __init__(self, A, B, C, D): - # Here we're going to convert inputs to matrices, if the user gave a non - # 2-D array or matrix type. - matrices = [A, B, C, D] + def __init__(self, A=0, B=0, C=0, D=1): + """StateSpace constructor. The default constructor is the unit gain + direct feedthrough system.""" + + # Here we're going to convert inputs to matrices, if the user gave a + # non-matrix type. + matrices = [A, B, C, D] for i in range(len(matrices)): - if (isinstance(matrices[i], (int, long, float, complex))): - # Convert scalars to matrices, if necessary. - matrices[i] = sp.matrix(matrices[i]) - elif isinstance(matrices[i], sp.ndarray): - # Convert 0- or 1-D arrays to matrices, if necessary. - if len(matrices[i].shape) < 2: - matrices[i] = sp.matrix(matrices[i]) - elif len(matrices[i].shape) == 2: - # If we're already a 2-D array or a matrix, then perfect! - pass - else: - raise ValueError("A, B, C, and D cannot have > 2 \ - dimensions.") - else: - # If the user gave us a non-numeric type. - raise ValueError("A, B, C, and D must be arrays or matrices.") + # Convert to matrix first, if necessary. + matrices[i] = sp.matrix(matrices[i]) [A, B, C, D] = matrices self.A = A Modified: branches/control-0.4a/src/xferfcn.py =================================================================== --- branches/control-0.4a/src/xferfcn.py 2011-02-08 22:13:47 UTC (rev 50) +++ branches/control-0.4a/src/xferfcn.py 2011-02-08 22:13:53 UTC (rev 51) @@ -51,7 +51,69 @@ import scipy.signal as signal import bdalg as bd import statesp +from lti2 import Lti2 +class xTransferFunction(Lti2): + """The TransferFunction class is derived from the Lti2 parent class. The + main data members are 'num' and 'den', which are 3-D numpy arrays of + MIMO numerator and denominator coefficients. For instance, + + >>> num[2, 5] = numpy.array([1, 4, 8]) + + means that the numerator of the transfer function from the 6th input to the + 3rd output is set to s^2 + 4s + 8. + + Note that numpy requires all polynomials in within an array to have the same + order. Be sure to pad leading coefficients with 0 as necessary.""" + + def __init__(self, num=sp.array([[[1.]]]), den=sp.array([[[1.]]])): + """This is the constructor. The default transfer function is 1 (unit + gain direct feedthrough).""" + + # Make num and den into 3-d numpy arrays, if necessary. + data = [num, den] + for i in range(len(data)): + if isinstance(data[i], (int, long, float, complex)): + # Convert scalar to 3-d array. + data[i] = sp.array([[[data[i]]]]) + elif isinstance(data[i], sp.ndarray): + if data[i].ndim == 0: + # Convert scalar to 3-d array. + data[i] = sp.array([[[data[i]]]]) + elif data[i].ndim == 1: + # Convert SISO transfer function polynomial to 3-d array. + data[i] = sp.array([[data[i]]]) + elif data[i].ndim == 3: + # We already have a MIMO system. + pass + else: + # If the user passed in a anything else, then it's unclear + # what the meaning is. + raise ValueError("The numerator and denominator inputs \ + must be scalars or vectors (for SISO), or 3-d arrays (for \ + MIMO).") + [num, den] = data + + print num + print den + + inputs = num.shape[1] + outputs = num.shape[0] + + if inputs != den.shape[1]: + raise ValueError("The numerator and denominator matrices must have \ + the same column (input) size.") + if outputs != den.shape[0]: + raise ValueError("The numerator and denominator matrices must have \ + the same row (output) size.") + + self.num = num + self.den = den + Lti2.__init__(self, inputs, outputs) + + print self.inputs + print self.outputs + class TransferFunction(signal.lti): """The TransferFunction class is used to represent linear input/output systems via its transfer function. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |