-
Notifications
You must be signed in to change notification settings - Fork 391
FAQ: cannot evaluate ... since variables ... are free
jgillis edited this page Apr 30, 2021
·
1 revision
Consider the following code:
x = MX.sym('x');
y = MX.sym('y');
z = x+y;
f = Function('f',{x},{z}); % Matlab
f = Function('f',[x],[z]); # Python
f(3)
The error message reads Cannot evaluate "f:(i0)->(o0) MXFunction" since variables [y] are free.
You can read this messages as "You forgot to list the symbol y" in the list of inputs when constructing 'f'".
Indeed, what numerical value should f(3)
correspond to? The presence of symbol y
makes this ill-defined.
Fix to:
f_fixed = Function('f',{x,y},{z}); % Matlab
f_fixed = Function('f',[x,y],[z]); # Python
Note that you can still call f
symbolically:
f(MX(3))
yields (3+y)
.
In fact this symbolic calling feature is the reason that the declaration of Function f
is allowed at all.
In future CasADi, we may opt to error at declaration unless the user explicitly indicates the desire to have free variables.