Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
where Q is a symmetric matrix and A, Q and E are square matrices
of the same dimension.
"""
# Make sure we have access to the right slycot routines
try:
from slycot import sb03md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb03md'")
try:
from slycot import sb04md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb04md'")
# Reshape 1-d arrays
if len(shape(A)) == 1:
A = A.reshape(1, A.size)
if len(shape(Q)) == 1:
Q = Q.reshape(1, Q.size)
if C is not None and len(shape(C)) == 1:
C = C.reshape(1, C.size)
if E is not None and len(shape(E)) == 1:
E = E.reshape(1, E.size)
# Determine main dimensions
if size(A) == 1:
# dico = 'D'
# else:
dico = 'C'
#TODO: Check system is stable, perhaps a utility in ctrlutil.py
# or a method of the StateSpace class?
if np.any(np.linalg.eigvals(sys.A).real >= 0.0):
raise ValueError("Oops, the system is unstable!")
if type=='c' or type=='o':
#Compute Gramian by the Slycot routine sb03md
#make sure Slycot is installed
try:
from slycot import sb03md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb03md'")
if type=='c':
tra = 'T'
C = -np.dot(sys.B,sys.B.transpose())
elif type=='o':
tra = 'N'
C = -np.dot(sys.C.transpose(),sys.C)
n = sys.states
U = np.zeros((n,n))
A = np.array(sys.A) # convert to NumPy array for slycot
X,scale,sep,ferr,w = sb03md(n, C, A, U, dico, job='X', fact='N', trana=tra)
gram = X
return _ssmatrix(gram)
elif type=='cf' or type=='of':
#Compute cholesky factored gramian from slycot routine sb03od
try:
--------
>>> rsys = balred(sys, orders, method='truncate')
"""
if method!='truncate' and method!='matchdc':
raise ValueError("supported methods are 'truncate' or 'matchdc'")
elif method=='truncate':
try:
from slycot import ab09md, ab09ad
except ImportError:
raise ControlSlycot("can't find slycot subroutine ab09md or ab09ad")
elif method=='matchdc':
try:
from slycot import ab09nd
except ImportError:
raise ControlSlycot("can't find slycot subroutine ab09nd")
#Check for ss system object, need a utility for this?
#TODO: Check for continous or discrete, only continuous supported right now
# if isCont():
# dico = 'C'
# elif isDisc():
# dico = 'D'
# else:
dico = 'C'
job = 'B' # balanced (B) or not (N)
equil = 'N' # scale (S) or not (N)
if alpha is None:
if dico == 'C':
alpha = 0.
:math:`A X A^T - E X E^T + Q = 0`
where Q is a symmetric matrix and A, Q and E are square matrices
of the same dimension. """
# Make sure we have access to the right slycot routines
try:
from slycot import sb03md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb03md'")
try:
from slycot import sb04qd
except ImportError:
raise ControlSlycot("can't find slycot module 'sb04qd'")
try:
from slycot import sg03ad
except ImportError:
raise ControlSlycot("can't find slycot module 'sg03ad'")
# Reshape 1-d arrays
if len(shape(A)) == 1:
A = A.reshape(1,A.size)
if len(shape(Q)) == 1:
Q = Q.reshape(1,Q.size)
if C is not None and len(shape(C)) == 1:
C = C.reshape(1,C.size)
# Make sure we can import required slycot routine
try:
from slycot import sb02md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02md'")
try:
from slycot import sb02mt
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02mt'")
# Make sure we can find the required slycot routine
try:
from slycot import sg02ad
except ImportError:
raise ControlSlycot("can't find slycot module 'sg02ad'")
# Reshape 1-d arrays
if len(shape(A)) == 1:
A = A.reshape(1,A.size)
if len(shape(B)) == 1:
B = B.reshape(1,B.size)
if len(shape(Q)) == 1:
Q = Q.reshape(1,Q.size)
if R is not None and len(shape(R)) == 1:
R = R.reshape(1,R.size)
if S is not None and len(shape(S)) == 1:
S = S.reshape(1,S.size)
elif type=='o':
tra = 'N'
C = -np.dot(sys.C.transpose(),sys.C)
n = sys.states
U = np.zeros((n,n))
A = np.array(sys.A) # convert to NumPy array for slycot
X,scale,sep,ferr,w = sb03md(n, C, A, U, dico, job='X', fact='N', trana=tra)
gram = X
return _ssmatrix(gram)
elif type=='cf' or type=='of':
#Compute cholesky factored gramian from slycot routine sb03od
try:
from slycot import sb03od
except ImportError:
raise ControlSlycot("can't find slycot module 'sb03od'")
tra='N'
n = sys.states
Q = np.zeros((n,n))
A = np.array(sys.A) # convert to NumPy array for slycot
if type=='cf':
m = sys.B.shape[1]
B = np.zeros_like(A)
B[0:m,0:n] = sys.B.transpose()
X,scale,w = sb03od(n, m, A.transpose(), Q, B, dico, fact='N', trans=tra)
elif type=='of':
m = sys.C.shape[0]
C = np.zeros_like(A)
C[0:n,0:m] = sys.C.transpose()
X,scale,w = sb03od(n, m, A, Q, C.transpose(), dico, fact='N', trans=tra)
gram = X
return _ssmatrix(gram)
# Make sure we can import required slycot routine
try:
from slycot import sb02md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02md'")
try:
from slycot import sb02mt
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02mt'")
# Make sure we can find the required slycot routine
try:
from slycot import sg02ad
except ImportError:
raise ControlSlycot("can't find slycot module 'sg02ad'")
# Reshape 1-d arrays
if len(shape(A)) == 1:
A = A.reshape(1,A.size)
if len(shape(B)) == 1:
B = B.reshape(1,B.size)
if len(shape(Q)) == 1:
Q = Q.reshape(1,Q.size)
if R is not None and len(shape(R)) == 1:
R = R.reshape(1,R.size)
if S is not None and len(shape(S)) == 1:
S = S.reshape(1,S.size)
dimension as A.")
if (size(E) > 1 and shape(E)[0] != shape(E)[1]) or \
(size(E) > 1 and shape(E)[0] != n) or \
(size(E) == 1 and n > 1):
raise ControlArgument("E must be a square matrix with the same \
dimension as A.")
if not (asarray(Q) == asarray(Q).T).all():
raise ControlArgument("Q must be a symmetric matrix.")
# Make sure we have access to the write slicot routine
try:
from slycot import sg03ad
except ImportError:
raise ControlSlycot("can't find slycot module 'sg03ad'")
# Solve the generalized Lyapunov equation by calling Slycot
# function sg03ad
try:
A,E,Q,Z,X,scale,sep,ferr,alphar,alphai,beta = \
sg03ad('C','B','N','T','L',n,A,E,eye(n,n),eye(n,n),-Q)
except ValueError(ve):
if ve.info < 0 or ve.info > 4:
e = ValueError(ve.message)
e.info = ve.info
elif ve.info == 1:
e = ValueError("The matrix contained in the upper \
Hessenberg part of the array A is not in \
upper quasitriangular form")
e.info = ve.info
elif ve.info == 2:
def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
# Make sure we can import required slycot routine
try:
from slycot import sb02md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02md'")
try:
from slycot import sb02mt
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02mt'")
# Make sure we can find the required slycot routine
try:
from slycot import sg02ad
except ImportError:
raise ControlSlycot("can't find slycot module 'sg02ad'")
# Reshape 1-d arrays
if len(shape(A)) == 1:
A = A.reshape(1,A.size)
if len(shape(B)) == 1:
B = B.reshape(1,B.size)
if len(shape(Q)) == 1:
Q = Q.reshape(1,Q.size)
def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
# Make sure we can import required slycot routine
try:
from slycot import sb02md
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02md'")
try:
from slycot import sb02mt
except ImportError:
raise ControlSlycot("can't find slycot module 'sb02mt'")
# Make sure we can find the required slycot routine
try:
from slycot import sg02ad
except ImportError:
raise ControlSlycot("can't find slycot module 'sg02ad'")
# Reshape 1-d arrays
if len(shape(A)) == 1:
A = A.reshape(1,A.size)