Hot answers tagged

41

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


35

There are two performance problems here. The first is relatively minor: MultinormalDistribution[μ, Σ] is evaluated in each slave kernel, returned to the master kernel, and sent back to the slave kernels as part of the RandomVariate call. In your example, this is a packed array of about 80KB in size: not large, yet not small either, and this behaviour may ...


33

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


29

MathLink can be used to establish communication between two kernels on different machines using TCP/IP. A basic example that illustrates the technique can be found in the Mathematica documentation. The basic sequence of events is like this: The server creates an endpoint using LinkCreate. The client connects using LinkConnect. When one side wants to send ...


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

IronPython requires .NET 4.0 to run. As of V8, Mathematica launches .NET 2.x by default. See this question for details about how to use .NET 4.0. Having done that, we need to load the IronPython assembly into the .NET framework: Needs["NETLink`"] InstallNET[]; $pythonDll = "C:\\Program Files (x86)\\IronPython 2.7.1\\IronPython.dll"; ...


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

To access the errors, you need to invoke the Front End directly from the kernel. In effect, you end up telling the kernel to tell the FE to tell the kernel to do something, so that the FE can report any errors it finds. The method I use is ClearAll[getFrontEndErrors]; SetAttributes[getFrontEndErrors, HoldAllComplete]; getFrontEndErrors[expr_] := ...


17

Being the fan of Mathematica<->CLR interop that I am, your question has inspired me to try to get IronPython fully working with Mathematica for the last couple of days. I haven't yet had total luck. Part of my problem is that I don't have a Windows Mathematica license, so I can't fully double-check my work. While I'm trying to hunt down Mathematica for ...


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

Have a look at this; http://reference.wolfram.com/mathematica/guide/MathLinkCLanguageFunctions.html I haven't used it in C/C++ but it works fine in C# and Java. Basically you create a connection to a Mathematica kernel and then pass it native data types. Works nicely. Here is some sample code in Java that I used when I first did this; import ...


16

There are a few problems with your code. If you fix those up, as I did, your program will run fine. First off, the reason you see "no source available" when you pause the program is probably that when you break, the program is down inside a MathLink function, and so it is complaining that it doesn't have access to the MathLink library source code. To debug ...


16

Under Linux the WSTP shared libraries that ship with Mathematica 10.0.1 for both 64 and 32 bit (libWSTP64i4.so and libWSTP32i4.so) appear to be broken. Link with the static versions libWSTP64i4.a or libWSTP32i4.a instead. The Windows and OS X versions are OK.


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


15

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

See this link. I have found a solution. Works fine to me. Steps: 1-Create a script named runMath with the content: #!/usr/local/bin/MathematicaScript -script value=ToExpression[$ScriptCommandLine[[2]]]; (*The next line prints the script name.*) (*Print[$ScriptCommandLine[[1]]];*) Print[value]; 2-I gave execution privilege to the file. sudo chmod +x ...


14

To call Mathematica from Clojure, you will want to use Clojure's Java interop to access the Wolfram JLink classes to script Mathematica access. Start by launching Clojure with a classpath that includes the JLink.jar, for example with this batch file: @echo off set MATHEMATICA=C:/M/9.0 set JLINKJAR=%MATHEMATICA%/SystemFiles/Links/JLink/JLink.jar java -cp ...


14

To use .NET/Link from a Mono program, you need to make sure that the system can find the MathLink shared library. This generally means adding the appropriate path to an environment variable that the system uses for library lookups. You can do this is in the standard way that is appropriate for your OS/shell program, either in a shell config file or on the ...


14

You will find a complete, step by step description of how to write such a program here. Here's a small test program that adds 42 and 137, written by following the tutorial closely: /* mlcall.c */ #include <mathlink.h> #include <stdio.h> int main(int argc, char *argv[]) { MLENV env; MLINK link; int errno; int packet; env = ...


13

Yes, it is, but it's cumbersome (at least as of Mathematica 8). The hardest part is that you have to manually do a lot of the juggling required to work with .NET generics and extension methods. For example, let's translate a straightforward solution to Project Euler's Problem #1 ("Add all the natural numbers below one thousand that are multiples of 3 or ...


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


12

Setting up MathLink connections between kernels acting as peers (as opposed to in a master-slave arrangement) is sparsely documented, and the critical function you need to make this work, i.e. LinkActivate, is undocumented altogether (although, if you clear its ReadProtected attribute, you will see that it is merely a synonym for LinkConnect, which itself is ...


11

Once again thanks to John Fultz we know a command that returns the complete list of these packets: MathLink`CallFrontEnd[FrontEnd`NeedCurrentFrontEndSymbolsPacket[]][[1, 1, 4]] Results from Mathematica 7: "" "Null" "CompoundExpression" "List" "Execute" "FrontEndExecute" "KernelExecute" "OpenParallelLinksPacket" "KernelStartupCompleted" ...


11

If you want to use port forwarding, you'll need to know that for every MathLink connection, two different ports are used. The full syntax for TCPIP link names looks like this: LinkCreate["8000@1.2.3.4,8001@1.2.3.4", LinkProtocol -> "TCPIP"] 8000 and 8001 are the port numbers while 1.2.3.4 is your IP address. You can pass only a single port number to ...


11

Let me present an alternative approach. The whole commandline work and link naming and connecting can be simplified. What we need is the location of the LinkSnooper program and the location of your MathLink program and then you can set up everything in exactly one call to Install from Mathematica. I have tried to make the following, where I demonstrate it ...


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


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.



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