Skip to content

Commit 674f2e8

Browse files
jamestjspclaude
andcommitted
Fix minreal() non-deterministic behavior from uninitialized memory
- Change empty() to zeros() for padded B/C matrices in minreal() - Fix pytest fixture syntax in minreal_test.py - Restore original assertion: nreductions == 2 The bug caused garbage memory values in extra columns when ninputs != noutputs, leading to non-deterministic tb01pd results. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent ce85019 commit 674f2e8

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

control/statesp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,9 +1176,9 @@ def minreal(self, tol=0.0):
11761176
if self.nstates:
11771177
try:
11781178
from .slicot_compat import tb01pd
1179-
B = empty((self.nstates, max(self.ninputs, self.noutputs)))
1179+
B = zeros((self.nstates, max(self.ninputs, self.noutputs)))
11801180
B[:, :self.ninputs] = self.B
1181-
C = empty((max(self.noutputs, self.ninputs), self.nstates))
1181+
C = zeros((max(self.noutputs, self.ninputs), self.nstates))
11821182
C[:self.noutputs, :] = self.C
11831183
A, B, C, nr = tb01pd(self.nstates, self.ninputs, self.noutputs,
11841184
self.A, B, C, tol=tol)

control/tests/minreal_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from itertools import permutations
1414

1515

16-
@pytest.fixture
17-
def fixedseed(scope="class"):
16+
@pytest.fixture(scope="class")
17+
def fixedseed():
1818
np.random.seed(5)
1919

2020

@@ -79,9 +79,8 @@ def testMinrealBrute(self):
7979
# Find the closest zero
8080
assert min(abs(z1 - z)) <= 1e-7
8181

82-
# Make sure some reductions occurred (exact count depends on minreal impl)
83-
# (May vary between slycot and slicot implementations)
84-
assert nreductions >= 0
82+
# Verify expected reductions occur (seed=5 yields 2 reducible systems)
83+
assert nreductions == 2
8584

8685
def testMinrealSS(self):
8786
"""Test a minreal model reduction"""

0 commit comments

Comments
 (0)