Message Boards Message Boards

LibraryLink Example

Posted 12 years ago
The documentation for LibraryLink has the following example
 DLLEXPORT int demo_TI_R(WolframLibraryData libData,
 mint Argc, MArgument *Args, MArgument Res) {
 MTensor T0;
 mint I0;
 double R0;
 T0 = MArgument_getMTensor(Args[0]);
 I0 = MArgument_getInteger(Args[1]);
 R0 = libData->MTensorVector_getReal(T0, I0);
 MArgument_setReal(Res, R0);
return LIBRARY_NO_ERROR;
}

I am trying to figure out what the command MTensorVector_getReal does. This looks like it is undocumented.

Similarly, in the following example from the documentation, it is not clear what funStruct is, and where it is coming from. Should it be libData instead?
 DLLEXPORT int demo1_TI_R(WolframLibraryData libData,
 mint Argc, MArgument *Args, MArgument Res) {
 MTensor T0;
 mint I0;
 double R0;
 T0 = MArgument_getMTensor(Args[0]);
 I0 = MArgument_getInteger(Args[1]);
 R0 = funStruct->MTensorVector_getReal( T0, I0);
 libData->MTensor_free(T0);
MArgument_setReal(Res, R0);
return LIBRARY_NO_ERROR;
}

Any clarifications on these would be very much appreciated. 
POSTED BY: Asim Ansari
3 Replies
This is a documentation error. That line should read:
libData->MTensor_getReal(T0, &I0, &R0);
The function extracts a real value from the tensor at the given index. As has been noted in this thread, those example programs have several errors due to not being updated as LibraryLink evolved during its development.
POSTED BY: Todd Gayley
I am trying to figure out what the command MTensorVector_getReal does. This looks like it is undocumented. 

I think that this code is not meant to "work".  It is meant to give a feel of what your code might look like.

"MArgument_getMTensor" seems to be a made-up name for a made-up function with an implication that it will return a Tensor when it is given
an "argument" that is passed into the function "demo1_TI_R".  As you can see, that is the way in which "MArgument_getMTensor" is used.

The story is similar for the function that you ask about ("MTensorVector_getReal").  The function has not been defined.  However, from the 
way that it is used, you see that it returns a Real when it is given a Tensor and a Vector.  An example of a function that would have that sort 
of behavior would be something like "get the maximum item of the inner product".  I think the documentation is attempting to be less confusing 
or perhaps just more general by not giving a concrete example here.
Hello Asim Ansari:

I checked an earlier revision of this documentation (in fact, the earliest revision) and this is how the section in question appeared:
 DLLEXPORT int demo1_TI_R(mint Argc, MArgument *Args, MArgument Res) {
     MTensor T0;
     mint I0;
     double R0;
     T0 = MArgument_getMTensor(Args[0]);
     I0 = MArgument_getInteger(Args[1]);
     R0 = funStruct->MTensorVector_getReal( T0, I0);
     funStruct->MTensor_free(T0);
     MArgument_setReal(Res, R0);
    return DLL_NO_ERROR;
}

Therefore, it seems that there was a name change from 'funStruct' to 'libData' that was incomplete.

Since this is all essentially pseudo-code, there is no good way for me to confirm for certain.  I will submit a report to the appropriate developer.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract