M-2: Manipulation Mastery

This lesson is on instructing Maple whether to use symbolic vs explicit numeric evaluation. To give concrete examples, we’ll be introducing commands to add up a sum, to compute binomial coefficients, and commands for factorization.

Again, make sure you keep all of your complete work. After completing this lesson you can read the homework formatting specification which gives more a precise description of how to hand in your full work.

Summer Days

Just as LaTeX has a method for typesetting sums (here, the sum of squares)

\[\sum_{i=1}^{100} i^2\]

so does Maple have a command for evaluating or representing sums (again, the sum of squares)

> sum(i^2, i=1..100);

If you typed this in correctly, the result will be 338350. (The choice of i was arbitrary: you can use any variable name as the sum index.) What about square roots?

> sum(i^(1/2), i=1..100);

This is an example where knowing the exact answer is less helpful than an approximation, since it shows a sum of 100 terms. Type the following exactly:

> evalf(%);

The % symbol is Maple shorthand for “the result of the previous thing you asked Maple to evaluate.” In this case, it should give you approximately 671.46, the floating-point evaluation (recall evalf() from the last lesson) of the previous result. Occasionally you might want the second-last result, can this also be done? Type ?% to find out.

Note: though % can be very useful when using Maple to work through a problem, we recommend avoiding the use of it on assignments. If you think you will need to reuse a result on your homework, assign a variable to the value.

Note: there are other ways to coerce Maple to doing a floating-point calculation instead of an exact one. Writing i^0.5 or i^(1/2.) in the sum would have had the same final effect, and also made all intermediate calculations into floating-point numbers.

Symbolic Sums

Maple also has a facility for computing sums symbolically. Type in

> sum(i, i=1..n);

and you will get a familiar output… actually, you may recognize it better after calling factor(%); which is the command for factoring a polynomial.

Before proceeding further, it’s helpful to illustrate a common pitfall and how to get around it. On the next two lines, execute

> i := 4;          # define variable i
> sum(i, i=1..n);  # i is used now as an index

This will produce an error (read it). Maple gets confused about the global variable i versus the index i in the sum. Here are three ways to fix it:

  • write the next sum with j instead of i, although this is not really a sustainable solution
  • “undefine” i: this is done with the command > i := 'i';
  • undefine all variables: this is done with the command > restart;

After doing any of these things, sum(i, i=1..n); will work.

restart will wipe out Maple’s internal state (be careful!), but it won’t erase the lines that you have on the page. If you have a long worksheet that you are trying to use as a self-contained re-runnable demonstration, you could make the first line restart; to ensure it starts afresh every time you start re-executing lines from the beginning. (If you use Maple in this way you might also like to use Edit > Execute > Worksheet a.k.a. the !!! button, which will execute the whole worksheet from scratch starting from the first line and proceeding in order; and ctrl-J and ctrl-K which insert new spots for commands.)

Back to doing our sums. Sometimes a numerical answer is the best route (as with adding up square roots above). But sometimes a symbolic answer actually determines the same thing, but more efficiently. To demonstrate, we introduce the binomial(a, b); command which computes \(\binom{a}{b}\), and ask: what is the sum of \((-1)^i\binom{1000000}{i}\) when we take i ranging from 0 to 1000000? Computing this directly will probably cause your version of Maple to crash or become frozen. But the more general sum

> sum((-1)^i*binomial(n, i), i=0..n);

will give you 0: the answer does not even depend on n.

Exercise (nickname: formulae)

Answer one of the following two questions.

  1. An arithmetic-geometric series is one whose terms are \(i \cdot b^i \). Find the general formula (in terms of b and n) for its sum, where the lower bound on i is 1 and the upper bound is n.
  2. A casino offers a new game where you flip a coin n times, and then win \(t \times 2^t\) dollars where t is the number of heads. What is the expected value of the prize that you will win in this game? In other words, what is a fair price for them to charge if they want to break even in the long run? (The answer will be expressed in terms of n. Recall that the probability of getting t heads in n flips is \(2^{-n}\binom{n}{t}\)).

Substituting and Evaluating

We’ll be talking about how to define and plot functions in the next lesson. The essential idea in defining a function is expressed already in subs(«var»=«value», «expr»); which gives you the result of substituting a variable with a value in a given expression. For example, run

> xChooseTwo := binomial(x, 2);
> fiveChooseTwo := subs(x=5, xChooseTwo);

This will result in the output binomial(5, 2). Now compare: when you type in binomial(5, 2) directly Maple will evaluate it to 10, but in this case subs() did mere syntactic replacement. To find out what really is its value, type in

 > eval(fiveChooseTwo);

and you’ll get the actual value. You can get to the same result more quickly by doing a combined substitution-and-evaluation:

> eval(xChooseTwo, x=5);

Algebra

