diff --git a/control/statesp.py b/control/statesp.py index 172b392c0..4c8321342 100644 --- a/control/statesp.py +++ b/control/statesp.py @@ -194,20 +194,15 @@ def _remove_useless_states(self): """ - # Indices of useless states. - useless = [] - - # Search for useless states. - for i in range(self.states): - if (all(self.A[i, :] == zeros((1, self.states))) and - all(self.B[i, :] == zeros((1, self.inputs)))): - useless.append(i) - # To avoid duplicate indices in useless, jump to the next - # iteration. - continue - if (all(self.A[:, i] == zeros((self.states, 1))) and - all(self.C[:, i] == zeros((self.outputs, 1)))): - useless.append(i) + # Search for useless states and get the indices of these states + # as an array. + ax1_A = np.where(~self.A.any(axis=1))[0] + ax1_B = np.where(~self.B.any(axis=1))[0] + ax0_A = np.where(~self.A.any(axis=0))[1] + ax0_C = np.where(~self.C.any(axis=0))[1] + useless_1 = np.intersect1d(ax1_A, ax1_B, assume_unique=True) + useless_2 = np.intersect1d(ax0_A, ax0_C, assume_unique=True) + useless = np.union1d(useless_1, useless_2) # Remove the useless states. self.A = delete(self.A, useless, 0)