Hot answers tagged

43

Here is my pitch to use LibraryLink, which is a really nice new technology in version 8. I am not going to pretend this is easy by any stretch of the imagination, because it involves a decent amount of knowledge of both Mathematica and C compilers. In this particular case I am using Visual Studio C++ Express plus the Microsoft Windows SDK 7.1. For the ...


39

I wrote a package to automatically generate all the boilerplate needed for LibraryLink and for managed library expressions based on a template that describes a class interface: LTemplate Here's how it works Write a template (a Mathematica) expression that describes a C++ class interface This template is used to generate LibraryLink-compatible C ...


34

On Windows, C/C++ functions that have been compiled into DLLs can be accessed reasonably easily using NETLink. Let's say we have the following C++ DLL definition: #include "stdafx.h" BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { return TRUE; } extern "C" __declspec(dllexport) void helloMma(double a, double b, int n, double ...


26

Presuming that your c++ code is already written, then I don't know how the code generation feature would be helpful. That said, in order of simplicity, I would have Get, ReadList, Import, and both LibraryLink and MathLink. Get and ReadList are by far the simplest. Provided that your c++ program outputs to stdout (std::cout), then it is simply val = ...


24

Here are three very simple examples to show how to call a Fortran subroutine using LibraryLink. First the subroutine is compiled into object file. Then a wrapper is used to call the Fortran subroutine and compiled into dynamic library. At the end, the library is loaded into Mathematica and run. In the examples Mathematica Version 8 is used. FIRST EXAMPLE ...


22

I suggest to use MathLink, which you can automate using the CCompilerDriver`. This is a safe alternative, since you won't crash the kernel if your code crashes. Once tested, this should not be hard to convert to library link. As an explicit example, consider a function which receives a list of integers and squares it. First, here is a function to create the ...


18

Yes, I did something like that and it runs very very nicely. What I implemented is a dynamic Newton fractale visualizer where you can manipulate the number and position of the complex roots, the colours and the gamma correction settings from the Mathematica side. These values are sent to a parallel C++ implementation which calculates the fractale into a ...


18

Taking user5601's suggestion to do a little demo, I quickly whipped this up as an example of ProcessLink being used to do non-trivial communication between Mathematica and an external program, but with much less ceremony than using ProcessLink or MathLink. Let's take this little Go program: package main import "net/http" import "bufio" import "os" import ...


17

Note that ListContourPlot3D takes the coordinates to be the position indices by default. If you want to keep the coordinates used in generating the data, then you have to include it. data = Flatten[ Table[{x, y, z, x^2 + y^2 + z^2 + RandomReal[0.1]}, {x, -2, 2, 0.2}, {y, -2, 2, 0.2}, {z, -2, 2, 0.2}], 2]; plot = ListContourPlot3D[data, Contours ...


16

Let me give you a very basic example, how you can employ an asynchronously running LibraryLink function for this specific task. I will not do any real packet listen, but only explain the general setup. Usually, a LibraryFunction in Mathematica is written in C and wrapped with a very small boiler plate code that is needed to attach the function directly to ...


14

Here is a more general approach. It is based on the 2D method from here. It assumes the polyhedron is not self-intersecting but imposes no requirement of convexity or even connectedness, other than that it be closed and bounded. Strictly speaking, I think this will work for an unbounded polyhedron provided it contains no vertical ray. For ease of exposition ...


13

I managed to get something going: Needs["CCompilerDriver`"] src = " #include \"WolframLibrary.h\" #include <boost/date_time/gregorian/gregorian.hpp> EXTERN_C DLLEXPORT int dow(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) { mint year = MArgument_getInteger(Args[0]); mint month = ...


12

My advice is to not rely on the code samples in the documentation. It has proven to be unreliable. For instance In the documentation to MTensor_getComplexData the example doesn't even use the function. The documentation to MTensor_free can barely be called documentation since it does not explain about memory allocation etc The documentation to AbortQ ...


11

Here is a fast method that will "often" work. Roughly, it requires that the convex polygon have no sharp angles between faces. Preprocessing goes as follows. Create triangles from the polygons. So a 5-gon with vertices {a,b,c,d,e} would become the set of triangles {{a,b,c},{a,c,d},{a,d,e}}. For each vertex we average it's star (set of points connected by ...


