37
37
38
38
import warnings
39
39
import numpy as np
40
- from numpy import shape , size , copy , zeros , eye , dot , \
41
- finfo , inexact , atleast_2d
40
+ from numpy import copy , eye , dot , finfo , inexact , atleast_2d
42
41
43
42
import scipy as sp
44
- from scipy .linalg import eigvals , solve_discrete_are , solve
43
+ from scipy .linalg import eigvals , solve
45
44
46
- from .exception import ControlSlycot , ControlArgument , slycot_check
45
+ from .exception import ControlSlycot , ControlArgument , ControlDimension , \
46
+ slycot_check
47
47
from .statesp import _ssmatrix
48
48
49
49
# Make sure we have access to the right slycot routines
@@ -137,9 +137,9 @@ def lyap(A, Q, C=None, E=None, method=None):
137
137
method = _slycot_or_scipy (method )
138
138
if method == 'slycot' :
139
139
if sb03md is None :
140
- raise ControlSlycot ("can 't find slycot module 'sb03md'" )
140
+ raise ControlSlycot ("Can 't find slycot module 'sb03md'" )
141
141
if sb04md is None :
142
- raise ControlSlycot ("can 't find slycot module 'sb04md'" )
142
+ raise ControlSlycot ("Can 't find slycot module 'sb04md'" )
143
143
144
144
# Reshape input arrays
145
145
A = np .array (A , ndmin = 2 )
@@ -197,7 +197,7 @@ def lyap(A, Q, C=None, E=None, method=None):
197
197
from slycot import sg03ad
198
198
199
199
except ImportError :
200
- raise ControlSlycot ("can 't find slycot module 'sg03ad'" )
200
+ raise ControlSlycot ("Can 't find slycot module 'sg03ad'" )
201
201
202
202
# Solve the generalized Lyapunov equation by calling Slycot
203
203
# function sg03ad
@@ -266,11 +266,11 @@ def dlyap(A, Q, C=None, E=None, method=None):
266
266
if method == 'slycot' :
267
267
# Make sure we have access to the right slycot routines
268
268
if sb03md is None :
269
- raise ControlSlycot ("can 't find slycot module 'sb03md'" )
269
+ raise ControlSlycot ("Can 't find slycot module 'sb03md'" )
270
270
if sb04qd is None :
271
- raise ControlSlycot ("can 't find slycot module 'sb04qd'" )
271
+ raise ControlSlycot ("Can 't find slycot module 'sb04qd'" )
272
272
if sg03ad is None :
273
- raise ControlSlycot ("can 't find slycot module 'sg03ad'" )
273
+ raise ControlSlycot ("Can 't find slycot module 'sg03ad'" )
274
274
275
275
# Reshape input arrays
276
276
A = np .array (A , ndmin = 2 )
@@ -312,7 +312,7 @@ def dlyap(A, Q, C=None, E=None, method=None):
312
312
"method='scipy' not valid for Sylvester equation" )
313
313
314
314
# Solve the Sylvester equation by calling Slycot function sb04qd
315
- X = sb04qd (n , m , - A , asarray ( Q ) .T , C )
315
+ X = sb04qd (n , m , - A , Q .T , C )
316
316
317
317
# Solve the generalized Lyapunov equation
318
318
elif C is None and E is not None :
@@ -400,18 +400,18 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True, method=None):
400
400
try :
401
401
from slycot import sb02md
402
402
except ImportError :
403
- raise ControlSlycot ("can 't find slycot module 'sb02md'" )
403
+ raise ControlSlycot ("Can 't find slycot module 'sb02md'" )
404
404
405
405
try :
406
406
from slycot import sb02mt
407
407
except ImportError :
408
- raise ControlSlycot ("can 't find slycot module 'sb02mt'" )
408
+ raise ControlSlycot ("Can 't find slycot module 'sb02mt'" )
409
409
410
410
# Make sure we can find the required slycot routine
411
411
try :
412
412
from slycot import sg02ad
413
413
except ImportError :
414
- raise ControlSlycot ("can 't find slycot module 'sg02ad'" )
414
+ raise ControlSlycot ("Can 't find slycot module 'sg02ad'" )
415
415
416
416
# Reshape input arrays
417
417
A = np .array (A , ndmin = 2 )
@@ -501,10 +501,7 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True, method=None):
501
501
'R' , n , m , 0 , A , E , B , Q , R , S )
502
502
503
503
# Calculate the closed-loop eigenvalues L
504
- L = zeros (n )
505
- L .dtype = 'complex64'
506
- for i in range (n ):
507
- L [i ] = (alfar [i ] + alfai [i ]* 1j ) / beta [i ]
504
+ L = np .array ([(alfar [i ] + alfai [i ]* 1j ) / beta [i ] for i in range (n )])
508
505
509
506
# Calculate the gain matrix G
510
507
G = solve (R_b , dot (B_b .T , dot (X , E_b )) + S_b .T )
@@ -599,7 +596,7 @@ def dare(A, B, Q, R, S=None, E=None, stabilizing=True, method=None):
599
596
600
597
Rmat = _ssmatrix (R )
601
598
Qmat = _ssmatrix (Q )
602
- X = solve_discrete_are (A , B , Qmat , Rmat , e = E , s = S )
599
+ X = sp . linalg . solve_discrete_are (A , B , Qmat , Rmat , e = E , s = S )
603
600
if S is None :
604
601
G = solve (B .T .dot (X ).dot (B ) + Rmat , B .T .dot (X ).dot (A ))
605
602
else :
@@ -616,18 +613,18 @@ def _dare_slycot(A, B, Q, R, S=None, E=None, stabilizing=True):
616
613
try :
617
614
from slycot import sb02md
618
615
except ImportError :
619
- raise ControlSlycot ("can 't find slycot module 'sb02md'" )
616
+ raise ControlSlycot ("Can 't find slycot module 'sb02md'" )
620
617
621
618
try :
622
619
from slycot import sb02mt
623
620
except ImportError :
624
- raise ControlSlycot ("can 't find slycot module 'sb02mt'" )
621
+ raise ControlSlycot ("Can 't find slycot module 'sb02mt'" )
625
622
626
623
# Make sure we can find the required slycot routine
627
624
try :
628
625
from slycot import sg02ad
629
626
except ImportError :
630
- raise ControlSlycot ("can 't find slycot module 'sg02ad'" )
627
+ raise ControlSlycot ("Can 't find slycot module 'sg02ad'" )
631
628
632
629
# Reshape input arrays
633
630
A = np .array (A , ndmin = 2 )
@@ -660,51 +657,15 @@ def _dare_slycot(A, B, Q, R, S=None, E=None, stabilizing=True):
660
657
661
658
# Solve the generalized algebraic Riccati equation by calling the
662
659
# Slycot function sg02ad
663
- try :
660
+ with warnings . catch_warnings () :
664
661
sort = 'S' if stabilizing else 'U'
662
+ warnings .simplefilter ("error" , category = SlycotResultWarning )
665
663
rcondu , X , alfar , alfai , beta , S_o , T , U , iwarn = \
666
664
sg02ad ('D' , 'B' , 'N' , 'U' , 'N' , 'N' , sort ,
667
665
'R' , n , m , 0 , A , E , B , Q , R , S )
668
666
669
- except ValueError as ve :
670
- if ve .info < 0 or ve .info > 7 :
671
- e = ValueError (ve .message )
672
- e .info = ve .info
673
- elif ve .info == 1 :
674
- e = ValueError ("The computed extended matrix pencil is " +
675
- "singular, possibly due to rounding errors" )
676
- e .info = ve .info
677
- elif ve .info == 2 :
678
- e = ValueError ("The QZ algorithm failed" )
679
- e .info = ve .info
680
- elif ve .info == 3 :
681
- e = ValueError ("Reordering of the generalized eigenvalues failed" )
682
- e .info = ve .info
683
- elif ve .info == 4 :
684
- e = ValueError ("After reordering, roundoff changed values of " +
685
- "some complex eigenvalues so that leading " +
686
- "eigenvalues in the generalized Schur form no " +
687
- "longer satisfy the stability condition; this " +
688
- "could also be caused due to scaling" )
689
- e .info = ve .info
690
- elif ve .info == 5 :
691
- e = ValueError ("The computed dimension of the solution does " +
692
- "not equal N" )
693
- e .info = ve .info
694
- elif ve .info == 6 :
695
- e = ValueError ("The spectrum is too close to the boundary of " +
696
- "the stability domain" )
697
- e .info = ve .info
698
- elif ve .info == 7 :
699
- e = ValueError ("A singular matrix was encountered during the " +
700
- "computation of the solution matrix X" )
701
- e .info = ve .info
702
- raise e
703
-
704
- L = zeros (n )
705
- L .dtype = 'complex64'
706
- for i in range (n ):
707
- L [i ] = (alfar [i ] + alfai [i ]* 1j )/ beta [i ]
667
+ # Calculate the closed-loop eigenvalues L
668
+ L = np .array ([(alfar [i ] + alfai [i ]* 1j ) / beta [i ] for i in range (n )])
708
669
709
670
# Calculate the gain matrix G
710
671
G = solve (dot (B_b .T , dot (X , B_b )) + R_b ,
@@ -721,21 +682,20 @@ def _slycot_or_scipy(method):
721
682
return 'slycot'
722
683
elif method == 'scipy' or (method is None and not slycot_check ()):
723
684
return 'scipy'
724
- > >> >> >> d84fdc6 (add method = 'scipy' to mateqn functions )
725
685
else :
726
- raise ValueError ("unknown method %s" % method )
686
+ raise ValueError ("Unknown method %s" % method )
727
687
728
688
729
689
# Utility function to check matrix dimensions
730
690
def _check_shape (name , M , n , m , square = False , symmetric = False ):
731
691
if square and M .shape [0 ] != M .shape [1 ]:
732
- raise ControlArgument ("%s must be a square matrix" % name )
692
+ raise ControlDimension ("%s must be a square matrix" % name )
733
693
734
694
if symmetric and not _is_symmetric (M ):
735
695
raise ControlArgument ("%s must be a symmetric matrix" % name )
736
696
737
697
if M .shape [0 ] != n or M .shape [1 ] != m :
738
- raise ControlArgument ("Incompatible dimensions of %s matrix" % name )
698
+ raise ControlDimension ("Incompatible dimensions of %s matrix" % name )
739
699
740
700
741
701
# Utility function to check if a matrix is symmetric
0 commit comments