Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite2877a1

Browse files
bnavigatormurrayrm
authored andcommitted
Check for symmetric matrices with machine precision (#348)
* check for symmetric matrices with machine precision* mateqn_test array instead of deprecated matrix* test mateqn parameter checks* check for int first
1 parentcb25633 commite2877a1

File tree

2 files changed

+180
-106
lines changed

2 files changed

+180
-106
lines changed

‎control/mateqn.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
Author: Bjorn Olofsson
4242
"""
4343

44-
fromnumpyimportshape,size,array,asarray,copy,zeros,eye,dot
44+
fromnumpyimportshape,size,asarray,copy,zeros,eye,dot, \
45+
finfo,inexact,atleast_2d
4546
fromscipy.linalgimporteigvals,solve_discrete_are,solve
4647
from .exceptionimportControlSlycot,ControlArgument
4748
from .statespimport_ssmatrix
@@ -122,7 +123,7 @@ def lyap(A, Q, C=None, E=None):
122123
ifsize(Q)>1andshape(Q)[0]!=shape(Q)[1]:
123124
raiseControlArgument("Q must be a quadratic matrix.")
124125

125-
ifnot(asarray(Q)==asarray(Q).T).all():
126+
ifnot_is_symmetric(Q):
126127
raiseControlArgument("Q must be a symmetric matrix.")
127128

128129
# Solve the Lyapunov equation by calling Slycot function sb03md
@@ -188,7 +189,7 @@ def lyap(A, Q, C=None, E=None):
188189
raiseControlArgument("E must be a square matrix with the same\
189190
dimension as A.")
190191

191-
ifnot(asarray(Q)==asarray(Q).T).all():
192+
ifnot_is_symmetric(Q):
192193
raiseControlArgument("Q must be a symmetric matrix.")
193194

194195
# Make sure we have access to the write slicot routine
@@ -309,7 +310,7 @@ def dlyap(A,Q,C=None,E=None):
309310
ifsize(Q)>1andshape(Q)[0]!=shape(Q)[1]:
310311
raiseControlArgument("Q must be a quadratic matrix.")
311312

312-
ifnot(asarray(Q)==asarray(Q).T).all():
313+
ifnot_is_symmetric(Q):
313314
raiseControlArgument("Q must be a symmetric matrix.")
314315

315316
# Solve the Lyapunov equation by calling the Slycot function sb03md
@@ -371,7 +372,7 @@ def dlyap(A,Q,C=None,E=None):
371372
raiseControlArgument("E must be a square matrix with the same\
372373
dimension as A.")
373374

374-
ifnot(asarray(Q)==asarray(Q).T).all():
375+
ifnot_is_symmetric(Q):
375376
raiseControlArgument("Q must be a symmetric matrix.")
376377

377378
# 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):
500501
size(B)==1andn>1:
501502
raiseControlArgument("Incompatible dimensions of B matrix.")
502503

503-
ifnot(asarray(Q)==asarray(Q).T).all():
504+
ifnot_is_symmetric(Q):
504505
raiseControlArgument("Q must be a symmetric matrix.")
505506

506-
ifnot(asarray(R)==asarray(R).T).all():
507+
ifnot_is_symmetric(R):
507508
raiseControlArgument("R must be a symmetric matrix.")
508509

509510
# 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):
603604
size(S)==1andm>1:
604605
raiseControlArgument("Incompatible dimensions of S matrix.")
605606

606-
ifnot(asarray(Q)==asarray(Q).T).all():
607+
ifnot_is_symmetric(Q):
607608
raiseControlArgument("Q must be a symmetric matrix.")
608609

609-
ifnot(asarray(R)==asarray(R).T).all():
610+
ifnot_is_symmetric(R):
610611
raiseControlArgument("R must be a symmetric matrix.")
611612

612613
# 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):
775776
size(B)==1andn>1:
776777
raiseControlArgument("Incompatible dimensions of B matrix.")
777778

778-
ifnot(asarray(Q)==asarray(Q).T).all():
779+
ifnot_is_symmetric(Q):
779780
raiseControlArgument("Q must be a symmetric matrix.")
780781

781-
ifnot(asarray(R)==asarray(R).T).all():
782+
ifnot_is_symmetric(R):
782783
raiseControlArgument("R must be a symmetric matrix.")
783784

784785
# 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):
882883
size(S)==1andm>1:
883884
raiseControlArgument("Incompatible dimensions of S matrix.")
884885

885-
ifnot(asarray(Q)==asarray(Q).T).all():
886+
ifnot_is_symmetric(Q):
886887
raiseControlArgument("Q must be a symmetric matrix.")
887888

888-
ifnot(asarray(R)==asarray(R).T).all():
889+
ifnot_is_symmetric(R):
889890
raiseControlArgument("R must be a symmetric matrix.")
890891

891892
# 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):
960961
# Invalid set of input parameters
961962
else:
962963
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()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp