8000 gen_casadi_test: Fix instantiating np.array with list of MX objects · pymoca/pymoca@649e22a · GitHub
[go: up one dir, main page]

Skip to content

Commit 649e22a

Browse files
committed
gen_casadi_test: Fix instantiating np.array with list of MX objects
Starting from, NumPy 1.20 behaves differently when instantiating arrays from array-like objects. NumPy used to be agnostic about the type of the object inside the list (MX in our case), but now it actually tries to interpret it as an array if it can. In this particular case, we deliberately _did not_ want this behavior. We use something akin to the work-around described in [0] to get the old behavior back. For more details, see: [0] https://numpy.org/devdocs/release/1.20.0-notes.html under "ArrayLike objects which do not define __len__ and __getitem__" or [1] numpy/numpy#17973
1 parent 153921e commit 649e22a

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

test/gen_casadi_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,10 @@ def test_delay_for_loop_with_expand_vectors(self):
724724
ref_model = Model()
725725

726726
def _array_mx(name, n):
727-
return np.array([ca.MX.sym("{}[{}]".format(name, i+1)) for i in range(n)])
727+
arr = np.empty(n, dtype=object)
728+
for i in range(n):
729+
arr[i] = ca.MX.sym("{}[{}]".format(name, i+1))
730+
return arr
728731

729732
x = _array_mx("x", 3)
730733
y = _array_mx("y", 3)

0 commit comments

Comments
 (0)
0