Evaluate MX without variables

  • Dominique Vercammen

    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)])
    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.


Log in to post a comment.