Skip to content

Commit 32323d4

Browse files
committed
Refactored code to be more pythonic
Mainly list comprehensions instead of for-loops
1 parent 2c25b73 commit 32323d4

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

control/modelsimp.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,12 @@ def modred(sys, ELIM, method='matchdc'):
149149

150150

151151
#Check system is stable
152-
D,V = np.linalg.eig(sys.A)
153-
for e in D:
154-
if e.real >= 0:
155-
raise ValueError("Oops, the system is unstable!")
152+
if any(e.real >= 0.0 for e in np.linalg.eigvals(sys.A)):
153+
raise ValueError("Oops, the system is unstable!")
154+
156155
ELIM = np.sort(ELIM)
157-
NELIM = []
158156
# Create list of elements not to eliminate (NELIM)
159-
for i in range(0,len(sys.A)):
160-
if i not in ELIM:
161-
NELIM.append(i)
157+
NELIM = [i for i in range(len(sys.A)) if i not in ELIM]
162158
# A1 is a matrix of all columns of sys.A not to eliminate
163159
A1 = sys.A[:,NELIM[0]]
164160
for i in NELIM[1:]:
@@ -255,12 +251,8 @@ def balred(sys, orders, method='truncate'):
255251
dico = 'C'
256252

257253
#Check system is stable
258-
D,V = np.linalg.eig(sys.A)
259-
# print D.shape
260-
# print D
261-
for e in D:
262-
if e.real >= 0:
263-
raise ValueError("Oops, the system is unstable!")
254+
if any(e.real >= 0.0 for e in np.linalg.eigvals(sys.A)):
255+
raise ValueError("Oops, the system is unstable!")
264256

265257
if method=='matchdc':
266258
raise ValueError ("MatchDC not yet supported!")

control/statefbk.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,9 @@ def gram(sys,type):
355355

356356
#TODO: Check system is stable, perhaps a utility in ctrlutil.py
357357
# or a method of the StateSpace class?
358-
D,V = np.linalg.eig(sys.A)
359-
for e in D:
360-
if e.real >= 0:
361-
raise ValueError("Oops, the system is unstable!")
358+
if any(e.real >= 0.0 for e in np.linalg.eigvals(sys.A)):
359+
raise ValueError("Oops, the system is unstable!")
360+
362361
if type=='c':
363362
tra = 'T'
364363
C = -np.dot(sys.B,sys.B.transpose())
@@ -380,4 +379,3 @@ def gram(sys,type):
380379
X,scale,sep,ferr,w = sb03md(n, C, A, U, dico, job='X', fact='N', trana=tra)
381380
gram = X
382381
return gram
383-

0 commit comments

Comments
 (0)