Package ptolemy.math

Class FractionArrayMath


  • public class FractionArrayMath
    extends java.lang.Object
    A library for mathematical operations on Fraction arrays.

    Unless explicitly noted otherwise, all array arguments are assumed to be non-null. If a null array is passed to a method, a NullPointerException will be thrown in the method or called methods.

    Since:
    Ptolemy II 5.0
    Version:
    $Id$
    Author:
    Adam Cataldo
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Red (acataldo)
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected FractionArrayMath()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static int _commonLength​(Fraction[] array1, Fraction[] array2, java.lang.String methodName)
      Throw an exception if the two arrays are not of the same length, or if either array is null.
      static Fraction[] add​(Fraction[] array, Fraction z)
      Return a new array that is the formed by adding z to each element of the input array.
      static Fraction[] add​(Fraction[] array1, Fraction[] array2)
      Return a new array that is the element-by-element sum of the two input arrays.
      static Fraction[] allocCopy​(Fraction[] array)
      Return a new array that is a copy of the array argument.
      static Fraction[] append​(Fraction[] array1, int idx1, int length1, Fraction[] array2, int idx2, int length2)
      Return a new array that is the result of appending length2 elements of array2, starting from the array2[idx2] to length1 elements of array1, starting from array1[idx1].
      static Fraction[] append​(Fraction[] array1, Fraction[] array2)
      Return a new array that is the result of appending array2 to the end of array1.
      static Fraction[] divide​(Fraction[] num, Fraction[] den)
      Return a new array that is the element-by-element division of the first array by the second array.
      static Fraction dotProduct​(Fraction[] array1, Fraction[] array2)
      Return the dot product of the two arrays.
      static boolean equals​(Fraction[] array1, Fraction[] array2)
      Returns true if the two input arrays have all elements equal.
      static Fraction[] multiply​(Fraction[] array, Fraction factor)
      Return a new array that is constructed from the argument by multiplying each element in the array by the second argument, which is a Fraction.
      static Fraction[] multiply​(Fraction[] array1, Fraction[] array2)
      Return a new array that is the element-by-element multiplication of the two input arrays.
      static Fraction[] negative​(Fraction[] array)
      Return a new array that is the formed by the additive inverse of each element of the input array (-array[i]).
      static Fraction[] subtract​(Fraction[] array1, Fraction[] array2)
      Return a new array that is the element-by-element difference of the two input arrays, i.e.
      static Fraction sum​(Fraction[] array)
      Return the sum of the elements in the array.
      static double[] toDoubleArray​(Fraction[] array)
      Return a new array that is formed by converting the Fractions in the argument array to doubles.
      static java.lang.String toString​(Fraction[] array)
      Return a new String representing the array, formatted as in Java array initializers.
      static java.lang.String toString​(Fraction[] array, java.lang.String elementDelimiter, java.lang.String vectorBegin, java.lang.String vectorEnd)
      Return a new String representing the array, formatted as specified by the ArrayStringFormat argument.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FractionArrayMath

        protected FractionArrayMath()
    • Method Detail

      • add

        public static final Fraction[] add​(Fraction[] array,
                                           Fraction z)
        Return a new array that is the formed by adding z to each element of the input array.
      • add

        public static final Fraction[] add​(Fraction[] array1,
                                           Fraction[] array2)
        Return a new array that is the element-by-element sum of the two input arrays. If the lengths of both arrays are 0, return a new array of length 0. If the two arrays do not have the same length, throw an IllegalArgumentException.
      • allocCopy

        public static final Fraction[] allocCopy​(Fraction[] array)
        Return a new array that is a copy of the array argument.
        Parameters:
        array - An array of Fractions.
        Returns:
        A new array of Fractions.
      • append

        public static final Fraction[] append​(Fraction[] array1,
                                              Fraction[] array2)
        Return a new array that is the result of appending array2 to the end of array1. This method simply calls append(array1, 0, array1.length, array2, 0, array2.length)
      • append

        public static final Fraction[] append​(Fraction[] array1,
                                              int idx1,
                                              int length1,
                                              Fraction[] array2,
                                              int idx2,
                                              int length2)
        Return a new array that is the result of appending length2 elements of array2, starting from the array2[idx2] to length1 elements of array1, starting from array1[idx1]. Appending empty arrays is supported. In that case, the corresponding idx may be any number. Allow System.arraycopy() to throw array access exceptions if idx .. idx + length - 1 are not all valid array indices, for both of the arrays.
        Parameters:
        array1 - The first array of Fractions.
        idx1 - The starting index for array1.
        length1 - The number of elements of array1 to use.
        array2 - The second array of Fractions, which is appended.
        idx2 - The starting index for array2.
        length2 - The number of elements of array2 to append.
        Returns:
        A new array of doubles.
      • divide

        public static final Fraction[] divide​(Fraction[] num,
                                              Fraction[] den)
        Return a new array that is the element-by-element division of the first array by the second array.
        Parameters:
        num - The numerator array of Fractions.
        den - The denominator array of Fractions.
        Returns:
        A new array of Fractions.
      • dotProduct

        public static final Fraction dotProduct​(Fraction[] array1,
                                                Fraction[] array2)
        Return the dot product of the two arrays. If the lengths of the array are both 0, return 0/1. If the two arrays do not have the same length, throw an IllegalArgumentException.
      • equals

        public static final boolean equals​(Fraction[] array1,
                                           Fraction[] array2)
        Returns true if the two input arrays have all elements equal.
        Parameters:
        array1 - The first input array.
        array2 - The second input array.
        Returns:
        True if array1 == array2.
      • multiply

        public static final Fraction[] multiply​(Fraction[] array1,
                                                Fraction[] array2)
        Return a new array that is the element-by-element multiplication of the two input arrays. If the lengths of both arrays are 0, return a new array of length 0. If the two arrays do not have the same length, throw an IllegalArgumentException.
      • multiply

        public static final Fraction[] multiply​(Fraction[] array,
                                                Fraction factor)
        Return a new array that is constructed from the argument by multiplying each element in the array by the second argument, which is a Fraction. If the sizes of the array is 0, return a new array of size 0.
        Parameters:
        array - An array of Fractions.
        factor - A Fraction.
        Returns:
        A new array of Fractions.
      • negative

        public static final Fraction[] negative​(Fraction[] array)
        Return a new array that is the formed by the additive inverse of each element of the input array (-array[i]).
      • subtract

        public static final Fraction[] subtract​(Fraction[] array1,
                                                Fraction[] array2)
        Return a new array that is the element-by-element difference of the two input arrays, i.e. the first array minus the second array (array1[i] - array2[i]). If the lengths of both arrays are 0, return a new array of length 0.
      • sum

        public static final Fraction sum​(Fraction[] array)
        Return the sum of the elements in the array. Return 0/1 if the length of the array is 0.
      • toDoubleArray

        public static final double[] toDoubleArray​(Fraction[] array)
        Return a new array that is formed by converting the Fractions in the argument array to doubles. If the length of the argument array is 0, return a new array of length 0.
        Parameters:
        array - An array of Fractions.
        Returns:
        A new array of doubles.
      • toString

        public static final java.lang.String toString​(Fraction[] array)
        Return a new String representing the array, formatted as in Java array initializers.
      • toString

        public static final java.lang.String toString​(Fraction[] array,
                                                      java.lang.String elementDelimiter,
                                                      java.lang.String vectorBegin,
                                                      java.lang.String vectorEnd)
        Return a new String representing the array, formatted as specified by the ArrayStringFormat argument. To get a String in the Ptolemy expression language format, call this method with ArrayStringFormat.exprASFormat as the format argument.
      • _commonLength

        protected static final int _commonLength​(Fraction[] array1,
                                                 Fraction[] array2,
                                                 java.lang.String methodName)
        Throw an exception if the two arrays are not of the same length, or if either array is null. An exception is NOT thrown if both arrays are of length 0. If no exception is thrown, return the common length of the arrays.
        Parameters:
        array1 - The first array of Fractions.
        array2 - The second array of Fractions.
        methodName - A String representing the method name of the caller, without parentheses.
        Returns:
        The common length of both arrays.