RobWorkProject  23.9.11-
Classes | Namespaces | Functions
Mathematica.hpp File Reference

Implementation of the Wolfram Symbolic Transfer Protocol (WSTP) to allow communication with Mathematica. More...

#include <rw/core/Ptr.hpp>
#include <rw/core/macros.hpp>
#include <list>
#include <vector>

Classes

class  Mathematica
 Implementation of the Wolfram Symbolic Transfer Protocol (WSTP) to allow communication with Mathematica. More...
 
struct  Mathematica::Link
 Representation of a link. More...
 
class  Mathematica::Expression
 A representation of a Mathematica expression. More...
 
class  Mathematica::String
 A string primitive. More...
 
class  Mathematica::Integer
 An integer primitive. More...
 
class  Mathematica::Real
 A real primitive. More...
 
class  Mathematica::Symbol
 A symbol primitive. More...
 
class  Mathematica::FunctionBase
 A base interface for function expressions. More...
 
class  Mathematica::Function
 A user definable function expression. More...
 
class  Mathematica::Array< T >
 An Array primitive. More...
 
class  Mathematica::AutoExpression
 Convenience class for automatic Expression deduction. More...
 
class  Mathematica::Packet
 A Packet expression. More...
 

Namespaces

 rwlibs
 Extension libraries for RobWork.
 
 rwlibs::mathematica
 Implementation of the Wolfram Symbolic Transfer Protocol (WSTP) to allow communication with Mathematica.
 

Functions

std::ostream & operator<< (std::ostream &out, const Mathematica::Expression &expression)
 Print a Mathematica expression to an output stream. More...
 

Detailed Description

Implementation of the Wolfram Symbolic Transfer Protocol (WSTP) to allow communication with Mathematica.

Example of basic usage:

Mathematica m;
m.initialize();
const Mathematica::Link::Ptr l = m.launchKernel();
Mathematica::Packet::Ptr result;
*l >> result;
*l << "Solve[x^2 + 2 y^3 == 3681 && x > 0 && y > 0, {x, y}, Integers]";
*l >> result;
std::cout << *result << std::endl;

Construction of a Mathematica environment, m, makes it possible to create WSTP links (Mathematica::Link). Please note that the Mathematica object automatically closes all links at destruction. WSTP links can be used for different types of interprocess communication with Wolfram Symbolic expressions. In this example we launch and connect to a kernel.

The first expression (the In[1]:= prompt) is received from the kernel with the stream operator *l >> result. We simply ignore this value, and send the first command to be evaluated with the stream operator. The result is then retrieved and printed:

 ReturnPacket[
   List[
     List[
        Rule[x, 15],
        Rule[y, 12]],
     List[
        Rule[x, 41],
        Rule[y, 10]],
     List[
        Rule[x, 57],
        Rule[y, 6]]]]

Streaming of a string to the link will implicitly create a ToExpression expression and wrap it in an EvaluatePacket. The kernel will evaluate the expression in the packet and send back a ReturnPacket with the result (as an expression). Many different types of packets can be sent and received on the link (see Mathematica::PacketType). It is up to the user to deal with the different packet types, and to parse the results received.