Message Boards Message Boards


LibraryFunctionLoad with variable number of arguments

Posted 7 years ago
4 Replies
1 Total Likes

Hi, I have studied compiling functions from language C to Mathematica as LibraryLink. Do you know how can I connect function with variable count of inputs? I used precompiled example:

DLLEXPORT int demoNoArguments(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res)
    mint b = Argc;
    MArgument_setInteger(Res, b);
    return LIBRARY_NO_ERROR;

From this code is clear that number of arguments is defined somewhere outside.

I would like prepare override function (multiple definitions of symbol) like this:

demo1=LibraryFunctionLoad["demo", "demoNoArguments", {Integer}, Integer];
demo1=LibraryFunctionLoad["demo", "demoNoArguments", {Integer,  Integer}, Integer];

or better:

demo1=LibraryFunctionLoad["demo", "demoNoArguments", {_Integer..}, Integer];

but if I try to define function again, I see message:

"LibraryFunction::overload: The function demoNoArguments has already been loaded with argument types {Integer,Integer}. The new function will be an overload for different types. >>"

second choice is not possible to use because I see error:

LibraryFunction::btype: "Invalid type and/or rank specification in _Integer... "

Does anyone have any advice?

4 Replies


Not sure whether it answers your question, but if you need tensor-like arguments, then here is how Mathematica does it:

F = Compile[{{n, _Integer,1}},n^2, CompilationTarget -> "C" ];


Posted 7 years ago


Thank you for your answer. Using matrix or tensor is workaround but good idea. Next worse workaround is this:

demo1[arg___Integer] := (LibraryFunctionLoad["demo", "demoNoArguments",
     ConstantArray[Integer, Length[{arg}]], Integer][arg])

Problem is that symbol associated with compiled function cannot be defined for other argument.

If you want a variable number of arguments of the same (numerical) type, use an array instead. I would not call it a "workaround". I'd call it the proper way to do it.

If their types are not the same, or if you have a non-numeric type (e.g. string), you can use MathLink to pass arguments to your library function.

Posted 4 years ago

Thank you for the advice. Now I'm solving other problems but I hope to get back to the topic in time.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract