|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
public interface MathLink
MathLink is the low-level interface that is the root of all link objects in J/Link. The methods in MathLink correspond roughly to a subset of those in the C-language MathLink API. Most programmers will deal instead with objects of type KernelLink, a higher-level interface that extends MathLink and incorporates the assumption that the program on the other side of the link is a Mathematica kernel.
You create objects of type MathLink via the MathLinkFactory.createLink() method. Again, though, most programmers will use KernelLink instead of MathLink.
Most MathLink methods throw a MathLinkException if a link-related error occurs. Examples would be calling endPacket() before sending a complete expression, or calling getFunction() when an integer is waiting on the link.
For additional information about these methods, see the J/Link User Guide, and also the MathLink documentation in the Mathematica book. Most of these methods are substantially similar, if not identical, to their C counterparts as documented in the book.
KernelLink,
MathLinkFactory| Field Summary | |
|---|---|
static int |
BEGINDLGPKT
Constant returned by nextPacket. |
static int |
CALLPKT
Constant returned by nextPacket. |
static int |
DISPLAYENDPKT
Constant returned by nextPacket. |
static int |
DISPLAYPKT
Constant returned by nextPacket. |
static int |
ENDDLGPKT
Constant returned by nextPacket. |
static int |
ENTEREXPRPKT
Constant returned by nextPacket. |
static int |
ENTERTEXTPKT
Constant returned by nextPacket. |
static int |
EVALUATEPKT
Constant returned by nextPacket. |
static int |
EXPRESSIONPKT
|
static int |
FEPKT
|
static int |
FIRSTUSERPKT
|
static int |
ILLEGALPKT
|
static int |
INPUTNAMEPKT
Constant returned by nextPacket. |
static int |
INPUTPKT
Constant returned by nextPacket. |
static int |
INPUTSTRPKT
Constant returned by nextPacket. |
static int |
LASTUSERPKT
|
static int |
MENUPKT
Constant returned by nextPacket. |
static int |
MESSAGEPKT
Constant returned by nextPacket. |
static int |
MLABORTMESSAGE
Used in putMessage() to cause the current Mathematica evaluation to be aborted. |
static int |
MLE_ARRAY_TOO_SHALLOW
|
static int |
MLE_BAD_COMPLEX
|
static int |
MLE_CONNECT_TIMEOUT
|
static int |
MLE_CREATION_FAILED
|
static int |
MLE_FIRST_USER_EXCEPTION
|
static int |
MLE_LINK_IS_NULL
|
static int |
MLE_NON_ML_ERROR
|
static int |
MLE_OUT_OF_MEMORY
|
static int |
MLE_WRAPPED_EXCEPTION
|
static int |
MLEOK
|
static int |
MLEUSER
|
static int |
MLINTERRUPTMESSAGE
|
static int |
MLTERMINATEMESSAGE
|
static int |
MLTKERR
|
static int |
MLTKFUNC
Constant for use in putNext() or returned by getNext() and getType(). |
static int |
MLTKINT
Constant for use in putNext() or returned by getNext() and getType(). |
static int |
MLTKREAL
Constant for use in putNext() or returned by getNext() and getType(). |
static int |
MLTKSTR
Constant for use in putNext() or returned by getNext() and getType(). |
static int |
MLTKSYM
Constant for use in putNext() or returned by getNext() and getType(). |
static int |
OUTPUTNAMEPKT
Constant returned by nextPacket. |
static int |
RESUMEPKT
Constant returned by nextPacket. |
static int |
RETURNEXPRPKT
Constant returned by nextPacket. |
static int |
RETURNPKT
Constant returned by nextPacket. |
static int |
RETURNTEXTPKT
Constant returned by nextPacket. |
static int |
SUSPENDPKT
Constant returned by nextPacket. |
static int |
SYNTAXPKT
Constant returned by nextPacket. |
static int |
TEXTPKT
Constant returned by nextPacket. |
static int |
TYPE_BIGDECIMAL
Constant for use in getArray(). |
static int |
TYPE_BIGINTEGER
Constant for use in getArray(). |
static int |
TYPE_BOOLEAN
Constant for use in getArray(). |
static int |
TYPE_BYTE
Constant for use in getArray(). |
static int |
TYPE_CHAR
Constant for use in getArray(). |
static int |
TYPE_COMPLEX
Constant for use in getArray(). |
static int |
TYPE_DOUBLE
Constant for use in getArray(). |
static int |
TYPE_EXPR
Constant for use in getArray(). |
static int |
TYPE_FLOAT
Constant for use in getArray(). |
static int |
TYPE_INT
Constant for use in getArray(). |
static int |
TYPE_LONG
Constant for use in getArray(). |
static int |
TYPE_SHORT
Constant for use in getArray(). |
static int |
TYPE_STRING
Constant for use in getArray(). |
| Method Summary | |
|---|---|
void |
activate()
Same as connect(). |
boolean |
addMessageHandler(java.lang.Class cls,
java.lang.Object obj,
java.lang.String methName)
Specifies the Java method you want called as a message handler. |
int |
bytesToGet()
Returns the number of bytes that remain to be read in the element that is currently being read in textual form. |
int |
bytesToPut()
Gives the number of bytes that remain to be sent in the element that is currently being sent in textual form. |
int |
checkFunction(java.lang.String f)
Reads a function name and argument count and compares the function name with f. |
void |
checkFunctionWithArgCount(java.lang.String f,
int argCount)
Reads a function name and argument count and compares the function name with f and the argument count with argCount. |
boolean |
clearError()
Clears the link error condition, if possible. |
void |
close()
Closes the link. |
void |
connect()
Connects the link, if it has not already been connected. |
void |
connect(long timeoutMillis)
Connects the link, but waits at most timeoutMillis for a response from the other side. |
long |
createMark()
Creates a mark at the current point in the incoming MathLink data stream. |
void |
destroyMark(long mark)
Destroys a mark. |
void |
endPacket()
Call it when you are finished writing the contents of a single packet. |
int |
error()
Gives the code corresponding to the current error state of the link. |
java.lang.String |
errorMessage()
Gives a textual message describing the current error. |
void |
flush()
Immediately transmits any data buffered for sending over the link. |
int |
getArgCount()
Reads the argument count of an expression being read manually. |
java.lang.Object |
getArray(int type,
int depth)
Reads an array of the specified type and depth. |
java.lang.Object |
getArray(int type,
int depth,
java.lang.String[] heads)
Reads an array of the specified type and depth and records the heads at each level. |
boolean |
getBoolean()
Reads the Mathematica symbols True or False as a boolean. |
boolean[] |
getBooleanArray1()
Reads a list as a one-dimensional array of booleans. |
boolean[][] |
getBooleanArray2()
Reads an array as a two-dimensional array of booleans. |
byte[] |
getByteArray1()
Reads a list or array as a one-dimensional array of bytes. |
byte[][] |
getByteArray2()
Reads an array as a two-dimensional array of bytes. |
byte[] |
getByteString(int missing)
Reads a Mathematica string as an array of bytes. |
char[] |
getCharArray1()
Reads a list or array as a one-dimensional array of chars. |
char[][] |
getCharArray2()
Reads an array as a two-dimensional array of chars. |
java.lang.Object |
getComplex()
Reads a complex number. |
java.lang.Object[] |
getComplexArray1()
Reads a list as a one-dimensional array of complex numbers. |
java.lang.Object[][] |
getComplexArray2()
Reads a matrix as a two-dimensional array of complex numbers. |
java.lang.Class |
getComplexClass()
Gives the Java class that you have specified to map to Mathematica's Complex numbers. |
byte[] |
getData(int len)
Gets a specified number of bytes in the textual form of the expression currently being read. |
double |
getDouble()
Reads a Mathematica real number or integer as a double. |
double[] |
getDoubleArray1()
Reads a list or array as a one-dimensional array of doubles. |
double[][] |
getDoubleArray2()
Reads an array as a two-dimensional array of doubles. |
Expr |
getExpr()
Reads an expression from the link and creates an Expr from it. |
float[] |
getFloatArray1()
Reads a list or array as a one-dimensional array of floats. |
float[][] |
getFloatArray2()
Reads an array as a two-dimensional array of floats. |
MLFunction |
getFunction()
Reads a function name and argument count. |
int[] |
getIntArray1()
Reads a list or array as a one-dimensional array of ints. |
int[][] |
getIntArray2()
Reads an array as a two-dimensional array of ints. |
int |
getInteger()
Reads a Mathematica integer as an int. |
long[] |
getLongArray1()
Reads a list as a one-dimensional array of longs. |
long[][] |
getLongArray2()
Reads an array as a two-dimensional array of longs. |
long |
getLongInteger()
Reads a Mathematica integer as a long. |
int |
getMessage()
Deprecated. |
int |
getNext()
Gives the type of the next element in the expression currently being read. |
short[] |
getShortArray1()
Reads a list or array as a one-dimensional array of shorts. |
short[][] |
getShortArray2()
Reads an array as a two-dimensional array of shorts. |
java.lang.String |
getString()
Reads a Mathematica character string. |
java.lang.String[] |
getStringArray1()
Reads a list as a one-dimensional array of strings. |
java.lang.String[][] |
getStringArray2()
Reads an array as a two-dimensional array of strings. |
java.lang.String |
getSymbol()
Reads a Mathematica symbol as a string. |
int |
getType()
Gives the type of the current element in the expression currently being read. |
boolean |
messageReady()
Deprecated. |
java.lang.String |
name()
Gives the name of the link. |
void |
newPacket()
Discards the current packet, if it has been partially read. |
int |
nextPacket()
"Opens" the next packet arriving on the link. |
Expr |
peekExpr()
Creates an Expr from the current expression, but does not drain it off the link. |
void |
put(boolean b)
Sends the boolean value as the Mathematica symbol True or False. |
void |
put(double d)
Sends a double value. |
void |
put(int i)
Sends an integer value. |
void |
put(long i)
Sends a long value. |
void |
put(java.lang.Object obj)
Sends an object, including strings and arrays. |
void |
put(java.lang.Object obj,
java.lang.String[] heads)
Sends an array object. |
void |
putArgCount(int argCount)
Specifies the argument count for a composite expression being sent manually. |
void |
putByteString(byte[] data)
Sends an array of bytes to Mathematica as a string. |
void |
putData(byte[] data)
Used for sending elements in textual form. |
void |
putData(byte[] data,
int len)
An alternative form of putData() that lets you specify how many bytes from the array should be sent, rather than just sending the whole thing. |
void |
putFunction(java.lang.String f,
int argCount)
Sends a function name and argument count. |
void |
putMessage(int msg)
Sends a low-level MathLink message. |
void |
putNext(int type)
Identifies the type of data element that is to be sent. |
void |
putSize(int size)
Specifies the size in bytes of an element being sent in textual form. |
void |
putSymbol(java.lang.String s)
Sends a symbol name. |
boolean |
ready()
Indicates whether the link has data waiting to be read. |
boolean |
removeMessageHandler(java.lang.String methName)
Removes a message handler you previously set up with addMessageHandler. |
void |
seekMark(long mark)
Resets the current position in the incoming MathLink data stream to an earlier point. |
boolean |
setComplexClass(java.lang.Class cls)
Specifies the class you want to map to Mathematica's Complex numbers. |
void |
setError(int err)
Sets the link's error state to the specified value. |
boolean |
setYieldFunction(java.lang.Class cls,
java.lang.Object obj,
java.lang.String methName)
Sets the Java method you want called as a yield function. |
void |
transferExpression(MathLink source)
Writes a complete expression from the link source to this link. |
void |
transferToEndOfLoopbackLink(LoopbackLink source)
Writes the entire contents of the LoopbackLink source to this link. |
| Field Detail |
|---|
static final int ILLEGALPKT
static final int CALLPKT
static final int EVALUATEPKT
static final int RETURNPKT
static final int INPUTNAMEPKT
static final int ENTERTEXTPKT
static final int ENTEREXPRPKT
static final int OUTPUTNAMEPKT
static final int RETURNTEXTPKT
static final int RETURNEXPRPKT
static final int DISPLAYPKT
static final int DISPLAYENDPKT
static final int MESSAGEPKT
static final int TEXTPKT
static final int INPUTPKT
static final int INPUTSTRPKT
static final int MENUPKT
static final int SYNTAXPKT
static final int SUSPENDPKT
static final int RESUMEPKT
static final int BEGINDLGPKT
static final int ENDDLGPKT
static final int FIRSTUSERPKT
static final int LASTUSERPKT
static final int FEPKT
static final int EXPRESSIONPKT
static final int MLTERMINATEMESSAGE
static final int MLINTERRUPTMESSAGE
static final int MLABORTMESSAGE
static final int MLTKFUNC
static final int MLTKSTR
static final int MLTKSYM
static final int MLTKREAL
static final int MLTKINT
static final int MLTKERR
static final int MLEOK
static final int MLEUSER
static final int MLE_NON_ML_ERROR
static final int MLE_LINK_IS_NULL
static final int MLE_OUT_OF_MEMORY
static final int MLE_ARRAY_TOO_SHALLOW
static final int MLE_BAD_COMPLEX
static final int MLE_CREATION_FAILED
static final int MLE_CONNECT_TIMEOUT
static final int MLE_WRAPPED_EXCEPTION
static final int MLE_FIRST_USER_EXCEPTION
static final int TYPE_BOOLEAN
static final int TYPE_BYTE
static final int TYPE_CHAR
static final int TYPE_SHORT
static final int TYPE_INT
static final int TYPE_LONG
static final int TYPE_FLOAT
static final int TYPE_DOUBLE
static final int TYPE_STRING
static final int TYPE_BIGINTEGER
static final int TYPE_BIGDECIMAL
static final int TYPE_EXPR
static final int TYPE_COMPLEX
| Method Detail |
|---|
void close()
void connect()
throws MathLinkException
All the methods that read from the link will connect it if necessary. The connect() method lets you deliberately control the point in the program where the connection occurs, without having to read anything.
MathLinkException
void connect(long timeoutMillis)
throws MathLinkException
timeoutMillis - the number of milliseconds to wait
MathLinkExceptionconnect()
void activate()
throws MathLinkException
MathLinkExceptionconnect()
java.lang.String name()
throws MathLinkException
MathLinkExceptionvoid newPacket()
This is a useful cleanup function. You can call it when you are finished examining the contents of a packet that was opened with nextPacket() or waitForAnswer(), whether you have read the entire packet contents or not. You can be sure that the link is then in a state where you are ready to read the next packet.
It is also frequently used in a catch block for a MathLinkException, to clear off any unread data in a packet before returning to the normal program flow.
nextPacket()
int nextPacket()
throws MathLinkException
Most programmers will use this method rarely, if ever. J/Link provides higher-level functions in the KernelLink interface that hide these low-level details of the packet loop.
MathLinkExceptionnewPacket()
void endPacket()
throws MathLinkException
Calling endPacket() is not strictly necessary, but it is good style, and it allows J/Link to immediately generate a MathLinkException if you are not actually finished with writing the data you promised to send.
MathLinkExceptionint error()
errorMessage(),
clearError()boolean clearError()
java.lang.String errorMessage()
error()void setError(int err)
err - the error code
boolean ready()
throws MathLinkException
MathLinkException
void flush()
throws MathLinkException
Any calls that read from the link will flush it, so you only need to call flush() manually if you want to make sure data is sent right away even though you are not reading from the link immediately. Calls to ready() will not flush the link, so if you are sending something and then polling ready() waiting for the result to arrive (as opposed to just calling nextPacket() or waitForAnswer()), you must call flush to ensure that the data is sent.
MathLinkException
int getNext()
throws MathLinkException
To check the type of a partially read element without advancing to the next element, use getType().
MathLinkException
int getType()
throws MathLinkException
Unlike getNext(), getType() will not advance to the next element if the current element has only been partially read.
MathLinkExceptiongetNext()
void putNext(int type)
throws MathLinkException
putNext() is rarely needed. The two most likely uses are to put expressions whose heads are not mere symbols (e.g., Derivative[2][f]) or to put data in textual form. Calls to putNext() must be followed by putSize() and putData(), or by putArgCount() for the MLTKFUNC type. Here is how you could send Derivative[2][f]:
ml.putNext(MathLink.MLTKFUNC); // The func we are putting has head Derivative[2], arg f
ml.putArgCount(1); // this 1 is for the 'f'
ml.putNext(MathLink.MLTKFUNC); // The func we are putting has head Derivative, arg 2
ml.putArgCount(1); // this 1 is for the '2'
ml.putSymbol("Derivative");
ml.put(2);
ml.putSymbol("f");
type - one of MLTKINT, MLTKREAL, MLTKSTR, MLTKSYM, MLTKFUNC
MathLinkExceptionputSize(int),
putData(byte[]),
putArgCount(int)
int getArgCount()
throws MathLinkException
This method can be used after getNext() or getType() returns the value MLTKFUNC. The argument count is always followed by the head of the expression. The head is followed by the arguments; the argument count tells how many there will be.
MathLinkExceptiongetNext(),
getType()
void putArgCount(int argCount)
throws MathLinkException
Use it after a call to putNext() with the MLTKFUNC type.
argCount - the number of aruments in the expression being sent
MathLinkExceptionputNext(int)
void putSize(int size)
throws MathLinkException
A typical sequence would be putNext(), followed by putSize(), then putData().
size - the size of the data, in bytes, that will be written
with the following putData()
MathLinkExceptionputData(byte[]),
putNext(int)
int bytesToPut()
throws MathLinkException
After you have called putSize(), the link knows how many bytes you have promised to send. This method lets you determine how many you still need to send, in the unlikely event that you lose track after a series of putData() calls.
MathLinkExceptionputSize(int),
putData(byte[])
int bytesToGet()
throws MathLinkException
Lets you keep track of your progress reading an element through a series of getData() calls.
MathLinkException
MathLinkExceptiongetData(int)
void putData(byte[] data)
throws MathLinkException
The so-called "textual" means of sending data is rarely used. Its main use is to allow a very large string to be sent, where the string data is not held in a single String object. The most important use of this technique in the C-language MathLink API was to send integers and reals that were too large to fit into an int or double. This use is unnecessary in J/Link, since Java has BigInteger and BigDecimal classes, and these objects can be sent directly with put().
data - the textual representation of the data
MathLinkExceptionputNext(int),
putSize(int)
void putData(byte[] data,
int len)
throws MathLinkException
data - len -
MathLinkExceptionputData(byte[])
byte[] getData(int len)
throws MathLinkException
You can use bytesToGet() to determine if more getData() calls are needed to completely read the element.
len - the maximum number of bytes to read
MathLinkExceptionbytesToGet(),
getNext(),
getType()
java.lang.String getString()
throws MathLinkException
Because both Java and Mathematica strings are in Unicode, the read string is an exact match to its Mathematica representation.
MathLinkException
byte[] getByteString(int missing)
throws MathLinkException
In contrast with getString(), this method strips the incoming (16-bit Unicode) character
data into single-byte representation. Characters that cannot be represented faithfully
in single-byte form are replaced by the byte specified by the missing
parameter. This method is primarily useful if you know the incoming data contains only
ASCII characters and you want the data in the form of a byte array.
missing - the byte to replace non-ASCII characters with
MathLinkExceptiongetString()
void putByteString(byte[] data)
throws MathLinkException
Use this instead of put() if you have string data in the form of a byte array. In the C-language MathLink API, MLPutByteString was useful because the more obviously-named MLPutString required a specially-encoded string. Because Java strings and Mathematica strings are both Unicode, put() works in J/Link without requiring special encoding. Thus, putByteString() has little use.
data - the string data to send
MathLinkExceptionput(Object)
java.lang.String getSymbol()
throws MathLinkException
Because Java strings and Mathematica symbols are in Unicode, the read string is an exact match to its Mathematica representation.
MathLinkExceptiongetString()
void putSymbol(java.lang.String s)
throws MathLinkException
Both Java strings and Mathematica symbols are in Unicode, so you can send symbols with the full Unicode character set.
s - the symbol name
MathLinkException
boolean getBoolean()
throws MathLinkException
MathLinkException
void put(boolean b)
throws MathLinkException
b - the value to send
MathLinkException
int getInteger()
throws MathLinkException
MathLinkException
void put(int i)
throws MathLinkException
i - the int to send
MathLinkException
long getLongInteger()
throws MathLinkException
MathLinkException
void put(long i)
throws MathLinkException
i - the value to send
MathLinkException
double getDouble()
throws MathLinkException
MathLinkException
void put(double d)
throws MathLinkException
d - the double to send
MathLinkException
boolean[] getBooleanArray1()
throws MathLinkException
The expression being read must be a list or other depth-1 expression of the symbols True and False. The "1" suffix in the method name indicates that the returned array has depth 1.
The expression does not need to have head List. In other words, it could be List[False, True] or Foo[True, True]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a depth-1 array of True and FalsegetArray(int, int)
boolean[][] getBooleanArray2()
throws MathLinkException
The expression being read must be a matrix of the symbols True and False. The "2" suffix in the method name indicates that the returned array has depth 2.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[True, False], List[True, True]] or Foo[Bar[True, False], Bar[True, True]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a 2-deep array of True and FalsegetArray(int, int)
byte[] getByteArray1()
throws MathLinkException
The expression being read must be a list or deeper array of integers. Values outside the range of a byte are converted via casting. The "1" suffix in the method name indicates that the returned array has depth 1. If the arriving expression has depth greater than 1 (e.g., it is a matrix), it will be flattened to a 1-dimensional array.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
byte[][] getByteArray2()
throws MathLinkException
The expression being read must be a matrix or deeper array of integers. Values outside the range of a byte are converted via casting. The "2" suffix in the method name indicates that the returned array has depth 2. If the arriving expression has depth greater than 2, it will be flattened to a 2-dimensional array. If the arriving expression has a depth less than 2 (i.e., it is a flat list), a MathLinkException will be thrown.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
char[] getCharArray1()
throws MathLinkException
The expression being read must be a list or deeper array of integers. Values outside the range of a char are converted via casting. The "1" suffix in the method name indicates that the returned array has depth 1. If the arriving expression has depth greater than 1 (e.g., it is a matrix), it will be flattened to a 1-dimensional array.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
char[][] getCharArray2()
throws MathLinkException
The expression being read must be a matrix or deeper array of integers. Values outside the range of a char are converted via casting. The "2" suffix in the method name indicates that the returned array has depth 2. If the arriving expression has depth greater than 2, it will be flattened to a 2-dimensional array. If the arriving expression has a depth less than 2 (i.e., it is a flat list), a MathLinkException will be thrown.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
short[] getShortArray1()
throws MathLinkException
The expression being read must be a list or deeper array of integers. Values outside the range of a short are converted via casting. The "1" suffix in the method name indicates that the returned array has depth 1. If the arriving expression has depth greater than 1 (e.g., it is a matrix), it will be flattened to a 1-dimensional array.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
short[][] getShortArray2()
throws MathLinkException
The expression being read must be a matrix or deeper array of integers. Values outside the range of a short are converted via casting. The "2" suffix in the method name indicates that the returned array has depth 2. If the arriving expression has depth greater than 2, it will be flattened to a 2-dimensional array. If the arriving expression has a depth less than 2 (i.e., it is a flat list), a MathLinkException will be thrown.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
int[] getIntArray1()
throws MathLinkException
The expression being read must be a list or deeper array of integers. The "1" suffix in the method name indicates that the returned array has depth 1. If the arriving expression has depth greater than 1 (e.g., it is a matrix), it will be flattened to a 1-dimensional array.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
int[][] getIntArray2()
throws MathLinkException
The expression being read must be a matrix or deeper array of integers. The "2" suffix in the method name indicates that the returned array has depth 2. If the arriving expression has depth greater than 2, it will be flattened to a 2-dimensional array. If the arriving expression has a depth less than 2 (i.e., it is a flat list), a MathLinkException will be thrown.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of integersgetArray(int, int)
long[] getLongArray1()
throws MathLinkException
The expression being read must be a list or other depth-1 expression of integers. The "1" suffix in the method name indicates that the returned array has depth 1.
The expression does not need to have head List. In other words, it could be List[1, 2] or Foo[1, 2]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a depth-1 array of integersgetArray(int, int)
long[][] getLongArray2()
throws MathLinkException
The expression being read must be a matrix of integers. The "2" suffix in the method name indicates that the returned array has depth 2.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1, 2], List[3, 4]] or Foo[Bar[1, 2], Bar[3, 4]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a depth-2 array of integersgetArray(int, int)
float[] getFloatArray1()
throws MathLinkException
The expression being read must be a list or deeper array of real numbers or integers. The "1" suffix in the method name indicates that the returned array has depth 1. If the arriving expression has depth greater than 1 (e.g., it is a matrix), it will be flattened to a 1-dimensional array.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1., 2.], List[3., 4.]] or Foo[Bar[1., 2.], Bar[3., 4.]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of reals or integersgetArray(int, int)
float[][] getFloatArray2()
throws MathLinkException
The expression being read must be a matrix or deeper array of real numbers or integers. The "2" suffix in the method name indicates that the returned array has depth 2. If the arriving expression has depth greater than 2, it will be flattened to a 2-dimensional array. If the arriving expression has a depth less than 2 (i.e., it is a flat list), a MathLinkException will be thrown.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1., 2.], List[3., 4.]] or Foo[Bar[1., 2.], Bar[3., 4.]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an of array reals or integersgetArray(int, int)
double[] getDoubleArray1()
throws MathLinkException
The expression being read must be a list or deeper array of real numbers or integers. The "1" suffix in the method name indicates that the returned array has depth 1. If the arriving expression has depth greater than 1 (e.g., it is a matrix), it will be flattened to a 1-dimensional array.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1., 2.], List[3., 4.]] or Foo[Bar[1., 2.], Bar[3., 4.]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of reals or integersgetArray(int, int)
double[][] getDoubleArray2()
throws MathLinkException
The expression being read must be a matrix or deeper array of real numbers or integers. The "2" suffix in the method name indicates that the returned array has depth 2. If the arriving expression has depth greater than 2, it will be flattened to a 2-dimensional array. If the arriving expression has a depth less than 2 (i.e., it is a flat list), a MathLinkException will be thrown.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1., 2.], List[3., 4.]] or Foo[Bar[1., 2.], Bar[3., 4.]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not an array of reals or integersgetArray(int, int)
java.lang.String[] getStringArray1()
throws MathLinkException
The expression being read must be a list or other depth-1 expression of strings or symbols. The "1" suffix in the method name indicates that the returned array has depth 1.
The expression does not need to have head List. In other words, it could be List["A", "B"] or Foo["A", "B"]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a depth-1 array of strings or symbolsgetArray(int, int)
java.lang.String[][] getStringArray2()
throws MathLinkException
The expression being read must be a matrix of strings or symbols. The "2" suffix in the method name indicates that the returned array has depth 2.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List["A", "B"], List["C", D"]] or Foo[Bar["A", "B"], Bar["C", "D"]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a matrix of strings or symbolsgetArray(int, int)
java.lang.Object[] getComplexArray1()
throws MathLinkException
The expression being read must be a list or other depth-1 expression of Complex. The "1" suffix in the method name indicates that the returned array has depth 1.
This method will read the expression, but return null, if no Java class has yet been established to use for complex numbers by calling setComplexClass.
The expression does not need to have head List. In other words, it could be List[1+I, 2-I] or Foo[1+I, 2-I]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a depth-1 array of ComplexsetComplexClass(Class),
getArray(int, int)
java.lang.Object[][] getComplexArray2()
throws MathLinkException
The expression being read must be a matrix of Complex. The "2" suffix in the method name indicates that the returned array has depth 2.
The expression does not need to have head List. It can have any heads, at any depth. In other words, it could be List[List[1+I, 2-I], List[3+I, 4-I]] or Foo[Bar[1+I, 2-I], Bar[3+I, 4-I]]. The information about the heads is lost; if you need this information you will need to either use getArray(int, int, String[]) or read the expression as an Expr and examine it using the Expr methods.
MathLinkException - if the incoming expression is not a matrix of ComplexsetComplexClass(Class),
getArray(int, int)
java.lang.Object getArray(int type,
int depth)
throws MathLinkException
The getXXXArrayN methods are all just convenience methods that call this method. Use this method instead when you want to read an array of dimensionality deeper than 2. Cast the result of this method to the desired array type, as in:
int[][][] result = (int[][][]) ml.getArray(TYPE_INT, 3);
For the types TYPE_BYTE, TYPE_CHAR, TYPE_SHORT, TYPE_INT, TYPE_FLOAT, and TYPE_DOUBLE,
if the expression has depth greater than depth, it will be flattened to a
Java array of dimension depth. If the expression has a depth less than
depth, a MathLinkException will be thrown.
The maximum depth supported is 5.
The expression does not need to have head List. It can have any heads, at any depth, and the heads do not have to agree at each depth, meaning that you could read the expression {foo[1, 2], bar[3, 4]} as an int[][]. The information about the heads is lost; if you need this information you can use the alternative signature getArray(int type, int depth, String[] heads), or read the expression as an Expr and examine it using the Expr methods.
type - The desired type for the returned array. Must be one of TYPE_BOOLEAN, TYPE_BYTE,
TYPE_CHAR, TYPE_SHORT, TYPE_INT, TYPE_LONG, TYPE_FLOAT, TYPE_DOUBLE, TYPE_STRING,
TYPE_BIGINTEGER, TYPE_BIGDECIMAL, TYPE_EXPR, or TYPE_COMPLEX.depth - the depth of the returned array
MathLinkException - if the incoming expression is not an array of the requested type
or is not as deep as requested
MathLinkExceptiongetArray(int, int, String[])
java.lang.Object getArray(int type,
int depth,
java.lang.String[] heads)
throws MathLinkException
Identical to getArray(int type, int depth), except that this form fills a String array with the heads of the expression found at each level. To use this method, allocate a String array of length 'depth' and pass it in. For example:
// Assume that the expression {foo[1, 2], foo[3, 4]} is waiting on the link.
String[] heads = new String[2];
int[][] intArray2D = (int[][]) ml.getArray(MathLink.TYPE_INT, 2, heads);
// Now heads[0] is "List" and heads[1] is "foo".
This method does not enforce a requirement that the heads be identical across a level.
If the expression looks like {foo[1, 2], bar[3, 4]} then the heads array would become
{"List", "foo"}, ignoring the fact that foo was not the head of every subexpression at level
1. In other words, if heads[i] is "foo", then it is only guaranteed that some expression
at level i had head foo, not that all of them did. If you want to be absolutely sure about
the heads of every subpart, read the expression as an Expr and use the Expr methods to inspect it.
type - The desired type for the returned array. Must be one of TYPE_BOOLEAN, TYPE_BYTE,
TYPE_CHAR, TYPE_SHORT, TYPE_INT, TYPE_LONG, TYPE_FLOAT, TYPE_DOUBLE, TYPE_STRING,
TYPE_BIGINTEGER, TYPE_BIGDECIMAL, TYPE_EXPR, or TYPE_COMPLEX.depth - the depth of the returned arrayheads - an array of length 'depth' that will be filled with the heads at each level
MathLinkException - if the incoming expression is not an array of the requested type,
or is not as deep as requestedgetArray(int, int)
MLFunction getFunction()
throws MathLinkException
MathLinkException
void putFunction(java.lang.String f,
int argCount)
throws MathLinkException
Follow this with calls to put the arguments.
f - the function nameargCount - the number of arguments to follow
MathLinkException
int checkFunction(java.lang.String f)
throws MathLinkException
f.
If they do not match, a MathLinkException is thrown.
Similar to getFunction() in that it reads the name and argument count off the link. Use it in situations where you want an error to occur if the function is not what you expect.
f - the function name that you are expecting
MathLinkException - if the current expression is not a function, or if it does not have the specified namecheckFunctionWithArgCount(String, int)
void checkFunctionWithArgCount(java.lang.String f,
int argCount)
throws MathLinkException
f and the argument count with argCount.
If they do not match, a MathLinkException is thrown.
Similar to getFunction() in that it reads the name and argument count off the link. Use it in situations where you want an error to occur if the function is not what you expect.
f - the function name that you are expectingargCount - the argument count that you are expecting
MathLinkException - if the current expression is not a function, or if it does not have the specified namecheckFunction(String)
java.lang.Object getComplex()
throws MathLinkException
This method will read the number, but return null, if no Java class has yet been established to use for complex numbers.
MathLinkException - if the current expression is not an integer, real, or Mathematica Complex.setComplexClass(Class)
void transferExpression(MathLink source)
throws MathLinkException
source to this link.
source - the link to read from
MathLinkExceptiontransferToEndOfLoopbackLink(LoopbackLink)
void transferToEndOfLoopbackLink(LoopbackLink source)
throws MathLinkException
source to this link.
source - the LoopbackLink to read from
MathLinkExceptiontransferExpression(MathLink)
Expr getExpr()
throws MathLinkException
The returned Expr can be examined and manipulated later.
MathLinkExceptionpeekExpr()
Expr peekExpr()
throws MathLinkException
Like getExpr(), but peekExpr() does not actually remove anything from the link. In other words, it leaves the link in the same state it was in before peekExpr() was called.
MathLinkExceptiongetExpr()
int getMessage()
throws MathLinkException
This method no longer functions in J/Link 2.0. You must use addMessageHandler() if you want to be able to receive messages from Mathematica.
MathLinkExceptionaddMessageHandler(Class, Object, String)
void putMessage(int msg)
throws MathLinkException
To abort a Mathematica computation, use putMessage(MLABORTMESSAGE). If a computation is successfully aborted, it will return the symbol $Aborted.
Do not confuse this type of message, used mainly for communicating requests to interrupt or abort computations, with Mathematica warning messages, which are unrelated.
msg - the message to send; will be one of MLABORTMESSAGE or MLINTERRUPTMESSAGE. Most users
will only be concerned with MLABORTMESSAGE.
MathLinkException
boolean messageReady()
throws MathLinkException
This method no longer functions in J/Link 2.0. You must use addMessageHandler() if you want to be able to receive messages from Mathematica.
MathLinkExceptionaddMessageHandler(Class, Object, String)
long createMark()
throws MathLinkException
Marks can returned to later, to re-read data. A common use is to create a mark, call some method for reading data, and if a MathLinkException is thrown, seek back to the mark and try a different method of reading the data.
Make sure to always call destroyMark() on any marks you create. Failure to do so will cause a memory leak.
Some of the usefulness of marks in the C-language MathLink API is obviated by J/Link's Expr class.
MathLinkExceptionseekMark(long),
destroyMark(long)void seekMark(long mark)
mark - the mark, created by createMark(), that identifies the desired position to reset tocreateMark(),
seekMark(long)void destroyMark(long mark)
mark - the mark to destroy
void put(java.lang.Object obj)
throws MathLinkException
Only a limited set of Java objects can be usefully sent across a link with this method. These are objects whose "values" have a meaningful representation in Mathematica:
null: sent as the symbol Null. strings: sent as Mathematica strings arrays: sent as lists of the appropriate dimensions Expr: sent as expressions Wrapper classes (e.g., Boolean, Integer, Float, etc.): sent as their values BigDecimal and BigInteger: sent as their values complex numbers: sent as ComplexAll other objects have no meaningful representation in Mathematica. For these objects, the relatively useless obj.toString() is sent. The put(Object) method in the KernelLink interface will put these objects "by reference".
obj - the object to put
MathLinkException
void put(java.lang.Object obj,
java.lang.String[] heads)
throws MathLinkException
int[][] a = {{1,2},{3,4}};
// The following are equivalent, sending to Mathematica the matrix: {{1,2},{3,4}}
ml.put(a);
ml.put(a, null);
ml.put(a, new String[] {"List", "List"});
obj - must be an arrayheads - the heads at each dimension
MathLinkExceptionboolean setComplexClass(java.lang.Class cls)
After calling setComplexClass(), you can use getComplex() to read an incoming Complex and create an instance of your class, and you can use put() to send objects of your class to Mathematica as Complex.
To be suitable, your class must have a public constructor of the form
MyComplex(double re, double im), and it must have two public methods
double re() and double im().
cls - the class to map to Mathematica Complex numbers
getComplexClass(),
getComplex()java.lang.Class getComplexClass()
You specify the class you want using setComplexClass().
setComplexClass(Class)
boolean setYieldFunction(java.lang.Class cls,
java.lang.Object obj,
java.lang.String methName)
The method must be public and its signature must be (V)Z (e.g., public boolean foo()).
You can pass null for cls if obj is provided.
If the method is static, pass null as obj.
Yield functions are an advanced topic, and are discussed in greater detail in the User Guide. Few users will need to use one.
cls - the class in which the method residesobj - the object on which the method should be invoked (use null if the method is static)methName - the name of the method to invoke
boolean addMessageHandler(java.lang.Class cls,
java.lang.Object obj,
java.lang.String methName)
Do not confuse this type of message, used mainly for communicating requests to interrupt or abort a computation, with Mathematica warning and error messages, which are unrelated.
The method you specify will be added to the set that are called whenever a MathLink
message is received by your Java program. The method must be public and its signature must
be (II)V (e.g., public void foo(int msgType, int ignore)). You can pass null for
cls if obj is provided. If the method is static, pass null
for obj. The first argument passed to your function when it is called is
the integer code giving the message type (e.g., MLABORTMESSAGE, MLINTERRUPTMESSAGE, etc.)
The second argument is undocumented and should be ignored.
Do not attempt to use the link from within your message handler function.
You can set more than one message handler, hence the name "addMessageHandler".
Message handlers are an advanced technique. Few programmers will need to use one.
cls - the class in which the method residesobj - the object on which the method should be invoked (use null if the method is static)methName - the name of the method to invoke
removeMessageHandler(String)boolean removeMessageHandler(java.lang.String methName)
Do not confuse this type of message, used mainly for communicating requests to interrupt or abort a computation, with Mathematica warning and error messages, which are unrelated.
Message handlers are an advanced topic, and are discussed in greater detail in the User Guide. Few users will need to use one.
methName - the name of the method to remove
addMessageHandler(Class, Object, String)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||