# Tag Info

24

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

21

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

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

17

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

16

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

14

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

14

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

12

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

10

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

9

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

8

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

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

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

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

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.

6

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

5

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

5

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

5

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

5

Preface: Since you want to convert other C++ classes to MTensor I have to tell you, that I don't see a possibility for that, because I haven't found how to tell the Wolfram Library to take a chunk of already allocated data and use it for an MTensor. What I do is the other way around. I use the internal data of an MTensor as underlying array for my ...

5

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

4

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

4

This is too long for a comment, but let me share some ideas. For the second part of the answer, I'm very skeptical whether it will work, but if you are investigating into the issue, you probably want to check this path. First of all: where f(t) is an arbitrary function For this general case you won't be able to always create a compiled function, ...

3

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

3

Have a look at the memory management write up for tensor. I suspect you are returning a pointer to an automatic tensor that is being cleaned up. LibrayLink Interaction Can you elaborate more on your thoughts, because I read the documentation exactly the opposite way: "For automatic passing, the MTensor is owned by the library function only when that ...

3

There are a lot of def functions in the header file "WolframCompileLibrary.h", which make the type conversion very easy and straight forward. I'm using Mathematica version 8. For example, the following functions can be used to get data from the MTensor variables, they will return pointers to the basic m types such as mint, mreal, or mcomplex from the ...

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