Programming

Maple has three major programming abilities. First off, it can export expressions for use in Fortran and C programs. It can also export expressions that can be used with the LaTeX and troff editors. Next, it can allow the user to read/write data, output, and worksheets. Lastly, it has a powerful internal programming language that can be used to create user specific functions, libraries and procedures.


Exporting Maple to Other Programs

Let us go ahead and define a fairly complicated expression:

> sample:=( Pi +
> ((x+9)/(sin(x) +y)) *
> ((3*cos(x+y)) / (sqrt(x+y) + y^(x-1))) -
> ((x+y)/Pi)^2);

                                                                      2
                                  (x + 9)cos(x + y)            (x + y)
        sample := Pi + 3------------------------------------ ---------
                                             1/2    (x - 1)         2
                         (sin(x) + y) ((x + y)   + y       )      Pi

We can invoke both the Fortran and C libraries with the readlib command:

> readlib (fortran);

proc(x) ... end

> readlib(C);

proc() ... end

And now we can produce code to calculate sample in both Fortran and C:

> fortran([a=sample],optimized);
      t6 = x+y
      t10 = y**(x-1)
      t15 = t6**2
      t16 = Pi**2
      a = 0.3141593E1+3*(x+9)/(sin(x)+y)*cos(t6)/(sqrt(t6)+t10)-t15/t16

> C([a=sample], optimized);
      t6 = x+y;
      t8 = sqrt(t6);
      t10 = pow(y,x-1.0);
      t15 = t6*t6;
      t16 = Pi*Pi;
      a = 0.3141592653589793E1+3.0*(x+9.0)/(sin(x)+y)*cos(t6)/(t8+t10)-t15/t16;

We can export the expression as a LaTeX expression and save it as the file latex.out:

> latex(sample, 'latex.out');
\pi +{\frac {\left (3\,x+27\right
)\cos(x+y)}{\left (\sin(x)+y\right )
\left (\sqrt {x+y}+{y}^{x-1}\right )}}-{\frac
{\left (x+y\right )^{2}}
{{\pi }^{2}}}

And we can produce the expression for use with troff:

> eqn(sample);

                                                                  2
                              (x + 9) cos(x +y)            (x + y)
          eqn(Pi + 3------------------------------------ ---------)
                                          1/2   (x - 1)         2
                     (sin(x) + y) ((x + y)   + y       )      Pi


Importing and Exporting Maple Files

So far, you have notices that all of the examples require you to type data and expressions directly into Maple. It would be nice if Maple could read and write data files. Well, it can. Take a simple unformatted file called datafile:

% cat datafile
18 74 56 75
9 8 45 23
90 28 28 9
84 37 58 49

Maple can read this data just fine:

> readlib(readdata);

proc(fname) ... end

> readdata(datafile, integer, 4);

     [[18, 74, 56, 75], [9, 8, 45, 23], [90,28, 28, 9], [84, 37, 58, 49]]

Now we can place this data into a matrix, A, and transpose it:

> A := convert(",matrix);

                                 [ 18  74  56 75 ]
                                 [               ]
                                 [  9   8  45 23 ]
                            A := [               ]
                                 [ 90  28  28  9 ]
                                 [               ]
                                 [ 84  37  58 49 ]

> At := linalg[transpose](A);

                                  [ 18   9  90 84 ]
                                  [               ]
                                  [ 74   8  28 37 ]
                            At := [               ]
                                  [ 56  45  28 58 ]
                                  [               ]
                                  [ 75  23   9 49 ]

We can now export this data as ASCII text:

> for i from 1 to 4 do
>    printf(`%d %d %d %d \n`, At[i,1],
At[i,2], At[i,3], At[i,4] );
> od;
18 9 90 84
74 8 28 37
56 45 28 58
75 23 9 49

Maple also has the ability to save and restore the entire session for later use. Maple files are binary with a .m

extension. The save, read, writeto, and appendto commands are used.

We can go ahead and start using Maple and establish our worksheet.

> a:=Pi/2;

                                  a := 1/2 Pi

> b:=9/5;

                                    b := 9/5

> c:=a/b;

                                  c := 5/18 Pi

When we are ready, just go ahead and save the worksheet. Note: The filename needs to be in the back-quote, not the apostrophe. The backquote is usually located below the tilde (~) on the keyboard.

save `maple_session.m`;

We can now quit Maple assured that the session is ready to start back up. Later, we can read the file:

read `maple_session.m`;

And everything is back to the way it was:

> evalf(c);

                                  .8726646262

The writeto and appendto commands are formatted the same as the save and read commands. Writeto directs all Maple output to a file instead of a terminal. If the file already exists, Maple will overwrite that file, essentially deleting all prior contents. Appendto is similar to writeto except that Maple will now keep all previous file information and append all new output to the end of the desired file.


The Maple Programming Language

Maple has its own internal programming language. It is not the intent of this page to fully cover this language but rather to let you, the user, know that it is available. For more information on programming within Maple please see the references.

MapleÕs main method of custom programming is in the form of the procedure. A procedure is a series of commands and expression. Let us define a simple procedure that takes the square of a number and divides it by 2:

> sample := proc(x) (x^2)/2 end;

sample := proc(x) 1/2*x^2 end

sample is now defined as a procedure. We can now use it anywhere we want:

> a:=sample;

                                  a := sample

> b:=(sample/2);

                                b := 1/2 sample

> a(9);

                                      81/2

> b(5);

                                      25/4

Maple allows very complex programs, libraries, and protocols to be edited and utilized. Loops, If/Thens, etc are all ready to be added.


This page Maintained by Dale H. Leschnitzer
Last Modified Monday, November 4, 1996

L O S   A L A M O S   N A T I O N A L   L A B O R A T O R Y
Operated by the University of California for the U.S. Department of Energy

Copyright © 1998 UC - Disclaimer