Expr Class

A representation of arbitrary Wolfram Language expressions in .NET.

Definition

Namespace: Wolfram.NETLink
Assembly: Wolfram.NETLink (in Wolfram.NETLink.dll) Version: 2.0.0.0
C#
[SerializableAttribute]
public sealed class Expr : IDisposable, ISerializable
Inheritance
Object    Expr
Implements
IDisposable, ISerializable

Remarks

The Expr class is a representation of arbitrary Wolfram Language expressions in .NET. Exprs are created by reading an expression from a link (using the GetExpr method), they can be decomposed into component Exprs with properties and methods like Head and Part(Int32), and their structure can be queried with methods like Length, NumberQ, and VectorQ. All these methods will be familiar to Wolfram Language programmers, and their Expr counterparts work similarly.

Like Wolfram Language expressions, Exprs are immutable, meaning they can never be changed once they are created. Operations that might appear to modify an Expr (like Delete(Int32)) return new modified Exprs without changing the original. Because Exprs are immutable, they are also thread-safe, meaning that any number of threads can access a given Expr at the same time.

Exprs are stored initially in a very efficient way, and they can be created and written to links very quickly. When you call operations that inspect their structure or that extract component parts, however, it is likely that they must be unpacked into a more .NET-native form that requires more memory.

In its present state, Expr has four main uses:

(1) Storing expressions read from a link so that they can be later written to another link. This use replaces functionality that C-language programmers would use a loopback link for. (.NET/Link has an ILoopbackLink interface as well, but Expr affords an even easier method.)
C#
Expr e = ml.GetExpr();
// ... Later, write it to a different MathLink:
otherML.Put(e);
e.Dispose();
Note that if you just want to move an expression immediately from one link to another, you can use the IMathLink method TransferExpression(IMathLink) and avoid creating an Expr to store it.

(2) Many of the IKernelLink methods take either a string or an Expr. If it is not convenient to build a string of Wolfram Language input, you can use an Expr. There are two ways to build an Expr: you can use a constructor, or you can create a loopback link as a scratchpad, build the expression on this link with a series of Put calls, then read the expression off the loopback link using GetExpr. Here is an example that creates an Expr that represents 2+2 and computes it in a Wolfram System session using these two techniques:

C#
// First method: Build it using Expr constructors:
Expr symbolPlus = new Expr(ExpressionType.Symbol, "Plus");
Expr e1 = new Expr(symbolPlus, 2, 2);
// ml is a KernelLink
string result = ml.EvaluateToOutputForm(e1, 0);

// Second method: Build it on an ILoopbackLink with MathLink calls:
ILoopbackLink loop = MathLinkFactory.CreateLoopbackLink();
loop.PutFunction("Plus", 2);
loop.Put(2);
loop.Put(2);
Expr e2 = loop.GetExpr();
loop.Close();
result = ml.EvaluateToOutputForm(e2, 0);
e2.Dispose();
(3) Getting a string representation of an expression. Sometimes you want to be able to produce a readable string form of an entire expression, particularly for debugging. The ToString method will do this for you:
C#
// This code will print out the next expression waiting on the link without
// consuming it, so that the state of the link is unchanged:
Console.WriteLine("Next expression is: " + ml.PeekExpr());
(4) Examining the structure or properties of an expression. Although it is possible to do this sort of thing with MathLink calls, it is very difficult in general. Expr lets you read an entire expression from a link and then examine it using a very high-level interface and without having to worry about managing your current position in an incoming stream of data. Expr has Wolfram Language-like methods and properties like Head, Part(Int32), IntegerQ, VectorQ, and many others to assist in examining an expression.

Constructors

Expr(Object) Creates an Expr whose value is given by the supplied object.
Expr(ExpressionType, String) Creates an Expr representing a Wolfram Language Integer, Real, String, or Symbol whose value is given by the supplied string (for example "2", "3.14", "Plus", or "True").
Expr(Object, Object) Creates an Expr with the given head and arguments.

Properties

Args Gets an array of Exprs representing the arguments of this Expr.
Dimensions Gets an array of integers representing the dimensions of this Expr. Works like the Wolfram Language function Dimensions.
Head Gets the Expr representing the head of this Expr. Works like the Wolfram Language function Head.
Item Gets a part based on its position index. This is the indexer for the class.
Length Gets the length of this Expr. Works like the Wolfram Language Length function.

Methods

AsArray Converts the Expr to an array of the requested type and depth.
AsDouble Gives the double value for Exprs that can be represented as doubles.
AsInt64 Gives the Int64 value for Exprs that can be represented as integers.
AtomQ Tells whether the Expr represents a Wolfram Language atom. Works like the Wolfram Language function AtomQ.
ComplexQ Tells whether the Expr represents a Wolfram Language Complex number.
CreateFromLink Creates an Expr by reading it off a link.
Delete Returns a new Expr that has the same head but the nth element deleted (counted from the end if n is negative). Works like the Wolfram Language function Delete.
Dispose Frees resources that the Expr uses internally.
Equals Implements a value-based equality comparison that is similar to the Wolfram Language SameQ function.
(Overrides ObjectEquals(Object))
Finalize The finalizer frees resources that the Expr uses internally.
(Overrides ObjectFinalize)
GetHashCode
(Overrides ObjectGetHashCode)
GetObjectData Populates the SerializationInfo object with the Expr's internal state information.
GetTypeGets the Type of the current instance.
(Inherited from Object)
Insert Returns a new Expr that has the same head but with e inserted into position n (counted from the end if n is negative). Works like the Wolfram Language function Insert.
IntegerQ Tells whether the Expr represents a Wolfram Language integer. Works like the Wolfram Language function IntegerQ.
ListQ Tells whether the Expr represents a Wolfram Language list (that is, it has head List). Works like the Wolfram Language function ListQ.
MatrixQ The elements can be of any type (except lists).
MatrixQ(ExpressionType) Requires that every element be of the specified type.
NumberQ Tells whether the Expr represents a Wolfram Language number (real, integer, rational, or complex). Works like the Wolfram Language function NumberQ.
Part(Int32) Gives the Expr representing the specified part of this Expr.
Part(Int32) Extracts a part more than one level deep.
Put Not intended for general use.
RationalQ Tells whether the Expr represents a Wolfram Language Rational number.
RealQ Tells whether the Expr represents a Wolfram Language Real number.
StringQ Tells whether the Expr represents a Wolfram Language string. Works like the Wolfram Language function StringQ.
SymbolQ Tells whether the Expr represents a Wolfram Language symbol.
Take Returns a new Expr that has the same head but only the first n elements of this Expr (or last n elements if n is negative). Works like the Wolfram Language function Take.
ToString Returns a representation of the Expr as a Wolfram Language InputForm string.
(Overrides ObjectToString)
TrueQ Tells whether the Expr represents the Wolfram Language symbol True. Works like the Wolfram Language function TrueQ.
VectorQ The elements can be of any type (except lists).
VectorQ(ExpressionType) Requires that every element be of the specified type.

Operators

Equality(Expr, Expr) Implements a value-based equality comparison that is similar to the Wolfram Language SameQ function.
(Expr to Double) Converts the Expr to a double value. This is the same operation as calling the AsDouble method.
(Expr to Int64) Converts the Expr to a long integer value. This is the same operation as calling the AsInt64 method.
(Expr to String) Converts the Expr to a string representation. This is the same operation as calling the ToString method.
Inequality(Expr, Expr) Implements a value-based inequality comparison.

Fields

See Also