MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

ANNOUNCE: Math::ematica 1.105 - Perl talks to Mathematica talks to Perl

  • To:
  • Subject: [mg11028] ANNOUNCE: Math::ematica 1.105 - Perl talks to Mathematica talks to Perl
  • From: Ulrich Pfeifer <>
  • Date: Mon, 16 Feb 1998 18:15:32 -0500
  • Organization: University of Dortmund, Germany
  • References: <68n2lt$>

New in 1.105: 

        Calling Perl from a running Mathematica session (calling back
        from a spawned sub-kernel was possible bevore).

Ulrich Pfeifer
    Math::ematica - Perl extension for connecting Mathematica(TM)

      use Math::ematica qw(:PACKET :TYPE :FUNC);

    This is alpha software. User visible changes can happen any time.

    The module is completely rewritten. Literally no line of the old
    is used (don't ask - I've learned a few things since these days ;-).
    you are using the old 1.006 version, note that the interface has
    changed. If there is an overwhelming outcry, I will provide some
    backward compatibility stuff.

    Feel free to suggest modifications and/or extensions. I don not use
    Mathematica for real work right now and may fail to foresee the most
    urgent needs. Even if you think that the interface is great, you are
    invited to complete the documentation (and fix grammos and typos).
    I am no native English speaker, I will delay the writing of real
    documentation until the API has stabilized.

    I do develop this module using Mathematica 3.0.1 on a Linux 2.0.30
    Let me know, if it does work with other versions of Mathematica or
    not work on other *nix flavors.

    The `Math::ematica' module provides an interface to the MathLink(TM)
    library. Functions are not exported and should be called as methods.
    Therefore the Perl names have the 'ML' prefix stripped. Since Perl
    handle multiple return values, methods fetching elements from the
    return the values instead of passing results in reference

    The representation of the data passed between Perl and Mathematica
    straight forward exept the symbols which are represented as blessed
    scalars in Perl.

Exported constants
         The `PACKET' tag identifies constants used as packet types.

           print "Got result packet" if $link->NextPacket == RETURNPKT;

         The `TYPE' tag identifies constants used as elements types.

           print "Got a symbol" if $link->GetNext == MLTKSYM;

Exported functions
         The `FUNC' tag currently only contains the `symbol' function
         returns the symbol for a given name.

           $sym = symbol 'Sin';

The plain interface
    This set of methods gives you direct access to the MathLink
    Don't despair if you don't know them too much. There is a convenient
    layer ontop of them ;-). Methods below are only commented if they do
    behave different than the corresponding C functions. Look in your
    MathLink manual for details.


    The constructor is just a wrapper around `MLOpenArgv'.

      $ml = new Math::ematica '-linklaunch', '-linkname', 'math

    The link is automatically activated on creation and will be closed


      print $link->ErrorMessage;















    The method does the appropriate `MLDisownString' call for you.


    The module does the appropriate `MLDisownSymbol' call for you. It
    blesses the result string into the package `Math::ematica::symbol'.


    Returns the function name and argument count in list context. In
    contex only the function name is returned.


    Returns the array of reals.

The convenience interface

    Puts a single token according to the passed data type.

      $link->PutToken(1);               # MLPutInteger

    Symbols are translated to `MLPutFunction' if the arity is provided
    aditional parameter.

      $link->PutToken(symbol 'Pi');     # MLPutSymbol
      $link->PutToken(symbol 'Sin', 1); # MLPutFunction


    Reads the current packet and returns it as nested data structure.
    implementaion is not complete. But any packet made up of `MLTKREAL',
    `MLTKINT', `MLTKSTR', `MLTKSYM', and `MLTKFUNC' should translate
    correctely. A function symbol `List' is dropped automatically. So
    Mathematica expression `List[1,2,3]' translates to the Perl

    *Mabybe this is *too* convenient?*.


    Call is the main convenience interface. You will be able to do most
    not all using this call.

    Note that the syntax is nearly the same as you are used to as
    in Mathematica. Only the function names are moved inside the
    and separated with ',' from the arguments. The method returns the
    data structures read by `read_packet'.

      $link->call([symbol 'Sin', 3.14159265358979/2]); # returns
something near 1

    To get a table of values use:

      $link->call([symbol 'Table',
                   [symbol 'Sin', symbol 'x'],
                   [symbol 'List', symbol 'x',  0, 1, 0.1]]);

    This returns a reference to an array of doubles.

    You may omit the first `symbol'. *Maybe we should choose the default
    mapping to *Symbol* an require *Strings*s to be marked?*


    If you find this too ugly, you may `install' Mathematica functions
    Perl functions using the `install' method.


      Sin(Divide(Pi(),2.0)) # should return 1 (on machines which can
                            # represent '2.0' *exactely* in a double ;-)

    The `install' method takes the name of the mathematica function, the
    number of arguments and optional the name of the Perl function as


    Make shure that you do not call any *installed* function after the
    `$link' has gone. Wild things will happen!


    Is the sending part of `call'. It translates the expressions passed
to a
    Mathematica package and puts it on the link.


    This method allows to register your Perl functions to Mathematica.
    *Registered* functions may be called during calculations.

      sub addtwo {

      $link->register('AddTwo', \&addtwo, 'Integer', 'Integer');
      $link->call([symbol 'AddTwo',12, 3]) # returns 15

    You may register functions with unspecified argument types using

      sub do_print {
        print @_;
      $link->register('DoPrint', undef);


    This method allows to have Perl scripts installed in a running
    Mathematica session. The Perl script might look like this:

      use Math::ematica;
      sub addtwo {
        my ($x, $y) = @_;
        $x + $y;
      $ml->register('AddTwo', \&addtwo, 'Integer', 'Integer');
    Inside the Mathematica do:


    Admittedly, adding two numbers would be easier inside Mathematica.
    how about DNS lookups querying or SQL Databases?

    Ulrich Pfeifer <>

    See also the perl(1) manpage and your Mathematica and MathLink
    documentation. Also check the t/*.t files in the distribution.

    I wish to thank Jon Orwant of *The Perl Journal*, Nancy Blachman
    *The Mathematica Journal* and Brett H. Barnhart from *Wolfram

    Jon brought the earlier versions of this module to the attention of
    Nancy Blachman. She in turn did contact Brett H. Barnhart who was so
    kind to provide a trial license which made this work possible.

    So subscribe to *The Perl Journal* and *The Mathematica Journal* if
    are not subscribed already if you use this module (a Mathematica
    is needed anyway). You would be nice to nice people and may even
    something more about this module one day ;-)

    Special thanks to Randal L. Schwartz for naming this module.

    The Math:ematica module is Copyright (c) 1996,1997,1998 Ulrich
    Germany. All rights reserved.

    You may distribute under the terms of either the GNU General Public
    License or the Artistic License, as specified in the Perl README

    Mathematica and MathLink are registered trademarks of Wolfram

  • Prev by Date: Re: list combinations
  • Next by Date: Re: saving styles from notebooks: help, please!
  • Prev by thread: "Abelson & Sussman" meets Mathematica?
  • Next by thread: Re: ANNOUNCE: Math::ematica 1.105 - Perl talks to Mathematica talks to Perl