Accessing elements of array

444 views
Skip to first unread message

Ryan

unread,
Nov 4, 2022, 10:10:41 AM11/4/22
to CasADi
I have an array and want to extract the element at index i.

The array is originally an numpy array and the index variable is an mx symbolic variable.
I first tried:
test_array = np.array([1,2,3,4])
index = MX(2)

return test_array[index]
-> This results in an error, which is due to some conflict with numpy>1.20.0

Then I tried:
test_array = np.array([1,2,3,4])
test_array_mx = MX(test_array)
index = MX(2)

return test_array_mx(index)
>> MX([1,2,3,4][2])

So both of those options do not work for accessing the 2nd element of the array. Is there a way to do so? Or is there a way to bring MX([1,2,3,4][2]) to MX(3)

Bruno M L

unread,
Nov 4, 2022, 12:47:20 PM11/4/22
to CasADi
Hi! 

Try using the "full" command, something like MX_variable.full().

Or are you trying to define a casadi variable which is equal to a number? Then, use DM and not MX.

Make sure you read carefully Section 3 of the documentation.

Ryan

unread,
Nov 4, 2022, 3:12:09 PM11/4/22
to CasADi
Thanks for your input.
I can see that I can get the variable at index i if the array is DM, but this is only possible if the index is a real number and not if the index is also a symbolic variable (SX or MX).
As there is no option to extract the current  value from a SX variable to use it as the index, I am still stuck.
I also tried the full command, which only works for DM matrices, but this also doesn't help, as DM_get doesn't allow get(DM, bool, SX) or get(DM, bool, MX)

Bruno M L

unread,
Nov 4, 2022, 4:48:17 PM11/4/22
to CasADi
I don't understand very well why you are trying to do this. Maybe rewrite your code so that you don't need this.
Reply all
Reply to author
Forward
0 new messages