1 | initial version |
Since your expression can be numerically evaluated only if all arguments are numeric, you can try :
sage: def foo(x, y): return e^(-x-y).n() if all(map(lambda u:SR(u).is_numeric(), (x, y))) else e^(-x-y)
sage: foo(a,b)
e^(-a - b)
sage: foo(1,1)
0.135335283236613
sage: foo(1,a)
e^(-a - 1)
sage: foo(x,1)
e^(-x - 1)
Alternative :
sage: def foo(x, y):
....: r=e^(-x-y)
....: try:
....: return r.n()
....: except:
....: return r
....:
sage: foo(a, b)
e^(-a - b)
sage: foo(a, 1)
e^(-a - 1)
sage: foo(1, 1)
0.135335283236613
HTH,