<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

+<!-- This stuff makes the Sage cell instances work right -->

+ <script src="http://aleph.sagemath.org/static/jquery.min.js"></script>

+ <script src="http://aleph.sagemath.org/embedded_sagecell.js"></script>

+ sagecell.makeSagecell({inputLocation: '#FirstFunc',

+ template: sagecell.templates.restricted});

+ sagecell.makeSagecell({inputLocation: '#FirstExpr',

+ template: sagecell.templates.restricted});

+ sagecell.makeSagecell({inputLocation: '#Simp1',

+ template: sagecell.templates.restricted});

+ sagecell.makeSagecell({inputLocation: '#Simp2',

+ template: sagecell.templates.restricted});

+ sagecell.makeSagecell({inputLocation: '#Solve1',

+ template: sagecell.templates.restricted});

+ sagecell.makeSagecell({inputLocation: '#Solve2',

+ template: sagecell.templates.restricted});

<title>Functions and Symbolic Expressions</title>

<meta name="author" content="Byungchul Cha, Karl-Dieter Crisman, Dan Drake, and Jason Grout" />

<h1>Functions and Symbolic Expressions</h1>

-<p>In the example we did <a href="Evaluate.html"

-class=internal">earlier</a>, it looked like one could define an

-arbitrary function and evaluate it. However, it turns out that there

-are a few syntactic restrictions.</p>

+<p>In the examples we saw earlier, when learning how to

+<a href="Evaluate.html" class=internal>evaluate commands</a> and how to

+<a href="Plot.html" class=internal>plot</a>, we saw that normally one could

+define an arbitrary function and then evaluate it.</p>

-<p>One has already been mentioned - the need to declare variables ahead

-of time. This is in keeping with a philosophy in Sage (and Python)

+<div id="FirstFunc"><script type="text/x-sage">f(t)=200*3^(-2*t)+100

+plot(f,(t,1,10))</script></div>

+<p>By the way, this example of exponential decay to a baseline shows the special

+meanings of '*' and '^' as multiplication and exponentiation. Although it's possible

+to enable implicit multiplication in Sage, this is discouraged, due to the potential

+<p>This is also one of the motivations for the syntactic restriction mentioned before -

+the need to declare variables ahead of time if one does <i>not</i> declare a function

+with its inputs. Compare the following multivariate examples.

+<li><code>F1 = t+2*r</code></li>

+<li><code>F2(r,t) = t+2*r</code></li>

+With the second syntax, it is completely clear that <code>F2(1,2)=4</code>. With the first

+syntax, should it be 4 or 5? You can check that it gives a warning if we try to evaluate it.

+However, if we explicitly substitute in the arguments, we are okay.</p>

+<div id="FirstExpr"><script type="text/x-sage">var('r,t')

+print F1(r=1,t=2)</script></div>

+<p>This is in keeping with a philosophy in Sage (and Python)

that, in general, explicit is better than implicit. It can be very frustrating

to have a command generate unexpected output when the problem is just that

-the user typed <code>yy</code> by accident instead of <code>y</code>!

-followed rigorously, but is important to keep in mind.</p>

+the user typed <code>yy</code> by accident instead of <code>y</code>!

+While Sage does try on occasion to 'guess' what the user intended, usually it's just

+as likely that a typo or other input error occurred.</p>

-<p>One place this appears is in the following scenario.</p>

+<p>So why not always use functions? Well, one might not <i>want</i> to claim that something

+is a function of all its variables! For instance, <code>f(t)=P*e^(r*t)</code> really makes

+more sense as a function of a single variable with some parameters the user can put in as needed.</p>

-<p>[var('r t'); FV=100*e^(r*t)]</p>

+<p>And when we do not use the function notation at all, we have what is called a "symbolic

+expression". For another example, try the following comparison, with <code>g=x^2</code>.

+<li><code>g(3)=3*x^2</code></li>

+<li><code>g(3)=9</code></li>

-<p>There are several things worth pointing out with this example. First

-of all, it should be clear from this example (at least, for anyone who

-has taught continuous compounding of interest as an example of

-exponential functions) that '*' and '^' have special meaning as

-multiplication and exponentiation.</p>

+<p>Notwithstanding these issues, symbolic expressions can be quite useful.

+This is particularly true with simplification and solving. Below, we define

+an expression $z$, and then do various manipulations with it.</p>

-<p>Second, although this <i>appears</i> to be a function, it is in fact

-not one! Notice that we don't use the usual $f(x)$ functional notation.

-This is what is known as a symbolic expression - one which requires

-explicit mentioning of its variables!</p>

+<div id="Simp1"><script type="text/x-sage">var('z')

+print simplify(expand(z))</script></div>

-<p>Indeed, if we were to use the syntax $FV(10,.05)$, which variable

-should 10 refer to? The first one? The first one alphabetically? One

-sees a similar reason for avoiding this with a simpler example.</p>

-<p>Should $g(3)=3x^2$ or $9$? So instead we use the function notation

-when a function is needed.</p>

-<p>However, symbolic expressions can be quite useful. This is

-particularly true with simplification and solving. Below, we define an

-expression $z$, and then do various manipulations with it.</p>

-<p>[z = (x+1)^3; expand(z); factor(expand(z)); simplify(expand(z))]</p>

-<p>Sage can do fairly interesting simplifications thanks to Maxima being

-the underlying engine for such things. Here, we are using the 'dot'

+<p>Sage can do fairly interesting simplifications thanks to Maxima being

+the underlying engine for such things. Below, we are using the 'dot'

-<p>[z = ((x - 1)^(3/2) - (x + 1)*sqrt(x - 1))/sqrt((x - 1)*(x + 1));

-z.simplify_rational()]</p>

-<p>One small note is that to view a nicer-looking version of this, the

-'show' command is useful.</p>

+<div id="Simp1"><script type="text/x-sage">var('z')

+z = ((x - 1)^(3/2) - (x + 1)*sqrt(x - 1))/sqrt((x - 1)*(x + 1))

+z.simplify_rational()</script></div>

-<p>[show(z.simplify_rational())]</p>

+<p>This looks a little ugly. To use FIXME: which? MathJax/jsmath to make it look nicely

+typeset, try using <code>show()</code>.</p>

-<p>Finally, it is extremely helpful to be able to solve many symbolic

-equations via symbolic expressions.</p>

+<div id="Simp2"><script type="text/x-sage">var('z')

+z = ((x - 1)^(3/2) - (x + 1)*sqrt(x - 1))/sqrt((x - 1)*(x + 1))

+show(z.simplify_rational())</script></div>

-<p>[solve(x^2==4,x)]</p>

+<p>One may solve many symbolic equations via symbolic expressions. As we've seen, the single

+equals sign <code>=</code> is for giving a name to something, as we did with <code>z</code>

+and the somewhat complicated expression above. So we use <code>==</code>, a double equals sign,

+to signify an equation.</p>

-<p>This is the main place the double equals sign is useful for

-beginners. We can also solve more complicated ones, including where there might

-be a dummy variable in the solution. Again, don't forget to let Sage know

+<div id="Solve1"><script type="text/x-sage">solve(x^2==4,x)</script></div>

+<p>We can also solve more complicated ones. Again, don't forget to let Sage know

about your variables.</p>

-<p>[y=var('y'); solve([x+y == 3, 2*x+2*y == 6],x,y)]</p>

+<div id="Solve2"><script type="text/x-sage">y=var('w')

+solve([x+w == 3, 2*x+2*w == 6],x,w)</script></div>

+<p>In this case, there is a dummy variable. The <code>r1</code> signifies that an arbitrary <i>r</i>eal

+number can be substituted.</p>