if statement in CasADi function

2,349 views
Skip to first unread message

Marco Vaccari

unread,
Jul 15, 2016, 9:33:55 AM7/15/16
to CasADi
Hi everybody,

I have a python function fun_py(x, y, z) built in a way similar to this

def fun_py(x,y,z):
    value1 = 1.0
    value2 = 2.0 
    if x[0] > value2:
       out = x+y+z
    elif x[0] > value1:
      out = y+z
    else: 
      out = z
    
    return out

I want now to build a Function
fun_cs = Function('fun_cs', [x,y,z], [fun_py]).

 When I run this command, I have this error: TypeError - cannot compute truth value of casadi ex-elem symbolic expression. 
Do you know if there is a way to compute this?

Best regards,
Marco 

Joel Andersson

unread,
Jul 15, 2016, 11:08:00 AM7/15/16
to CasADi
Hi!

Try the following:

out = if_else(x[0] > value2, x+y+z, if_else(x[0] > value1, y+z, z))

Joel

Marco Vaccari

unread,
Jul 15, 2016, 12:09:04 PM7/15/16
to CasADi
Hi Joel,

I'll try and then I'll let you know. Thanks for now.

Marco Vaccari

unread,
Jul 15, 2016, 12:42:37 PM7/15/16
to CasADi
Hi Joel, 
thanks for your help, it works but I have a further request for you. If the python function is:

def fun_py(x,y,z):
    value1 = 1.0
    value2 = 2.0
    value3 = 3.0
    if x[0] > value2 and x[0] < value3 :
       out = x+y+z
    elif x[0] > value1:
      out = y+z
    else: 
      out = z

how can I translate the AND statement?

Best,
Marco

Il giorno venerdì 15 luglio 2016 17:08:00 UTC+2, Joel Andersson ha scritto:

Joel Andersson

unread,
Jul 19, 2016, 10:24:51 PM7/19/16
to CasADi
casadi.logic_and

Marco Vaccari

unread,
Jul 20, 2016, 10:04:11 AM7/20/16
to CasADi
Thanks for your reply. 

Il giorno mercoledì 20 luglio 2016 04:24:51 UTC+2, Joel Andersson ha scritto:
casadi.logic_and
Reply all
Reply to author
Forward
0 new messages