com.wolfram.jlink
Class Expr

java.lang.Object
  extended by com.wolfram.jlink.Expr
All Implemented Interfaces:
java.io.Serializable

public final class Expr
extends java.lang.Object
implements java.io.Serializable

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.

See Also:
Serialized Form

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

INTEGER

public static final int INTEGER
A type constant representing integers, for use in an Expr constructor, vectorQ(type), or matrixQ(type).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

REAL

public static final int REAL
A type constant representing real numbers, for use in an Expr constructor, vectorQ(type), or matrixQ(type).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

STRING

public static final int STRING
A type constant representing strings, for use in an Expr constructor, vectorQ(type), or matrixQ(type).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

SYMBOL

public static final int SYMBOL
A type constant representing symbols, for use in an Expr constructor, vectorQ(type), or matrixQ(type).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

RATIONAL

public static final int RATIONAL
A type constant representing rational numbers, for use in an Expr constructor, vectorQ(type), or matrixQ(type).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

COMPLEX

public static final int COMPLEX
A type constant representing complex numbers, for use in an Expr constructor, vectorQ(type), or matrixQ(type).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

BIGINTEGER

public static final 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).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

BIGDECIMAL

public static final 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).

See Also:
Expr(int, String), vectorQ(int), matrixQ(int), Constant Field Values

SYM_SYMBOL

public static final Expr SYM_SYMBOL
Unused for now.


SYM_INTEGER

public static final Expr SYM_INTEGER
Unused for now.


SYM_REAL

public static final Expr SYM_REAL
Unused for now.


SYM_STRING

public static final Expr SYM_STRING
Unused for now.


SYM_RATIONAL

public static final Expr SYM_RATIONAL
Unused for now.


SYM_COMPLEX

public static final Expr SYM_COMPLEX
Unused for now.


SYM_LIST

public static final Expr SYM_LIST
Unused for now.


SYM_TRUE

public static final Expr SYM_TRUE
Unused for now.


SYM_FALSE

public static final Expr SYM_FALSE
Unused for now.


SYM_NULL

public static final Expr SYM_NULL
Unused for now.


INT_ONE

public static final Expr INT_ONE
Unused for now.


INT_ZERO

public static final Expr INT_ZERO
Unused for now.


INT_MINUSONE

public static final Expr INT_MINUSONE
Unused for now.

Constructor Detail

Expr

public 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").

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.

Parameters:
val -

Expr

public Expr(Expr head,
            Expr[] args)
Creates an Expr with the given head and arguments.

Parameters:
head - an Expr giving the head of this Expr
args - an array of Exprs giving the arguments of this Expr; pass null or an empty array for no arguments
Method Detail

createFromLink

public static Expr createFromLink(MathLink ml)
                           throws MathLinkException
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.

Parameters:
ml -
Returns:
the newly-created expr read off the link
Throws:
MathLinkException

equals

public boolean equals(java.lang.Object obj)
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.


head

public Expr head()
Gives a new Expr representing the head of this Expr. Works like the Mathematica function Head.

Returns:
the head

args

public Expr[] args()
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.

Returns:
the dimensions, as an array

part

public Expr part(int i)
Gives a new Expr representing the specified part of this Expr. Works like the Mathematica function Part.

Parameters:
i - the index of the desired part
Returns:
the specified part, as an Expr
Throws:
java.lang.IllegalArgumentException - if i is beyond the bounds of the expression

part

public Expr part(int[] ia)
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.

re

public double re()
          throws ExprFormatException
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
Throws:
ExprFormatException
See Also:
im()

im

public double im()
          throws ExprFormatException
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
See Also:
re()

toString

public java.lang.String toString()
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

asInt

public int asInt()
          throws ExprFormatException
Gives the integer value for Exprs that can be represented as integers (this is exactly the set for which integerQ() returns true).

Returns:
the integer value
Throws:
ExprFormatException - if the Expr cannot be represented as an integer (e.g., if it is a function)
See Also:
asDouble(), asString(), asArray(int, int)

asLong

public long asLong()
            throws ExprFormatException
Gives the long value for Exprs that can be represented as integers (this is exactly the set for which integerQ() returns true).

Returns:
the long value
Throws:
ExprFormatException - if the Expr cannot be represented as a long (e.g., if it is a function)
See Also:
asDouble(), asString(), asArray(int, int)

asDouble

public double asDouble()
                throws ExprFormatException
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)
See Also:
asInt(), asArray(int, int), asString()

asString

public java.lang.String asString()
                          throws ExprFormatException
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)
See Also:
toString(), asInt(), asDouble(), asArray(int, int)

asBigInteger

public java.math.BigInteger asBigInteger()
                                  throws ExprFormatException
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)
See Also:
asLong(), asDouble(), asBigDecimal()

asBigDecimal

public java.math.BigDecimal asBigDecimal()
                                  throws ExprFormatException
Gives the BigDecimal value for Exprs that can be represented as BigDecimals (this is exactly the set for which integerQ() or realQ() returns true).

Returns:
the BigDecimal value
Throws:
ExprFormatException - if the Expr cannot be represented as a BigDecimal (e.g., if it is a function)
See Also:
asLong(), asDouble(), asBigInteger()

asArray

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
See Also:
asInt(), asDouble(), asString()

put

public void put(MathLink ml)
         throws MathLinkException
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.

Parameters:
ml -
Throws:
MathLinkException

take

public 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). 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.

delete

public 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). 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.

insert

public 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). 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.


J/Link is Copyright (c) 1999-2008, Wolfram Research, Inc. All rights reserved.