Class UtilityFunctions


  • public class UtilityFunctions
    extends java.lang.Object
    This class provides additional functions for use in the Ptolemy II expression language. All of the methods in this class are static and return an instance of Token. The expression language identifies the appropriate method to use by using reflection, matching the types of the arguments.
    Since:
    Ptolemy II 0.2
    Version:
    $Id$
    Author:
    Christopher Hylands Brooks, Tobin Fricke, Bart Kienhuis, Edward A. Lee, Steve Neuendorffer, Neil Smyth, Yang Zhao. Contributor: Daniel Crawl
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Yellow (eal)
    • Constructor Detail

      • UtilityFunctions

        public UtilityFunctions()
    • Method Detail

      • arrayType

        public static ArrayToken arrayType​(Token t)
        Return a new UnsizedArrayToken whose element type is the same as the given type.
        Returns:
        a new UnsizedArrayToken.
      • arrayTypeReturnType

        public static Type arrayTypeReturnType​(Type type)
        Return a new ArrayType whose element type is the same as the given type.
        Parameters:
        type - The type of the array.
        Returns:
        a new ArrayType.
      • arrayType

        public 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.
        Parameters:
        t - The element type of the array.
        numberOfTimes - The array length.
        Returns:
        a new ArrayToken.
      • arrayTypeReturnType

        public static Type arrayTypeReturnType​(Type type1,
                                               Type type2)
        Return the (not quite exact) return type of the arrayType function above. This function always returns an ArrayType whose element type is the first argument, with length equal to the second argument. The result type is reported as being unsized, which may result in an inexact type being inferred for expressions including the arrayType function.
        Parameters:
        type1 - The type of the first argument to the corresponding function.
        type2 - The type of the second argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • asURL

        public static java.lang.String asURL​(java.lang.String fileName)
        Convert the argument from a fileName to a URL that begins with "file:".
        Parameters:
        fileName - The name of the file to be converted.
        Returns:
        the URL that is equivalent to the file
      • castReturnType

        public static Type castReturnType​(Type type1,
                                          Type type2)
                                   throws IllegalActionException
        Return the type of the first argument.
        Parameters:
        type1 - The type being cast to.
        type2 - The type being cast.
        Returns:
        The type being cast to.
        Throws:
        IllegalActionException
      • choleskyDecomposition

        public static double[][] choleskyDecomposition​(double[][] A)
                                                throws IllegalActionException
        Returns the lower triangular matrix L that satisfies LL*=A
        Parameters:
        A - a positive definite matrix (implies also symmetric)
        Returns:
        lower triangular matrix L such that A=LL*
        Throws:
        IllegalActionException
      • concatenate

        public static ArrayToken concatenate​(ArrayToken token1,
                                             ArrayToken token2)
                                      throws IllegalActionException
        Concatenate two arrays. The arrays must have the same type. Example: concatenate({1,2,3},{4,5,6}) == {1,2,3,4,5,6}.
        Parameters:
        token1 - First array from which tokens are copied to the result.
        token2 - Second array from which tokens are copied to the result.
        Returns:
        An array containing all of the elements of the first argument (in order), followed by all of the arguments of the second argument (in order).
        Throws:
        IllegalActionException - If the arrays do not have compatible types.
        Since:
        Ptolemy II 4.1
      • concatenate

        public static ArrayToken concatenate​(ArrayToken token)
                                      throws IllegalActionException
        Concatenate an array of arrays into a single array. Example: concatenate({{1,2,3},{4,5},{6,7}}) == {1,2,3,4,5,6,7}.
        Parameters:
        token - Array of arrays which are to be concatenated.
        Throws:
        IllegalActionException - If the argument is not an array of arrays.
        Since:
        Ptolemy II 4.1
      • concatenateReturnType

        public static Type concatenateReturnType​(Type firstArrayType,
                                                 Type secondArrayType)
                                          throws IllegalActionException
        Return the return type of the two argument concatenate method, given the types of the arguments. This specialized method is called by the CachedMethod class and used to provide a better return type.
        Parameters:
        firstArrayType - The type of the predicate function.
        secondArrayType - The type of the array to be filtered.
        Returns:
        The type of the result, which is an array of the least upper bound of the array arguments. The length of the array type is the sum of the length of the arguments.
        Throws:
        IllegalActionException - If the types of the array arguments are not Arrays.
      • concatenateReturnType

        public static Type concatenateReturnType​(Type firstArrayType)
                                          throws IllegalActionException
        Return the return type of the one argument concatenate method, given the type of the argument. This specialized method is called by the CachedMethod class and used to provide a better return type.
        Parameters:
        firstArrayType - The type of the predicate function.
        Returns:
        The type of the result, which is an array of the firstArrayType arguments. The length of the array type may be unknown.
        Throws:
        IllegalActionException - If the types of the array argument is not an Array.
      • constants

        public static RecordToken constants()
        Return a record token that contains the names of all the constants and their values.
        Returns:
        A token containing the names of all the constants and their values.
        Since:
        Ptolemy II 2.1
      • emptyArray

        public static ArrayToken emptyArray​(Token prototype)
        Return an empty array with its element type matching the specified token.
        Parameters:
        prototype - A token specifying the element type.
        Returns:
        An empty array.
      • emptyArrayReturnType

        public static Type emptyArrayReturnType​(Type prototype)
        Return the type of an empty array of unspecified length.
        Parameters:
        prototype - A token specifying the element type.
        Returns:
        An empty array.
      • filter

        public static ArrayToken filter​(FunctionToken predicate,
                                        ArrayToken array)
                                 throws IllegalActionException
        Extract a sub-array consisting of all of the elements of an array for which the given predicate function returns true. If the given array has type {X}, then the given function should have type function(x:X)(boolean). Example usage:

        even = function(x:int)(x%2==0) filter(even,[1:1:20].toArray)

        Parameters:
        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.
        Returns:
        The subarray of this array containing exactly those elements, in order, for which the given function returns a BooleanToken with value true. Any other return value results in the element being omitted. If the given function never returns true, then return an empty array with an element type that matches the specified array.
        Throws:
        IllegalActionException - If applying the function triggers an exception.
        Since:
        Ptolemy II 4.1
      • filter

        public static ArrayToken filter​(FunctionToken predicate,
                                        ArrayToken array,
                                        IntToken sizeLimit)
                                 throws IllegalActionException
        Extract a sub-array consisting of all of the elements of an array for which the given predicate function returns true. If the given array has type {X}, then the given function should have type function(x:X)(boolean). Example usage:

        even = function(x:int)(x%2==0) filter(even,[1:1:20].toArray)

        Parameters:
        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.
        Returns:
        The subarray of this array containing exactly those elements, in order, for which the given function returns a BooleanToken with value true. Any other return value results in the element being omitted. If the given function never returns true, then return an empty array with an element type that matches the specified array.
        Throws:
        IllegalActionException - If applying the function triggers an exception.
        Since:
        Ptolemy II 4.1
      • filterReturnType

        public static Type filterReturnType​(Type predicateType,
                                            Type arrayTokenType)
                                     throws IllegalActionException
        Return the return type of the filter method, given the types of the argument.
        Parameters:
        predicateType - The type of the predicate function.
        arrayTokenType - The type of the array to be filtered.
        Returns:
        The type of the result, which is the same as the array type to be filtered.
        Throws:
        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.
      • filterReturnType

        public static Type filterReturnType​(Type predicateType,
                                            Type arrayTokenType,
                                            Type sizeLimitType)
                                     throws IllegalActionException
        Return the return type of the filter method, given the types of the argument.
        Parameters:
        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.
        Returns:
        The type of the result, which is the same as the array type to be filtered.
        Throws:
        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.
      • find

        public static ArrayToken find​(ArrayToken array)
                               throws IllegalActionException
        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'.
        Parameters:
        array - An array of boolean tokens.
        Returns:
        An array of integers giving the indices of 'true' elements in the array given as an argument.
        Throws:
        IllegalActionException - If the specified array is not a boolean array.
        Since:
        Ptolemy II 4.1
        See Also:
        find(ArrayToken, Token)
      • find

        public 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).
        Parameters:
        array - An array.
        Returns:
        An array of integers giving the indices of elements in the specified array that match the specified token.
        Since:
        Ptolemy II 4.1
        See Also:
        find(ArrayToken)
      • findFile

        public static java.lang.String findFile​(java.lang.String name)
        Find a file or directory. If the file does not exist as is, then search the current working directory, the user's home directory, and finally, the classpath.
        Parameters:
        name - Path of a file or directory to find.
        Returns:
        Canonical absolute path if the file or directory is found, otherwise the argument is returned unchanged.
      • freeMemory

        public static LongToken freeMemory()
        Return the approximate number of bytes available for future object allocation. Note that requesting a garbage collection may change this value.
        Returns:
        The approximate number of bytes available.
        See Also:
        totalMemory()
      • gaussian

        public static DoubleToken gaussian​(double mean,
                                           double standardDeviation)
        Return a Gaussian random number.
        Parameters:
        mean - The mean.
        standardDeviation - The standard deviation.
        Returns:
        An observation of a Gaussian random variable.
      • gaussian

        public static ArrayToken gaussian​(double mean,
                                          double standardDeviation,
                                          int length)
        Return an array of Gaussian random numbers.
        Parameters:
        mean - The mean.
        standardDeviation - The standard deviation.
        length - The length of the array.
        Returns:
        An array of doubles with IID Gaussian random variables.
      • gaussian

        public static DoubleMatrixToken gaussian​(double mean,
                                                 double standardDeviation,
                                                 int rows,
                                                 int columns)
        Return a matrix of Gaussian random numbers.
        Parameters:
        mean - The mean.
        standardDeviation - The standard deviation.
        rows - The number of rows.
        columns - The number of columns.
        Returns:
        A matrix of observations of a Gaussian random variable.
      • getenv

        public static StringToken getenv​(java.lang.String variableName)
        Get a variable from the environment.
        Parameters:
        variableName - The name of the environment variable to get
        Returns:
        the environment variable, or null if the variable is not defined.
      • getProperty

        @Deprecated
        public static java.lang.String getProperty​(java.lang.String propertyName)
        Deprecated.
        Get the specified property from the environment. An empty string is returned if the argument environment variable does not exist, though if certain properties are not defined, then we make various attempts to determine them and then set them. See the javadoc page for java.util.System.getProperties() for a list of system properties.

        The following properties are handled specially

        "ptolemy.ptII.dir"
        vergil usually sets the ptolemy.ptII.dir property to the value of $PTII. However, if we are running under Web Start, then this property might not be set, in which case we look for "ptolemy/kernel/util/NamedObj.class" and set the property accordingly.
        "ptolemy.ptII.dirAsURL"
        Return $PTII as a URL. For example, if $PTII was c:\ptII, then return file:/c:/ptII/.
        "user.dir"
        Return the canonical path name to the current working directory. This is necessary because under JDK1.4.1 System.getProperty() returns c:/foo whereas most of the other methods that operate on path names return C:/foo.
        Parameters:
        propertyName - The name of property.
        Returns:
        A String containing the string value of the property.
      • inferType

        public static java.lang.String inferType​(java.lang.String string)
                                          throws IllegalActionException
        Infer the type of the given string as an expression in the expression language. Return a string representation of the given type.
        Parameters:
        string - The string to be parsed and evaluated.
        Returns:
        A string representing an inferred type.
        Throws:
        IllegalActionException
      • intersect

        public static Token intersect​(RecordToken record1,
                                      RecordToken record2)
                               throws IllegalActionException
        Find the intersection of two records. The field names are intersected, and the values are taken from the first record.

        Example: intersect({a = 1, b = 2}, {a = 3, c = 4}) returns {a = 1}.

        Parameters:
        record1 - The first record to intersect. The result gets its values from this record.
        record2 - The second record to intersect.
        Returns:
        A RecordToken containing the result.
        Throws:
        IllegalActionException
      • iterate

        public static ArrayToken iterate​(FunctionToken function,
                                         int length,
                                         Token initial)
                                  throws IllegalActionException
        Iterate the specified function to produce an array of the specified length. The first element of the output array is the initial argument, the second is the result of applying the function to initial, the third is the result of applying the function to that result, etc. The length argument is required to be greater than 1.
        Parameters:
        function - A single-argument function to iterate.
        length - The length of the resulting array.
        initial - The first element of the result.
        Returns:
        A new array that is the result of applying the function repeatedly.
        Throws:
        IllegalActionException - If the specified function does not take exactly one argument, or if an error occurs applying the function.
      • iterateReturnType

        public static Type iterateReturnType​(Type functionType,
                                             Type lengthType,
                                             Type initialType)
                                      throws IllegalActionException
        Return the return type of the iterate method, given the types of the argument.
        Parameters:
        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.
        Returns:
        The type of the result.
        Throws:
        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.
      • loadLibrary

        public static void loadLibrary​(java.lang.String library)
        Load a library by first using the default platform dependent System.loadLibrary() method. If the library cannot be loaded using System.loadLibrary(), then search for the library using 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.
        Parameters:
        library - the name of the library to be loaded. The name should not include the platform dependent suffix.
      • map

        public static ArrayToken map​(FunctionToken function,
                                     ArrayToken array)
                              throws IllegalActionException
        Apply the specified function to the specified array and return an array with the results. The function must take at least one argument. If the function takes more than one argument, then the specified array should be an array of arrays, where each subarray is a set of arguments. Since arrays in the expression language can only contain elements of the same type, this method will only work for functions whose arguments are all of the same type.
        Parameters:
        function - A function with at least one argument.
        array - The array to which to apply the function.
        Returns:
        A new array that is the result of applying the function to the specified array.
        Throws:
        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.
      • mapReturnType

        public static Type mapReturnType​(Type functionType,
                                         Type arrayTokenType)
                                  throws IllegalActionException
        Return the return type of the map method, given the types of the argument.
        Parameters:
        functionType - The type of the function to map.
        arrayTokenType - The type of array to apply the specified function on.
        Returns:
        The type of the result.
        Throws:
        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.
      • max

        public static ScalarToken max​(ArrayToken array)
                               throws IllegalActionException
        Return the maximum of the contents of the array.
        Parameters:
        array - An array of scalar tokens.
        Returns:
        The largest element of the array.
        Throws:
        IllegalActionException - If the array is empty or it contains tokens that are not scalar or it contains complex tokens.
      • maxReturnType

        public static Type maxReturnType​(Type type)
        Return the (exact) return type of the max function above. If the argument is an array type, then return its element type, otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • min

        public static ScalarToken min​(ArrayToken array)
                               throws IllegalActionException
        Return the minimum of the contents of the array.
        Parameters:
        array - An array of scalar tokens.
        Returns:
        The largest element of the array.
        Throws:
        IllegalActionException - If the array is empty or it contains tokens that are not scalar or it contains complex tokens.
      • minReturnType

        public static Type minReturnType​(Type type)
        Return the (exact) return type of the min function above. If the argument is an array type, then return its element type, otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • multivariateGaussian

        public static ArrayToken multivariateGaussian​(double[] mean,
                                                      double[][] S)
                                               throws IllegalActionException
        Generate a sample from a multivariate Gaussian distribution.
        Parameters:
        mean - The mean array.
        S - The covariance matrix, whose length must match that of the mean array and be square.
        Returns:
        a sample from a multivariate Gaussian distribution
        Throws:
        IllegalActionException
      • parseMoML

        public static ActorToken parseMoML​(java.lang.String moml)
                                    throws java.lang.Exception
        Parse the string provided and return the result wrapped in a token.
        Parameters:
        moml - The MoML string.
        Returns:
        The result of parsing the MoML.
        Throws:
        java.lang.Exception - If the MoML is invalid or the results is not an instance of Entity.
      • property

        public static StringToken property​(java.lang.String propertyName)
        Get the specified property from the environment. An empty string is returned if the argument environment variable does not exist. See the javadoc page for java.util.System.getProperties() for a list of system properties. Example properties include:
        "java.version"
        the version of the JDK.
        "ptolemy.ptII.dir"
        The value of $PTII, which is the name of the directory in which Ptolemy II is installed.
        "ptolemy.ptII.dirAsURL"
        The value of $PTII as a URL, which is the name of the directory in which Ptolemy II is installed.
        "user.dir"
        The canonical path name to the current working directory.
        Parameters:
        propertyName - The name of property.
        Returns:
        A token containing the string value of the property.
        See Also:
        StringUtilities.getProperty(String)
      • random

        public 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.
        Parameters:
        length - The length of the array.
        Returns:
        An array of doubles with IID random variables.
      • random

        public 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.
        Parameters:
        rows - The number of rows.
        columns - The number of columns.
        Returns:
        A matrix of IID random variables.
      • randomReturnType

        public static Type randomReturnType​(Type type)
        Return the (exact) return type of the random function above. If the argument is BaseType.INT, then return and ArrayType of BaseType.DOUBLE, otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • readFile

        public static StringToken readFile​(java.lang.String filename)
                                    throws IllegalActionException
        Get the string text contained in the specified file. The argument is first interpreted using findFile(), so file names relative to the current working directory, the user's home directory, or the classpath are understood. If the file contains text that is a valid expression in the expression language, then that text can interpreted using the eval() function in ptolemy.data.expr.ASTPtFunctionApplicationNode. For example: eval(readFile("filename"))

        Note that readFile() does not work inside applets, use readResource(String) instead.

        Parameters:
        filename - The name of the file to read from.
        Returns:
        A StringToken containing the text contained in the specified file.
        Throws:
        IllegalActionException - If the file cannot be opened.
        See Also:
        ASTPtFunctionApplicationNode, readResource(String)
      • readMatrix

        @Deprecated
        public static DoubleMatrixToken readMatrix​(java.lang.String filename)
                                            throws IllegalActionException
        Deprecated.
        Use eval(readFile()) instead.
        Read a file that contains a matrix of reals in Matlab notation.
        Parameters:
        filename - The filename.
        Returns:
        The matrix defined in the file.
        Throws:
        IllegalActionException - If the file cannot be opened.
      • readResource

        public static StringToken readResource​(java.lang.String name)
                                        throws IllegalActionException
        Get the string text contained in the specified resource, which is a file that is specified relative to the Java classpath. Resource strings look like filenames without a leading slash. If the file contains text that is a valid expression in the expression language, then that text can interpreted using the eval() function in ptolemy.data.expr.ASTPtFunctionApplicationNode. For example: eval(readFile("filename"))

        Parameters:
        name - The name of the resource to read from.
        Returns:
        A StringToken containing the text contained in the specified resource.
        Throws:
        IllegalActionException - If the resource cannot be opened.
        See Also:
        ASTPtFunctionApplicationNode, readFile(String)
      • repeat

        public static ArrayToken repeat​(IntToken numberOfTimes,
                                        Token element)
        Create an array that contains the specified element repeated the specified number of times.
        Parameters:
        numberOfTimes - The number of times to repeat the element.
        element - The element to repeat.
        Returns:
        A new array containing the specified element repeated the specified number of times.
      • repeatReturnType

        public static Type repeatReturnType​(Type type1,
                                            Type type2)
        Return the (exact) return type of the repeat function above. This function always returns an ArrayType whose element type is the second argument.
        Parameters:
        type1 - The type of the first argument to the corresponding function.
        type2 - The type of the second argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • sort

        public static ArrayToken sort​(ArrayToken array)
                               throws IllegalActionException
        Return a new array that is the sorted contents of a specified array, in ascending order. The specified array can contain scalar tokens (except complex) or string tokens. If the specified array is empty, then it is returned.
        Parameters:
        array - An array of scalar tokens.
        Returns:
        A new sorted array.
        Throws:
        IllegalActionException - If the array contains tokens that are complex or are not scalar or string tokens.
      • sortReturnType

        public static Type sortReturnType​(Type type)
        Return the (exact) return type of the sort function above. If the argument is an array type with elements that are strings or scalars other than complex, then return the argument; otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • sortAscending

        public static ArrayToken sortAscending​(ArrayToken array)
                                        throws IllegalActionException
        Return a new array that is the sorted contents of a specified array, in ascending order. The specified array can contain scalar tokens (except complex) or string tokens. If the specified array is empty, then it is returned. This method is identical to sort(), and is provided only for naming uniformity.
        Parameters:
        array - An array of scalar tokens.
        Returns:
        A new sorted array.
        Throws:
        IllegalActionException - If the array contains tokens that are complex or are not scalar or string tokens.
        See Also:
        sort(ArrayToken), sortDescending(ArrayToken)
      • sortAscendingReturnType

        public static Type sortAscendingReturnType​(Type type)
        Return the (exact) return type of the sortAscending function above. If the argument is an array type with elements that are strings or scalars other than complex, then return the argument; otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • sortDescending

        public static ArrayToken sortDescending​(ArrayToken array)
                                         throws IllegalActionException
        Return a new array that is the sorted contents of a specified array, in descending order. The specified array can contain scalar tokens (except complex) or string tokens. If the specified array is empty, then it is returned.
        Parameters:
        array - An array of scalar tokens.
        Returns:
        A new sorted array.
        Throws:
        IllegalActionException - If the array contains tokens that are complex or are not scalar or string tokens.
      • sortDescendingReturnType

        public static Type sortDescendingReturnType​(Type type)
        Return the (exact) return type of the sortDescending function above. If the argument is an array type with elements that are strings or scalars other than complex, then return the argument; otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • subarray

        public static ArrayToken subarray​(ArrayToken array,
                                          IntToken index,
                                          IntToken count)
                                   throws IllegalActionException
        Return the contiguous subarray of the specified array starting at the specified index and of the specified length. If the specified index is out of range, or if the specified length extends beyond the end of the array, then return an empty array with the same type as the specified array.
        Parameters:
        array - The array.
        index - The index of the beginning of the subarray.
        count - The length of the subarray.
        Returns:
        The extracted subarray.
        Throws:
        IllegalActionException - If the index argument is less than zero.
        Since:
        Ptolemy II 4.1
      • subarrayReturnType

        public 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.
        Parameters:
        arrayType - The type of the array argument.
        indexType - The type of the index argument.
        countType - The type of the count argument.
        Returns:
        The extracted subarray.
        Since:
        Ptolemy II 4.1
      • sum

        public static final Token sum​(ArrayToken array)
                               throws IllegalActionException
        Return the sum of the elements in the specified array. This method is polymorphic in that it can sum any array whose elements support addition. There is special support for strings, which are added not via their add method (which concatenates two strings), but are accumulated in a StringBuffer, which is much more efficient.
        Parameters:
        array - An array.
        Returns:
        The sum of the elements of the array.
        Throws:
        IllegalActionException - If the length of the array is zero, or if the array elements do not support addition.
      • sumReturnType

        public static Type sumReturnType​(Type type)
        Return the (exact) return type of the sum function above. If the argument is an array type, then return its element type, otherwise return BaseType.UNKNOWN.
        Parameters:
        type - The type of the argument to the corresponding function.
        Returns:
        The type of the value returned from the corresponding function.
      • totalMemory

        public static LongToken totalMemory()
        Return the approximate number of bytes used by current objects and available for future object allocation.
        Returns:
        The total number of bytes used by the JVM.
        See Also:
        freeMemory()
      • traceEvaluation

        public static java.lang.String traceEvaluation​(java.lang.String string)
                                                throws IllegalActionException
        Evaluate the given string as an expression in the expression language. Instead of returning the resulting value, return a trace of the evaluation, including such useful information as what registered method is actually invoked.
        Parameters:
        string - The string to be parsed and evaluated.
        Returns:
        A string representing an evaluation trace.
        Throws:
        IllegalActionException
      • within

        public static BooleanToken within​(Token token1,
                                          Token token2,
                                          double distance)
                                   throws IllegalActionException
        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. Exactly what this means depends on the data type. This method uses the isCloseTo() method of the first token.
        Parameters:
        token1 - The first token.
        token2 - The second token.
        distance - The distance criterion.
        Returns:
        a true-valued token if the first two arguments are close enough.
        Throws:
        IllegalActionException - If the first two arguments cannot be compared.
      • yesNoQuestion

        public static BooleanToken yesNoQuestion​(StringToken prompt)
        Query the user for a yes-no answer and return a boolean. This function will open a dialog if a GUI is available, and otherwise will use standard input and output.
      • zeroMatrix

        @Deprecated
        public static DoubleMatrixToken zeroMatrix​(int rows,
                                                   int columns)
        Deprecated.
        Use zeroMatrixDouble instead.
        Return a double zero matrix with the given number of rows and columns.
        Returns:
        The zero matrix with the given number of rows and columns.
      • zeroMatrixComplex

        public static ComplexMatrixToken zeroMatrixComplex​(int rows,
                                                           int columns)
        Return a complex zero matrix with the given number of rows and columns.
        Returns:
        The zero matrix with the given number of rows and columns.
      • zeroMatrixDouble

        public static DoubleMatrixToken zeroMatrixDouble​(int rows,
                                                         int columns)
        Return a double zero matrix with the given number of rows and columns.
        Returns:
        The zero matrix with the given number of rows and columns.
      • zeroMatrixInt

        public static IntMatrixToken zeroMatrixInt​(int rows,
                                                   int columns)
        Return a int zero matrix with the given number of rows and columns.
        Returns:
        The zero matrix with the given number of rows and columns.
      • zeroMatrixLong

        public static LongMatrixToken zeroMatrixLong​(int rows,
                                                     int columns)
        Return a long zero matrix with the given number of rows and columns.
        Returns:
        The zero matrix with the given number of rows and columns.