2.13.4 Handling Lists, Arrays and Other Expressions
MathLink allows you to exchange data of any type with external programs. For more common types of data, you simply need to give appropriate :ArgumentTypes: or :ReturnType: specifications in your MathLink template file.
Basic type specifications.
Here is the MathLink template for a function that takes a list of integers as its argument.
:Begin:
:Function: h
:Pattern: h[a_List]
:Arguments: a
:ArgumentTypes: IntegerList
:ReturnType: Integer
:End:
Here is the C source code for the function. Note the extra argument alen which is used to pass the length of the list.
int h(int *a, long alen)
int i, tot=0;
for(i=0; i<alen; i++)
tot += a[i];
return tot;
This installs an external program containing the specifications for the function h.
In[1]:= Install["hprog"]
Out[1]=
This calls the external code.
In[2]:= h[{3, 5, 6}]
Out[2]=
This does not match the pattern h[a_List] so does not call the external code.
In[3]:= h[67]
Out[3]=
The pattern is matched, but the elements in the list are of the wrong type for the external code, so $Failed is returned.
In[4]:= h[{a, b, c}]
Out[4]=
You can mix basic types of arguments in any way you want. Whenever you use IntegerList or RealList, however, you have to include an extra argument in your C program to represent the length of the list.
Here is an :ArgumentTypes: specification.
:ArgumentTypes: IntegerList, RealList, Integer
Here is a possible corresponding C function declaration.
void f(int *a, long alen, double *b, long blen, int c)
Note that when a list is passed to a C program by MathLink its first element is assumed to be at position 0, as is standard in C, rather than at position 1, as is standard in Mathematica.
In addition, following C standards, character strings specified by String are passed as char * objects, terminated by \0 null bytes. Section 2.13.5 discusses how to handle special characters.
MathLink functions for sending data to Mathematica.
When you use a MathLink template file, what mprep and mcc actually do is to create a C program that includes explicit calls to MathLink library functions. If you want to understand how MathLink works, you can look at the source code of this program. Note when you use mcc, you typically need to give a g option, otherwise the source code that is generated is automatically deleted.
If your external function just returns a single integer or floatingpoint number, then you can specify this just by giving Integer or Real as the :ReturnType: in your MathLink template file. But because of the way memory allocation and deallocation work in C, you cannot directly give :ReturnType: specifications such as IntegerList or RealList. And instead, to return such structures, you must explicitly call MathLink library functions within your C program, and give Manual as the :ReturnType: specification.
Here is the MathLink template for a function that takes an integer as an argument, and returns its value using explicit MathLink functions.
:Begin:
:Function: bits
:Pattern: bits[i_Integer]
:Arguments: i
:ArgumentTypes: Integer
:ReturnType: Manual
:End:
The function is declared as void.
void bits(int i) {
int a[32], k;
This puts values into the C array a.
for(k=0; k<32; k++)
a[k] = i%2;
i >>= 1;
if (i==0) break;
if (k<32) k++;
This sends k elements of the array a back to Mathematica.
MLPutIntegerList(stdlink, a, k);
return ;
}
This installs the program containing the external function bits.
In[5]:= Install["bitsprog"]
Out[5]=
The external function now returns a list of bits.
In[6]:= bits[14]
Out[6]=
If you declare an array in C as int a[n1][n2][n3] then you can use MLPutIntegerArray() to send it to Mathematica as a depth 3 list.
...
Here is a declaration for a 3dimensional C array.
int a[8][16][100];
This sets up the array dims and initializes it to the dimensions of a.
long dims[] = 8, 16, 100;
...
This sends the 3dimensional array a to Mathematica, creating a depth 3 list.
MLPutIntegerArray(stdlink, a, dims, NULL, 3);
...
You can use MathLink functions to create absolutely any Mathematica expression. The basic idea is to call a sequence of MathLink functions that correspond directly to the FullForm representation of the Mathematica expression.
This sets up the Mathematica function Plus with 2 arguments.
MLPutFunction(stdlink, "Plus", 2);
This specifies that the first argument is the integer 77.
MLPutInteger(stdlink, 77);
And this specifies that the second argument is the symbol x.
MLPutSymbol(stdlink, "x");
In general, you first call MLPutFunction(), giving the head of the Mathematica function you want to create, and the number of arguments it has. Then you call other MathLink functions to fill in each of these arguments in turn. Section 2.1 discusses the general structure of Mathematica expressions and the notion of heads.
This creates a Mathematica list with 2 elements.
MLPutFunction(stdlink, "List", 2);
The first element of the list is a list of 10 integers from the C array r.
MLPutIntegerList(stdlink, r, 10);
The second element of the main list is itself a list with 2 elements.
MLPutFunction(stdlink, "List", 2);
The first element of this sublist is a floatingpoint number.
MLPutReal(stdlink, 4.5);
The second element is an integer.
MLPutInteger(stdlink, 11);
MLPutIntegerArray() and MLPutRealArray() allow you to send arrays which are laid out in memory in the onedimensional way that C preallocates them. But if you create arrays during the execution of a C program, it is more common to set them up as nested collections of pointers. You can send such arrays to Mathematica by using a sequence of MLPutFunction() calls, ending with an MLPutIntegerList() call.
...
This declares a to be a nested list of lists of lists of integers.
int ***a;
...
This creates a Mathematica list with n1 elements.
MLPutFunction(stdlink, "List", n1);
for (i=0; i<n1; i++) {
This creates a sublist with n2 elements.
MLPutFunction(stdlink, "List", n2);
for (j=0; j<n2; j++) {
This writes out lists of integers.
MLPutIntegerList(stdlink, a[i][j], n3);
}
}
...
It is important to realize that any expression you create using MathLink functions will be evaluated as soon as it is sent to Mathematica. This means, for example, that if you wanted to transpose an array that you were sending back to Mathematica, all you would need to do is to wrap a Transpose around the expression representing the array. You can then do this simply by calling MLPutFunction(stdlink, "Transpose", 1); just before you start creating the expression that represents the array.
The idea of postprocessing data that you send back to Mathematica has many uses. One example is as a way of sending lists whose length you do not know in advance.
This creates a list in Mathematica by explicitly appending successive elements.
In[7]:= t = {}; Do[t = Append[t, i^2], {i, 5}]; t
Out[7]=
This creates a list in which each successive element is in a nested sublist.
In[8]:= t = {}; Do[t = {t, i^2}, {i, 5}]; t
Out[8]=
Flatten flattens out the list.
In[9]:= Flatten[t]
Out[9]=
Sequence automatically flattens itself.
In[10]:= {Sequence[1, Sequence[4, Sequence[ ]]]}
Out[10]=
In order to call MLPutIntegerList(), you need to know the length of the list you want to send. But by creating a sequence of nested Sequence objects, you can avoid having to know the length of your whole list in advance.
This sets up the List around your result.
MLPutFunction(stdlink, "List", 1);
while( condition ) {
generate an element
Create the next level Sequence object.
MLPutFunction(stdlink, "Sequence", 2);
Put the element.
MLPutInteger(stdlink, i );
}
This closes off your last Sequence object.
MLPutFunction(stdlink, "Sequence", 0);
Basic functions for explicitly getting data from Mathematica.
Just as MathLink provides functions like MLPutInteger() to send data from an external program into Mathematica, so also MathLink provides functions like MLGetInteger() that allow you to get data from Mathematica into an external program.
The list that you give for :ArgumentTypes: in a MathLink template can end with Manual, indicating that after other arguments have been received, you will call MathLink functions to get additional expressions.
:Begin:
:Function: f
The function f in Mathematica takes 3 arguments.
:Pattern: f[i_Integer, x_Real, y_Real]
All these arguments are passed directly to the external program.
:Arguments: i, x, y
Only the first argument is sent directly to the external function.
:ArgumentTypes: Integer, Manual
:ReturnType: Real
:End:
The external function only takes one explicit argument.
double f(int i) {
This declares the variables x and y.
double x, y;
MLGetReal() explicitly gets data from the link.
MLGetReal(stdlink, &x);
MLGetReal(stdlink, &y);
return i+x+y;
}
MathLink functions such as MLGetInteger(link, pi) work much like standard C library functions such as fscanf(fp, "%d", pi). The first argument specifies the link from which to get data. The last argument gives the address at which the data that is obtained should be stored.
Getting a function via MathLink.
:Begin:
:Function: f
The function f in Mathematica takes a list of integers as an argument.
:Pattern: f[a:___Integer]
The list is passed directly to the external program.
:Arguments: a
The argument is to be retrieved manually by the external program.
:ArgumentTypes: Manual
:ReturnType: Integer
:End:
The external function takes no explicit arguments.
int f(void) {
This declares local variables.
long n, i;
int a[MAX];
This checks that the function being sent is a list, and stores how many elements it has in n.
MLCheckFunction(stdlink, "List", &n);
This gets each element in the list, storing it in a[i].
for (i=0; i<n; i++)
MLGetInteger(stdlink, a+i);
...
}
In simple cases, it is usually possible to ensure on the Mathematica side that the data you send to an external program has the structure that is expected. But in general the return value from MLCheckFunction() will be nonzero only if the data consists of a function with the name you specify.
Note that if you want to get a nested collection of lists or other objects, you can do this by making an appropriate sequence of calls to MLCheckFunction().
Getting lists of numbers.
When an external program gets data from Mathematica, it must set up a place to store the data. If the data consists of a single integer, as in MLGetInteger(stdlink, &n), then it suffices just to have declared this integer using int n.
But when the data consists of a list of integers of potentially any length, memory must be allocated to store this list at the time when the external program is actually called.
MLGetIntegerList(stdlink, &a, &n) will automatically do this allocation, setting a to be a pointer to the result. Note that memory allocated by functions like MLGetIntegerList() is always in a special reserved area, so you cannot modify or free it directly.
Here is an external program that will be sent a list of integers.
int f(void) {
This declares local variables. a is an array of integers.
long n;
int *a;
This gets a list of integers, making a be a pointer to the result.
MLGetIntegerList(stdlink, &a, &n);
...
This disowns the memory used to store the list of integers.
MLDisownIntegerList(stdlink, a, n);
...
}
If you use IntegerList as an :ArgumentTypes: specification, then MathLink will automatically disown the memory used for the list after your external function exits. But if you get a list of integers explicitly using MLGetIntegerList(), then you must not forget to disown the memory used to store the list after you have finished with it.
Getting arrays of numbers.
MLGetIntegerList() extracts a onedimensional array of integers from a single Mathematica list. MLGetIntegerArray() extracts an array of integers from a collection of lists or other Mathematica functions nested to any depth.
The name of the Mathematica function at level i in the structure is stored as a string in heads[i]. The size of the structure at level i is stored in dims[i], while the total depth is stored in d.
If you pass a list of complex numbers to your external program, then MLGetRealArray() will create a twodimensional array containing a sequence of pairs of real and imaginary parts. In this case, heads[0] will be "List" while heads[1] will be "Complex".
Note that you can conveniently exchange arbitraryprecision numbers with external programs by converting them to lists of digits in Mathematica using IntegerDigits and RealDigits.
Getting character strings and symbol names.
If you use String as an :ArgumentTypes: specification, then MathLink will automatically disown the memory that is used to store the string after your function exits. This means that if you want to continue to refer to the string, you must allocate memory for it, and explicitly copy each character in it.
If you get a string using MLGetString(), however, then MathLink will not automatically disown the memory used for the string when your function exits. As a result, you can continue referring to the string. When you no longer need the string, you must nevertheless explicitly call MLDisownString() in order to disown the memory associated with it.
Getting an arbitrary function.
If you know what function to expect in your external program, then it is usually simpler to call MLCheckFunction(). But if you do not know what function to expect, you have no choice but to call MLGetFunction(). If you do this, you need to be sure to call MLDisownSymbol() to disown the memory associated with the name of the function that is found by MLGetFunction().
