Hot answers tagged

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


9

This is my try in creating a minimal example with only one variable. There are a couple problems to solve: Minuit2 is in C++, so we need a way to integrate it with mathematica through mathlink. This is a bit cumbersome, but in principle straightforward. This first example uses a mathematica function with minimum user input, that is, it does not uses a user-...


6

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


6

a bit of an extended comment, Note there is no need for a delayed defintion of your pdf: pdf[r_] = Simplify[2 (Piecewise[{{0, r <= 0.59}, {1.36814, Inequality[0.59, Less, r, LessEqual, 0.7]}, {0, r > 0.7}}, Indeterminate] + Piecewise[{{0, r <= 0.7}, {1.99139, Inequality[0.7, Less, r, LessEqual, 0.85]}, {0, r > 0.85}}, ...


5

LibraryLink's complex type, mcomplex, is defined as two contiguous double values. In C++, std::complex<double> has exactly the same layout. This is guaranteed since C++11, but should hold in most other cases too in practice. This means that if you get a complex array from Mathematica, mcomplex *arr = MTensor_getComplexData(t); then you can ...


5

This can be done without using any Mathematica features. You can keep the matrix as a global variable on the C++ side. You will have a function for initializing it, a function for destroying it, and a function for the distance calculation. Then just call the initializer and cleanup function manually from Mathematica when you need to. Note: Storing a ...


4

As Szabolcs says, the problem is that you aren't linking with the MathLink libraries. The docs you cite give a method of doing this, but you might find it easiest (and it's easiest to explain) to simply add the ml32i4m.lib file to your Visual Studio project. (Use ml64i4m.lib, of course, if you are building a 64-bit program.) Right-click on your project in ...


4

tl; dr Report errors through return values. MathLink programs can return any Mathematica expression they like which makes structured error handling possible. It looks like you have a function that returns integers with a template like :Begin: :Function: get_number :Pattern: getNumber[x_Integer] :Arguments: {x} :ArgumentTypes: {Integer32} ...


3

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


3

Yes, you can. This would involve transferring the matrix to Mathematica, invoking the multiplication function, then transferring the result back. Doings this for the kinds of small (4 by 4 and 3 by 3) matrices that come up in your application area is going to be slower than either using pure Mathematica or pure C++ (due to the transfer) take more ...


3

Then let me give you a minimal example using LibraryLink. Create a file named my_vector.cpp (it is in my "tmp" folder here). #include "mathlink.h" #include "WolframLibrary.h" #include <vector> DLLEXPORT mint WolframLibrary_getVersion(){ return WolframLibraryVersion; } DLLEXPORT int WolframLibrary_initialize( WolframLibraryData libData) { return ...


2

Yes: Just use return MLPutFunction(mlp, "EvaluatePacket", 1L) && MLPutFunction(mlp, "ToString", 1L) // <<<--- ! && MLPutFunction(mlp, "ToExpression", 1L) /* ... */


2

This is a linking error, not a compilation error. It occurs because the linker can't find MLEvaluate(): the documentation states that MLEvaluate() is only available for use in mprep MathLink template programs. That's because this function is defined in the C source output by mprep when processing a template files. Template files apply only when you ...


2

Based on Todd Gayley's response here and trying it out, it can be done using MLTransferExpression(NULL, link) This will remove one complete (sub)expression form the link. The code for the example from the question would be int argc = 2; if (! MLTestHeadWithArgCount(link, "f", &argc)) { // fail } MLTransferExpression(NULL, link); int i; ...


2

You are inconsistent with the number of entries in your arrays. You seem to want 5. mymatrix = Table[RandomInteger[1], {5}, {5}]; hub = {1, 1, 1, 1, 1}; auth = {0, 0, 0, 0, 0}; Table[If[mymatrix[[i, j]] == 1, auth[[i]] = auth[[i]] + hub[[j]]], {i, 5}, {j, 5}]; auth or mymatrix = Table[RandomInteger[1], {5}, {5}]; hub = {1, 1, 1, 1, 1}; auth = {0, 0, ...


2

I'm not sure if this answers your question but it's a bit long for a comment. One efficient approach is to convert the parts before and after radix into a base that is a power of 2 e.g. 2^16, and then process that list so each bigit ("bignum digit") is encoded as a hex string. Here is an example. Map[StringDelete[ToString[BaseForm[#, 16]], "\n" ~~ __] &...


2

You might be able to pass the GMP internal array of limbs directly as a list, and then call FromDigits with base = 2^(limb size). This won't require any work on behalf of GMP, though I don't know anything about Mathematica's internals to say how efficient it would be on their end. UPDATE: For the reverse, you could call IntegerDigits.


2

First approach is to create C wrappers for the C++ code. The very simple example I work below defines a class "bola" which represents a sphere with a radius. The member function gives the surface. So, the forward definition (C++ style) of the class functions is in "bola.h" and the definitions of the functions are in "bola.cpp". I give the code for these two ...


2

Use SetDelayed (:=) a = x^2; b := a^2 ?b b:=a^2 As you can see, b is still a^2. http://reference.wolfram.com/language/ref/SetDelayed.html


1

Thanks to Szabolcs for pointing out the right way. So I rewrite the real Inverse version into complex version, and pasted here for reference. libnewsource="#include<complex> #include<Eigen/Dense> #include<Eigen/LU> #include \"WolframLibrary.h\" DLLEXPORT mint WolframLibrary_getVersion(){return WolframLibraryVersion;} DLLEXPORT int ...


1

I guess that this is a linkage issue. The generated output.tm.c file is compiled a C code whereas "your code" is compiled as C++ code. Thus the function output will have C++ linkage while the function __tr0 expects the function to have C linkage. In your C++ code, declare the function with C linkage using extern "C", i.e.: #include "mathlink.h" using ...



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