# Tag Info

23

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

16

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

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

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

13

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

11

Here are a couple of thoughts: 1) If you want to link to NR, I suggest using LibraryLink since the overhead of MathLink may be too much for some applications. (In version 5, I used MathLink to link against BLAS libraries but an overhead was noticeable.) 2) A lot of quite sophisticated, high performance algorithms are already implemented in Mathematica. I'd ...

10

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

9

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

9

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

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

You may want to look at this presentation. In the past, I have found using .NetLink to be the easiest. You can call c dll's very easily on Windows, without the need for templates as in Mathlink

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

4

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

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

2

Use the "TargetSystemID" option to CreateLibrary (this exists for CreateExecutable and CreateObjectFile as well) to compile for a 32-bit or 64-bit target. "TargetSystemID" -> "Windows" generates a 32-bit binary "TargetSystemID" -> "Windows-x86-64" generates a 64-bit binary If you want to use this library from Mathematica, create a Mathematica package ...

1

The data types defined for LibraryLink are just simple typedefs for standard data types. --> see WolframLibrary.h typedef int mint; /* 32-bit architecture */ typedef long long mint; /* 64-bit architecture */ typedef double mreal; The MArgument_setter and MArgument_getter are just simple #defines for accessing the union MArgument: typedef union { ...

1

Turning Sjoerd's comment into an answer: For forming file-name strings you should use FileNamejoin. This command takes care of the OS-specific file-name separators. The separator for directories for your specific OS is stored in the variable \$PathnameSeparator and can be used too if you ever have to tweak things manually. In your case, when I see this ...

1

Just thought I'd share. I had the same problem after re-installing VS2010 Ultimate (I also have 2008). To fix it, I copied the following libcmt.lib libcmt.pdb libcmtd.lib libcmtd.pdb oldnames.lib from C:\Program Files\Microsoft Visual Studio 11.0\VC\lib to C:\Program Files\Microsoft Visual Studio 10.0\VC\lib Hope this helps someone.

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