ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 21 May 2011 18:49:08 +0200Difference between f and f(r)https://ask.sagemath.org/question/8123/difference-between-f-and-fr/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](http://flask.sagenb.org/home/pub/64/), 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?Sat, 21 May 2011 06:14:49 +0200https://ask.sagemath.org/question/8123/difference-between-f-and-fr/Answer by kcrisman for <p>I was experimenting with symbolic functions, and I was wondering what is the difference between these two forms of creating one:</p>
<pre><code>sage: f1 = function('f', r)
sage: f2(r) = function('f', r)
</code></pre>
<p>I have made some experiments <a href="http://flask.sagenb.org/home/pub/64/">in this Sage worksheet</a>, 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.</p>
<p>Any ideas?</p>
https://ask.sagemath.org/question/8123/difference-between-f-and-fr/?answer=12369#post-id-12369First 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)
f(3)
sage: f2(3)
f(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.
Sat, 21 May 2011 09:23:12 +0200https://ask.sagemath.org/question/8123/difference-between-f-and-fr/?answer=12369#post-id-12369Comment by DSM for <p>First off, what is missing here (but is in your worksheet) is <code>var("r")</code> at the beginning. Otherwise your first one is calling R!</p>
<p>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:</p>
<pre><code>sage: f1.subs(r=3)
f(3)
sage: f2(3)
f(3)
sage: preparse("f2(r) = function('f', r)")
'__tmp__=var("r"); f2 = symbolic_expression(function(\'f\', r)).function(r)'
</code></pre>
<p>What happens is that the <code>.function</code> method turns it into something with a specific variable <code>r</code> that is the input. If you had 'constants' like 'a' or 'b' in your function, this would make more sense, e.g.</p>
<pre><code>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)
</code></pre>
<p>So you could only substitute <code>r</code> with the function syntax; for the rest you'd need the <code>.subs</code> method.</p>
<p>Otherwise, they should be pretty much identical, I think. You have to be a little careful with whether you call the second one <code>f2(r)</code> or just <code>f2</code>, I think, in things like numerical integration, maybe. I can't remember the exact contexts in which that matters, but it is not frequent.</p>
https://ask.sagemath.org/question/8123/difference-between-f-and-fr/?comment=21700#post-id-21700@kcrisman: fixed trivial typo.Sat, 21 May 2011 14:22:54 +0200https://ask.sagemath.org/question/8123/difference-between-f-and-fr/?comment=21700#post-id-21700Comment by Juanlu001 for <p>First off, what is missing here (but is in your worksheet) is <code>var("r")</code> at the beginning. Otherwise your first one is calling R!</p>
<p>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:</p>
<pre><code>sage: f1.subs(r=3)
f(3)
sage: f2(3)
f(3)
sage: preparse("f2(r) = function('f', r)")
'__tmp__=var("r"); f2 = symbolic_expression(function(\'f\', r)).function(r)'
</code></pre>
<p>What happens is that the <code>.function</code> method turns it into something with a specific variable <code>r</code> that is the input. If you had 'constants' like 'a' or 'b' in your function, this would make more sense, e.g.</p>
<pre><code>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)
</code></pre>
<p>So you could only substitute <code>r</code> with the function syntax; for the rest you'd need the <code>.subs</code> method.</p>
<p>Otherwise, they should be pretty much identical, I think. You have to be a little careful with whether you call the second one <code>f2(r)</code> or just <code>f2</code>, I think, in things like numerical integration, maybe. I can't remember the exact contexts in which that matters, but it is not frequent.</p>
https://ask.sagemath.org/question/8123/difference-between-f-and-fr/?comment=21695#post-id-21695Hmm very subtle differences. Anyway, great answer, thanks!Sat, 21 May 2011 18:49:08 +0200https://ask.sagemath.org/question/8123/difference-between-f-and-fr/?comment=21695#post-id-21695