Next: File Input/Output of Up: Programming in Maple Previous: Interfacing with other

Calling External Programs

Often one wants to get Maple to call an external program, which might have been written in C or Fortran. There are really two reasons we might want to do this. Obviously, if Maple cannot so something, and another program can, it may make sense to use the other program, instead of reimplementing an algorithm in Maple. The other reason is efficiency. Although Maple is efficient at symbolic computations, it is not generally efficient at machine precision numerical computations. E.g. you may want to use a Fortran library routine to compute numerical eigenvectors.

Communication of data in Maple must be done via files. The Maple program must write the data needed by the external program out to an input file. Maple can start running the external program with the system command. The external program must read the data from the input file and must write its results into an output file. After the program has finished executing, Maple reads the results from the output file back into Maple. A sketch of the Maple code to do this would be


interface(quiet=true);
writeto(input);
... # write any data into the file input
writeto(terminal);
interface(quiet=false);
system(...); # execute the external program
read output;
... # continue processing in Maple

Let us look at this more closely. The first command, interface(quiet=true); turns off all diagnostics from Maple, i.e. bytes used messages, warnings, etc. so that they will not be written into the input file. This is reset by interface(quiet=false); after the data has been written to the input file.

The next command writeto(input); opens the file named input for writing. All Maple output from this point will go into this file. Output will typically be created by the lprint command. The command writeto overwrites the file. If you want to just append some data to what is already in the file, use the command appendto instead of writeto.

After the data has been written to the file, the file is closed implicitly by resetting output back to the terminal with the command writeto(terminal);.

The system command is used to execute the external program. The exact call that the system command makes will depend on the system. Under Unix, the call might look something like


system(`foo < in > out`);

The external program foo is executed on the input file in and its results are written to the file out. Note that the system command returns a value indicating whether the external program terminated normally. Under Unix, it returns 0 for normal termination.

Finally, we read the results in the out file back into Maple using the read command and continue execution in Maple.

In this mode, Maple calls the external program like a subroutine. After reading the results back into Maple, Maple continues execution. This is actually a very simple means for calling an external program, and easy to debug. You can check the files to make sure that they have the data in the format expected. However, the disadvantage is that communicating data via a file of text is not the most efficient approach. In many applications this inefficiency will not matter if the work done by the external program is significant.


bondaren@thsun1.jinr.ru