Skip to content

Commit 443a7e3

Browse files
committed
Replaced inv with solve in mateqns
1 parent 29e7f95 commit 443a7e3

2 files changed

Lines changed: 23 additions & 24 deletions

File tree

control/mateqn.py

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

44-
from numpy.linalg import inv
4544
from scipy import shape, size, asarray, asmatrix, copy, zeros, eye, dot
46-
from scipy.linalg import eigvals, solve_discrete_are
45+
from scipy.linalg import eigvals, solve_discrete_are, solve
4746
from .exception import ControlSlycot, ControlArgument
4847

4948
__all__ = ['lyap', 'dlyap', 'dare', 'care']
@@ -557,9 +556,9 @@ def care(A,B,Q,R=None,S=None,E=None):
557556

558557
# Calculate the gain matrix G
559558
if size(R_b) == 1:
560-
G = dot(dot(1/(R_ba),asarray(B_ba).T) , X)
559+
G = dot(dot(1/(R_ba), asarray(B_ba).T), X)
561560
else:
562-
G = dot(dot(inv(R_ba),asarray(B_ba).T) , X)
561+
G = dot(solve(R_ba, asarray(B_ba).T), X)
563562

564563
# Return the solution X, the closed-loop eigenvalues L and
565564
# the gain matrix G
@@ -660,9 +659,9 @@ def care(A,B,Q,R=None,S=None,E=None):
660659

661660
# Calculate the gain matrix G
662661
if size(R_b) == 1:
663-
G = dot(1/(R_b),dot(asarray(B_b).T,dot(X,E_b))+asarray(S_b).T)
662+
G = dot(1/(R_b), dot(asarray(B_b).T, dot(X,E_b)) + asarray(S_b).T)
664663
else:
665-
G = dot(inv(R_b),dot(asarray(B_b).T,dot(X,E_b))+asarray(S_b).T)
664+
G = solve(R_b, dot(asarray(B_b).T, dot(X, E_b)) + asarray(S_b).T)
666665

667666
# Return the solution X, the closed-loop eigenvalues L and
668667
# the gain matrix G
@@ -699,7 +698,7 @@ def dare(A,B,Q,R,S=None,E=None):
699698
Rmat = asmatrix(R)
700699
Qmat = asmatrix(Q)
701700
X = solve_discrete_are(A, B, Qmat, Rmat)
702-
G = inv(B.T.dot(X).dot(B) + Rmat) * B.T.dot(X).dot(A)
701+
G = solve(B.T.dot(X).dot(B) + Rmat, B.T.dot(X).dot(A))
703702
L = eigvals(A - B.dot(G))
704703
return X, L, G
705704

@@ -825,11 +824,11 @@ def dare_old(A,B,Q,R,S=None,E=None):
825824

826825
# Calculate the gain matrix G
827826
if size(R_b) == 1:
828-
G = dot( 1/(dot(asarray(B_ba).T,dot(X,B_ba))+R_ba) , \
829-
dot(asarray(B_ba).T,dot(X,A_ba)) )
827+
G = dot(1/(dot(asarray(B_ba).T, dot(X, B_ba)) + R_ba), \
828+
dot(asarray(B_ba).T, dot(X, A_ba)))
830829
else:
831-
G = dot( inv(dot(asarray(B_ba).T,dot(X,B_ba))+R_ba) , \
832-
dot(asarray(B_ba).T,dot(X,A_ba)) )
830+
G = solve(dot(asarray(B_ba).T, dot(X, B_ba)) + R_ba, \
831+
dot(asarray(B_ba).T, dot(X, A_ba)))
833832

834833
# Return the solution X, the closed-loop eigenvalues L and
835834
# the gain matrix G
@@ -930,11 +929,11 @@ def dare_old(A,B,Q,R,S=None,E=None):
930929

931930
# Calculate the gain matrix G
932931
if size(R_b) == 1:
933-
G = dot( 1/(dot(asarray(B_b).T,dot(X,B_b))+R_b) , \
934-
dot(asarray(B_b).T,dot(X,A_b)) + asarray(S_b).T)
932+
G = dot(1/(dot(asarray(B_b).T, dot(X,B_b)) + R_b), \
933+
dot(asarray(B_b).T, dot(X,A_b)) + asarray(S_b).T)
935934
else:
936-
G = dot( inv(dot(asarray(B_b).T,dot(X,B_b))+R_b) , \
937-
dot(asarray(B_b).T,dot(X,A_b)) + asarray(S_b).T)
935+
G = solve(dot(asarray(B_b).T, dot(X,B_b)) + R_b, \
936+
dot(asarray(B_b).T, dot(X,A_b)) + asarray(S_b).T)
938937

