\magnification = \magstep1
\centerline{MATH 392 -- Seminar in Computational Commutative Algebra}
\centerline{First Computer Laboratory Exercises}
\centerline{February 4, 2019}
\bigskip
\noindent
{\it Background and Goals}
\bigskip
A large part of our work this summer will be carrying out calculations
of Gr\"obner bases and other computations within the computer algebra
system called {\it Maple\/}.  This week, we will want to get familiar
doing mathematics in this context.
\bigskip
\noindent
{\it Note:}  Several other mathematical software packages, including the freeware 
Sage and Macaulay 2 can also be used for these, although the
syntax of commands is different and Macaulay 2 has no 
builtin graphics.  If you want to get copies of either of these
for your own use, let me know and I can help you with installation,
how those programs work, etc.  

\bigskip
\noindent
{\it General Information about Maple}
\bigskip
\noindent
{\it Launching Maple}
\bigskip
\noindent
To get into Maple, you will need to:
\bigskip
\item{1)}  Log onto the campus network with your username and password.
\item{2)}  You should now see the lab desktop with ``icons''
(technically, ``shortcuts'' in Windows) for Maple, etc.   
You will double click on the Maple icon.
\item{3)}  From the Maple opening screen, select {\it New Worksheet} to 
produce a file that behaves as described below.  (The {\it New Document}
produces a similar workspace, but without the input prompts, and set 
up rather differently.)
\item{4)}  After a few more seconds you should see a new 
Maple window, with a ``subwindow'' marked
Untitled (1) opened inside it.  The Untitled (1) window is a 
blank {\it Maple worksheet\/}.  
\bigskip
\noindent
{\it Maple Worksheets}
\bigskip
Worksheets are integrated text/graphics/mathematics
documents where any or all of the following can be done: 
\medskip
\itemitem{a)} you can type in commands from the keyboard  to ask Maple to 
perform many different kinds of calculations, read data
from external files, store work in files, etc.
\itemitem{b)}  output generated by Maple from your input
commands (numerical values,
symbolic formulas, and graphics) will be displayed, 
\itemitem{c)} you can modify commands, generating new output, store
your results for use later, etc.    
\itemitem{d)} you can enter text to annotate and explain the 
results of computations.
\medskip
\noindent
Take a few seconds and notice the 
features of this window -- especially
the File, Edit, View, Insert, etc. pull-down menus across the top,
the ``tool bar'' below them with the icons for various operations
(can you guess what each of them does?)
the ``scroll bar'' on the right that you can use to move around 
within the worksheet, to see previous input and output lines, etc. 
Most of the common operations appear both as items in one of the 
pull-down menus and as toolbar buttons.  The toolbar buttons are
faster to use, but somewhat limited in the options they offer, so 
you will want to have a general idea what is accessible from 
where in the pull-down menus.  Take a few minutes and look
these over.

