|
41 | 41 | Author: Bjorn Olofsson
|
42 | 42 | """
|
43 | 43 |
|
44 |
| -fromnumpyimportshape,size,array,asarray,copy,zeros,eye,dot |
| 44 | +fromnumpyimportshape,size,asarray,copy,zeros,eye,dot, \ |
| 45 | +finfo,inexact,atleast_2d |
45 | 46 | fromscipy.linalgimporteigvals,solve_discrete_are,solve
|
46 | 47 | from .exceptionimportControlSlycot,ControlArgument
|
47 | 48 | from .statespimport_ssmatrix
|
@@ -122,7 +123,7 @@ def lyap(A, Q, C=None, E=None):
|
122 | 123 | ifsize(Q)>1andshape(Q)[0]!=shape(Q)[1]:
|
123 | 124 | raiseControlArgument("Q must be a quadratic matrix.")
|
124 | 125 |
|
125 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 126 | +ifnot_is_symmetric(Q): |
126 | 127 | raiseControlArgument("Q must be a symmetric matrix.")
|
127 | 128 |
|
128 | 129 | # Solve the Lyapunov equation by calling Slycot function sb03md
|
@@ -188,7 +189,7 @@ def lyap(A, Q, C=None, E=None):
|
188 | 189 | raiseControlArgument("E must be a square matrix with the same\
|
189 | 190 | dimension as A.")
|
190 | 191 |
|
191 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 192 | +ifnot_is_symmetric(Q): |
192 | 193 | raiseControlArgument("Q must be a symmetric matrix.")
|
193 | 194 |
|
194 | 195 | # Make sure we have access to the write slicot routine
|
@@ -309,7 +310,7 @@ def dlyap(A,Q,C=None,E=None):
|
309 | 310 | ifsize(Q)>1andshape(Q)[0]!=shape(Q)[1]:
|
310 | 311 | raiseControlArgument("Q must be a quadratic matrix.")
|
311 | 312 |
|
312 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 313 | +ifnot_is_symmetric(Q): |
313 | 314 | raiseControlArgument("Q must be a symmetric matrix.")
|
314 | 315 |
|
315 | 316 | # Solve the Lyapunov equation by calling the Slycot function sb03md
|
@@ -371,7 +372,7 @@ def dlyap(A,Q,C=None,E=None):
|
371 | 372 | raiseControlArgument("E must be a square matrix with the same\
|
372 | 373 | dimension as A.")
|
373 | 374 |
|
374 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 375 | +ifnot_is_symmetric(Q): |
375 | 376 | raiseControlArgument("Q must be a symmetric matrix.")
|
376 | 377 |
|
377 | 378 | # Solve the generalized Lyapunov equation by calling Slycot
|
@@ -500,10 +501,10 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True):
|
500 | 501 | size(B)==1andn>1:
|
501 | 502 | raiseControlArgument("Incompatible dimensions of B matrix.")
|
502 | 503 |
|
503 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 504 | +ifnot_is_symmetric(Q): |
504 | 505 | raiseControlArgument("Q must be a symmetric matrix.")
|
505 | 506 |
|
506 |
| -ifnot(asarray(R)==asarray(R).T).all(): |
| 507 | +ifnot_is_symmetric(R): |
507 | 508 | raiseControlArgument("R must be a symmetric matrix.")
|
508 | 509 |
|
509 | 510 | # Create back-up of arrays needed for later computations
|
@@ -603,10 +604,10 @@ def care(A, B, Q, R=None, S=None, E=None, stabilizing=True):
|
603 | 604 | size(S)==1andm>1:
|
604 | 605 | raiseControlArgument("Incompatible dimensions of S matrix.")
|
605 | 606 |
|
606 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 607 | +ifnot_is_symmetric(Q): |
607 | 608 | raiseControlArgument("Q must be a symmetric matrix.")
|
608 | 609 |
|
609 |
| -ifnot(asarray(R)==asarray(R).T).all(): |
| 610 | +ifnot_is_symmetric(R): |
610 | 611 | raiseControlArgument("R must be a symmetric matrix.")
|
611 | 612 |
|
612 | 613 | # Create back-up of arrays needed for later computations
|
@@ -775,10 +776,10 @@ def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
|
775 | 776 | size(B)==1andn>1:
|
776 | 777 | raiseControlArgument("Incompatible dimensions of B matrix.")
|
777 | 778 |
|
778 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 779 | +ifnot_is_symmetric(Q): |
779 | 780 | raiseControlArgument("Q must be a symmetric matrix.")
|
780 | 781 |
|
781 |
| -ifnot(asarray(R)==asarray(R).T).all(): |
| 782 | +ifnot_is_symmetric(R): |
782 | 783 | raiseControlArgument("R must be a symmetric matrix.")
|
783 | 784 |
|
784 | 785 | # Create back-up of arrays needed for later computations
|
@@ -882,10 +883,10 @@ def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
|
882 | 883 | size(S)==1andm>1:
|
883 | 884 | raiseControlArgument("Incompatible dimensions of S matrix.")
|
884 | 885 |
|
885 |
| -ifnot(asarray(Q)==asarray(Q).T).all(): |
| 886 | +ifnot_is_symmetric(Q): |
886 | 887 | raiseControlArgument("Q must be a symmetric matrix.")
|
887 | 888 |
|
888 |
| -ifnot(asarray(R)==asarray(R).T).all(): |
| 889 | +ifnot_is_symmetric(R): |
889 | 890 | raiseControlArgument("R must be a symmetric matrix.")
|
890 | 891 |
|
891 | 892 | # Create back-up of arrays needed for later computations
|
@@ -960,3 +961,12 @@ def dare_old(A, B, Q, R, S=None, E=None, stabilizing=True):
|
960 | 961 | # Invalid set of input parameters
|
961 | 962 | else:
|
962 | 963 | raiseControlArgument("Invalid set of input parameters.")
|
| 964 | + |
| 965 | + |
| 966 | +def_is_symmetric(M): |
| 967 | +M=atleast_2d(M) |
| 968 | +ifisinstance(M[0,0],inexact): |
| 969 | +eps=finfo(M.dtype).eps |
| 970 | +return ((M-M.T)<eps).all() |
| 971 | +else: |
| 972 | +return (M==M.T).all() |