ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 10 Jan 2017 05:08:56 -0600Why use symbolic functions?http://ask.sagemath.org/question/36084/why-use-symbolic-functions/ So, I can do the following by introducing x as a symbolic function.
sage: var('b,c,t')
(b, c, t)
sage: x(t) = sqrt(b^2 + c^2*t^2) - b
sage: x
t |--> -b + sqrt(c^2*t^2 + b^2)
sage: diff(x,t)
t |--> c^2*t/sqrt(c^2*t^2 + b^2)
I can introduce x as a variable with assigned symbolic values, and get the same result.
sage: var('b,c,t')
(b, c, t)
sage: x = sqrt(b^2 + c^2*t^2) - b
sage: x
-b + sqrt(c^2*t^2 + b^2)
sage: diff(x,t)
c^2*t/sqrt(c^2*t^2 + b^2)
So, since I can do the same thing using variables, why use symbolic functions at all? Can symbolic functions do things that variables can't do?
Thanks in advance.Sat, 24 Dec 2016 13:44:49 -0600http://ask.sagemath.org/question/36084/why-use-symbolic-functions/Answer by eric_g for <p>So, I can do the following by introducing x as a symbolic function.</p>
<pre><code>sage: var('b,c,t')
(b, c, t)
sage: x(t) = sqrt(b^2 + c^2*t^2) - b
sage: x
t |--> -b + sqrt(c^2*t^2 + b^2)
sage: diff(x,t)
t |--> c^2*t/sqrt(c^2*t^2 + b^2)
</code></pre>
<p>I can introduce x as a variable with assigned symbolic values, and get the same result.</p>
<pre><code>sage: var('b,c,t')
(b, c, t)
sage: x = sqrt(b^2 + c^2*t^2) - b
sage: x
-b + sqrt(c^2*t^2 + b^2)
sage: diff(x,t)
c^2*t/sqrt(c^2*t^2 + b^2)
</code></pre>
<p>So, since I can do the same thing using variables, why use symbolic functions at all? Can symbolic functions do things that variables can't do?</p>
<p>Thanks in advance.</p>
http://ask.sagemath.org/question/36084/why-use-symbolic-functions/?answer=36087#post-id-36087The main difference between symbolic functions and symbolic variables is that symbolic functions are callable, i.e. you can evaluate the value of the function at any given argument via the parentheses operator:
sage: x(t) = sqrt(b^2 + c^2*t^2) - b
sage: x(0)
-b + sqrt(b^2)
sage: x(pi)
-b + sqrt(pi^2*c^2 + b^2)
whereas for a symbolic variable, you have to use the `subs` method:
sage: x = sqrt(b^2 + c^2*t^2) - b
sage: x.subs(t=0)
-b + sqrt(b^2)
Sun, 25 Dec 2016 11:53:21 -0600http://ask.sagemath.org/question/36084/why-use-symbolic-functions/?answer=36087#post-id-36087Comment by eric_g for <p>The main difference between symbolic functions and symbolic variables is that symbolic functions are callable, i.e. you can evaluate the value of the function at any given argument via the parentheses operator:</p>
<pre><code>sage: x(t) = sqrt(b^2 + c^2*t^2) - b
sage: x(0)
-b + sqrt(b^2)
sage: x(pi)
-b + sqrt(pi^2*c^2 + b^2)
</code></pre>
<p>whereas for a symbolic variable, you have to use the <code>subs</code> method:</p>
<pre><code>sage: x = sqrt(b^2 + c^2*t^2) - b
sage: x.subs(t=0)
-b + sqrt(b^2)
</code></pre>
http://ask.sagemath.org/question/36084/why-use-symbolic-functions/?comment=36245#post-id-36245Yes, if you want to see what Sage is doing "in the background" when you type `x(b,c,t) = sqrt(b^2 + c^2*t^2) - b`, use the `preparse` command:
sage: preparse("x(b,c,t) = sqrt(b^2 + c^2*t^2) - b")
'__tmp__=var("b,c,t"); x = symbolic_expression(sqrt(b**Integer(2) + c**Integer(2)*t**Integer(2)) - b).function(b,c,t)'
As you can see, Sage is running `var("b,c,t")` for you.Tue, 10 Jan 2017 05:08:56 -0600http://ask.sagemath.org/question/36084/why-use-symbolic-functions/?comment=36245#post-id-36245Comment by omoplata for <p>The main difference between symbolic functions and symbolic variables is that symbolic functions are callable, i.e. you can evaluate the value of the function at any given argument via the parentheses operator:</p>
<pre><code>sage: x(t) = sqrt(b^2 + c^2*t^2) - b
sage: x(0)
-b + sqrt(b^2)
sage: x(pi)
-b + sqrt(pi^2*c^2 + b^2)
</code></pre>
<p>whereas for a symbolic variable, you have to use the <code>subs</code> method:</p>
<pre><code>sage: x = sqrt(b^2 + c^2*t^2) - b
sage: x.subs(t=0)
-b + sqrt(b^2)
</code></pre>
http://ask.sagemath.org/question/36084/why-use-symbolic-functions/?comment=36107#post-id-36107Thanks. I noted also noted that I can declare the variables I need inside the parentheses on the left hand side. For example, I can define,
sage: x(b,c,t) = sqrt(b^2 + c^2*t^2) - b
without declaring b,c and t to be variables beforehand. But it appears that sage defines these variables in the background while defining the function x(t). So, afterwards, I can input
a
and not get an error, because it appears to have been defined in the background. But inputting a hitherto unused value, such as y, will give an error. Also, if I did not put b and c within parentheses on the left hand side without declaring them beforehand, that would also be an error.Tue, 27 Dec 2016 13:24:49 -0600http://ask.sagemath.org/question/36084/why-use-symbolic-functions/?comment=36107#post-id-36107