\bigskip
\noindent
{\it Input and Text Regions in a Worksheet\/}
\bigskip
Now look at the Untitled (1) window itself.  On the first
line of the worksheet, you should see a $[>$ in red.  This 
is the Maple {\it input prompt} -- the signal that Maple
is ready to accept a command from you and try to execute it.
A new input prompt will be generated automatically in a worksheet each time 
you enter a command and execute it {\it at the end of a worksheet in 
progress\/}. You can also go back and insert new commands and output 
at any point in a worksheet as follows.  You can {\it insert an input 
prompt\/} and an input region at any point in the worksheet by  
placing the cursor at the desired location and
pressing the toolbar button marked with $[>$.  (After executing
an inserted command like this, Maple will drop to the 
next input line, so to insert several input lines in the
middle of a worksheet, you will need repeat the above.) 
  
As we mentioned before, Maple worksheets can contain text
as well as commands and output.  To create a text region you can 
press the $[>$ button above, then the
button marked by a capital T (this changes the region into 
a text region).   Any text can now be entered, and this will
be treated as an ``inert'' comment.  That is, it will appear as
you enter it when you print out the worksheet, and it will
not be treated as Maple input. 


\bigskip
\noindent
{\it Saving and Reloading your Maple Worksheets}
\bigskip
When you begin working on a
worksheet, you will usually want to {\it save your work\/} every
once in a while in case
a computer problem develops, or in case
you need more than one lab session to complete the work you are
doing.  This can be done most directly by saving to your network
drive following these directions:
\item{2)} Select the SAVE option from the FILE
pull-down menu, or click the toolbar button between the open folder and the 
printer icons.  (The button shows one of the ancient 3 1/2 ``floppy'' removable
storage diskettes -- none of you probably ever used one of those since they became 
obsolete around 2000!)
\item{3)} If you are saving your work for the first time, you 
will see the SAVE AS dialog box.  Highlight your network drive, then go to the File 
Name box, and type in a name for the file where the worksheet will be 
stored (it's safest to avoid using spaces in filenames!).  Then click the 
OK button to write the worksheet to the network drive.
(You will only need
to type the filename in once in a session.  Subsequent 
saves just update the file.)
\item{4)}  When you have the worksheet saved as you want it,
you can exit Maple.  
\item{5)}  To update the worksheet further in a later session,
you can read the worksheet back into Maple using the OPEN
option from the FILE Menu, or the ``open folder'' toolbar button.  Get into Maple 
as above, 
and select OPEN from the FILE Menu.  Maple will prompt you as above for the
name of the worksheet with a dialog box very like the SAVE AS
box described above.  Highlight the appropriate network drive, then 
the worksheet you want, and click OK.  IMPORTANT NOTE FOR FUTURE REFERENCE:  When
you read a previously created worksheet into a new Maple session, you 
{\it have not\/} actually executed any of the commands in it in the 
new session.  If you want to make use of any results in that
worksheet, you will need to execute the commands again.  In the 
EDIT pull-down menu, you will see an EXECUTE option that lets you 
recompute an entire worksheet or a section of one.
\bigskip
\noindent
\bigskip
\noindent
{\it Printing}
\bigskip
OPEN the worksheet
you want to print, and press the toolbar icon that looks
like a printer, and press OK on the PRINT dialog box (the settings
should be set up correctly to print automatically).  Your 
output should appear shortly on the printer. 
\bigskip
\noindent
{\it Getting Out}
\bigskip
When you leave, quit the Maple window (FILE/EXIT) or the X button on the top
of the window, and log off the campus network.
\bigskip
\noindent
{\it A First Sample Maple Session}
\bigskip
Let's get right down to work and walk through a first sample
sample session!  First, you 
will need to get into Windows and Maple as described in the General 
Information section above.  

\bigskip
\noindent
{\it 2D Plotting}
\bigskip
The basic Maple command for 2D plotting, used for 
graphs of the form $y = f(x)$ and for parametric curves
$\alpha(t) = (x(t),y(t))$ is called {\tt plot}.  The basic format for graphs 
$y = f(x)$ is 
\medskip
\centerline{\tt plot(function,range,options);}
\medskip
\noindent
You fill in the information as follows to generate the plot you 
want:
\item{1)}  {\tt function} is the function to be plotted -- the
simplest way to specify one is via a formula for $f(x)$ (an 
{\it expression\/} in Maple)
\item{2)} {\tt range} is the range of $x$-values you want
to see plotted, entered in the form {\tt x = low..high}, and
\item{3)}  {\tt options} can be used to control the form
of the plot if desired.  No options need be specified however
if you don't want to. More on this later.
\bigskip
\noindent
The basic format for parametric curves $\alpha(t)=(x(t),y(t))$ is
\medskip
\centerline{\tt plot([x-comp,y-comp,t-range],options);}
\medskip
\noindent
where
\medskip
\item{1)}  {\tt x-comp} and {\tt y-comp} are the $x$- and $y$-component
functions of the curve to be plotted (each of these can be an 
{\it expression\/} involving the variable (parameter) $t$,
\item{2)} {\tt t-range} is the range of $t$-values you want
to see plotted, and
\item{3)}  {\tt options} can be used to control the form
of the plot if desired.  No options need be specified however
if you don't want to. 
\bigskip
\noindent
Note that the $x$- and $y$-components and the $t$-range all 
go inside a set of square brackets $[,]$, separated by commas.
This is also Maple's notation for a {\it vector, or ordered list\/}.

