Tag Info

Hot answers tagged

36

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 ...


32

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 ...


24

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 = ...


21

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 ...


20

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 ...


17

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 ...


16

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

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 ...


13

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 ...


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 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 = ...


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 = ...


9

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 ...


9

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.


9

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 ...


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

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

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

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; ...


7

An observation, rather than an answer to the why question. The question suggests, that the problem might have something to do with the used lapack library. As it turns out, at least on my machine it doesn't. We can replace the C-code with the most simple routine which is taking an input tensor and returning exactly the same tensor as result. MTensor ta = ...


7

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 ...


7

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 ...


7

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.


7

I think the issue is that the demo source file hasn't been properly updated. Originally it used MLCheckFunction, which was later deprecated in favor of MLTestHead. There is, however an incompatible change in the type of the last argument: MLTestHead takes a pointer to int, while MLCheckFunction used to take a pointer to long. The following test fails ...


6

On OS X the usual solution is to set the DYLD_LIBRARY_LINK environment variable to specify custom search paths for shared libraries. This is quite inconvenient when Mathematica is launched as a GUI program, but there is an alternative solution using install names: It is possible to hard code the install location of a shared library into the library itself. ...


6

For reference, I put my code lines and comments on the site. Nothing new for the experienced, I am certain, but maybe it can spare a few hours to a newcomer to Mathematica&Co, like me. I have (1) followed Szabolcs advice and insisted with MathLink (the framework with the .tm file) and tried (2) Halirutan solution of using Mathlink commands in the ...


6

What you observed is a bit unfortunate because these libraries should be easy to use together (because they're meant to be used together). Here's a "workaround" that could make your life easier in certain cases. You can use C++ instead of C and take advantage of overloaded functions. I have a header called mlp.h with the following: #include ...



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