11

I'm not sure whether I will get everything right here, but to my knowledge the key-point is indeed MTensor_disown. When you call loadFun you basically move the write-priviliges for the array to your library functions. This means, changing values inside the library will be transparent on the Mathematica side. Let's load the library functions: loadFun = ...


10

LibraryLink vs. MathLink The Wolfram Library Link is a way to run code which is placed in a so called shared or dynamic library directly by loading the library into a running MathKernel. One way to send and to receive data are, as you mentioned, the MArgument_* functions. But this is not the only way! As shown in the examples of the LibraryLink Tutorial ...


10

You may want to look at this presentation: Integrating C and Mathematica. In the past, I have found using .NET/Link to be the easiest. You can call C DLL's very easily on Windows, without the need for templates as in MathLink.


10

I can partially answer to my own questions. Amazingly, but it is easier to use the internal MKL. Let us consider my question about multiplication a band matrix by a dense matrix. The corresponding function is mkl_zdiamm. I wrote the following code (diamm.c) #include <stdio.h> #include <stdlib.h> #include <WolframLibrary.h> #include ...


10

At the end of this post you'll find the code for a small benchmark to compare LibraryLink with standard passing vs LibraryLink with MathLink based passing on two counts: function call overhead; use a function that does nothing, has no arguments, returns nothing (llNone and mlNone for LibraryLink and MathLink, respectively) passing real arrays; use a ...


9

Does the library call back functions are slower then the macro since they need to "call back" to Mathematica? I'm pretty sure this is not the case. I guess that the difference is really that the WolframLibrary.h is a public interface which hides all implementation, while the WolframCompileLibrary.h is used by the CCodeGenerator` and gives you at least ...


8

I see a third way, which is a combination of your two choices, but its usefulness depends on the problem you are solving. I used this once to get the best from both worlds. What I needed was a similar thing to ComponentMeasurements in 3D. Therefore, I needed a component labeling algorithm (similar to MorphologicalComponents) which takes a volume, binarizes ...


8

In Version 10 we have BoundaryDiscretizeGraphics and RegionMember to the rescue. So here we go: Reproducing the graphics: data = Flatten[Table[{x, y, z, x^2 + y^2 + z^2 + RandomReal[0.1]}, {x, -2, 2, 0.2}, {y, -2, 2, 0.2}, {z, -2, 2, 0.2}], 2]; (* Thanks to Taliesin Beynon for the tip *) plot = ListContourPlot3D[data, Contours -> {1}, Mesh -> ...


8

No, it is not possible, without creating a custom C++ compiler with enhanced run-time which would support reflection, because C++ does not have a reflection API (JLink capability of accessing properties or methods is based on Java Reflection). There are some libraries / attempts to provide reflection-like functionality to C++, but all of them are (AFAIK) ...


8

Let me describe a way which works through all systems and simplifies the distribution of library code within a package a lot. First I want to point out that there are two major scenarios here: You are currently developing a package containing LibraryLink functions. When you are actively working on such a package, it is most likely not installed in your ...


8

Mathematicas invocation of the compiler doesn't know about where to find the Fortran library. With a little help, however, we can point the way. Mind you this was done on a Mac but the Linux variant of Unix will behave similar. Needs["CCompilerDriver`"]; CreateLibrary[{"MMA.cc", "fadd.o"}, "myadd", "Debug" -> True, "TargetDirectory" -> ".", ...


8

You can look here for more examples and details: http://community.wolfram.com/groups/-/m/t/189092 http://community.wolfram.com/groups/-/m/t/189735 In short, do the following (call the file DoubleIt.c): #include "WolframLibrary.h" DLLEXPORT int DoubleIt(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) { mint x; ...


8

You have not defined any message text in Mathematica. The text you supply in the C code is the message tag, e.g libData->Message("myerror"); Then you need to define the actual message content in Mathematica: LibraryFunction::myerror = "Here's my message" The relevant documentation page is here.



Only top voted, non community-wiki answers of a minimum length are eligible