Tag Info

Hot answers tagged

30

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


27

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


22

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


22

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


20

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


19

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

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


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


15

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


15

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 SetAttributes[getFrontEndErrors, HoldAll]; getFrontEndErrors[gexpr_] := Module[{nb}, ...


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

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

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


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


12

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


11

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

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


11

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


10

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


10

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


10

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


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

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

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

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


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

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


8

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



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