MathGroup Archive 1994

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

Search the Archive

Re: MathLink

Riccardo Rigon (rrigon at writes:

>Dear mathgrouper, I am moving my first steps into MathLink.
>Suppose I would calculate the covariance of two list with an external program:
>The following is the code I wrote:
>double mlvariance P(( ml_doublep,ml_doublep, long)); <---THIS SHOULD BE WRONG
>:Function:      mlcovariance
>:Pattern:       MLCovariance[ list:{___Real},list:{___Real} ]
>:Arguments:     { Reverse[list], Reverse[list] }
>:ArgumentTypes: { RealList,RealList }
>:ReturnType:    Real
>:Evaluate:      MLCovariance[ sequence___Real,sequence___Real]:=
>MLCovariance[{sequence},{sequence} ]
>#include "mathlink.h"
>double mlvariance(list1,list2, len)
>        ml_doublep list1,list2; long len;
>        double res = 0, ras=0, rus=0;
>        long lin=len;
>        while(len--) {res += list1[len];ras +=list2[len];
>        return rus/(lin-1)-(res/(lin-1))*(ras/lin);
>double main(argc, argv)
>        int argc; char* argv[];
>        return MLMain(argc, argv);
>I did not have find any reference to the first line of the template (.tm)
>file. Looking at the mathlink.h file I can see that P(s) is substituted at
>compiling time with s, but still I do not have clear what is the use of
>this first line and how I have to set it. Can anyone help?
>I think that the problem of passing and receiving matrixes and lists of
>real numbers to and from external programs is an issue of quite general
>interest and WRI should deserve to it the time to write some clear
>examples. The Mathlink
>Reference Guide in my own view is still too much criptic on the subject.

Whenever a template function is to receive a list via either the RealList
or IntegerList keywords, the function must be written to take a long
parameter immediately following each list. This long will be the length of
the list, which is determined for you by the C "wrapper" code that mprep
writes. Thus, the (ANSI-style) prototype for your function should be

double mlvariance(double *list1, long len1, double *list2, long len2);

The P() macro, defined in mathlink.h, is just a simple utility for writing
code that compiles under K&R C, but also gives the benefits of prototyping
under ANSI C. On non-ANSI compilers, P(x) preprocesses to just (), whereas
in ANSI compilers, it becomes x. Programmers should feel free to use it for
their function prototypes if they are interested in supporting both ANSI
and non-ANSI compilers.

There is a MathLink tutorial on MathSource (item number 0206-693) that
discusses many issues in MathLink programming in greater detail than the
MathLink Reference Guide, including how to receive and return lists. I
recommend that all MathLink programmers get it.

By the way, I notice a couple other problems with your template file. The
pattern MLCovariance[list:{___Real},list:{___Real}] will only match two
identical lists, because the same name is used for both patterns. I assume
this is not intended. A similar problem exists for
MLCovariance[sequence___Real,sequence___Real], a strange pattern indeed.

I hope this is helpful.

Todd Gayley

  • Prev by Date: FindMinimum
  • Next by Date: MMA to Illustrator
  • Previous by thread: MathLink
  • Next by thread: [no subject]