On Aug 20, 4:23 pm, Roger <[EMAIL PROTECTED]> wrote:
> What is the difference between:
>
> sage: derivative(3*x,x)(2)
>
> which gives an error, and
>
> sage: f(x) = 3*x
> sage: derivative(f,x)(2)
>
> which returns 3, and
>
> sage: f(x) = 3*x
> sage: derivative(f(x),x)(2)
>
> which also gives an error?
The difference is that f is a Callable Function, whereas f(x) and 3*x
are Symbolic Expressions. The signature of the Callable Function (the
signature is what arguments it can be called with and what it returns)
is defined when you create it with the assignment, and the signature
doesn't change under differentiation. When you call a Symbolic
Expression, however, sage looks at its variables to figure out what to
do--passing more arguments than there are variables results in a
ValueError--and the variables do change under differentiation. Let's
go spelunking:
sage: f(x) = 3*x
sage: parent(f)
Callable function ring with arguments (x,)
sage: parent(f(x))
Symbolic Ring
sage: parent(3*x)
Symbolic Ring
The parents all stay the same under differentiation:
sage: parent(derivative(f,x))
Callable function ring with arguments (x,)
sage: parent(derivative(f(x),x))
Symbolic Ring
sage: parent(derivative(3*x,x))
Symbolic Ring
The variables change, though:
sage: derivative(f,x).variables()
()
sage: derivative(f(x),x).variables()
()
sage: derivative(3*x).variables()
()
That doesn't matter for calling a Callable Function, but it matters
for calling a Symbolic Expression. So that's what the difference is.
Since the Callable Function does what you want, but the Symbolic
Expression doesn't, is this a misfeature of Symbolic Expression?
Well, it's certainly reasonable that lots of operations should change
the variables of a Symbolic Expression:
sage: var('y')
sage: (3*x).variables()
(x,)
sage: ((3*x)*y).variables()
(x, y)
That makes a lot of sense. So should you be allowed to call a
Symbolic Expression with more arguments than it has variables, with
the extras being ignored? Well, many times sending in too many
arguments is probably an indication of a mistake, so in that regard
it's nice that sage complains. I think the best thing is to realize
that calling a Symbolic Expression with a list of arguments is a cute
trick when it's unambiguous, but in non-obvious cases you should be
explicit:
sage: derivative(3*x,x)(x=2)
3
I'm new to sage myself, and the reason I decided to chase this stuff
down is because I was pretty confused by your examples. It seems like
the distinction between a Callable Function and a Symbolic Expression
is important and likely to trip people up. It's a fine distinction
from a programming standpoint, but one that's not usually made in
mathematics. It has a similar feel to Mathematica's distinction
between Set (=), and SetDelayed (:=), which confused the hell out of
me for quite a while since it's not a distinction that's made in
mathematics. Could this go in a list of "gotchas" somewhere, until
some careful words on the subject could be added to the tutorial?
Also, to the implementers, since f knows that its argument is called x
(and this is certainly important for something like derivative(f,x)),
why not
sage: f(x=2)
6
instead of
sage: f(x=2)
Traceback (click to the left for traceback)
...
TypeError: __call__() got an unexpected keyword argument 'x'
Regards,
JM
P.S. Apologies if this shows up several times. I'm having serious
troubles with the Google groups interface. How do you get support on
that?
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---