[MATLAB] strange behaviour when duplicating symbolic variables

38 views
Skip to first unread message

E Grabovic

unread,
Mar 30, 2023, 2:30:02 AM3/30/23
to CasADi
Dear all,

lately i noticed a strange behaviour when manipulating symbolic SX arrays. In particular, after reassigning the content of a variable to a new one, changing elements of the latter also affects the original.
A simple example may be more explicative:

import casadi.*
a = [SX.sym('x'); 3];
b = a;
b(2) = 7;
now, if i check 
>>a =

[x, 7]
it inherits the change performed on b.
I am not sure if this is the intended behaviour or a bug, but i wanted to share this since it costed me a lot of time to debug one of my projects.

Thanks,
Eugeniu

Marios Gkionis

unread,
Mar 30, 2023, 7:18:10 AM3/30/23
to CasADi
Yes, that is a peculiarity of CASADI you should be aware of.

It is basically a mutable object (SX.sym and the other symbolic objects), which means that operation in one would happen to the other as well. It happens in Python with objects from classes as well, in which case you solve it using the copy() function.

Otherwise, build b with the same commands as you build a, which is more cumbersome.

Hope it helps
Reply all
Reply to author
Forward
0 new messages