Hot answers tagged mathlink
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 ...
18
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 ...
18
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";
...
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 ...
15
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
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 ...
14
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 ...
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 ...
12
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 ...
11
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 ...
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 ...
11
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
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 ...
9
Instead of driving a remote kernel directly via MathLink, you should consider implementing this task with webMathematica:
webMathematica 3 added support for queueing long running computations, which can also use compute kernels.
you can connect to webMathematica's web frontend with a web browser to inspect the state of the running computation at any time.
...
9
NaN (or Not-a-Number is used in floating point arithmetic to represent values that are undefined or unrepresentable, such as $0/0,\ \infty/\infty$, etc. Mathematica typically returns Indeterminate for these, but several other languages return NaN.
To work with NaNs, you must load the ComputerArithmetic package as <<ComputerArithmetic` prior to calling ...
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
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 ...
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) ...
8
I don't know if this answer helps you but what I wanted to convey would be too lengthy for a comment. First of all quoting from your comment
"...the objective function (it is t, so fairly trivial)"
this problem actually renders to be so simple that any mechanism involving Compile or binding with external optimization routine (e.g CPLEX) involving ...
8
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 ...
8
LinkSnooper is a great tool any time you need to see what's traveling across a link. You can actually get it completely set up to monitor an installable MathLink program in a single Install command.
The syntax for Install is
Install["path to .exe"]
When using LinkSnooper, it is not the .exe that you want to launch directly, but rather LinkSnooper as the ...
8
I consider the loopback link solution to this problem to be simple and ideal. I appreciate the cleverness of the LinkedList/Bag/Sequence types of solutions that have been proposed in other answers, but they seem to require a little too much thought for my taste.
This type of problem is one of the practical applications that motivated the creation of ...
7
Here I describe the steps I took to make it work on my Macbook Pro using Mac OS X 10.7.4. It shouldn't be very different on Windows but the idea is the same. I'll try to make this post as simple as possible explaining all the details that I can.
Fortran Libraries
Lets begin by compiling the fortran libraries. To do this you will need to download the MINUIT ...
7
A few ideas:
If it's a package that is to be used regularly, I'd put it in one of the standard locations where M searches for packages, such as e.g. $UserBaseDirectory/Applications.
SetDirectory doesn't affect the search locations for packages. SetDirectory sets what's called the "working directory", while Get searches only those locations which are ...
7
Using NULL as the destination link, as in MLTransferExpression(NULL, loop); is indeed the "correct" way to do this.
One small caveat: You cannot use that technique if you have not built a complete expression on the loopback link. For example, if you are building a complicated expression on the loopback link bit by bit, then decide halfway through to bail ...
7
See: http://reference.wolfram.com/mathematica/tutorial/HandlingListsArraysAndOtherExpressions.html
From that documentation page:
In order to call functions like MLPutFunction(), you need to know the length of the list you want to send. But by creating a sequence of nested Sequence objects, you can avoid having to know the length of your whole list in ...
7
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 ...
7
Suppose you create a file addtwo.tm :
:Begin:
:Function: addtwo
:Pattern: AddTwo[i_Integer, j_Integer]
:Arguments: { i, j }
:ArgumentTypes: { Integer, Integer }
:ReturnType: Integer
:End:
:Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine integers x and y."
int addtwo( int i, int j)
{
return i+j;
}
int ...
Only top voted, non community-wiki answers of a minimum length are eligible




