Greetings!
I am looking for an efficient way to substitute parts of MX expressions. Consider the following simple, illustrative example:
from casadi import *
from casadi.tools import *
X = struct_msym([entry("x", shape=2), entry("y", shape=4)])
exp = sum(X["x"]) + sum(X["y"])
try:
exp_new = casadi.substitute(exp, X["x"], 2 * X["x"])
except RuntimeError as e:
print(e) # Argument #0 is not symbolic
exp_new = casadi.substitute(exp, X.cat, vertcat([2 * X["x"], X["y"]]))
So, I have a large number of MX variables, X, and I have an expression constructed out of these variables, exp. I want to substitute some of the MX variables in this expression for something else. The attempt which I would have hoped would work is in the try clause, but it does not work because "Argument #0 is not symbolic".
The only way I have found of getting what I want is the final line of code, but this seems terribly inefficient (both during the actual substitution but also later when I want to evaluate exp_new).
Is there a better way of doing the substitution? Or is my final approach not as terrible as it seems? Or is this an exercise in futility?