\bigskip
\noindent
{\it Example 1}
\bigskip
Enter commands like this to plot functions of $x$:
\medskip 
\centerline{\tt plot(3*sin(x) - 4*cos(x),x = -2*Pi..2*Pi);}
\medskip
\noindent
Next, let's see the parametric 
curve $\alpha(t) = (t^2 - 1, t^3 - t)$ from class.  Using the {\tt plot}
command, as described above, display a plot of this curve for
$t \in [-3,3]$, using 
\medskip
\centerline{\tt plot([t\^{}2 - 1,t\^{}3 - t,t = -3..3]);}
\bigskip
\noindent
{\it What is a Maple Command?}
\bigskip
It may help to 
think of one as a {\it program} or {\it function of one or more variables} 
that Maple knows how to compute, given ``inputs'' from you, although
you must then keep straight the distinction between these functions
Maple knows how to execute, and the functions or formulas you 
enter for Maple to work with!

Now that we have seen some first examples of Maple commands,
here is some more information about the {\it syntax rules\/} that
Maple uses to decide if what you have typed in is a well-formed
command it can execute, and what the {\it semantics\/} or
meaning of the commands are.

Almost all of the built-in Maple commands have a syntax
like the {\tt plot} command discussed above.  The format is either
\medskip
\centerline{{\tt name(values);}\qquad or\qquad {\tt name(values):}}
\medskip
\noindent
In this general description, {\tt name} is the name of the command or function 
(like ``plot'' yesterday), there is a matching set of open and close 
parentheses following the name, and they surround the 
{\tt values} -- the input from you the command needs to do its job 
(like the formula of the function to be plotted and the range of $x$-values
you want).  The
values are listed separated by commas within the parentheses.  
The two forms of the command above tell Maple to execute the command
when you press ENTER on that input line.  In the first case -- ending 
the command with a semicolon (;) -- you are instructing Maple to 
display the output.  With colon (:), Maple will
execute the command but not display the output (this is useful sometimes for 
intermediate steps in a big computation where you don't want or don't need
to see the output).  

