The Expr class is a representation of arbitrary Mathematica expressions in Java.
Exprs are created by reading an expression from a link (using the getExpr() method),
they can be decomposed into component Exprs with methods like head() and part(), and
their structure can be queried with methods like length(), numberQ(), and matrixQ().
All these methods will be familiar to Mathematica programmers, and their Expr
counterparts work similarly. Like Mathematica epxressions, Exprs are immutable, meaning
they can never be changed once they are created. Operations that might appear to modify
an Expr (like delete()) return new modified Exprs without changing the original.
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
Java-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. (J/Link has a LoopbackLink interface as well, but Expr affords an even easier
method.)
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 MathLink method transferExpression() and avoid creating an Expr to store it.
(2) Many of the KernelLink methods take either a string or an Expr. If it is not convenient
to build a string of Mathematica 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 MathLink 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 Mathematica using these two techniques:
// First method: Build it using Expr constructors:
Expr e1 = new Expr(new Expr(Expr.SYMBOL, "Plus"), new Expr[]{new Expr(2), new Expr(2)});
// ml is a KernelLink
String result = ml.evaluateToOutputForm(e1, 72);
// Second method: Build it on a LoopbackLink with MathLink calls:
LoopbackLink loop = MathLinkFactory.createLoopbackLink();
loop.putFunction("Plus", 2);
loop.put(2);
loop.put(2);
Expr e2 = loop.getExpr();
loop.close();
result = ml.evaluateToOutputForm(e2, 72);
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:
// This code will print out the next expression waiting on the link without
// consuming it, so that the state of the link is unchanged:
System.out.println("Next expression is: " + ml.peekExpr().toString());
(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 is a work in progress. It will be expanded in the future.
BIGDECIMAL
A type constant representing floating point numbers larger than can fit in a Java double, for use in an Expr constructor, vectorQ(type), or matrixQ(type).
static int
BIGINTEGER
A type constant representing integers larger than can fit in a Java int, for use in an Expr constructor, vectorQ(type), or matrixQ(type).
static int
COMPLEX
A type constant representing complex numbers, for use in an Expr constructor, vectorQ(type), or matrixQ(type).
SYMBOL
A type constant representing symbols, for use in an Expr constructor, vectorQ(type), or matrixQ(type).
Constructor Summary
Expr(java.math.BigDecimal val)
Creates an Expr representing a large Mathematica Real with the specified value.
Expr(java.math.BigInteger val)
Creates an Expr representing a large Mathematica Integer with the specified value.
Expr(double val)
Creates an Expr representing a Mathematica Real with the specified value.
Expr(double[] val)
Creates an Expr representing a Mathematica list of reals with the specified value.
Expr(double[][] val)
Creates an Expr representing a Mathematica matrix of reals with the specified value.
Expr(Expr head,
Expr[] args)
Creates an Expr with the given head and arguments.
Expr(int[] val)
Creates an Expr representing a Mathematica list of integers with the specified value.
Expr(int[][] val)
Creates an Expr representing a Mathematica matrix of integers with the specified value.
Expr(int type,
java.lang.String val)
Creates an Expr representing a Mathematica Integer, Real, String, or Symbol whose value is
given by the supplied string (for example "2", "3.14", or "Plus").
Expr(long val)
Creates an Expr representing a Mathematica Integer with the specified value.
Expr(java.lang.String val)
Creates an Expr representing a Mathematica String with the specified value.
args()
Gives an array of Exprs representing the arguments of this Expr.
java.lang.Object
asArray(int reqType,
int depth)
Gives a Java array representation with the requested depth and element type.
java.math.BigDecimal
asBigDecimal()
Gives the BigDecimal value for Exprs that can be represented as BigDecimals (this is exactly
the set for which integerQ() or realQ() returns true).
java.math.BigInteger
asBigInteger()
Gives the BigInteger value for Exprs that can be represented as BigIntegers (this is exactly
the set for which integerQ() or realQ() returns true).
double
asDouble()
Gives the double value for Exprs that can be represented as doubles (this is exactly
the set for which integerQ() or realQ() or rationalQ() returns true).
int
asInt()
Gives the integer value for Exprs that can be represented as integers (this is exactly
the set for which integerQ() returns true).
long
asLong()
Gives the long value for Exprs that can be represented as integers (this is exactly
the set for which integerQ() returns true).
java.lang.String
asString()
Gives the string value for Exprs that can be represented as strings (this is exactly
the set for which stringQ() or symbolQ() returns true).
boolean
atomQ()
Tells whether the Expr represents a Mathematica atom.
boolean
bigDecimalQ()
Tells whether the Expr represents a Mathematica real (floating-point) number, but requires more digits
to store than can fit into a Java double.
boolean
bigIntegerQ()
Tells whether the Expr represents a Mathematica integer, but requires more digits to store than can fit into a Java int.
boolean
complexQ()
Tells whether the Expr represents a complex number.
createFromLink(MathLink ml)
This factory method will only be used by advanced programmers who are creating their own
classes that implement the MathLink interface.
insert(Expr e,
int n)
Returns a new Expr that has the same head but with e inserted into position n (counted from the
end if n is negative).
boolean
integerQ()
Tells whether the Expr represents a Mathematica integer.
int
length()
Gives the length (the number of arguments) of this Expr.
boolean
listQ()
Tells whether the Expr represents a Mathematica list (that is, it has head List).
boolean
matrixQ()
Tells whether the Expr represents a Mathematica matrix (that is, it has head List,
every element has head List, and no deeper parts are themselves lists).
boolean
matrixQ(int eType)
Tells whether the Expr represents a Mathematica matrix, every element of which is of
the specified type.
boolean
numberQ()
Tells whether the Expr represents a number (real, integer, rational, or complex).
A type constant representing floating point numbers larger than can fit in a Java double, for use in an Expr constructor, vectorQ(type), or matrixQ(type).
Creates an Expr representing a Mathematica Integer, Real, String, or Symbol whose value is
given by the supplied string (for example "2", "3.14", or "Plus").
Parameters:
type - the type of the Expr; must be one of INTEGER, REAL, BIGINTEGER, BIGDECIMAL, STRING, or SYMBOL
val - the value of the Expr, interpreted according to the type argument
Throws:
java.lang.IllegalArgumentException - if an unsupported type is specified
Expr
public Expr(long val)
Creates an Expr representing a Mathematica Integer with the specified value.
Parameters:
val -
Expr
public Expr(double val)
Creates an Expr representing a Mathematica Real with the specified value.
Parameters:
val -
Expr
public Expr(java.lang.String val)
Creates an Expr representing a Mathematica String with the specified value.
Parameters:
val -
Expr
public Expr(int[] val)
Creates an Expr representing a Mathematica list of integers with the specified value.
Parameters:
val -
Expr
public Expr(double[] val)
Creates an Expr representing a Mathematica list of reals with the specified value.
Parameters:
val -
Expr
public Expr(int[][] val)
Creates an Expr representing a Mathematica matrix of integers with the specified value.
Parameters:
val -
Expr
public Expr(double[][] val)
Creates an Expr representing a Mathematica matrix of reals with the specified value.
Parameters:
val -
Expr
public Expr(java.math.BigInteger val)
Creates an Expr representing a large Mathematica Integer with the specified value.
Parameters:
val -
Expr
public Expr(java.math.BigDecimal val)
Creates an Expr representing a large Mathematica Real with the specified value.
This factory method will only be used by advanced programmers who are creating their own
classes that implement the MathLink interface. You would call this method in your
implementation of getExpr(). In other words, this method exists not as a means for casual
users to create Exprs from a link (use the MathLink method getExpr() instead), but so that MathLink
implementors can write their own getExpr() methods without having to know anything
about the internals of the Expr class. Exprs know how to read themselves
off a link.
Implements an equality comparison that is similar to Mathematica's SameQ. It is
not guaranteed to have the same behavior as SameQ. For example, it is possible
in some rare circumstances to have two Exprs that contain real numbers, and Mathematica
would say the Exprs satisfy SameQ but they would not satsify equals() in Java.
Overrides:
equals in class java.lang.Object
Parameters:
obj -
hashCode
public int hashCode()
Overrides:
hashCode in class java.lang.Object
dispose
public void dispose()
Frees resources that the Expr uses internally. The object should not be used
after dispose() has been called. You should get in the habit of calling
dispose() on Exprs as soon as you are finished using them.
Gives an array of Exprs representing the arguments of this Expr. For Exprs of type RATIONAL and COMPLEX,
returns a two-argument array giving the numerator/denominator or re/im parts, repsectively. If there are
no args (this is a function with zero arguments, or an atom of type INTEGER, REAL, STRING, SYMBOL, BIGINTEGER,
or BIGDECIMAL), then a 0-length aray is returned.
Returns:
an array of the arguments, as Exprs.
length
public int length()
Gives the length (the number of arguments) of this Expr. Works like the Mathematica
function Length.
Returns:
the length
dimensions
public int[] dimensions()
Gives an array of integers representing the dimensions of this Expr. Works like the
Mathematica function Dimensions.
Gives a new Expr representing the specified part of this Expr. Works like the Mathematica
function Part.
This form of part() allows you to extract a part more than one level deep. Thus,
e.part(new int[] {3,4}) is like the Mathematica function Part[e, 3, 4] or e[[3]][[4]].
Parameters:
ia - the index of the desired part
Returns:
the specified part, as an Expr
Throws:
java.lang.IllegalArgumentException - if any of the part specifications are beyond the bounds of the expression.
Gives the real part of an Expr that represents a complex number. For integers and reals,
it gives the number itself. Works much like the Mathematica function Re.
This method is meaningful only for Exprs that can represent Mathematica complex numbers
(which is the set of Exprs for which complexQ, integerQ, realQ, or rationalQ would
return true). Otherwise, it throws ExprFormatException.
Returns:
The real part of the complex number, or 0 if this Expr is not an integer, real, rational, or complex number
Gives the imaginary part of an Expr that represents a complex number.
Works much like the Mathematica function Im.
This method is meaningful only for Exprs that can represent Mathematica complex numbers
(which is the set of Exprs for which complexQ, integerQ, realQ, or rationalQ would
return true). Otherwise, it throws ExprFormatException.
Returns:
The imaginary part of the complex number
Throws:
ExprFormatException - if this Expr is not an integer, real, rational, or complex number
Gives a readable string representation. This representation is in what amounts
to Mathematica FullForm, but it is not guaranteed to be usable directly as
Mathematica input.
Overrides:
toString in class java.lang.Object
Returns:
the string form
atomQ
public boolean atomQ()
Tells whether the Expr represents a Mathematica atom.
Works like the Mathematica function AtomQ.
Returns:
true, if the Expr is an atom; false otherwise
stringQ
public boolean stringQ()
Tells whether the Expr represents a Mathematica string.
Works like the Mathematica function StringQ.
Returns:
true, if the Expr is a string; false otherwise
symbolQ
public boolean symbolQ()
Tells whether the Expr represents a Mathematica symbol. Works like the test
in Mathematica: Head[e] === Symbol.
Returns:
true, if the Expr is a symbol; false otherwise
integerQ
public boolean integerQ()
Tells whether the Expr represents a Mathematica integer.
Works like the Mathematica function IntegerQ.
Returns:
true, if the Expr is an integer; false otherwise
realQ
public boolean realQ()
Tells whether the Expr represents a real (floating-point) number. Will be false if it
is an integer. Works like the test in Mathematica: Head[e] === Real.
Returns:
true, if the Expr is a non-integer real number; false otherwise
rationalQ
public boolean rationalQ()
Tells whether the Expr represents a rational number. Will be false if it
is an integer. Works like the test in Mathematica: Head[e] === Rational.
Returns:
true, if the Expr is a non-integer rational number; false otherwise
complexQ
public boolean complexQ()
Tells whether the Expr represents a complex number. Will be false if it
is an integer or real. Works like the test in Mathematica: Head[e] === Complex.
Returns:
true, if the Expr is a complex number; false otherwise
numberQ
public boolean numberQ()
Tells whether the Expr represents a number (real, integer, rational, or complex).
Works like the Mathematica function NumberQ.
Returns:
true, if the Expr is a number type; false otherwise
bigIntegerQ
public boolean bigIntegerQ()
Tells whether the Expr represents a Mathematica integer, but requires more digits to store than can fit into a Java int.
Returns:
true, if the Expr is a big integer; false otherwise
bigDecimalQ
public boolean bigDecimalQ()
Tells whether the Expr represents a Mathematica real (floating-point) number, but requires more digits
to store than can fit into a Java double.
Returns:
true, if the Expr is a "bigfloat" number; false otherwise
trueQ
public boolean trueQ()
Tells whether the Expr represents the Mathematica symbol True.
Works like the Mathematica function TrueQ.
Returns:
true, if the Expr is the symbol True; false otherwise
listQ
public boolean listQ()
Tells whether the Expr represents a Mathematica list (that is, it has head List).
Works like the Mathematica function ListQ.
Returns:
true, if the Expr has head List; false otherwise
vectorQ
public boolean vectorQ()
Tells whether the Expr represents a Mathematica vector (that is, it has head List,
and no parts are themselves lists). Works like the Mathematica function VectorQ.
Returns:
true, if the Expr is a vector; false otherwise
vectorQ
public boolean vectorQ(int eType)
Tells whether the Expr represents a Mathematica vector, every element of which is
of the specified type. Works like the Mathematica function VectorQ.
Parameters:
eType - an integer constant representing the queried type. Will be one of INTEGER, REAL, STRING,
SYMBOL, RATIONAL, COMPLEX.
Returns:
true, if the Expr is a vector with elements of the specified type; false otherwise
matrixQ
public boolean matrixQ()
Tells whether the Expr represents a Mathematica matrix (that is, it has head List,
every element has head List, and no deeper parts are themselves lists). Works like
the Mathematica function MatrixQ.
Returns:
true, if the Expr is a matrix; false otherwise
matrixQ
public boolean matrixQ(int eType)
Tells whether the Expr represents a Mathematica matrix, every element of which is of
the specified type. Works like the Mathematica function MatrixQ.
Parameters:
eType - an integer constant representing the queried type. Will be one of INTEGER, REAL, STRING,
SYMBOL, RATIONAL, COMPLEX.
Returns:
true, if the Expr is a matrix with elements of the specified type; false otherwise
Gives the double value for Exprs that can be represented as doubles (this is exactly
the set for which integerQ() or realQ() or rationalQ() returns true).
Returns:
the double value
Throws:
ExprFormatException - if the Expr cannot be represented as a double (e.g., if it is a function)
Gives the string value for Exprs that can be represented as strings (this is exactly
the set for which stringQ() or symbolQ() returns true). Do not confuse this method
with toString(), which returns a string representation of any Expr in FullForm style.
Returns:
the string value
Throws:
ExprFormatException - if the Expr cannot be represented as a string (e.g., if it is a function)
Gives the BigInteger value for Exprs that can be represented as BigIntegers (this is exactly
the set for which integerQ() or realQ() returns true). The number will be truncated if it is a real.
Returns:
the BigInteger value
Throws:
ExprFormatException - if the Expr cannot be represented as a BigInteger (e.g., if it is a function)
public java.lang.Object asArray(int reqType,
int depth)
throws ExprFormatException
Gives a Java array representation with the requested depth and element type. The element
type must be either INTEGER or REAL, and the current maximum depth is 2. It will throw a
MathLinkException if this Expr does not represent a rectangular array of the specified depth,
if the elements of the Expr are not of the specified type, or if the heads are not "List" at every level.
try {
int[][] a = (int[][]) e.asArray(Expr.INTEGER, 2);
// ... now work with a
} catch (ExprFormatException exc) {
// e was not a depth-2 array of integers
}
Parameters:
reqType - an integer constant representing the requested type
depth - the depth (number of dimensions) of the returned array. Currently the max is 2.
Returns:
the array value
Throws:
ExprFormatException - if the Expr cannot be represented as an array of the
desired type (e.g., if it is an integer, or if it is not the right shape)
java.lang.IllegalArgumentException - if type is not INTEGER or REAL, or depth > 2
Not intended for general use. To write an Expr on a link, use the MathLink put(Expr)
method. This method is only public because developers of MathLink implementations
must call it inside their put(Object) methods if the object's type is Expr.
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 Mathematica function Take.
Parameters:
n - the number of elements to take from the beginning (or end if n is negative).
Returns:
the shortened Expr.
Throws:
java.lang.IllegalArgumentException - if n is beyond the bounds of the expression.
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 Mathematica function Delete.
Parameters:
n - the index of the element to delete (counted from the end if n is negative).
Returns:
the shortened Expr.
Throws:
java.lang.IllegalArgumentException - if n is beyond the bounds of the expression.
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 Mathematica function Insert.
Parameters:
e - the element to insert.
n - the index at which to perform the insertion (counted from the end if n is negative).
Returns:
the new Expr.
Throws:
java.lang.IllegalArgumentException - if n is beyond the bounds of the expression.