I would like to run the same code both:
- with as inputs MX variables, so that it's differentiable;
- with as inputs just constants.
I'll give a small toy example:
from casadi import *
t = MX("t",1)
out = MX.zeros(2)
out = tin+2
out = tin-2
print f(t) #MX variable as input
>> mapping(dense 2-by-1 matrix, dependencies: [[0,0],(t+2),(t-2)], nonzeros: [0(1),0(2),])
print f(5) #constant (i.e., non-casadi variable) as input
>> mapping(dense 2-by-1 matrix, dependencies: [[0,0],7,3], nonzeros: [0(1),0(2),])
outfun = MXFunction(,[f(5)])
The second mapping doesn't contain any variables anymore, so is it possible to just convert this MX to a constant array, without all the hassle of creating a function without inputs, initiating it, evaluating it and outputting it?
Hello! Sorry for not answering earlier. I missed this post. The MX class currently does not support constant folding. The behavior that you are requestion does indeed make sense, and we are working towards having both constant folding and more simplications for the MX type.
There is a ticket for this by the way: https://sourceforge.net/apps/trac/casadi/ticket/172
Log in to post a comment.