Skip to content

Commit 8285cbf

Browse files
committed
Removed redundant numpy.linalg.solve calls
Also improved the error message for the singularity check.
1 parent 4d70d00 commit 8285cbf

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

control/statesp.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,16 @@ def feedback(self, other=1, sign=-1):
448448

449449
F = eye(self.inputs) - sign * D2 * D1
450450
if matrix_rank(F) != self.inputs:
451-
raise ValueError("I - sign * D2 * D1 is singular.")
451+
raise ValueError("I - sign * D2 * D1 is singular to working precision.")
452452

453453
# Precompute F\D2 and F\C2 (E = inv(F))
454-
E_D2 = solve(F, D2)
455-
E_C2 = solve(F, C2)
454+
# We can solve two linear systems in one pass, since the
455+
# coefficients matrix F is the same. Thus, we perform the LU
456+
# decomposition (cubic runtime complexity) of F only once!
457+
# The remaining back substitutions are only quadratic in runtime.
458+
E_D2_C2 = solve(F, concatenate((D2, C2), axis=1))
459+
E_D2 = E_D2_C2[:, :other.inputs]
460+
E_C2 = E_D2_C2[:, other.inputs:]
456461

457462
T1 = eye(self.outputs) + sign * D1 * E_D2
458463
T2 = eye(self.inputs) + sign * E_D2 * D1

0 commit comments

Comments
 (0)