# Short Introduction to Maple in the MLC Lab # Mth 341 Linear Algebra Spring 2000 # Bent Petersen # # The workstations in the MLC lab are PCs running Windows NT 4.0. In # order to use the machines you must have an ORST account. When you # logon to a machine in the MLC lab your ORST directory on the ORST # server will be visible as drive Z: This is where you should keep your # personal files. Then they will be available from any PC in the MLC lab # (and many other labs) when you login. # # Login # The machines in the lab are normally left on, but the monitors may be # turned off. If the monitor is off, then switch it on. Next press the # Ctrl-Alt-Delete keys simultaneously. You should get a login prompt. # Enter your ORST user name and press the Tab key (not the Enter key). # Then enter your ORST password and press the Enter key. # # Next you may see a message about a slow network connection. This # message is bogus, but it is accompanied by a question, "Do you want to # download your profile?" You should answer yes if you want your # customary layout of desktop icons, wallpaper, and so forth. Changes # made to your profile during the current session will be saved to # server when you logout. You should get in the habit of answering yes # to this question. # # Next you may see a question about a default Novell server. Just answer # "none" unless you have a reason to answer otherwise. This question # should never appear again. # # To start Maple (or Matlab, or Mathematica, ... ) select the Start # button (lower left corner of the screen), then Programs from the menu, # etc. If you don't know the appropriate steps here ask for help. # Writing all this out in detail produces an incredibly dull document. # # Logout # When you are done with your session, you should logout. To logout # select the Start button (this is a very strange Windows idiom), and # then select "Shut Down ..." and finally select "Logon as another # user." # # Another way to do it is to press Ctrl-Alt-Delete. A menu will appear. # Select logoff. That is the simplest way. # # Do not select Restart or Shutdown unless you have a reason to do so. # Do not turn off the PC. You may turn off the monitor if you wish. That # will save power and will reduce the load on the air-conditioner. # # Note - if you do not logout you leave your account open for the next # person to come along. That person will have access to your personal # files on the ORST server. Do not forget to logout! # # If you plan to leave the lab, even just for a few minutes, save your # work and logoff. When you return and logon, even to a different # machine, your work will be available. Do not select "Lock # Workstation." If you do, someone else wishing to use the workstation # may power-cycle it in order to gain access and some of your work may # be lost as a result. The same comments apply to relying on a password # protected screensaver. Don't do it. Save your work and logoff. You # have no claim on any workstation if you are not physically present. # # The Worksheet # When you are using Maple in a window environment it is possible to # move around on the worksheet by left-clicking the mouse. As a result, # commands may end up being executed in a nonlinear order. This can # cause some confusion, since there is no visual clue. One way to fix a # mess is to have Maple re-execute the whole worksheet (look on the Edit # menu). This works best if old expressions are cleaned up first, so it # is a good idea to start each worksheet with the command restart; # > restart; # # Note each Maple command must be terminated by a colon or a semicolon # (except help commands preceded by a question mark). You can spread the # command over several lines by postponing the terminating colon or # semicolon. You simply move to a new line by pressing Enter. Maple will # chatter at you when you move to a new line in this manner if the # previous command is unterminated. Ignore it, but keep in mind a # command will not be executed before it is properly terminated. # # You can also stack up several commands on one line by terminating them # individually with colons or semicolons. The effect of the colon is to # suppress output from the corresponding command, though the command is # still carried out. # # Maple has some built in constants # > Pi; evalf(Pi); I; I^2; Pi 3.141592654 I -1 # Note the upper case letters. If you enter pi you will just get the # Greek letter pi, not the real number pi. By the way, the evalf() # function takes a second parameter specifiying the precision in decimal # digits. # > evalf(Pi,60); 3.14159265358979323846264338327950288419716939937510582097494 # # You can also set the precision by assigning a value to Digits (the # default is 10). Maple usually does exact calculations, but when # floating point numbers are involved the Digits sets the precision. # Here's an amusing example # > Digits:=4: convert(evalf(Pi),`rational`); Digits:=10: 22/7 # # It is not obvious, but the conversion to a rational number makes use # of Digits. Thus we would not get the correct result if we simply # passes 4 as the second parameter in evalf() as you can see: # > convert(evalf(Pi,4),`rational`); 1571 ---- 500 # # Note by the way the Maple assignment operator is :=, be careful about # that! # # If you want a convenient rational number which yields pi to 100 # decimal places here it is # > Digits:=100: convert(evalf(Pi),`rational`); Digits:=10: 47319710263505107011489582824165518109876079459487 -------------------------------------------------- 15062331588226261899547243783212652098313206994173 # # You can never tell when you might need this result! # # Let's look a bit at symbolic manipulations now. Maple distinguishes # between functions and expressions. Here's one way to define a # function: # > f:=x->sin(3*x+x^2); 2 f := x -> sin(3 x + x ) # We can also define an expression: # > g:=sin(3*x+x^2); 2 g := sin(3 x + x ) # Important note: Both of the examples above assume that x has not # already been assigned a value. It needs to be an unassigned variable # (see below). # # That they are different we can see by trying to evaluate them: > f(1.0); g(1.0); -.7568024953 2 sin(3 x + x )(1.0) # # Note by using a decimal point we forced Maple to do a floating point # evaluation. We can also get the precise result of course # > f(1); sin(4) # # We can see very clearly that g is regarded as an expression involving # x. To evaluate it at say x=1 we may use the substitute command subs() # > subs(x=1,g); evalf(%,20); sin(4) -.75680249530792825137 # # This example also demonstrates the use of the percentage symbol to # represent the previously evaluated quantity. It is called the ditto # operator. Be very careful if you use the ditto operator on another # line - it always means the previously evaluted expression, which may # not be the previous expression that happens to be on your worksheet! # # An expression can also be evaluated by using the eval() function, but # do check help to make sure you don't have any surprises in more # complicated situations. The commands eval() and subs() work in quite # different ways. In the simple case that we illustrated here eval() is # actually the preferred command to use. # > eval(g,x=1); sin(4) # # We can convert an expression into a function by using the unapply() # command > h:=unapply(g,x); 2 h := x -> sin(3 x + x ) # # Some Maple commands work on expressions, some work on functions, and # some on both. For example, here are the derivatives of f and g. > D(f); diff(g,x); 2 x -> cos(3 x + x ) (3 + 2 x) 2 cos(3 x + x ) (3 + 2 x) # # In the case of the expression you have to specify the variable you are # differentiating with respect to. It should now be clear how to take # partial derivatives of expressions. # # As an analysist I have a strong preference for functions but an # algebraist may prefer expressions. Most of the time it does not matter # which you use, you can even use both simultaneously, as long as you # keep it clear in your mind. Whenever dealing with any symbolic # manipulation system of any kind keep your data types straight in your # mind! # # As a final general example let's bring back some fond memories from # calculus - the problem of integration. Here's on example to get you # started: # > Int(1/(1+x^6),x) = int(1/(1+x^6),x); / | 1 | ------ dx = 1/3 arctan(x) | 6 / 1 + x 2 - 1/12 sqrt(3) ln(-x + sqrt(3) x - 1) + 1/6 arctan(2 x - sqrt(3)) 2 + 1/12 sqrt(3) ln(x + sqrt(3) x + 1) + 1/6 arctan(2 x + sqrt(3)) # # Note the int() operator returns the integral, but the Int() operator # just returns a symbolic expression for the integral. This behavior of # the Int() function is known as postponed evaluation and is sometimes # useful. Other functions have unevaluated (or postponed) versions as # well. # # If you want a display like the one above but don't want to type the # integrand twice you can make use of the ditto operator and the fact # that you can assign any sequence of symbols in Maple: # > ex:=1/(1+x^6),x: Int(%) = int(%); / | 1 | ------ dx = 1/3 arctan(x) | 6 / 1 + x 2 - 1/12 sqrt(3) ln(-x + sqrt(3) x - 1) + 1/6 arctan(2 x - sqrt(3)) 2 + 1/12 sqrt(3) ln(x + sqrt(3) x + 1) + 1/6 arctan(2 x + sqrt(3)) # # You can achieve a similar result even more easily by using the value # function to evaluate an unevaluated expression: # > Int(1/(1+x^6),x): %=value(%); / | 1 | ------ dx = 1/3 arctan(x) | 6 / 1 + x 2 - 1/12 sqrt(3) ln(-x + sqrt(3) x - 1) + 1/6 arctan(2 x - sqrt(3)) 2 + 1/12 sqrt(3) ln(x + sqrt(3) x + 1) + 1/6 arctan(2 x + sqrt(3)) # # Note the use above of the colon to supress the output from the first # command, so it does not clutter up our display. # # One common error comes from trying to use a variable with an assigned # value as a dummy variable of integration (or in other contexts). # Here's an example: # > x:=3: int(x^4,x); Error, (in int) wrong number (or type) of arguments # # Obviously what's needed is some way to unassign a variable. Maple # provides a number of ways to do this. # > unassign('x'); int(x^4,x); 5 1/5 x # Note the use of the single quotes here. You can pass a number of # variables to unassign by separating them with commas, but each one # must be enclosed in single quotes. # # A simpler way to unassign one variable is to assign it its name # extracted by single quotes (this is a Maple idiom) # > x:=3; x:='x'; int(x^4,x); x := 3 x := x 5 1/5 x # Note we assigned x a value and then we unassigned it, after which we # could use it as an unassigned variable. This is quite convenient, but # sometimes the single quotes are hard to find on the keyboard and even # harder to see on the monitor. Thus, even though it is more typing you # may prefer to use the evaluate to a name function evaln() since it # does not require the pesky single quotes. # > x:=3; x:=evaln(x); int(x^4,x); x := 3 x := x 5 1/5 x # Before using the evaln() function in more complex situations, it would # be a good idea to check Maple's help page for it. You may get some # unexpected results! # # To do linear algebra we first need to load the linear algebra package. # > with(linalg): Warning, new definition for norm Warning, new definition for trace # # Note we terminate this command with a colon rather than a semicolon # since otherwise it produces quite a bit of output. Even with the # colon, it is not completely quiet. Fortunately you can ignore the # warnings. # # Matrices can be specified by giving a list of the individual rows as # lists (lots of brackets!), or by specifying the size, and then listing # all the entries row-wise: # > A:=matrix([[1,2,3,4],[0,-1,3,4],[2,-1,2,3]]); [1 2 3 4] [ ] A := [0 -1 3 4] [ ] [2 -1 2 3] > A:=matrix(3,4,[1,2,3,4,0,-1,3,4,2,-1,2,3]); [1 2 3 4] [ ] A := [0 -1 3 4] [ ] [2 -1 2 3] # # The rref() command produces the row-reduced-Gauss-Jordan-echelon form: # > R:=rref(A); [1 0 0 3/19] [ ] [ -1 ] [0 1 0 -- ] R := [ 19 ] [ ] [ 25 ] [0 0 1 -- ] [ 19 ] # # If you just want to solve a system of equations Ax=b you can do that. # Things work out best if you specify b as a vector (because to Maple # every matrix has 2 subscripts, even row and column matrices). Note a # vector is specified by giving a list of its entries and is displayed # horizontally by Maple. There is no notion of row or column vector. If # you need them they are 1 by N or N by 1 matrices. # > b:=vector([3,1,-4]); b := [3, 1, -4] > linsolve(A,b); [_t[1], 2/3 - 1/3 _t[1], 25/3 _t[1] + 55/3, - 19/3 _t[1] - 40/3] # # Note the solutions is returned as a vector containing a parameter. # Note Maple chose the first variable as the parameter, so we don't get # the solution in canonical form. Alternately we can row reduce the # augmented matrix and then read-off the solution with the canonical # parametrization. # > rref(augment(A,b)); [ -40] [1 0 0 3/19 ---] [ 19 ] [ ] [ -1 26 ] [0 1 0 -- -- ] [ 19 19 ] [ ] [ 25 15 ] [0 0 1 -- -- ] [ 19 19 ] # # Note you can use the augment() function to construct matrices as well: # > c1:=vector([1,2,3]); c2:=vector([0,1,4]); c3:=vector([1,3,7]); c1 := [1, 2, 3] c2 := [0, 1, 4] c3 := [1, 3, 7] # # Even though the vectors are displayed as rows they are augmented as # columns in the matrix. (To add rows to a matrix one uses the # stackmatrix() command.) # > C:=augment(c1,c2,c3); rank(C); rref(C); [1 0 1] [ ] C := [2 1 3] [ ] [3 4 7] 2 [1 0 1] [ ] [0 1 1] [ ] [0 0 0] # # Finally let's observe that symbolic solutions are also available if # you are sufficiently devious: # > b:=vector([b1,b2,b3]); Id:=diag(1,1,1); b := [b1, b2, b3] [1 0 0] [ ] Id := [0 1 0] [ ] [0 0 1] > X:=rref(augment(C,Id)); [1 0 1 0 4/5 -1/5] [ ] X := [0 1 1 0 -3/5 2/5 ] [ ] [0 0 0 1 -4/5 1/5 ] > R:=augment(col(X,1),col(X,2),col(X,3)); [1 0 1] [ ] R := [0 1 1] [ ] [0 0 0] > XX:=augment(col(X,4),col(X,5),col(X,6)); [0 4/5 -1/5] [ ] XX := [0 -3/5 2/5 ] [ ] [1 -4/5 1/5 ] > XXX:=evalm(XX &* b); XXX := [4/5 b2 - 1/5 b3, - 3/5 b2 + 2/5 b3, b1 - 4/5 b2 + 1/5 b3] > R1:=stackmatrix(row(R,1),row(R,2)); [1 0 1] R1 := [ ] [0 1 1] linsolve(R1,vector([XXX[1],XXX[2]])); [-_t[1] + 4/5 b2 - 1/5 b3, -_t[1] - 3/5 b2 + 2/5 b3, _t[1]] # # Of course the solution is only valid if the compatibility condition is # satisfied: # > XXX[3]=0; # Compatibilty condition b1 - 4/5 b2 + 1/5 b3 = 0 # # A few things to note here. The stackmatrix() command builds matrices # by stacking rows, the augment() command builds matrices by adding # columns, matrix multiplication is denoted by &*, and the evalm() # command force Maple to display a matrix. Note if you try the last # example above directly with the linsolve() command Maple will be # silent. This is Maple's way of indicating it could not find a solution # (very human). If you try row reducing the augmented matrix # augment(C,b) then Maple will return the incorrect answer (indicating # no solution for any b). Maple assumes that the symbolic expressions # that come up in the calculations are not 0 (unless identically 0) and # so gets an extraneous pivot in this example. Maple's behavior is # precisely what is wanted if you are working in the context of rational # functions, but not in our case. The method above is just one way # around this problem. Perhaps you can find a cleaner procedure. # # The multiplication operator in Maple is normally *. Maple has a # builtin symbolic simplification facility which works on symbols # without worrying too much about their nature. This facility assumes # that the multiplication denoted by * is commutative. Since matrix # multiplication in general is not commutative there is a potential # problem when simplifying matrix expressions. This is the reason that # &* is used for matrix multiplication. The simplify facility does not # assume commutativity for &*. # # That should give you enough to get started with Maple. Enjoy, but # expect to be frustrated once in a while. Maple is a powerful tool, but # it requires you to understand some of what is going on (and to keep # track of data types). # # There is more Maple and linear algebra material on the web page for # this class if you need more suggestions. Also Maple has a very good # built in help system. Use it - for example # > ?subs # # will bring up a window with helpful information concerning the subs() # function.