MAPLE Notes for Algebraic GeometryMichael Monagan
Department of Mathematics
Simon Fraser University
August, 1998.
Updated August 2002, September 2004, January 2009, January 2014.restart;These notes are for Maple V Release 10. 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 9 and forwards compatible with Maple 11.
<Text-field style="Heading 1" layout="Heading 1">Maple as a Calculator</Text-field>Input 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);For decimal numbers, 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);s := 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 LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkmbXNxcnRHRiQ2Iy1JI21uR0YkNiRRIjJGJy8lLG1hdGh2YXJpYW50R1Enbm9ybWFsRic= above, and you now want to get a numerical value to 3 decimal digits, use the evalf command to evaluate to floating point. Use the % character to refer to the previous Maple output. evalf(s,3);To input a formula, just use a symbol, e.g. LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEieEYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJw== and the arithmetic operators and functions known to Maple. For example, here is a quartic polynomial in LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEieEYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJw== .x^4-3*x+2;We are going to use this polynomial for a few calculations. We want to give it the name LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEiZkYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJw== 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 LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEiZkYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJw== is now a variable. It refers to the polynomial. Here is it's valuef;To evaluate this as a function at the point LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYlLUkjbWlHRiQ2I1EhRictRiM2JS1GLDYlUSJ4RicvJSdpdGFsaWNHUSV0cnVlRicvJSxtYXRodmFyaWFudEdRJ2l0YWxpY0YnLUkjbW9HRiQ2LVEiPUYnL0Y4USdub3JtYWxGJy8lJmZlbmNlR1EmZmFsc2VGJy8lKnNlcGFyYXRvckdGQi8lKXN0cmV0Y2h5R0ZCLyUqc3ltbWV0cmljR0ZCLyUobGFyZ2VvcEdGQi8lLm1vdmFibGVsaW1pdHNHRkIvJSdhY2NlbnRHRkIvJSdsc3BhY2VHUSwwLjI3Nzc3NzhlbUYnLyUncnNwYWNlR0ZRLUkjbW5HRiQ2JFEiMkYnRj5GKw== use the eval command as followseval(f,x=2);The following commands differentiate f with respect to x and factor f into irreducible factors over the field of rational numbers.diff(f,x);factor(f);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. To graph f(x) on the range LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYoLUkjbWlHRiQ2JVEiYUYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy1JI21vR0YkNi1RJiZsZXE7RicvRjNRJ25vcm1hbEYnLyUmZmVuY2VHUSZmYWxzZUYnLyUqc2VwYXJhdG9yR0Y9LyUpc3RyZXRjaHlHRj0vJSpzeW1tZXRyaWNHRj0vJShsYXJnZW9wR0Y9LyUubW92YWJsZWxpbWl0c0dGPS8lJ2FjY2VudEdGPS8lJ2xzcGFjZUdRLDAuMjc3Nzc3OGVtRicvJSdyc3BhY2VHRkwtRiw2JVEieEYnRi9GMkY1LUYsNiVRImJGJ0YvRjJGOQ== we use LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYoLUkjbWlHRiQ2JVEieEYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy1JI21vR0YkNi1RIj1GJy9GM1Enbm9ybWFsRicvJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHRj0vJSlzdHJldGNoeUdGPS8lKnN5bW1ldHJpY0dGPS8lKGxhcmdlb3BHRj0vJS5tb3ZhYmxlbGltaXRzR0Y9LyUnYWNjZW50R0Y9LyUnbHNwYWNlR1EsMC4yNzc3Nzc4ZW1GJy8lJ3JzcGFjZUdGTC1GLDYlUSJhRidGL0YyLUY2Ni1RIy4uRidGOUY7Rj5GQEZCRkRGRkZIL0ZLUSwwLjIyMjIyMjJlbUYnL0ZOUSYwLjBlbUYnLUYsNiVRImJGJ0YvRjJGOQ== . 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).diff(f,x)=0;fsolve(diff(f,x)=0,x);Here are some other polynomials and functionsf := x^2-3*x*y+y^2+x*y;factor(f);f := (1-t^2)/(1+t^2);limit(f,t=infinity);f := 2*t*sin(omega*t);diff(f,t);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 LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEiZkYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJw== but you want now to use the name LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYjLUkjbWlHRiQ2JVEiZkYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJw== as a symbol again, not as a variable. You can unassign the value of a name as followsf;f := 'f';f;Finally, the complex unit in Maple is I, not i, and the value 3.14159... is Pi not pi.
The symbols i and pi do not have any special meaning - so you can use them as variables.solve(x^2+1=0,x);I^2;i^2;sin(Pi);sin(pi);
<Text-field style="Heading 1" layout="Heading 1">Varieties in R^2 and R^3</Text-field>Equations are input using = .eqn := x^2+y^2=4;lhs(eqn);rhs(eqn);lhs(eqn)-rhs(eqn) = 0;Here is a linear system in R^2, it's solution and a graph of it. S := { x+y=1, x-y=0 };solve(S,{x,y});plots[implicitplot](S, x=-2..2, y=-2..2 );The following shows that solve does not return solutions as radicals automatically - one has to set a magic varaible (_EnvExplicit) to get radical solutions.S := { x^2+y^2=1, x+y=0 };solve(S,{x,y});_EnvExplicit := true;
solve(S,{x,y});The default for the grid option is [25,25]. The higher the values, the more accurate (less jagged) the plot will be. See ?plots[implicitplot]plots[implicitplot]( S, x=-2..2, y=-2..2, thickness=2, grid=[50,50] );Everything can be done in 3 dimensions. With surfaces. Note, in Maple 11, there is a new command intersectplot that shows the intersection of two surfaces in R^3.S := {x^2+y^2+z^2=1, y-z^2};solve(S,{x,y,z});plots[implicitplot3d]( S, x=-2..2, y=-2..2, z=-2..2, grid=[25,25,25], axes=frame, style=patchcontour);
<Text-field style="Heading 1" layout="Heading 1">Lists and Sets</Text-field>The simplest data structure in Maple is a list. The elements of a list may be of any type. To create a list of values enclose them in square brackets [, ]. Lists may be nested of course and the entries may be of any type.restart;
E := []; # the empty listL := [1,2,-3,4,1];M := [[1,2,3],[x,y,z]];To count the number of entries in a list use nops(L) command.nops(L);nops(M);To access the i'th element of a list (counting from 1) use a subscript. A negative subscript counts from the end.L[3];L[-1];M[2];M[2][2];Use the following to extract a sublistL[2..3];L[2..-1];To append (prepend) elements to a list use the following.op(L);[op(L),5];To test if an element is in a list usemember(2,L);Although you can assign to an entry of a list (as if it were an array) if the list has less than 100 elements, do not do this. It creates a copy of the entire list. So it's not efficient. Use Arrays .L[2] := 10;L;Maple sets are like mathematics sets, duplicates are removed. To create a set use squigley brackets { and } .
The examples should be self explanatory.{}; # The empty setS := {1,5,3,1};
T := {3,4,5,6};S[2];nops(S); # cardinaltiyS union T;S intersect T;S union {7};S minus T;member(5,S); # set membership
<Text-field style="Heading 1" layout="Heading 1">The seq, add and mul commands.</Text-field>The seq( f(i), i=a..b ), mul( f(i), i=a..b ), add( f(i), i=a..b ) useful for working with sets, lists and polynomials.restart;
seq( i, i=1..4 );seq( f(i), i=1..4 );creates a sequence of values which you can put in a list or set, e.g, L := [ seq(x[i], i=1..4) ];The add and mul commands work the same way except they create a sum (product) respectively.L := [x,y,z];add( L[i], i=1..3 );mul( L[i], i=1..3 );f := x^3-3*x^2+5;degree(f,x);coeff(f,x,2);[seq( coeff(f,x,i), i=0..degree(f,x) )];C := [1,0,3,5];add( C[i]*x^(i-1), i=1..nops(C) );Read the help pages?seq?add
<Text-field style="Heading 1" layout="Heading 1">Loops and If statements.</Text-field>restart;To 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 4 do i^2; od;If a loop index starts from 1 then you can omit the "from 1" clause. So the above loop may be writtenfor i to 4 do i^2; od;As an example, the Maple gcd command computes the gcd of two polynomials.
To compute the gcd of a list of 1 or more polynomials we can use a loop as follows.L := [x^6-1,x^4-1,x^12-1,x^3-1];g := L[1];
for i from 2 to nops(L) do g := gcd(g,L[i]) 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 divide an integer n by 2 until it is odd.n := 12;
while irem(n,2) = 0 do n := iquo(n,2); od;The value of n at the end of the loop isn;
<Text-field style="Heading 1" layout="Heading 1">Maple Programming</Text-field>A simple function, like the function f(x) = x^2 may be input using the arrow notation in Maple, as followsf := x -> x^2;f(2);
f(y);A procedure in Maple takes the form
proc( p1, p2, ... )
local l1, l2, ... ;
global g, g2, ... ;
statement1;
statement2;
....
statementn;
end procThere may be zero or more parameters, one or more locals, one or more globals and one or more statements in the procedure body.
The local and global statements are optional. Variables in the procedure body that are not explicitly declared as parameters, locals, or globals are declared to be local automatically if assigned to, otherwise they are global. The value returned by the procedure is the value of statementn, the last statement in the body of the procedure or the value of an explicit return statement. Type declarations for parameters and local variables need not be explicitly given. Some examples will help. f := proc(x)
y := x^2;
y-1;
end proc;f(2);Here is a Maple procedure to compute the LT(f), the leading term of a univariate polynomial f(x). It illustrates the use of the return statement. This time I've explicitly declared c and d to be local variables.LT := proc(f,x) local d,c;
if f=0 then return 0; fi;
d := degree(f,x);
c := coeff(f,x,d);
c*x^d;
end proc;LT(0,x);f := 3*x^3-5*x+1;LT(f,x);This next example is an implementation of the Euclidean algorithm in Q[x]. It uses the rem command to compute the remainder of f divided by g. It also use multi-assignments.(f,g) := (x^6-1,x^4-1);f;g;rem(f,g,x);EuclideanAlgorithm := proc(f,g,x) local c,d,r;
(c,d) := (f,g);
while d <> 0 do
r := rem(c,d,x);
(c,d) := (d,r);
od;
c;
end proc;EuclideanAlgorithm(f,g,x);Procedures may be nested, nested lexical scoping is used (a la Pascal).
Procedures may be returned and passed freely as parameters.
The simplest debugging tool is to insert print statements in the procedure.
For exampleEuclideanAlgorithm := proc(f,g,x) local c,d,r;
(c,d) := (f,g);
while d <> 0 do
r := rem(c,d,x);
print(REM(c,d) = r);
(c,d) := (d,r);
od;
c;
end proc;g := EuclideanAlgorithm(f,g,x);The next simplest debugging tool is the trace command. All assigment statements are displayed.trace(EuclideanAlgorithm);g := EuclideanAlgorithm(f,g,x);The printf command can be used to print more detailed information in a controlled format. It works just like the printf command in the C language. The main difference is the %a option for printing algebraic objects like polynomials. E.g.f := x^3-2*x^2+1;printf( "The polynomial f=%a has degree %d in %a\134n", f, 3, x );As a final example, here is a Maple procedure for the division algorithm which uses the LT procedure.DIVIDE := proc(f,g,x) # divide f by g
local q,r,t;
if g=0 then error "division by zero"; fi;
q := 0;
r := f;
while r <> 0 and degree(r) >= degree(g) do
t := LT(r,x)/LT(g,x);
q := q+t;
r := r-expand(t*g);
od;
return(q,r); # return both values
end:f := 2*x^4-3*x^3+1;
g := 2*x^2+1;q,r := DIVIDE(f,g,x):q;r;Check that the result is correct.expand( f-q*g-r );
<Text-field style="Heading 1" layout="Heading 1">Subscripted Names and Arrays</Text-field>Variables may be subscripted. For example, here is a polynomial in LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYnLUklbXN1YkdGJDYlLUkjbWlHRiQ2JVEieEYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy1GIzYjLUkjbW5HRiQ2JFEiMUYnL0Y2USdub3JtYWxGJy8lL3N1YnNjcmlwdHNoaWZ0R1EiMEYnLUkjbW9HRiQ2LVEiLEYnRj4vJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHRjQvJSlzdHJldGNoeUdGSS8lKnN5bW1ldHJpY0dGSS8lKGxhcmdlb3BHRkkvJS5tb3ZhYmxlbGltaXRzR0ZJLyUnYWNjZW50R0ZJLyUnbHNwYWNlR1EmMC4wZW1GJy8lJ3JzcGFjZUdRLDAuMzMzMzMzM2VtRictRiw2JUYuLUYjNiMtRjs2JFEiMkYnRj5GQEZDLUYsNiVGLi1GIzYjLUY7NiRRIjNGJ0Y+RkA=. You can assign to the subscripts.restart;
f := 1-x[1]*x[2]*x[3];x[1] := 3;f;There may be more than one subscript and the subscripts may be any value.
Arrays are like arrays from computing science. Here is how to create a one-dimensional array A with values indexed from 1 to 5.A := Array(1..5);By default, the entries in the array A are initialized to 0. You access and assign array entries using subscripts. For exampleA[1];A[1] := 3;A[1];for i from 2 to 5 do A[i] := x^i od;A;Arrays can be multidimensional. Here is a two dimensional Array with 2 rows and 3 columnsA := Array(1..2,1..3);for i to 3 do A[1,i] := i; od;A;numelems(A);op(2,A);