Maple has excellent facilities for symbolic manipulation. It is aware of a number of ways to manipulate expressions to either make them simpler or to expressly write them in a longer form for the purposes of extracting information.

The two functions expand(«expr») and simplify(«expr») do more or less what you would expect from their name. Use simplify when you want to ask Maple to try to express a result in a simpler form. For example,

> result := (x+y)^2/(x^2+2*x*y+y^2);
> simplify(result);

In particular, what is the difference between these two lines?

The expand() function tries to write everything as a longer combination of the most basic possible terms. Try

> (1+x)^5;
> expand(%);
> sin(2*x);
> expand(%);

and again, notice what the different outputs are.

Exercise

(Not to be handed in.) Using the above commands, get Maple to confirm that \(\sin^2(x)+\cos^2(x)=1\) and \((x-y)(x+y) = x^2-y^2\). Be careful with the syntax: Maple doesn’t necessarily treat all of its inputs the same way that a human would, so you need to be fairly explicit in how you use ^, *, and ().

A good question that you may have asked is, how can we turn \(x^2-y^2\) back in to its factored version? Use factor() again:

> factor(x^3-y^3)     # gives (x-y)*(x^2+x*y+y^2)

Contrast this with the function ifactor(), which takes an integer and writes it as a product of its prime factors.

Exercise (nickname: tutone)

What is the largest prime number whose square divides \(\binom{8675}{309}\)? Note: it is expected that you will use a bit of visual inspection in your solution.

Working With Algebraic Expressions

Since factor does complete factorization over the integers, you can use it to determine the roots to a polynomial when it factorizes into rational linear terms. But if the roots are irrational or imaginary, it won’t be able to help. Instead, you can use the solve() function, which finds all solutions to an equation (among other things). For example

> solve(x^2 = x + 1, x);

will give you the golden ratio and its conjugate. The first argument x^2 = x + 1 is what we want to solve, and the second argument x is what we want to “solve for.” To see how the second argument might be useful, try solving (a+b)^2 = cos(c) first for b, and then for c.

Exercise (nickname: expo)

Find an approximate decimal solution to \(\mathrm{e}^x = -1+\frac{1}{x}\). Caveat #1: use the exp() function to write the left-hand side, since otherwise Maple will think you’re talking about a symbolic variable named e. Caveat #2: use one of the methods introduced in the previous or current lesson to force a numerical calculation. (But it is also instructive to see what Maple thinks the exact solution should be.)

There are two other functions related to algebraic manipulation worth mentioning here:

  • collect(«expr», «var») writes the expression as a polynomial in the variable «var», grouping the terms according to the powers of «var»
  • coeff(«expr», «var», «power») extracts the coefficient of the «power»th power of «var»

Is Maple smart enough to rewrite  coeff((1+x)^n, x, m) as binomial(n, m)? Sadly, no. Nonetheless, these are quite useful functions. They also work on multivariate polynomials:

> coeff(expand((x+y+1)^20), x, 3);

will give you a polynomial in y that represents the sum of all terms in the expression containing \(x^3\).

Why did we write expand there? Try leaving it out and see what happens. In general: we strongly encourage you to try variants of commands that we give you, to see what happens. Experimentation is one of the most important ingredients in familiarizing yourself with a new software tool.

Note: Extracting coefficients is not the same as working with power series. We won’t cover this, but if you want to get a taste of how this is done in Maple, try running taylor(sin(x), x=0, 6) or looking up the powseries package.

; versus :

We’re starting to deal with pretty big expressions now. For example, the output of expand((x+y+1)^20) takes up more than a full screen on the typical computer. So writing

> F := expand((x+y+1)^20);

is pretty inconvenient. However, we can tell Maple to suppress its output (while still doing the exact same computation internally) by replacing ; with :. Run the following two lines

> F := expand((x+y+1)^20):  # colon -- hide the output
> coeff(F, x, 3);           # semicolon -- show the output

to see exactly how : compares to ;.

Exercise (nickname: dollar)

Consider the polynomial

\(\displaystyle\left(\sum_{i=0}^{100} (xy)^i\right) \left(\sum_{i=0}^{20} (x^5y)^i \right) \left(\sum_{i=0}^{10} (x^{10}y)^i \right) \left(\sum_{i=0}^4 (x^{25}y)^i \right).\)

Find the coefficient of \(x^{100}y^{19}\) in this expression by using coeff twice. (It represents the number of ways of making change for a dollar with exactly 19 coins.) As always you can introduce additional variables as needed to hold intermediate results from your individual steps. We’d prefer if you don’t try to do the whole exercise on one long line since this would probably be very hard for us to read.

At this point you know enough about writing Maple to understand the description of homework solution formatting, lesson M-H. Go check it out!

These are course notes for the University of Waterloo's course Math 600: Mathematical Software.
© 2012—. Written and developed by David Pritchard and Stephen Tosh. Contact (goes to the CEMC)