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.

1 Answer 1

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.

5
  • 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, 2011 at 4:13
  • 1
    I believe your MLGetFunction() followed by MLGetReal64Array()x2 would be correct. Jul 13, 2011 at 4: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. 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, 2011 at 5:33
  • @jmlopez, no I did not see that post. Looks interesting. Incidentally, Wolfram recommends using MLTestHead, instead of MLCheckFunction.
    – rcollyer
    Jul 13, 2011 at 12:28
  • @rcollyer, I did not see that one before. Thanks, I'll be using that instead in the future.
    – jmlopez
    Jul 13, 2011 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.