MuPAD - Dynamic Modules [1]

A first and very simple example

Andreas Sorgatz (andi@uni-paderborn.de) - 02. Dec. 1996

To introduce the usage and the creation of dynamic modules in MuPAD, a very simple example of the dynamic module demo which defines the function date() will be given.
[Usage] -- [Source] -- [Creation] -- [Availability]

How To Use Dynamic Modules

Figure 1 demonstrates the usage of the dynamic module demo, which contains a module function date(). First the module is loaded with loadmod(). After that the module function is called by using the prefix demo:: followed by the function name. The function date() returns the current date in a string, e.g. "Mon Dec 2 09:07:29 MET 1996".

Fig. 1: Using A Dynamic Module

 andi> mupad
                                                                  
    *----*    MuPAD 1.3  ---  Multi Processing Algebra Data Tool
   /|   /|
  *----* |    Copyright (c) 1992-96 by B. Fuchssteiner, Automath
  | *--|-*    University of Paderborn.  All rights reserved.
  |/   |/
  *----*      ------------- Developers Version -----------------

 >> loadmod("demo");                 # load the dynamic module #

                           demo

 >> demo::date();                    # call a module function  #

                 "Mon Dec  2 09:35:27 1996"

 >> string::subsop(%,0..2="Sun");    # Nice try ;-)            #

                 "Sun Dec  2 09:35:27 1996"

 >> unloadmod("demo"):               # unload its machine code #

 >> demo::date();                    # it's reloaded on demand #

                 "Mon Dec  2 09:36:57 1996"

 >> info(demo);                      # display information     #

 Module: 'demo' created on 02.Dec.96 by mmg R-1.3.0 
 A date function for MuPAD

 Interface:
 demo::date, demo::doc               # doc was created by mmg  #

 >> export(demo):                    # export local names      #

 >> date();                          # now it's a global name  #

                 "Mon Dec  2 09:40:10 1996"

 >> Date:=external("date","demo"):   # direct access           #
 >> Date();

                 "Mon Dec  2 09:42:27 1996"

The Source Code Of This Dynamic Module

Figure 2 shows the complete source code of the dynamic module demo.

Fig. 2: The Source Code Of The Dynamic Module demo

 // FILE : demo.C - a very simple dynamic modules
 // 
 MFUNC( date,                            // Define a module function 'date'
        MCnop )                          // No OPtion, default case
 {   
     time_t   clock;                     // Declare your C++ variables
     char    *cstrg;
 
     MFnargsCheck(0);                    // Do not allow any arguments
 
     time(&clock);                       // C++ code to get the current
     cstrg = ctime(&clock);              // date in form of a C string.
     cstrg[24] = '\0';                   // Remove the '\n'.
 
     MTcell mstrg = MFstring(cstrg);     // Convert into a MuPAD string
     MFreturn( mstrg );                  // Return it to MuPAD
 } MFEND

How To Create This Dynamic Module

To create a dynamic module the so-called module generator (mmg) is used (available with MuPAD). It is started with the name of the module source file to be processed as its argument. As a result the binary file 'demo.mdm will be placed in the current directory. Figure 3 demonstrates the usage of mmg. The option -v (verbose) tells the module generator to log all actions that are carried out. The option -V (Version) passes a string to the module generator which is used as version information (refer to figure 1).

Fig. 3: Creating The Dynamic Module dragon

 andi> mmg -v -V 'A date function for MuPAD' demo.C

 MMG -- MuPAD-Module-Generator -- V-1.3.0  Oct.96
 Mesg.: Scanning source file ... 
 Mesg.: 1 function(s) and 0 option(s) found in 17 lines
 Mesg.: Creating  extended module code ... 
 Mesg.: Compiling extended module code ... 

 CC -pic -G -DSOLARIS -c MMGdemo.C -o demo.o 
    -DPARI_C_PLUSPLUS -DLONG_IS_32BIT -DPOINTER_IS_32BIT 
    -I/wiwianka/user/andi/MuPAD/SOURCE/DEV/share/mmg/include/kernel 
    -I/wiwianka/user/andi/MuPAD/SOURCE/DEV/share/mmg/include/pari 
    -I/wiwianka/user/andi/MuPAD/SOURCE/DEV/share/mmg/include/mmt 
 
 Mesg.: Linking dynamic module ... 
 
 CC -pic -G -lC -o demo.mdm demo.o 
    -L/wiwianka/user/andi/MuPAD/SOURCE/DEV/solaris/lib 
 
 Mesg.: Ok

 andi> ls -l demo.*
 -rw-r--r--   1 andi     mupas         670 Dec  2 09:31 demo.C
 -rwxr-xr-x   1 andi     mupas       21692 Dec  2 09:32 demo.mdm*

Appendix

The module generator mmg is part of the MuPAD distribution. In addition, an ordinary C++ compiler like CC, cxx, xlC or the public domain GNU compiler g++ is needed to create dynamic modules.

For modules and module interfaces to usefull packages, please refer to the contrib directory of any official MuPAD ftp site.

Bibliography

[1] Integration of Magnum in MuPAD
[2] Using the NAGC Library in MuPAD
[3] How to create MuPAD scanners with (f)lex


Author: Andreas Sorgatz (andi@uni-paderborn.de)
Last update: 16. Dec. 1996