MX substitution

217 views
Skip to first unread message

Fredrik Magnusson

unread,
Nov 17, 2013, 9:52:08 AM11/17/13
to casadi...@googlegroups.com
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?

Joel Andersson

unread,
Nov 18, 2013, 4:22:32 AM11/18/13
to casadi...@googlegroups.com
Hello!

The best approach would be to try to avoid the subsitution in the first place. That being said, I'm sure there are ways of improving the efficiency of substitutions so that your piece of code above would be relatively efficient. Since I'm going on vacation, I won't be able to look into it before the second week of December.

Best,
Joel

Fredrik Magnusson

unread,
Nov 18, 2013, 4:38:20 AM11/18/13
to casadi...@googlegroups.com
I see, thank you. I will think upon this and see if I can manage without substitutions.
Reply all
Reply to author
Forward
0 new messages