Evaluate MX without variables

2012-09-20
2013-04-11
  • Hello all,

    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)
    def f(tin):
        out = MX.zeros(2)
        out[0] = tin+2
        out[1] = tin-2
        return out
    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)])
    outfun.init()
    outfun.evaluate()
    print outfun.output()
    >> [7,3]
    

    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?

     
  • Joel Andersson
    Joel Andersson
    2012-10-01

    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.