|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.wolfram.jlink.Expr
public final class Expr
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.
| Field Summary | |
|---|---|
static int |
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). |
static Expr |
INT_MINUSONE
Unused for now. |
static Expr |
INT_ONE
Unused for now. |
static Expr |
INT_ZERO
Unused for now. |
static int |
INTEGER
A type constant representing integers, for use in an Expr constructor, vectorQ(type), or matrixQ(type). |
static int |
RATIONAL
A type constant representing rational numbers, for use in an Expr constructor, vectorQ(type), or matrixQ(type). |
static int |
REAL
A type constant representing real numbers, for use in an Expr constructor, vectorQ(type), or matrixQ(type). |
static int |
STRING
A type constant representing strings, for use in an Expr constructor, vectorQ(type), or matrixQ(type). |
static Expr |
SYM_COMPLEX
Unused for now. |
static Expr |
SYM_FALSE
Unused for now. |
static Expr |
SYM_INTEGER
Unused for now. |
static Expr |
SYM_LIST
Unused for now. |
static Expr |
SYM_NULL
Unused for now. |
static Expr |
SYM_RATIONAL
Unused for now. |
static Expr |
SYM_REAL
Unused for now. |
static Expr |
SYM_STRING
Unused for now. |
static Expr |
SYM_SYMBOL
Unused for now. |
static Expr |
SYM_TRUE
Unused for now. |
static int |
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. |
|
| Method Summary | |
|---|---|
Expr[] |
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. |
static Expr |
createFromLink(MathLink ml)
This factory method will only be used by advanced programmers who are creating their own classes that implement the MathLink interface. |
Expr |
delete(int n)
Returns a new Expr that has the same head but the nth element deleted (counted from the end if n is negative). |
int[] |
dimensions()
Gives an array of integers representing the dimensions of this Expr. |
void |
dispose()
Frees resources that the Expr uses internally. |
boolean |
equals(java.lang.Object obj)
Implements an equality comparison that is similar to Mathematica's SameQ. |
int |
hashCode()
|
Expr |
head()
Gives a new Expr representing the head of this Expr. |
double |
im()
Gives the imaginary part of an Expr that represents a complex number. |
Expr |
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). |
Expr |
part(int i)
Gives a new Expr representing the specified part of this Expr. |
Expr |
part(int[] ia)
Gives a new Expr representing the specified part of this Expr. |
void |
put(MathLink ml)
Not intended for general use. |
boolean |
rationalQ()
Tells whether the Expr represents a rational number. |
double |
re()
Gives the real part of an Expr that represents a complex number. |
boolean |
realQ()
Tells whether the Expr represents a real (floating-point) number. |
boolean |
stringQ()
Tells whether the Expr represents a Mathematica string. |
boolean |
symbolQ()
Tells whether the Expr represents a Mathematica symbol. |
Expr |
take(int n)
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). |
java.lang.String |
toString()
Gives a readable string representation. |
boolean |
trueQ()
Tells whether the Expr represents the Mathematica symbol True. |
boolean |
vectorQ()
Tells whether the Expr represents a Mathematica vector (that is, it has head List, and no parts are themselves lists). |
boolean |
vectorQ(int eType)
Tells whether the Expr represents a Mathematica vector, every element of which is of the specified type. |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int INTEGER
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int REAL
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int STRING
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int SYMBOL
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int RATIONAL
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int COMPLEX
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int BIGINTEGER
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final int BIGDECIMAL
Expr(int, String),
vectorQ(int),
matrixQ(int),
Constant Field Valuespublic static final Expr SYM_SYMBOL
public static final Expr SYM_INTEGER
public static final Expr SYM_REAL
public static final Expr SYM_STRING
public static final Expr SYM_RATIONAL
public static final Expr SYM_COMPLEX
public static final Expr SYM_LIST
public static final Expr SYM_TRUE
public static final Expr SYM_FALSE
public static final Expr SYM_NULL
public static final Expr INT_ONE
public static final Expr INT_ZERO
public static final Expr INT_MINUSONE
| Constructor Detail |
|---|
public Expr(int type,
java.lang.String val)
type - the type of the Expr; must be one of INTEGER, REAL, BIGINTEGER, BIGDECIMAL, STRING, or SYMBOLval - the value of the Expr, interpreted according to the type argument
java.lang.IllegalArgumentException - if an unsupported type is specifiedpublic Expr(long val)
val - public Expr(double val)
val - public Expr(java.lang.String val)
val - public Expr(int[] val)
val - public Expr(double[] val)
val - public Expr(int[][] val)
val - public Expr(double[][] val)
val - public Expr(java.math.BigInteger val)
val - public Expr(java.math.BigDecimal val)
val -
public Expr(Expr head,
Expr[] args)
head - an Expr giving the head of this Exprargs - an array of Exprs giving the arguments of this Expr; pass null or an empty array for no arguments| Method Detail |
|---|
public static Expr createFromLink(MathLink ml)
throws MathLinkException
ml -
MathLinkExceptionpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectobj - public int hashCode()
hashCode in class java.lang.Objectpublic void dispose()
public Expr head()
public Expr[] args()
public int length()
public int[] dimensions()
public Expr part(int i)
i - the index of the desired part
java.lang.IllegalArgumentException - if i is beyond the bounds of the expressionpublic Expr part(int[] ia)
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]].
ia - the index of the desired part
java.lang.IllegalArgumentException - if any of the part specifications are beyond the bounds of the expression.
public double re()
throws ExprFormatException
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.
ExprFormatExceptionim()
public double im()
throws ExprFormatException
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.
ExprFormatException - if this Expr is not an integer, real, rational, or complex numberre()public java.lang.String toString()
toString in class java.lang.Objectpublic boolean atomQ()
public boolean stringQ()
public boolean symbolQ()
public boolean integerQ()
public boolean realQ()
public boolean rationalQ()
public boolean complexQ()
public boolean numberQ()
public boolean bigIntegerQ()
public boolean bigDecimalQ()
public boolean trueQ()
public boolean listQ()
public boolean vectorQ()
public boolean vectorQ(int eType)
eType - an integer constant representing the queried type. Will be one of INTEGER, REAL, STRING,
SYMBOL, RATIONAL, COMPLEX.
public boolean matrixQ()
public boolean matrixQ(int eType)
eType - an integer constant representing the queried type. Will be one of INTEGER, REAL, STRING,
SYMBOL, RATIONAL, COMPLEX.
public int asInt()
throws ExprFormatException
ExprFormatException - if the Expr cannot be represented as an integer (e.g., if it is a function)asDouble(),
asString(),
asArray(int, int)
public long asLong()
throws ExprFormatException
ExprFormatException - if the Expr cannot be represented as a long (e.g., if it is a function)asDouble(),
asString(),
asArray(int, int)
public double asDouble()
throws ExprFormatException
ExprFormatException - if the Expr cannot be represented as a double (e.g., if it is a function)asInt(),
asArray(int, int),
asString()
public java.lang.String asString()
throws ExprFormatException
ExprFormatException - if the Expr cannot be represented as a string (e.g., if it is a function)toString(),
asInt(),
asDouble(),
asArray(int, int)
public java.math.BigInteger asBigInteger()
throws ExprFormatException
ExprFormatException - if the Expr cannot be represented as a BigInteger (e.g., if it is a function)asLong(),
asDouble(),
asBigDecimal()
public java.math.BigDecimal asBigDecimal()
throws ExprFormatException
ExprFormatException - if the Expr cannot be represented as a BigDecimal (e.g., if it is a function)asLong(),
asDouble(),
asBigInteger()
public java.lang.Object asArray(int reqType,
int depth)
throws ExprFormatException
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
}
reqType - an integer constant representing the requested typedepth - the depth (number of dimensions) of the returned array. Currently the max is 2.
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 > 2asInt(),
asDouble(),
asString()
public void put(MathLink ml)
throws MathLinkException
ml -
MathLinkExceptionpublic Expr take(int n)
n - the number of elements to take from the beginning (or end if n is negative).
java.lang.IllegalArgumentException - if n is beyond the bounds of the expression.public Expr delete(int n)
n - the index of the element to delete (counted from the end if n is negative).
java.lang.IllegalArgumentException - if n is beyond the bounds of the expression.
public Expr insert(Expr e,
int n)
e - the element to insert.n - the index at which to perform the insertion (counted from the end if n is negative).
java.lang.IllegalArgumentException - if n is beyond the bounds of the expression.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||