One frequent source of errors in Maple sessions is unmatched
parentheses, (or square or curly brackets, etc.)  One ``hacker's trick''
is to count the open parens, count the close parens and make sure
you get the same number (of course, if you don't, you also have
to decide where to insert the missing one ...).
\bigskip
\noindent
{\it Getting Help On-Line}
\bigskip
The exact format Maple is expecting for each type of command is 
specified in the programming of the Maple system.  Usually, there
is {\it little or no freedom\/} in the ordering of what has to go where 
and in what format
in the list of values.  Much useful information on this for 
all the built-in commands, and LOTS of instructive examples are contained 
in the Maple on-line HELP facility.  For example try looking at the on-line help 
page for the {\tt plot command}.  (From the HELP pull-down menu, 
select Topic Search, type in the word ``plot,'' and press OK.)  Note
the example plot commands at the bottom and the links to related help pages.
\bigskip
\noindent
{\it Maple Expressions}
\bigskip
Formulas or expressions are entered in something like usual 
mathematical notation: 
\medskip
\item{1)} The symbols for addition, subtraction, 
multiplication, and division are $+,-,*,/$ respectively.  
\item{2)} The caret (\^{}) is the Maple symbol for raising to 
a power.
\item{3)} The asterisk symbol
for multiplication MUST be included whenever you are 
performing a product in a formula.  (It shows up as the ``dot for multiplication'' on 
the screen.) Moreover, everything must be 
entered in one linear string of characters, so you will need to use 
{\it parentheses\/} to group terms to get the expressions you want.  
The rule to keep in mind is: {\it Maple always evaluates expressions 
by doing powers first, then products and quotients, then sums and
products, left to right, unless parentheses are used to
override these built-in rules\/}.  For example, 
the Maple expression {\tt a + b\^{}2/c + d} is the same as the 
mathematical formula:
$$a + {b^2 \over c} + d.$$
If you really wanted 
$${a + b^2 \over c + d}$$
you will need to enter the expression
{\tt (a + b\^{}2)/(c + d)}.  What if you really wanted:
$${(a + b)^2 \over c + d}?$$
\item{4)} Maple ``knows'' all the usual elementary functions
from calculus.  The names of the most common ones are
{\tt sin, cos, tan, exp, ln, sqrt}.  To use one of these functions in 
a Maple formula, you put the name, followed by the ``argument'' (that 
is, the constant or expression you are applying the function to) 
{\it in parentheses\/}.
\bigskip
\noindent
{\it Example 2}
\bigskip
How would you enter each of the following formulas as Maple expressions?
Try typing each in as a Maple input line, and compare with the output they 
generate:
\medskip
\item{1)} $ 4xy^2 - 2x^3 + 3y - 7$
\item{}
\item{2)}  $1 + {1\over x^2} - {x+2\over x^4}$
\item{}
\item{3)}  ${-b + \sqrt{b^2 - 4 a c} \over 2 a}$
\bigskip
\noindent
{\it Implicit 2D Plotting}
\bigskip
The 2D plots we generated yesterday were all either graphs of functions 
$y = f(x)$,
or parametric curves.  In addition to these, we will also want to be able to 
plot the affine variety 
$${\bf V}(g(x,y)) = \{(x,y) \in {\bf R}^2 : g(x,y) = 0\}$$
for a general polynomial $g(x,y)$ in two variables.  The technical name for 
this in Maple is {\it implicit curve plotting}, since the equation $g(x,y) = 0$
defines one or more functions $y = f(x)$ ``implicitly.''  To plot 
these curves in Maple, we need a new command called {\tt implicitplot}.
This command is not part of the basic Maple system; instead it is a part of 
a separate {\it package\/} of commands for various plotting operations called
the {\tt plots} package.  Before you can use this command, you will need to 
``load'' the plots package with the command
\medskip
\centerline{\tt with(plots);}
\medskip
\noindent
(The output is the list of commands in the package; you should see {\tt implicitplot}
about halfway along the list.  If you don't want to see this each time, you 
can also put a colon after the {\tt with} command as described above.)

The format for the {\tt implicitplot} command is 
\medskip
\centerline{\tt implicitplot(expression,x-range,y-range);}
\medskip
\noindent
The expression should be the equation $g(x,y)$ (Maple assumes
you mean to plot $g(x,y) = 0$). This command generates
a plot of the part of the variety ${\bf V}(g(x,y))$ in the rectangular box 
in the plane defined by the $x$- and $y$-ranges.

\bigskip
\noindent
{\it Example 3}
\bigskip
\item{a)} For instance, try the following command:
\medskip
\centerline{\tt implicitplot(x\^{}3 - 3*x*y\^{}2 - 3,x=-3..3,y=-3..3);}
\medskip
\noindent
Try adding a text region answering the following question:  How many different 
``implicit functions'' $y = f(x)$ are defined by the equation 
$x^3 - 3xy^2 - 3 = 0$?  Also practice plotting these varieties:
\item{b)}  ${\bf V}(y^2 - x^3 + 9x)$
\item{c)}  ${\bf V}(x^3-3x + 2xy - y^4 - 1)$

