MAPLE Notes for MATH 40Michael Monagan
Department of Mathematics and Statistics
Simon Fraser University
August, 1998.
Updated August, 2002, 2008.restart;These notes are for Maple 9. They are platform independent, i.e., they are the same for the Macintosh, PC, and Unix versions of Maple. These notes should be backwards compatible with Maple 7, Maple 8, and forwards compatible with Maple 10 and Maple 11. Maple as a CalculatorInput of a numerical calculation uses +, -, *, /, and ^ for addition, subtraction, multiplication, division, and exponentiation respectively.1+2*3-2;2^3;120/105;Because the input involved integers, not decimal numbers, Maple calculates the exact fraction when there is a division, automatically cancelling out the greatest common divisor (GCD). In this case the GCD is 15, which you can calculate specifically asigcd(120,105);Observe that every command ends with a semicolon ; This is a gramatical requirement of Maple. If you forget, Maple will assume that the comand is not complete. This allows you to break long commands across a line. For example1+2*3/(2+3);We are not going to use decimal numbers in MATH 340 as all the calculations that we do will involve integers and polynomials with integer coefficients. However, here is how you would do some decimal calculations. The presence of a decimal point . in a number means that the number is a decimal number and Maple will, by default, do all calculations to 10 decimal places.120/105.0;sqrt(2.0);sqrt(2);Notice the difference caused by the presence of a decimal point in these examples. Now, if you have input an exact quantity, like the NiMtJSVzcXJ0RzYjIiIj above, and you now want to get a numerical value, use the evalf command to evaluate to floating point. Use the % character to refer to the previous Maple output. evalf(%);To input a formula, just use a symbol, e.g. NiMlInhH and the arithmetic operators and functions known to Maple. For example, here is a quartic polynomial in NiMlInhH .x^4-3*x+2;We are going to use this polynomial for a few calculations. We want to give it the name NiMlImZH so we can refer to it later. We do this using the assignment operation in Maple as followsf := x^4-3*x+2;The name NiMlImZH is now a variable. It refers to the polynomial. Here is it's valuef;To evaluate this as a function at the point NiMvJSJ4RyIiJA== use the eval command as followseval(f,x=3);The following commands differentiate f with respect to x and factor f into irreducible factors over the field of rational numbers and compute 10 digit numerical approximations to the real roots respectively.diff(f,x);factor(f);fsolve(f=0,x);You can graph functions using the plotting commands. The basic syntax for the plot command for a function of one variable is illustrated as follows: plot(f,x=0.5..1.5);In the graph I can see a local minimum near x=0.9. We can find this point using calculus. The command fsolve( f(x)=0, x ), on input of a polynomial f(x) computes 10 digit numerical approximations for the real roots of f(x).fsolve(diff(f,x)=0,x);We have used the name f as variable to refer to formulae and the symbols x for an uknown in a formula. Often you will have assigned to a name like we have done here to NiMlImZH but you want now to use the name NiMlImZH as a symbol again, not as a variable. You can unassign the value of a name as followsf;f := 'f';f;Let's use the sum command to find the sum of the first NiMlIm5H positive integers, namely,NiMvLSUkc3VtRzYkJSJrRy9GJzsiIiIlIm5HLCpGKkYqIiIjRiolJC4uLkdGKkYrRio=. The command sum( f(i), i=m..n ) computes the sum for us.f := sum( k, k=1..n );I don't recognize that answer. Do you? Let's try to simplify it.expand( f );factor( f );We will give a more detailed description of what the expand and factor commands do in the section on polynomials below.IntegersHere are some commands which do integer calculations that we will use in the course. Integers in Maple are not limited to the precision of the hardware on your computer. They are limited by an internal limit of Maple to approximately half a million digits. Any calculations that you do with large integers though will take longer for larger integers. Here is NiMqJCkiIiMiJCsiIiIi .2^100;The command irem(a,b) computes the integer remainder of a divided by b. The command iquo(a,b) computes the integer quotient of a divided by b. For examplea := 20;
b := 7;r := irem(a,b);q := iquo(a,b);It should be the case that a = b q + r . Let's checka = b*q + r;The commands igcd(a,b) and ilcm(a,b) compute the greatest common divisor and least common multiple of integers a and b, respectively. igcd(6,4);ilcm(6,4);The command igcdex(a,b,'s','t') outputs NiMvJSJnRy0lJEdDREc2JCUiYUclImJH . It also assigns NiQlInNHJSJ0Rw== integers satisfying the equation NiMvLCYqJiUic0ciIiIlImFHRidGJyomJSJ0R0YnJSJiR0YnRiclImdH and satisfying NiMyLSUkYWJzRzYjJSJzRy1GJTYjJSJiRw== and NiMyLSUkYWJzRzYjJSJ0Ry1GJTYjJSJhRw== . So this command implements the extended Euclidean algorithm. For exampleg := igcdex(3,5,'s','t');s;t;s*3+t*5;The command isprime(n) outputs false if n is composite and true if n is prime. The command ifactor(n) computes the integer factorization of an integer. For exampleisprime(997);isprime(1001);ifactor(1001);Now, if you are not sure what a command does, or how to use it, you can use Maple's on-line help system. You input ?command and then a return. Try the following?isprime?ifactorYou should get a window with some information about the command and how to use it. Almost all of the on-line help files have examples. If you don't know the name of the command, you can use the help browser to search for the command - see the Help menu at the top right of the window.For the RSA cryptosystem we need large primes, 100 digit primes. The command nextprime(n) outputs the first prime greater than or equal to n. Here is the first 100 digit prime.nextprime(10^100);LoopsTo do a sequence of calculations it will be handy to know how to use some of Maple's looping commands and also the if command. To execute a command in Maple conditionally use the if command which has either of the following forms
if <condition> then <statements> else <statements> fi
or just
if <condition> then <statements> fi
if 2>1 then print(good) else print(bad) fi;To execute one or more statements zero or more times in a loop use the for command. It has the following form
for <variable> from <start> to <end> do <statements> od
for i from 1 to 5 do i^2; od;To execute some statements while a condition is true use the while loop. It has the syntax
while <condition> do <statements> od
In the following example we repeatedly subtract b from a to compute the remainder.a := 26;
b := 10;
r := a;
while r >= b do r := r-b; od;
r;Here is a loop to calculate the GCD of two integers NiMlImFH and NiMlImJH using Euclid's algorithm. Notice that this loop has three statements in the body of the loop - between the do ... od, each of which is terminated by a semicolon. You don't have to put them on the same line as I have done here.a := 26;
b := 20;
while b <> 0 do
r := irem(a,b); a := b; b := r;
od;Thus 4 should be the GCD(64,20). A check with Mapleigcd(64,20);As another example of a loop, this time using subscripts and formatting print statements.
First we clear the value of r.r := 'r';r[0] := 142:
r[1] := 102:
i := 1:
while r[i] <> 0 do
r[i+1] := irem(r[i-1],r[i],'q');
i := i+1;
printf(" q[%d]=%d r[%d]=%d \134n", i, q, i, r[i] );
od:
g := r[i-1]:
printf(" Therefore the gcd(%d,%d) is %d \134n", r[0],r[1],g );Modular ArithmeticModular arithmetic is done using the mod operator in Maple. By default, Maple uses the positive range for the integers modulo NiMlIm1H, that is, the result is calculated in the range NiM7IiIhLCYlIm1HIiIiRichIiI=.
12 mod 7;2+3*3 mod 7;To compute NiMpJSJhRywkIiIiISIimod NiMlIm1H, you can do either of the following2^(-1) mod 7;1/2 mod 7;To compute NiMpJSJhRyUibkc= mod NiMlIm1H you can do either2 ^ 200 mod 7;2 &^ 200 mod 7;Use the latter. The difference is that in the first case, the integer NiMqJCkiIiMiJCsjIiIi was computed then reduced modulo NiMlIm1H . In the second case, all products were reduced modulo NiMlIm1H so no large integers occured.We will use a loop to verify that Fermat's (little) theorem holds for p = 13 but not for n = 14.p := 13;
for a from 0 to p-1 do (a^p mod p) = a od;n := 14;
for a from 0 to n-1 do (a^n mod p) = a od;Euler's NiMlJHBoaUc= function is available in the number theory packagewith(numtheory);phi(12);PolynomialsPolynomials in Maple are simply input as formulae using the arithmetic operators. For examplex^4-3*x^2+12;is a polynomial in one variable, NiMlInhH with integer coefficients. Here is a polynomial in two variables. a := (x-y)*(x^2-y^2)*(x^3-y^3);To multiply the factors of the polynomial out use the expand commandexpand(a);To factor the polynomial into prime factors with integer coefficients use the factor commandfactor(a);In MATH 340 we will only need polynomials in one variable and mostly work in the ring Zp[x] where NiMlInBH will be a prime integer. In what follows we show operations for Zp[x] and also Q[x]. For help for operations for polynomials see ?polynomial. For help for operations in Zp[x] see ?mod.
Here are two polynomialsa := 2*x^6-3*x^5+3*x+3;b := 3*x^4-4*x^3+1;The command eval( a(x), x=k ) evaluates the polynomial a(x) at x = k. The command Eval( a, x=k ) mod p does this modulo p . For exampleeval( a, x=2 );Eval( a, x=2 ) mod 7;Here is how we can tabulate the values of this polynomial for all values in NiMmJSJaRzYjIiIo. We conclude that a(x) has no roots. for k from 0 to 6 do Eval(a,x=k) mod 7 od;The command expand(a*b) multiplies out the product NiMqJiUiYUciIiIlImJHRiU=. The command Expand(a*b) mod p does the product modulo NiMlInBH, that is, all coefficients in the resulting polynomial are reduced modulo NiMlInBH . For examplep := 5;expand(a*b);Expand(a*b) mod p;The operations rem(a,b,x) and quo(a,b,x) compute, respectively, the remainder NiMlInJH and quotient NiMlInFH of NiMlImFHdivided by NiMlImJH satisfying NiMvJSJhRywmKiYlImJHIiIiJSJxR0YoRiglInJHRig= with NiMvJSJyRyIiIQ== or NiMyLSUkZGVnRzYjJSJyRy1GJTYjJSJiRw==. The corresponding operations for NiMmJSJaRzYjJSJwRw== are Rem(a,b,x) mod p and Quo(a,b,x) mod p . For exampler := rem(a,b,x);q := quo(a,b,x);expand( a = b*q+r );r := Rem(a,b,x) mod p;q := Quo(a,b,x) mod p;Expand( a = b*q+r ) mod p;The commands gcd(a,b) and lcm(a,b) compute, respectively the greatest common divisor and least common multiple of two polynomials. The corresponding operations for NiMmJSJaRzYjJSJwRw== are Gcd(a,b) mod p and Lcm(a,b) mod p. For examplegcd(x^4-2*x^2+2,x^4+1);Gcd(x^4-2*x^2+2,x^4+1) mod p;The command gcdex(a,b,x,'s','t') outputs NiMvJSJnRy0lJEdDREc2JCUiYUclImJH . It also outputs through the input parameters NiQlInNHJSJ0Rw== integers satisfying the equation NiMvLCYqJiUic0ciIiIlImFHRidGJyomJSJ0R0YnJSJiR0YnRiclImdH and satisfying NiMyLSUkZGVnRzYjJSJzRy1GJTYjJSJiRw== and NiMyLSUkZGVnRzYjJSJ0Ry1GJTYjJSJhRw== . The corresponding command for NiMmJSJaRzYjJSJwRw== is Gcdex(a,b,x,'s','t') mod p. For examplegcdex(a,b,x,'s','t');s;t;expand( s*a+t*b );Gcdex(a,b,x,'s','t') mod p;s;t;Expand(a*s+t*b) mod p;The command irreduc(a) outputs true if the polynomial NiMtJSJhRzYjJSJ4Rw== is irreducible and the command factor(a) outputs the factorization of NiMtJSJhRzYjJSJ4Rw== into irreducible factors over the integers. The corresponding commands for NiMmJSJaRzYjJSJwRw== are Irreduc(a) mod p and Factor(a) mod p. For examplefactor(a);factor(b);Factor(a) mod p;Factor(b) mod p;Notice that Maple does not factor the polynomial NiMsJiokKSUieEciIiMiIiJGKEYnISIi into the factors NiMqJiwmJSJ4RyIiIi0lJXNxcnRHNiMiIiMhIiJGJiwmRiVGJkYnRiZGJg==. The factorization is, by default, to factor into irreducible factors with integer coefficients.The Nextpoly(f,x) mod p command will compute the ``next'' polynomial in lexicographical order. We will use this and the Irreduc command to find the first irreducible cubic polynomial over the integers modulo 3.Nextpoly(x^2+1,x) mod 3;f := x^3;
while not Irreduc(f) mod 3 do f := Nextpoly(f,x) mod 3 od;Complex numbersThe complex unit NiMvJSJpRy0lJXNxcnRHNiMsJCIiIiEiIg== in Maple is displayed and input using the capital letter I.sqrt(-1);I^2;If you solve a polynomial equation using the solve command, Maple will try to return exact formulae for the roots. These can be complicated and in general, there are no formulae. You can use the fsolve command to get numerical approximations for the roots.eqn := x^4-x^3-7*x^2-12*x-6;solve(eqn,x);fsolve(eqn,x);fsolve(eqn,x,complex);The solve command first factors the polynomial and if there is a quadratic factor, it applies the quadratic formula. The fsolve command uses a Newton like iteration to compute the roots.factor(eqn);Arithmetic with complex numbers happens automatically. The commands Re(z) and Im(z) and abs(z) below compute the real part, imaginary part and modulus (magnitude) of z respectively.z := 2+3*I;z^2;1/z;Re(z);Im(z);abs(z);Another application of complex numbers is to computing the real and complex eigenvalues and eigenvectors of a real matrix. with(LinearAlgebra,Eigenvalues);A := Matrix([[0,1],[-1,0]]);Eigenvalues(A);Field ExtensionsThe command alias( alpha=RootOf( f(x) ) ); prepares Maple to treat NiMlJmFscGhhRw== in an appropriate extension field. In other words, it treats NiMlJmFscGhhRw== as the construction variable in F[NiMlJmFscGhhRw==]/(f(NiMlJmFscGhhRw==)) . For examplealias( alpha=RootOf(x^2-2) );What this means is that the relation NiMvLCYqJCklJmFscGhhRyIiIyIiIkYpRighIiIiIiE= holds. You can check that Maple knows this as followsa := alpha^2-2;simplify(alpha^2-2);A second example: obviously NiMqJCklJmFscGhhRyIiJCIiIg== = NiMlJmFscGhhRw==NiMqJCklJmFscGhhRyIiIyIiIg== = NiMqJiUmYWxwaGFHIiIiIiIjRiU= = NiMqJiIiIyIiIiUmYWxwaGFHRiU=.simplify(alpha^3);By default, Maple works over the field of rational numbers, i.e. it uses F = Q. For example, the polynomial NiMsJiokKSUieEciIiUiIiJGKCIiIyEiIg== does not factor over Q.a := x^4-2;factor( a );But if we are allowed to compute with NiMlJmFscGhhRw== then it does factor as followsfactor( a, alpha );This is the factorization of NiMsJiokKSUieEciIiUiIiJGKCIiIyEiIg== over Q[x]/(NiMsJiokKSUieEciIiMiIiJGKEYnISIi). You can check it in the usual way. Multiplying it out we getexpand(%);And since NiMvLCYqJCklJmFscGhhRyIiIyIiIkYpRighIiIiIiE= the above is indeed equal to NiMsJiokKSUieEciIiUiIiJGKCIiIyEiIg== .This second example is for an extension field where the base field F = NiMmJSJaRzYjIiIk.alias( beta=RootOf(x^2+1) );a := x^4+x^3+x+2;Factor( a ) mod 3;This is the factorization of NiMsKiokKSUieEciIiUiIiJGKCokKUYmIiIkRihGKEYmRigiIiNGKA== over the field NiMmJSJaRzYjIiIk .Factor(a,beta) mod 3;This is the factorization of NiMsKiokKSUieEciIiUiIiJGKCokKUYmIiIkRihGKEYmRigiIiNGKA== over the field NiMmJSJaRzYjIiIk[NiMlJWJldGFH]/(NiMsJiokKSUlYmV0YUciIiMiIiJGKEYoRig=).Let us set up the field NiMmJSJaRzYjIiIk[NiMlJmdhbW1hRw==]/(NiMsKCokKSUmZ2FtbWFHIiIkIiIiRigqJiIiI0YoRiZGKEYoRihGKA==).alias( gamma=RootOf(x^3+2*x+1) );To do arithmetic calculations in the field, for example to calculate NiMqJiIiIkYkJSZnYW1tYUchIiI= and NiMqJiwmIiIiRiUqJCklJmdhbW1hRyIiI0YlRiVGJSwmRiZGJUYoRiVGJQ== use the Normal command as follows Normal( 1/gamma ) mod 3;Normal( (1+gamma^2)*(gamma^2+gamma) ) mod 3;Here is a loop that calculates NiMpLCYqJiIiIyIiIiUmZ2FtbWFHRidGJ0YmRiclIm5H for n up to 15 , showing that NiMsJiomIiIjIiIiJSZnYW1tYUdGJkYmRiVGJg== is not a primitive element.for n from 0 to 15 do Normal((2*gamma+2)^n) mod 3; od;