Re: MathLink
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg234] Re: [mg207] MathLink
- From: tgayley (Todd Gayley)
- Date: Sun, 27 Nov 1994 23:20:18 -0600
Riccardo Rigon (rrigon at zeus.tamu.edu) 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: > >-------covariance.tm------------------------------------------ >double mlvariance P(( ml_doublep,ml_doublep, long)); <---THIS SHOULD BE WRONG > >:Begin: >:Function: mlcovariance >:Pattern: MLCovariance[ list:{___Real},list:{___Real} ] >:Arguments: { Reverse[list], Reverse[list] } >:ArgumentTypes: { RealList,RealList } >:ReturnType: Real >:End: > >:Evaluate: MLCovariance[ sequence___Real,sequence___Real]:= >MLCovariance[{sequence},{sequence} ] > > > >------covariance.c-------------------------------------------- > >#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]; >rus+=list1[len]*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 WRI