\bigskip
\noindent
{\it 3D Curve Plotting}
\bigskip
The {\tt spacecurve} command in the {\tt plots}
package can be used to draw  parametric curves in 3D. The format for 
the command is
\medskip
\centerline{\tt spacecurve([x(t),y(t),z(t),t=a..b]);}
\medskip
\noindent
where $x = x(t)$,$y = y(t)$,$z = z(t)$ are the parametric equations of 
the curve and the range of parameter values to be plotted is 
$a \le t \le b$.  (Note the placement of the square brackets which follows
the same pattern as the parametric version of {\tt plot}!) 
For instance, try entering
\medskip
\centerline{\tt spacecurve([t,t\^{}2,t\^{}3,t=-2..2]);}
\medskip
\noindent
to plot the {\it twisted cubic\/} curve from class.

The first plot you see here might be rather uninformative. Fortunately, Maple
also lets you add axes and look at a 3D plot from different viewpoints. 
Here's how:
\bigskip
\item{1)}  Click the left mouse button once over the graph.  This 
should bring up a ``bounding box'' for the graph with corners you 
can {\it drag and drop\/} with the mouse to resize the graph. 
\item{2)}  If you place the mouse over the graph now and hold the left
button down, when you move the cursor, you should see a box rotating
following the cursor position.  Release the left mouse button 
when you get to the position you want.
\item{3)}  If you click  
the right mouse button, you will bring up a menu containing the options
{\tt Redraw, Copy, Style, Axes, Color} and several others.  Select
{\tt Axes, Normal} with the left mouse button. Then repeat clicking the 
right mouse button and select {\tt Redraw}.
\bigskip
\noindent
Practice a few times repositioning the viewpoint (rotating 
the viewing box) and redrawing the graph.  

To draw several parametric curves in the same graph, you can include
several curve specifications in the form {\tt [x(t),y(t),z(t),t=a..b]} 
between curly braces {\tt $\{$,$\}$}, separated by commas inside the 
parentheses for the {\tt spacecurve\/} command.

\bigskip
\noindent
{\it Surface Plots}
\bigskip
\noindent
{\it a. Plotting a graph} $z = f(x,y)$.  
\bigskip
The  command for 
3D plotting is called {\tt plot3d} (naturally enough!)  Its format
is similar to, but not exactly the same as the format of {\tt plot}.
To draw a graph with {\tt plot3d\/}, you use a command of the format:  
\medskip
\centerline{\tt plot3d(function,xrange,yrange,options);}
\medskip\noindent
The {\tt function} is the function $f(x,y)$, entered
in the usual Maple syntax for expressions. The {\tt xrange}
and {\tt yrange} specify a rectangular box in the plane that the 
plot will be constructed over; the {\tt options} can be
used to specify how the plot is drawn.  Look at the online help
for {\tt plot3d[options]} if you want to see what things are 
possible.  Most of the options can also be invoked from the 
3D graphics output via the pop-up menu, or the 
toolbar buttons.  Using the PATCHCONTOUR
style is often a very effective way to visualize the shape of a surface.

For example, try
\medskip
\centerline{\tt plot3d(-x\^{}4+2*x\^{}2-x/2-y\^{}2-1,x=-2..2,y=-2..2);}
\medskip
\noindent
to plot the surface
$$z = -x^4 + 2x^2 - x/2 - y^2 - 1.$$

One very nice feature of Maple 3D graphics is that you can change viewpoint,
add axes, etc. using the mouse and the 
toolbar options in the 3D graphics window.  The method is the same as
that for the {\tt spacecurve} described above.

