2
$\begingroup$

Given that I have a ragged list as below,

raggedList = {U1, U2, ..., Un}

where, ui is a sublist($i=1,2,\cdots,n$) and their length are distinct. For example,

raggedList = 
  {{0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 0.8}, 
   {0.21, 0.25, 0.3, 0.6, 0.7, 0.8}, 
   {0.25, 0.3, 0.7, 0.8};

Namely, raggedList is not a tensor. For this case, obviously, I cannot use MArgument_getMTensor(Args[0]) to get that input.

$\endgroup$
  • 1
    $\begingroup$ You can use MathLink to pass such arguments. This is easy but slower than MTensors. Alternatively, you can flatten the array, then pass both its elements and the length of each sublist separately. This will be faster. I have used both approaches with LibraryLink. $\endgroup$ – Szabolcs Jul 31 '16 at 13:40
  • 3
    $\begingroup$ Will this representation help? $\endgroup$ – happy fish Jul 31 '16 at 14:23
  • $\begingroup$ Yes, that's what I meant. $\endgroup$ – Szabolcs Aug 1 '16 at 8:00
2
$\begingroup$

You can't. I mean you can't directly. This is not yet supported in LibraryLink, there is a method to parse it via MathLink(now called WSTP), but that's another story. You can however always flatten your ragged array, store the lengths of each sub-array and pass that as another argument too for later formatting.

Another method would be to pad your array with symbols that you know should not appear as elements of your sub-list, things like -1 if you know the matrix is non-negative.

A lot of things are not supported in LibraryLink others include lists of strings and images.

$\endgroup$
  • $\begingroup$ Padding with symbols doesn't help because symbols can't be a part of a packed array and only packed arrays are supported directly by LibraryLink. Images are supported directly. $\endgroup$ – Szabolcs Aug 1 '16 at 8:01
  • $\begingroup$ If you pad with symbols you get NaN in your array. And I meant list of images is not supported. You will need to pass a 3D image and then slice it up. $\endgroup$ – Vahagn Tumanyan Aug 1 '16 at 8:04
  • $\begingroup$ That is not true, or at least not true on all platforms. On my machine I get a LibraryFunction::cfta message. Using things like Infinity appears to work sometimes, but in reality is not reliable and will result in random weird behaviour. $\endgroup$ – Szabolcs Aug 1 '16 at 8:10
  • $\begingroup$ This is what happens when you try to use them: mathematica.stackexchange.com/questions/19026/… $\endgroup$ – Szabolcs Aug 1 '16 at 8:11
  • $\begingroup$ Hmm. That's possible. I tried with a toy example on my machine and it worked. Anyway. Thanks for clarification. $\endgroup$ – Vahagn Tumanyan Aug 1 '16 at 11:18

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.