939938
# Return the solution X, the closed-loop eigenvalues L and
940939
# the gain matrix G

control/tests/mateqn_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from numpy import matrix
4747
from numpy.testing import assert_array_almost_equal, assert_array_less
4848
# need scipy version of eigvals for generalized eigenvalue problem
49-
from scipy.linalg import inv, eigvals
49+
from scipy.linalg import eigvals, solve
5050
from scipy import zeros,dot
5151
from control.mateqn import lyap,dlyap,care,dare
5252
from control.exception import slycot_check
@@ -150,8 +150,8 @@ def test_care_g(self):
150150
# print("The solution obtained is", X)
151151
assert_array_almost_equal(
152152
A.T * X * E + E.T * X * A -
153-
(E.T * X * B + S) * inv(R) * (B.T * X * E + S.T) + Q, zeros((2,2)))
154-
assert_array_almost_equal(inv(R) * (B.T * X * E + S.T), G)
153+
(E.T * X * B + S) * solve(R, B.T * X * E + S.T) + Q, zeros((2,2)))
154+
assert_array_almost_equal(solve(R, B.T * X * E + S.T), G)
155155

156156
A = matrix([[-2, -1],[-1, -1]])
157157
Q = matrix([[0, 0],[0, 1]])
@@ -177,8 +177,8 @@ def test_dare(self):
177177
# print("The solution obtained is", X)
178178
assert_array_almost_equal(
179179
A.T * X * A - X -
180-
A.T * X * B * inv(B.T * X * B + R) * B.T * X * A + Q, zeros((2,2)))
181-
assert_array_almost_equal(inv(B.T * X * B + R) * B.T * X * A, G)
180+
A.T * X * B * solve(B.T * X * B + R, B.T * X * A) + Q, zeros((2,2)))
181+
assert_array_almost_equal(solve(B.T * X * B + R, B.T * X * A), G)
182182
# check for stable closed loop
183183
lam = eigvals(A - B * G)
184184
assert_array_less(abs(lam), 1.0)
@@ -192,7 +192,7 @@ def test_dare(self):
192192
# print("The solution obtained is", X)
193193
assert_array_almost_equal(
194194
A.T * X * A - X -
195-
A.T * X * B * inv(B.T * X * B + R) * B.T * X * A + Q, zeros((2,2)))
195+
A.T * X * B * solve(B.T * X * B + R, B.T * X * A) + Q, zeros((2,2)))
196196
assert_array_almost_equal(B.T * X * A / (B.T * X * B + R), G)
197197
# check for stable closed loop
198198
lam = eigvals(A - B * G)
@@ -210,9 +210,9 @@ def test_dare_g(self):
210210
# print("The solution obtained is", X)
211211
assert_array_almost_equal(
212212
A.T * X * A - E.T * X * E -
213-
(A.T * X * B + S) * inv(B.T * X * B + R) * (B.T * X * A + S.T) + Q,
213+
(A.T * X * B + S) * solve(B.T * X * B + R, B.T * X * A + S.T) + Q,
214214
zeros((2,2)) )
215-
assert_array_almost_equal(inv(B.T * X * B + R) * (B.T * X * A + S.T), G)
215+
assert_array_almost_equal(solve(B.T * X * B + R, B.T * X * A + S.T), G)
216216
# check for stable closed loop
217217
lam = eigvals(A - B * G, E)
218218
assert_array_less(abs(lam), 1.0)
@@ -228,7 +228,7 @@ def test_dare_g(self):
228228
# print("The solution obtained is", X)
229229
assert_array_almost_equal(
230230
A.T * X * A - E.T * X * E -
231-
(A.T * X * B + S) * inv(B.T * X * B + R) * (B.T * X * A + S.T) + Q,
231+
(A.T * X * B + S) * solve(B.T * X * B + R, B.T * X * A + S.T) + Q,
232232
zeros((2,2)) )
233233
assert_array_almost_equal((B.T * X * A + S.T) / (B.T * X * B + R), G)
234234
# check for stable closed loop

0 commit comments

Comments
 (0)