1
$\begingroup$

I have two arrays. The first one is an array of integers, and the second one is an array of doubles. I want to write a c function to return these arrays by using WSTP library to Mathematica. I know that I can return these arrays in this way:

void Func()
{
   ...
   WSPutInteger32List(stdlink, arrayOfIntegers, arrayOfIntegersLength);
   WSPutReal64List(stdlink, arrayOfDoubles, arrayOfDoublesLength);
}

By using this way, the output which is shown in Mathematica is not what I want. I want to show the arrays in Mathematica as follows

out[command's number]: {{arrayOfIntegers}, {arrayOfDoubles}}  

Can you guide me?

$\endgroup$
  • $\begingroup$ The answer should be familiar to you by now. Use Manual return. To return multiple results, you simply return a List of them. Use WSPutFunction to create the list. Is this sufficient information for you to do it? $\endgroup$ – Szabolcs Aug 31 '18 at 6:32
  • $\begingroup$ The key is that you are only allowed to return one expression. What's wrong with your code is that you are putting two expressions on the link. Wrapping them up in a list makes them into a single expression. $\endgroup$ – Szabolcs Aug 31 '18 at 6:35
  • $\begingroup$ @Szabolcs If it is possible, please write its code by using WSPutFunction $\endgroup$ – H.H Aug 31 '18 at 7:40
  • $\begingroup$ You'll find the answers to most of your questions posted so far in this tutorial: edenwaith.com/development/tutorials/mathlink/ML_Tut.pdf $\endgroup$ – Szabolcs Aug 31 '18 at 8:09
0
$\begingroup$

In the code you show, you are trying to return two expressions. This is not possible. A function can only return a single expression. The solution: wrap up the two results into a single expression by placing them into a list.

So, it is not possible to return a, b, but it is possible to return {a,b} instead.

You need to use PutFunction to achieve this.

Example code:

WSPutFunction(stdlink, "List" /* head */, 2 /* two elements in the list */);
  WSPutInteger32List(stdlink, arrayOfIntegers, arrayOfIntegersLength);
  WSPutReal64List(stdlink, arrayOfDoubles, arrayOfDoublesLength);

Given your interest in WSTP, I strongly suggest you read through this tutorial. Some things are outdated, but most of it is still relevant. Do read at least up to section 1.5 (inclusive), and preferably all of the 1st chapter.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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