0

I've been developing a MathLink application with a function that accepts two lists, e.g.

:Pattern: g[zi_List, fi_List]

which I intended to pull in to the function manually. Both lists can be real or complex with the result being complex if either parameter is complex. Additionally, fi could be a list of square matrices, but zi is to remain a one dimensional list.

Within the MathLink C API, the most straightforward seeming function to use is MLGetReal64Array which can handle both real and complex data types as Complex shows up as the innermost Head of the array. And, once complexity is determined, the array can be cast to std::complex<double> or the C99 complex type, if appropriate. Now, MLGetReal64Array doesn't handle non-rectangular Lists, so each List element must have the dimensionality of the others and be of the same type: real of complex. Oddly, though, with a function that accepts a single List parameter, MLGetReal64Array returns a data structure that has a one element List as its outermost element, i.e. inputing h[ {1, 3, 5} ] returns List[List[1,3,5]] on the c-side of things.

It turns out that for a two list function, like g, a single call to MLGetReal64Array will return both parameters at once, i.e. g receives List[ zi, fi ]. Since I plan on preprocessing each list for uniformity of structure and element type, ensuring that both had the same element type wouldn't be a problem. But, I'd like for fi to be a list of matrices, and MLGetReal64Array causes a MLEGSQ: MLGet() called out of sequence error.

So, my questions are: can I use MLGetReal64Array to get both lists? how would I go about it? And, if I can't use MLGetReal64Array, what are my alternatives?

I'm thinking that if MLGetReal64Array is correct about the structure, I can pop the outer List off the link by using MLGetFunction which would then allow me to use MLGetReal64Array for each parameter. As of yet, I haven't tried it. But, in the meantime, I would appreciate any suggestions.

rcollyer
  • 10,475
  • 4
  • 48
  • 75

1 Answers1

1

I'd create separate functions for the different cases you have. It's much easier to handle this logic on the Mathematica side than figure out what you have coming over the link in C.

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37
  • That is a good point, but it doesn't answer the structural issue of how do I use MathLink to pull in a 1D array (`zi`) and a 3D array (`fi`)? – rcollyer Jul 13 '11 at 04:13
  • 1
    I believe your MLGetFunction() followed by MLGetReal64Array()x2 would be correct. – Joshua Martell Jul 13 '11 at 04:18
  • 1
    @rcollyer, I remember you mentioning you were playing around with MathLink. Have you taken a look at my conclusion from my post [MathLink error messages](http://stackoverflow.com/questions/6526848/mathematica-mathlink-error-messages/6543062#6543062). I used what Joshua suggested, except since you already know that you expect a list you should use `MLCheckFunction(stdlink, "List", &n)` where `n` the place where the number of objects in the list will be stored. Then call the MLGet functions to obtain your data. – jmlopez Jul 13 '11 at 05:33
  • @jmlopez, no I did not see that post. Looks interesting. Incidentally, Wolfram [recommends](http://reference.wolfram.com/mathematica/tutorial/MathLinkInterface3.html) using [`MLTestHead`](http://reference.wolfram.com/mathematica/ref/c/MLTestHead.html), instead of `MLCheckFunction`. – rcollyer Jul 13 '11 at 12:28
  • @rcollyer, I did not see that one before. Thanks, I'll be using that instead in the future. – jmlopez Jul 13 '11 at 14:06