Ask Your Question

Difference between f and f(r)

asked 2011-05-20 23:14:49 -0500

Juanlu001 gravatar image

updated 2011-06-16 07:59:59 -0500

Kelvin Li gravatar image

I was experimenting with symbolic functions, and I was wondering what is the difference between these two forms of creating one:

sage: f1 = function('f', r)
sage: f2(r) = function('f', r)

I have made some experiments in this Sage worksheet, but except for some output differences, I don't know what will happen if I try to differentiate them, integrate them, put them into an equation, etc.

Any ideas?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2011-05-21 02:23:12 -0500

kcrisman gravatar image

updated 2011-05-21 07:21:39 -0500

DSM gravatar image

First off, what is missing here (but is in your worksheet) is var("r") at the beginning. Otherwise your first one is calling R!

As to the others, as you see on the worksheet, the difference is that the first one is a "symbolic expression", in which variables have to be substituted, while the second is a "callable symbolic expression", where you don't have to do this. Compare:

sage: f1.subs(r=3)
sage: f2(3)

sage: preparse("f2(r) = function('f', r)")
'__tmp__=var("r"); f2 = symbolic_expression(function(\'f\', r)).function(r)'

What happens is that the .function method turns it into something with a specific variable r that is the input. If you had 'constants' like 'a' or 'b' in your function, this would make more sense, e.g.

sage: var('a,b,c')
(a, b, c)
sage: f3(r)=a*b*c*function('f',r)
sage: f3
r |--> a*b*c*f(r)

So you could only substitute r with the function syntax; for the rest you'd need the .subs method.

Otherwise, they should be pretty much identical, I think. You have to be a little careful with whether you call the second one f2(r) or just f2, I think, in things like numerical integration, maybe. I can't remember the exact contexts in which that matters, but it is not frequent.

edit flag offensive delete link more


@kcrisman: fixed trivial typo.

DSM gravatar imageDSM ( 2011-05-21 07:22:54 -0500 )edit

Hmm very subtle differences. Anyway, great answer, thanks!

Juanlu001 gravatar imageJuanlu001 ( 2011-05-21 11:49:08 -0500 )edit

Your Answer


Add Answer

Question Tools


Asked: 2011-05-20 23:14:49 -0500

Seen: 400 times

Last updated: May 21 '11