# Call Mathematica functions from C++

Posted 3 years ago
5630 Views
|
2 Replies
|
11 Total Likes
|
 Here is an example of how researchers from CERN use Mathematica functions in C++.I have tried to dissect the code, but I still get plenty of errors when I try to compile it. Can anyone show a simple example of how to efficiently call Mathematica functions from C++? Answer
2 Replies
Sort By:
Posted 3 years ago
 There are several possible ways discussed in detail here:http://mathematica.stackexchange.com/questions/8438/minimal-effort-method-for-integrating-c-functions-into-mathematicaThere are more LibraryLink examples here:http://community.wolfram.com/groups/-/m/t/189735/Once you are familiar with LibraryLink, you might want to take a look at my package, LTemplate, which makes it much quicker to build complex LibraryLink applications:http://mathematica.stackexchange.com/questions/96127/how-to-simplify-writing-librarylink-code Answer
Posted 11 months ago
 This is a 2 years old post. But I think It wouldn't harm linking another post here. I have recently posted about a C++ library mathematica++ in the forum that I have been working on for long days. mathematica++ is a C++ library that uses WSTP and template magic so that user code can have more mathematica like syntax. Have a look at this post http://community.wolfram.com/groups/-/m/t/1419096An example code using mathematica++ looks like the following value result; typedef std::vector> ivv_type; shell << FactorInteger(2434500); // << Invoke mathematica function FactorInteger shell >> result; // << Fetch result ivv_type prime_powers = mathematica::cast(result); // << Cast to STL for(auto pp: prime_powers){ std::cout << pp << " ^ " << pp << std::endl; } // Prints the following output // 2 ^ 2 // 3 ^ 2 // 5 ^ 3 // 541 ^ 1 Mathematica Expressions can be built and stored in C++ symbol x("x"); value res; std::string method = "Newton"; shell << Values(FindRoot(ArcTan(1000 * Cos(x)), List(x, 1, 2), Rule("Method") = method)); shell >> res; std::vector results = cast>(res); std::cout << results << std::endl; // Prints 10.9956 Answer