\bigskip
\noindent
{\it Example 4}
\bigskip
We will build up a ``composite'' Maple graphics
display by combining a plot of a graph $z = f(x,y)$ produced
with {\tt plot3d} and a plot of a parametric curve produced
using {\tt spacecurve}.  This can be done via the {\tt display3d} command
in the {\tt plots} package.  First
we generate the plot of $z = x^2 - y^2$ for $(x,y) \in [-3,3]\times [-3,3]$,
and assign the output to the variable {\tt P1}.  
\medskip
\centerline{\tt P1:=plot3d(x\^{}2-y\^{}2,x=-3..3,y=-3..3):}
\medskip
\noindent
(The colon at the end of
the input line {\it suppresses the output\/}; if you want to see the 
output replace the colon with a semicolon.)  
Then, we generate a plot of the parametric curve 
$\alpha(t) = (t,t^2,t^3)$ for $t \in [-2,2]$, drawn with a relatively 
thick line and in black for contrast, and put the 
results into the variable {\tt P2}:
\medskip
\centerline{\tt P2:=spacecurve([t,t\^{}2,t\^{}3,t=-2..2],color=black,thickness=2):}
\medskip
\noindent
Finally, we display both graphs together:
\medskip
\centerline{\tt display3d($\{$P1,P2$\}$);}
\medskip
\noindent
(Here of course, you {\it do\/} want to see the output, presumably.)  Maple
is smart enough about these plots to combine the scales correctly 
and show the curve and surface in their proper relative positions!  
Look at this one for a while until you see how the curve and the 
surface are intersecting.  How many intersection points are there?

\bigskip
\noindent
{\it Exercises}
\bigskip
Do the following exercises in a new worksheet (i.e. separate from 
your work on the examples above), print and submit them with Problem Set 2, 
due Friday, February 8.
\bigskip
\noindent
{\it Exercise 1}
\bigskip
The line segment from $(a,b,c)$ to $(d,e,f)$ can be parametrized as follows
$$\alpha(t) = (a + t(d-a), b + t(e-b), c + t(f - c))$$
for $0 \le t \le 1$.
The points $P = (0,0,0)$, $Q = (2,0,0)$, $R = (0,\sqrt{3},1)$, 
$S = (0,-1,\sqrt{3})$
are four corners of a cube in ${\bf R}^3$
with edges $PQ$, $PR$, and $PS$, because
the vectors $\vec{u} = Q-P, \vec{v} = R-P, \vec{w} = S-P$
all have magnitude 2, and
$\vec{u}\cdot\vec{v} = \vec{u}\cdot\vec{w} = \vec{v}\cdot\vec{w} = 0$.
Your assignment, should you decide to accept it (just kidding!), is to 
create a picture of this cube by drawing the 12 edges together on the 
same set of axes in ${\bf R}^3$ (begin by drawing the line segments
from $P$ to $Q$, $P$ to $R$, and $P$ to $S$).   Also explain how you found 
the other 4 corners of the cube.  This can be done in a text region.

Any number of different 3D plots can be displayed together 
by assigning each to a variable, then putting the set or list
of plot variables names in the {\tt display3d} command.
\bigskip
\noindent
{\it Exercise 2 -- Implicit Surface Plots}
\bigskip
To plot an surface defined as a variety ${\bf V}(g(x,y,z))$. 
you can use the {\tt implicitplot3d} command.  Look up 
the on-line help for this command and use it in the following
exercise.  Generate a plot of the following variety:
$$S = {\bf V}(z^2 - (16 - x^2 - y^2)((x+2)^2 + y^2 - 1)((x-2)^2 + y^2 - 1)/10)$$
and explain the shape you see.  You will want to ``walk around'' this one 
a lot by rotating and looking at it from different viewpoints (For example, which $(x,y)$ correspond
to points on $S$ and why?  You will also need to think about
how you choose the $x$-, $y$-, and $z$-ranges.)  Suggestion:
Use the options {\tt style=PATCHCONTOUR} and {\tt grid=[30,30,30]}
to see detail.  Be aware though that this larger than normal numbers
of grid points will mean the surface takes longer to compute and ``render''
or draw.
\bye
