5252"""
5353
5454# External function declarations
55+ import numpy as np
5556from numpy import angle , any , array , empty , finfo , insert , ndarray , ones , \
5657 polyadd , polymul , polyval , roots , sort , sqrt , zeros , squeeze , exp , pi , \
5758 where , delete , real , poly , poly1d
58- import numpy as np
59+ import scipy as sp
5960from scipy .signal import lti , tf2zpk , zpk2tf , cont2discrete
6061from copy import deepcopy
6162from warnings import warn
@@ -126,7 +127,7 @@ def __init__(self, *args):
126127 data = [num , den ]
127128 for i in range (len (data )):
128129 # Check for a scalar (including 0d ndarray)
129- if (isinstance (data [i ], (int , float , complex )) or
130+ if (isinstance (data [i ], (int , float , complex , np . number )) or
130131 (isinstance (data [i ], ndarray ) and data [i ].ndim == 0 )):
131132 # Convert scalar to list of list of array.
132133 if (isinstance (data [i ], int )):
@@ -135,7 +136,7 @@ def __init__(self, *args):
135136 else :
136137 data [i ] = [[array ([data [i ]])]]
137138 elif (isinstance (data [i ], (list , tuple , ndarray )) and
138- isinstance (data [i ][0 ], (int , float , complex ))):
139+ isinstance (data [i ][0 ], (int , float , complex , np . number ))):
139140 # Convert array to list of list of array.
140141 if (isinstance (data [i ][0 ], int )):
141142 # Convert integers to floats at this point
@@ -146,7 +147,8 @@ def __init__(self, *args):
146147 elif (isinstance (data [i ], list ) and
147148 isinstance (data [i ][0 ], list ) and
148149 isinstance (data [i ][0 ][0 ], (list , tuple , ndarray )) and
149- isinstance (data [i ][0 ][0 ][0 ], (int , float , complex ))):
150+ isinstance (data [i ][0 ][0 ][0 ], (int , float , complex ,
151+ np .number ))):
150152 # We might already have the right format. Convert the
151153 # coefficient vectors to arrays, if necessary.
152154 for j in range (len (data [i ])):
@@ -363,7 +365,7 @@ def __rsub__(self, other):
363365 def __mul__ (self , other ):
364366 """Multiply two LTI objects (serial connection)."""
365367 # Convert the second argument to a transfer function.
366- if isinstance (other , (int , float , complex )):
368+ if isinstance (other , (int , float , complex , np . number )):
367369 other = _convertToTransferFunction (other , inputs = self .inputs ,
368370 outputs = self .inputs )
369371 else :
@@ -410,7 +412,7 @@ def __rmul__(self, other):
410412 """Right multiply two LTI objects (serial connection)."""
411413
412414 # Convert the second argument to a transfer function.
413- if isinstance (other , (int , float , complex )):
415+ if isinstance (other , (int , float , complex , np . number )):
414416 other = _convertToTransferFunction (other , inputs = self .inputs ,
415417 outputs = self .inputs )
416418 else :
@@ -458,7 +460,7 @@ def __rmul__(self, other):
458460 def __truediv__ (self , other ):
459461 """Divide two LTI objects."""
460462
461- if isinstance (other , (int , float , complex )):
463+ if isinstance (other , (int , float , complex , np . number )):
462464 other = _convertToTransferFunction (
463465 other , inputs = self .inputs ,
464466 outputs = self .inputs )
@@ -492,7 +494,7 @@ def __div__(self, other):
492494 # TODO: Division of MIMO transfer function objects is not written yet.
493495 def __rtruediv__ (self , other ):
494496 """Right divide two LTI objects."""
495- if isinstance (other , (int , float , complex )):
497+ if isinstance (other , (int , float , complex , np . number )):
496498 other = _convertToTransferFunction (
497499 other , inputs = self .inputs ,
498500 outputs = self .inputs )
@@ -1128,22 +1130,21 @@ def _convertToTransferFunction(sys, **kw):
11281130 # Each transfer function matrix row
11291131 # has a common denominator.
11301132 den [i ][j ] = list (tfout [5 ][i , :])
1131- # print(num)
1132- # print(den)
1133+
11331134 except ImportError :
11341135 # If slycot is not available, use signal.lti (SISO only)
11351136 if (sys .inputs != 1 or sys .outputs != 1 ):
11361137 raise TypeError ("No support for MIMO without slycot" )
11371138
1138- lti_sys = lti ( sys . A , sys . B , sys . C , sys . D )
1139- num = squeeze ( lti_sys . num )
1140- den = squeeze ( lti_sys . den )
1141- # print (num)
1142- # print (den)
1139+ # Do the conversion using sp.signal.ss2tf
1140+ # Note that this returns a 2D array for the numerator
1141+ num , den = sp . signal . ss2tf ( sys . A , sys . B , sys . C , sys . D )
1142+ num = squeeze (num ) # Convert to 1D array
1143+ den = squeeze (den ) # Probably not needed
11431144
11441145 return TransferFunction (num , den , sys .dt )
11451146
1146- elif isinstance (sys , (int , float , complex )):
1147+ elif isinstance (sys , (int , float , complex , np . number )):
11471148 if "inputs" in kw :
11481149 inputs = kw ["inputs" ]
11491150 else :
0 commit comments