5
$\begingroup$

Since I'm starting to look into C-programming using LibraryLink to connect Mathematica to computational expensive calculations, I was wondering about the two available options within LibraryLink for arrays of Datatypes, MTensor and MNumeric array. In principle they both supply arrays of numbers to an C-program plugin.

Is there any significant difference in functionality or applications where one would favor the use over the other?

Or is the fact that there are two independent solutions just because of historical reasons?

$\endgroup$

1 Answer 1

7
$\begingroup$

tl;dr Use MTensor unless you know why you need MNumericArray.


Each of the LibraryLink types maps to a Mathematica type.

  • MTensor – packed arrays (i.e. List)
  • MNumericArrayNumericArray
  • MSparseArraySparseArray
  • MImageImage

If you want to pass lists of numbers to your functions, then the function should take MTensor. Basically, numerical work = packed arrays = MTensor.

Think of (M)NumericArray as being merely for storing data that Mathematica doesn't directly support but is useful in a C program. If you want to store numbers of a very specific size/type, use this. Keep in mind that Mathematica can do almost nothing at all with NumericArrays. You would almost always need to convert them to a list to do any calculations. They only make sense if you specifically need another type than double or mint, which are the native types of Mathematica (i.e. "machine real" and "machine integer").

$\endgroup$
6
  • $\begingroup$ NumericArrays can, in principle, be used with functions created using FunctionCompile, but it takes a lot of effort to get it working in my experience. Hopefully it'll be a bit more accessible in V12.2. $\endgroup$ Nov 24, 2020 at 10:41
  • $\begingroup$ Thanks @Szabolcs for the clear explanation. One additional question on MTensor: Since it's equivalent to List, how can one implement e.g. a List of Lists of different length? As I understand the "numericarrayLibraryFunctions->MTensor_getDimensions" callback is just working with rectangular Tensors. If I want to supply nested lists of different length I would need to assign a specific numerical value as "end-marker" for the sub-list. Same problem holds if I return an non-regular nested list back to Mathematica. Is there any mechanism to simplify this? $\endgroup$
    – Rainer
    Nov 24, 2020 at 12:49
  • $\begingroup$ @Rainer To be accurate, it's a packed array, which is a very specific kind of list. List of lists of different lengths is not possible to access directly through LibraryLink. You can transfer such a thing through MathLink (from a LibraryLink library), but not access it directly, without copying. Nor is there a ready-made data structure you can use for it in C. You need to use your own representation. There were questions on this before, let me see if I can find them. $\endgroup$
    – Szabolcs
    Nov 25, 2020 at 10:20
  • $\begingroup$ One solution I sometimes use is that I encode the ragged matrix into a flat array, and decode in Mathematica. It's not pretty, but faster than MathLink. I'd only recommend it if you really need the speed. $\endgroup$
    – Szabolcs
    Nov 25, 2020 at 10:22
  • $\begingroup$ @Szabolcs thanks thas how I expected. I will implement a packed array with a stop integer value (e.g. -2^63 for Integer64) to find out the boundaries of the ragged lists in C... $\endgroup$
    – Rainer
    Nov 26, 2020 at 11:16

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.