Skip to content

FAQ: When creating an MX or SX symbol, why do we have to give the name of a variable as a string argument?

András Retzler edited this page Oct 7, 2022 · 2 revisions

TL;DR The name that you give as a string argument is an identifier that you will see if you print the CasADi expression.

CasADi doesn't know how do you call the variable in the programming language that you use. Indeed, in the programming environment, you can even have multiple references to the same CasADi symbol (see example below). By telling CasADi how do you call that symbol, it is able to print the expression for you in a meaningful way, so the main point is that it's useful for debugging.

Also related question: what happens if we give different name on RHS than the name on the LHS? Well, you can do that:

>> this_is_my_x=casadi.MX.sym('x'); %we created a symbol in the CasADi shared object's memory space
>> (this_is_my_x^2)+2

ans = 

(2+sq(x))
>> this_is_also_my_x = this_is_my_x %the new MATLAB variable `this_is_also_my_x` is just another reference to the same CasADi variable

this_is_also_my_x = 

x
>> this_is_also_my_x*2+this_is_my_x %CasADi sees both of them as "x". When you print an expression like this, this text is generated internally by CasADi, not MATLAB.

ans = 

((2.*x)+x)

Nevertheless, it's best to just use the same name in MATLAB and CasADi, to avoid confusions:

x=casadi.MX.sym('x');

FAQTODO add here any operations that use the name given as string.

Clone this wiki locally