fast_callable of sybolyc expression with no variables

37 views
Skip to first unread message

Jesús Torrado

unread,
Nov 24, 2014, 2:19:40 PM11/24/14
to sage-s...@googlegroups.com
Hi there,

I am using fast_callable in order to pass numpy arrays to symbolic expressions, as suggested [here](http://ask.sagemath.org/question/8383/using-symbolic-expressions-with-numpy-arrays/)

I encountered the following behaviour:

    sage: var('x')
    sage: f(x) = x**2
    sage: fast_callable(f)(2)
    4
    sage: f(x) = 0
    sage: fast_callable(f)(2)
    [...]
    ValueError [and no message here]
    sage: f(x) = 0
    sage: f(x) = 0
    sage: fast_callable(f, vars=[x])(2)
    0

I guess this is not a bad behaviour per se (though the exception looks like it has not been handled properly). Nevertheless, I think it would be nice that when `f.variables()==()`, `fast_callable` simply discards the input. It would save some testing for corner cases: if I pass `f` to a function that expects `f(x)` with one variable, one would have to check for the special case of a zero-defined `f`.

Any opinions?

Best,
Jesus Torrado

Nils Bruin

unread,
Nov 28, 2014, 2:19:23 PM11/28/14
to sage-s...@googlegroups.com

The case of 0 variables is only a special case. This occurs more generally for "symbolic functions"  that do not actually have all their arguments appear in their evaluating expression:

sage: f(x,y)=y
sage: fast_callable(f)(1,2)
ValueError:

In fact, the behaviour leads to silent differences in results that should probably lead to an error of some sort:

sage: f(3)
y
sage: fast_callable(f)(3)
3

As you remark, fast_callable(f,vars=[x,y]) does do the right:

sage: fast_callable(f,vars=(x,y))(1,2)
2
sage: fast_callable(f,vars=(x,y))(3)
ValueError:

so the problem is that fast_callable is using the wrong heuristics for determining "vars" when not explicitly given. From the printing it's pretty clear what should be used:

sage: f
(x, y) |--> y

(clearly the left hand side is the best guess for "vars" in this case)

but I have trouble finding an easy criterion to recognize that f needs different treatment from f(x,y):

sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: type(f(x,y))
<type 'sage.symbolic.expression.Expression'>
sage: f(x,y)
y

The heuristics used in fast_callable are problematic in other situations too:

sage: f(u,v)=x^2*u+v
sage: f
(u, v) |--> u*x ^2+ v
sage: f(3,5,7) #shouldn't we complain about the extra argument?
3*x^2 + 5
sage: fast_callable(f)(1,2)
ValueError:
sage: fast_callable(f)(3,5,7) #do we know for sure the x will land in the third position?
152
sage: fast_callable(f,vars=(u,v))(1,2) #perhaps the free x should cause an earlier error?
ValueError: Variable 'x' not found

Reply all
Reply to author
Forward
0 new messages