Hi!
When demonstrating stuff in class, I often want the variable to be an argument to a function:
def test_subs(f, a, x=x):
print f(a)
This works if f is a function. If f is not a function, Sage issues a DeprecationWarning (rightly, IMHO). So we could try this:
def test_subs(f, a, x=x):
print f(x=a)
...which sort of works, but not with a different variable:
sage: f = t^2 + 2
sage: test_subs(f, 1, t)
t^2 + 2
Another way to make it work with expressions is by dictionary substitution:
def test_subs(f, a, x=x):
print f({x:a})
...and that now works with expressions, but not with functions:
sage: f(t) = t^2 + 2
sage: test_subs(f, 1, t)
...
TypeError: no canonical coercion from <type 'dict'> to Callable function ring with argument t
I can fix this with a try/except block, but when discussing it with a much wiser colleague he pointed out that it made the learning curve rather steep for kids who can barely do calculus.
Is there a smarter way to do substitution, so that a function doesn't have to worry about whether the input is an expression or an honest-to-goodness function?