public class UtilityFunctions
extends java.lang.Object
Red (cxh) |
Yellow (eal) |
Constructor and Description |
---|
UtilityFunctions() |
Modifier and Type | Method and Description |
---|---|
static ArrayToken |
arrayType(Token t)
Return a new UnsizedArrayToken whose element type is the same
as the given type.
|
static ArrayToken |
arrayType(Token t,
IntToken numberOfTimes)
Return a new UnsizedArrayToken whose element type is the same
as the given type, and whose length is the given length.
|
static Type |
arrayTypeReturnType(Type type)
Return a new ArrayType whose element type is the same as the given
type.
|
static Type |
arrayTypeReturnType(Type type1,
Type type2)
Return the (not quite exact) return type of the arrayType
function above.
|
static java.lang.String |
asURL(java.lang.String fileName)
Convert the argument from a fileName to a URL that begins with
"file:".
|
static Token |
cast(Token token1,
Token token2)
Convert the second token to the type of the first.
|
static Type |
castReturnType(Type type1,
Type type2)
Return the type of the first argument.
|
static ArrayToken |
concatenate(ArrayToken token)
Concatenate an array of arrays into a single array.
|
static ArrayToken |
concatenate(ArrayToken token1,
ArrayToken token2)
Concatenate two arrays.
|
static Type |
concatenateReturnType(Type firstArrayType)
Return the return type of the one argument concatenate method, given the type
of the argument.
|
static Type |
concatenateReturnType(Type firstArrayType,
Type secondArrayType)
Return the return type of the two argument concatenate method,
given the types of the arguments.
|
static RecordToken |
constants()
Return a record token that contains the names of all the
constants and their values.
|
static ArrayToken |
emptyArray(Token prototype)
Return an empty array with its element type matching
the specified token.
|
static RecordToken |
emptyRecord()
Return an empty record.
|
static ArrayToken |
filter(FunctionToken predicate,
ArrayToken array)
Extract a sub-array consisting of all of the elements of an
array for which the given predicate function returns true.
|
static ArrayToken |
filter(FunctionToken predicate,
ArrayToken array,
IntToken sizeLimit)
Extract a sub-array consisting of all of the elements of an
array for which the given predicate function returns true.
|
static Type |
filterReturnType(Type predicateType,
Type arrayTokenType)
Return the return type of the filter method, given the types
of the argument.
|
static Type |
filterReturnType(Type predicateType,
Type arrayTokenType,
Type sizeLimitType)
Return the return type of the filter method, given the types
of the argument.
|
static ArrayToken |
find(ArrayToken array)
Find all true-valued elements in an array of boolean values,
returning an array containing the indices (in ascending order)
of all occurrences of the value 'true'.
|
static ArrayToken |
find(ArrayToken array,
Token match)
Find all elements in an array that match the specified token
and return an array containing their indices (in ascending order).
|
static java.lang.String |
findFile(java.lang.String name)
Find a file or directory.
|
static LongToken |
freeMemory()
Return the approximate number of bytes available for future
object allocation.
|
static DoubleToken |
gaussian(double mean,
double standardDeviation)
Return a Gaussian random number.
|
static ArrayToken |
gaussian(double mean,
double standardDeviation,
int length)
Return an array of Gaussian random numbers.
|
static DoubleMatrixToken |
gaussian(double mean,
double standardDeviation,
int rows,
int columns)
Return a matrix of Gaussian random numbers.
|
static RecordToken |
getenv()
Get a variable from the environment.
|
static StringToken |
getenv(java.lang.String variableName)
Get a variable from the environment.
|
static java.lang.String |
getProperty(java.lang.String propertyName)
Deprecated.
Use
StringUtilities.getProperty(String)
instead |
static java.lang.String |
inferType(java.lang.String string)
Infer the type of the given string as an expression in the
expression language.
|
static Token |
intersect(RecordToken record1,
RecordToken record2)
Find the intersection of two records.
|
static ArrayToken |
iterate(FunctionToken function,
int length,
Token initial)
Iterate the specified function to produce an array of the specified
length.
|
static Type |
iterateReturnType(Type functionType,
Type lengthType,
Type initialType)
Return the return type of the iterate method, given the types
of the argument.
|
static void |
loadLibrary(java.lang.String library)
Load a library by first using the default platform dependent
System.loadLibrary() method.
|
static ArrayToken |
map(FunctionToken function,
ArrayToken array)
Apply the specified function to the specified array and return
an array with the results.
|
static Type |
mapReturnType(Type functionType,
Type arrayTokenType)
Return the return type of the map method, given the types
of the argument.
|
static ScalarToken |
max(ArrayToken array)
Return the maximum of the contents of the array.
|
static UnsignedByteToken |
max(UnsignedByteToken x,
UnsignedByteToken y)
Return the maximum of two unsigned bytes.
|
static Type |
maxReturnType(Type type)
Return the (exact) return type of the max function above.
|
static ScalarToken |
min(ArrayToken array)
Return the minimum of the contents of the array.
|
static UnsignedByteToken |
min(UnsignedByteToken x,
UnsignedByteToken y)
Return the minimum of two unsigned bytes.
|
static Type |
minReturnType(Type type)
Return the (exact) return type of the min function above.
|
static ObjectToken |
model(java.lang.String classname)
FIXME.
|
static ArrayToken |
multivariateGaussian(ArrayToken mean,
DoubleMatrixToken covariance)
Generate a sample from a multivariate Gaussian distribution.
|
static ActorToken |
parseMoML(java.lang.String moml)
Parse the string provided and return the result wrapped in a token.
|
static StringToken |
property(java.lang.String propertyName)
Get the specified property from the environment.
|
static ArrayToken |
random(int length)
Return an array of IID random numbers with value greater than
or equal to 0.0 and less than 1.0.
|
static DoubleMatrixToken |
random(int rows,
int columns)
Return a matrix of IID random numbers with value greater than
or equal to 0.0 and less than 1.0.
|
static Type |
randomReturnType(Type type)
Return the (exact) return type of the random function above.
|
static StringToken |
readFile(java.lang.String filename)
Get the string text contained in the specified file.
|
static DoubleMatrixToken |
readMatrix(java.lang.String filename)
Deprecated.
Use eval(readFile()) instead.
|
static StringToken |
readResource(java.lang.String name)
Get the string text contained in the specified resource, which
is a file that is specified relative to the Java classpath.
|
static ArrayToken |
repeat(IntToken numberOfTimes,
Token element)
Create an array that contains the specified element
repeated the specified number of times.
|
static Type |
repeatReturnType(Type type1,
Type type2)
Return the (exact) return type of the repeat function above.
|
static ArrayToken |
sort(ArrayToken array)
Return a new array that is the sorted contents of a specified
array, in ascending order.
|
static ArrayToken |
sortAscending(ArrayToken array)
Return a new array that is the sorted contents of a specified
array, in ascending order.
|
static Type |
sortAscendingReturnType(Type type)
Return the (exact) return type of the sortAscending function above.
|
static ArrayToken |
sortDescending(ArrayToken array)
Return a new array that is the sorted contents of a specified
array, in descending order.
|
static Type |
sortDescendingReturnType(Type type)
Return the (exact) return type of the sortDescending function above.
|
static Type |
sortReturnType(Type type)
Return the (exact) return type of the sort function above.
|
static ArrayToken |
subarray(ArrayToken array,
IntToken index,
IntToken count)
Return the contiguous subarray of the specified array
starting at the specified index and of the specified length.
|
static Type |
subarrayReturnType(Type arrayType,
Type indexType,
Type countType)
Return the return type of the subarray() method, which is the
same as the array type.
|
static Token |
sum(ArrayToken array)
Return the sum of the elements in the specified array.
|
static Type |
sumReturnType(Type type)
Return the (exact) return type of the sum function above.
|
static LongToken |
totalMemory()
Return the approximate number of bytes used by current objects
and available for future object allocation.
|
static java.lang.String |
traceEvaluation(java.lang.String string)
Evaluate the given string as an expression in the expression
language.
|
static BooleanToken |
within(Token token1,
Token token2,
double distance)
Return true if the first argument is close in value to the second,
where "close" means that it is within the distance given by the
third argument.
|
static BooleanToken |
yesNoQuestion(StringToken prompt)
Query the user for a yes-no answer and return a boolean.
|
static DoubleMatrixToken |
zeroMatrix(int rows,
int columns)
Deprecated.
Use zeroMatrixDouble instead.
|
static ComplexMatrixToken |
zeroMatrixComplex(int rows,
int columns)
Return a complex zero matrix with the given number of rows and
columns.
|
static DoubleMatrixToken |
zeroMatrixDouble(int rows,
int columns)
Return a double zero matrix with the given number of rows and
columns.
|
static IntMatrixToken |
zeroMatrixInt(int rows,
int columns)
Return a int zero matrix with the given number of rows and
columns.
|
static LongMatrixToken |
zeroMatrixLong(int rows,
int columns)
Return a long zero matrix with the given number of rows and
columns.
|
public static ArrayToken arrayType(Token t)
public static Type arrayTypeReturnType(Type type)
type
- The type of the array.public static ArrayToken arrayType(Token t, IntToken numberOfTimes)
t
- The element type of the array.numberOfTimes
- The array length.public static Type arrayTypeReturnType(Type type1, Type type2)
type1
- The type of the first argument to the
corresponding function.type2
- The type of the second argument to the
corresponding function.public static java.lang.String asURL(java.lang.String fileName)
fileName
- The name of the file to be converted.public static Token cast(Token token1, Token token2) throws IllegalActionException
IllegalActionException
- If the token cannot be converted.public static Type castReturnType(Type type1, Type type2) throws IllegalActionException
type1
- The type being cast to.type2
- The type being cast.IllegalActionException
public static ArrayToken concatenate(ArrayToken token1, ArrayToken token2) throws IllegalActionException
token1
- First array from which tokens are copied to the result.token2
- Second array from which tokens are copied to the result.IllegalActionException
- If the arrays do not have
compatible types.public static ArrayToken concatenate(ArrayToken token) throws IllegalActionException
token
- Array of arrays which are to be concatenated.IllegalActionException
- If the argument is not an array of
arrays.public static Type concatenateReturnType(Type firstArrayType, Type secondArrayType) throws IllegalActionException
firstArrayType
- The type of the predicate function.secondArrayType
- The type of the array to be filtered.IllegalActionException
- If the types of the array arguments
are not Arrays.public static Type concatenateReturnType(Type firstArrayType) throws IllegalActionException
firstArrayType
- The type of the predicate function.IllegalActionException
- If the types of the array argument
is not an Array.public static RecordToken constants()
public static RecordToken emptyRecord() throws IllegalActionException
IllegalActionException
public static ArrayToken emptyArray(Token prototype)
prototype
- A token specifying the element type.public static ArrayToken filter(FunctionToken predicate, ArrayToken array) throws IllegalActionException
even = function(x:int)(x%2==0)
filter(even,[1:1:20].toArray)
predicate
- A function that takes exactly one parameter (of the
same type as the elements of the given array) and returns
a boolean value.array
- An array, on whose elements the given function
will operate.IllegalActionException
- If applying the function
triggers an exception.public static ArrayToken filter(FunctionToken predicate, ArrayToken array, IntToken sizeLimit) throws IllegalActionException
even = function(x:int)(x%2==0)
filter(even,[1:1:20].toArray)
predicate
- A function that takes exactly one parameter (of the
same type as the elements of the given array) and returns
a boolean value.array
- An array, on whose elements the given function
will operate.sizeLimit
- The maximum size of the resulting array,
or a negative number to specify no limit.IllegalActionException
- If applying the function
triggers an exception.public static Type filterReturnType(Type predicateType, Type arrayTokenType) throws IllegalActionException
predicateType
- The type of the predicate function.arrayTokenType
- The type of the array to be filtered.IllegalActionException
- If the specified function does not
take exactly one argument, or if the type signature of the function
is not compatible with the other array argument.public static Type filterReturnType(Type predicateType, Type arrayTokenType, Type sizeLimitType) throws IllegalActionException
predicateType
- The type of the predicate function.arrayTokenType
- The type of the array to be filtered.sizeLimitType
- The type of the sizeLimit argument, or
null if there is no specified size limit.IllegalActionException
- If the specified function does not
take exactly one argument, or if the type signature of the function
is not compatible with the other array argument.public static ArrayToken find(ArrayToken array) throws IllegalActionException
array
- An array of boolean tokens.IllegalActionException
- If the specified array is not
a boolean array.find(ArrayToken, Token)
public static ArrayToken find(ArrayToken array, Token match)
array
- An array.find(ArrayToken)
public static java.lang.String findFile(java.lang.String name)
name
- Path of a file or directory to find.public static LongToken freeMemory()
totalMemory()
public static DoubleToken gaussian(double mean, double standardDeviation)
mean
- The mean.standardDeviation
- The standard deviation.public static ArrayToken gaussian(double mean, double standardDeviation, int length)
mean
- The mean.standardDeviation
- The standard deviation.length
- The length of the array.public static DoubleMatrixToken gaussian(double mean, double standardDeviation, int rows, int columns)
mean
- The mean.standardDeviation
- The standard deviation.rows
- The number of rows.columns
- The number of columns.public static StringToken getenv(java.lang.String variableName)
variableName
- The name of the environment variable to getpublic static RecordToken getenv() throws IllegalActionException
IllegalActionException
@Deprecated public static java.lang.String getProperty(java.lang.String propertyName)
StringUtilities.getProperty(String)
insteadThe following properties are handled specially
c:/foo
whereas most of the other methods that operate
on path names return C:/foo
.
propertyName
- The name of property.public static java.lang.String inferType(java.lang.String string) throws IllegalActionException
string
- The string to be parsed and evaluated.IllegalActionException
public static Token intersect(RecordToken record1, RecordToken record2) throws IllegalActionException
Example: intersect({a = 1, b = 2}, {a = 3, c = 4}) returns {a = 1}.
record1
- The first record to intersect. The result gets
its values from this record.record2
- The second record to intersect.IllegalActionException
public static ArrayToken iterate(FunctionToken function, int length, Token initial) throws IllegalActionException
function
- A single-argument function to iterate.length
- The length of the resulting array.initial
- The first element of the result.IllegalActionException
- If the specified function does not
take exactly one argument, or if an error occurs applying the function.public static Type iterateReturnType(Type functionType, Type lengthType, Type initialType) throws IllegalActionException
functionType
- The type of the function to iterate.lengthType
- The type of the length argument.initialType
- The type of the first element of the result.IllegalActionException
- If the specified function does not
take exactly one argument, or if the type signature of the function
is not compatible with the other arguments.public static void loadLibrary(java.lang.String library)
findFile(String)
and if the library is found,
load it using System.load(). If the library is not found
by findFile() and the pathname contains a /
then we call System.loadLibrary() the filename after the last
/. This step is necessary to support native methods under
Webstart, which looks for libraries at the top level.
If all of the above fails, we throw the initial exception.library
- the name of the library to be loaded. The name
should not include the platform dependent suffix.public static ArrayToken map(FunctionToken function, ArrayToken array) throws IllegalActionException
function
- A function with at least one argument.array
- The array to which to apply the function.IllegalActionException
- If the specified function does not
take at least one argument, or if an error occurs applying the
function, or if the number of arguments does not match the subarray
lengths.public static Type mapReturnType(Type functionType, Type arrayTokenType) throws IllegalActionException
functionType
- The type of the function to map.arrayTokenType
- The type of array to apply the
specified function on.IllegalActionException
- If the specified function does not
take at least one argument, or if the type signature of the function
is not compatible with the array elements, or if the specified
function has different argument type.public static UnsignedByteToken max(UnsignedByteToken x, UnsignedByteToken y)
x
- An unsigned byte.y
- An unsigned byte.public static ScalarToken max(ArrayToken array) throws IllegalActionException
array
- An array of scalar tokens.IllegalActionException
- If the array is empty or
it contains tokens that are not scalar or it contains complex tokens.public static Type maxReturnType(Type type)
type
- The type of the argument to the corresponding function.public static UnsignedByteToken min(UnsignedByteToken x, UnsignedByteToken y)
x
- An unsigned byte.y
- An unsigned byte.public static ScalarToken min(ArrayToken array) throws IllegalActionException
array
- An array of scalar tokens.IllegalActionException
- If the array is empty or
it contains tokens that are not scalar or it contains complex tokens.public static Type minReturnType(Type type)
type
- The type of the argument to the corresponding function.public static ObjectToken model(java.lang.String classname) throws IllegalActionException
IllegalActionException
public static ArrayToken multivariateGaussian(ArrayToken mean, DoubleMatrixToken covariance) throws IllegalActionException
mean
- The mean.covariance
- The covariance.IllegalActionException
public static ActorToken parseMoML(java.lang.String moml) throws java.lang.Exception
moml
- The MoML string.java.lang.Exception
- If the MoML is invalid or the results is not an
instance of Entity.public static StringToken property(java.lang.String propertyName)
propertyName
- The name of property.StringUtilities.getProperty(String)
public static ArrayToken random(int length)
length
- The length of the array.public static DoubleMatrixToken random(int rows, int columns)
rows
- The number of rows.columns
- The number of columns.public static Type randomReturnType(Type type)
type
- The type of the argument to the corresponding function.public static StringToken readFile(java.lang.String filename) throws IllegalActionException
eval(readFile("filename"))
Note that readFile() does not work inside applets, use
readResource(String)
instead.
filename
- The name of the file to read from.IllegalActionException
- If the file cannot be opened.ASTPtFunctionApplicationNode
,
readResource(String)
@Deprecated public static DoubleMatrixToken readMatrix(java.lang.String filename) throws IllegalActionException
filename
- The filename.IllegalActionException
- If the file cannot be opened.public static StringToken readResource(java.lang.String name) throws IllegalActionException
eval(readFile("filename"))
name
- The name of the resource to read from.IllegalActionException
- If the resource cannot be opened.ASTPtFunctionApplicationNode
,
readFile(String)
public static ArrayToken repeat(IntToken numberOfTimes, Token element)
numberOfTimes
- The number of times to repeat the element.element
- The element to repeat.public static Type repeatReturnType(Type type1, Type type2)
type1
- The type of the first argument to the
corresponding function.type2
- The type of the second argument to the
corresponding function.public static ArrayToken sort(ArrayToken array) throws IllegalActionException
array
- An array of scalar tokens.IllegalActionException
- If the array contains
tokens that are complex or are not scalar or string tokens.public static Type sortReturnType(Type type)
type
- The type of the argument to the corresponding function.public static ArrayToken sortAscending(ArrayToken array) throws IllegalActionException
array
- An array of scalar tokens.IllegalActionException
- If the array contains
tokens that are complex or are not scalar or string tokens.sort(ArrayToken)
,
sortDescending(ArrayToken)
public static Type sortAscendingReturnType(Type type)
type
- The type of the argument to the corresponding function.public static ArrayToken sortDescending(ArrayToken array) throws IllegalActionException
array
- An array of scalar tokens.IllegalActionException
- If the array contains
tokens that are complex or are not scalar or string tokens.public static Type sortDescendingReturnType(Type type)
type
- The type of the argument to the corresponding function.public static ArrayToken subarray(ArrayToken array, IntToken index, IntToken count) throws IllegalActionException
array
- The array.index
- The index of the beginning of the subarray.count
- The length of the subarray.IllegalActionException
- If the index argument is less
than zero.public static Type subarrayReturnType(Type arrayType, Type indexType, Type countType)
arrayType
- The type of the array argument.indexType
- The type of the index argument.countType
- The type of the count argument.public static final Token sum(ArrayToken array) throws IllegalActionException
array
- An array.IllegalActionException
- If the length of the
array is zero, or if the array elements do not support
addition.public static Type sumReturnType(Type type)
type
- The type of the argument to the corresponding function.public static LongToken totalMemory()
freeMemory()
public static java.lang.String traceEvaluation(java.lang.String string) throws IllegalActionException
string
- The string to be parsed and evaluated.IllegalActionException
public static BooleanToken within(Token token1, Token token2, double distance) throws IllegalActionException
token1
- The first token.token2
- The second token.distance
- The distance criterion.IllegalActionException
- If the first two arguments cannot
be compared.public static BooleanToken yesNoQuestion(StringToken prompt)
@Deprecated public static DoubleMatrixToken zeroMatrix(int rows, int columns)
public static ComplexMatrixToken zeroMatrixComplex(int rows, int columns)
public static DoubleMatrixToken zeroMatrixDouble(int rows, int columns)
public static IntMatrixToken zeroMatrixInt(int rows, int columns)
public static LongMatrixToken zeroMatrixLong(int rows, int columns)