Class MathHelper

java.lang.Object
net.simplace.core.service.MathHelper

public class MathHelper extends Object
Static helper methods for aggregating lists of numbers (MIN, MAX, AVG, SUM, COUNT), element-wise array arithmetic, Gauss-Legendre integration and linear regression.
Author:
XPMUser
  • Constructor Details

    • MathHelper

      public MathHelper()
  • Method Details

    • MAX

      public static Number MAX(LinkedList aLinkedList, int i)
      Calculates the maximum of a list of values. null entries are skipped. The data type is determined from the first entry of the list.
      Parameters:
      aLinkedList - list of values, either numbers or (if i > -1) Double[] or Integer[] arrays
      i - index of the array element to use if the list contains arrays; -1 if the list contains plain numbers
      Returns:
      the maximum, or null if the list is null, empty or contains values of an unsupported type
    • MIN

      public static Number MIN(LinkedList aLinkedList, int i)
      Calculates the minimum of a list of values. null entries are skipped. The data type is determined from the first entry of the list.
      Parameters:
      aLinkedList - list of values, either numbers or (if i > -1) Double[] or Integer[] arrays
      i - index of the array element to use if the list contains arrays; -1 if the list contains plain numbers
      Returns:
      the minimum, or null if the list is null, empty or contains values of an unsupported type
    • AVG

      public static Number AVG(LinkedList aLinkedList, FWSimVariable.DATA_TYPE aDataType, int i)
      Calculates the average of a list of values. null entries are skipped and do not count as values. For integer data the average is rounded to the nearest integer.
      Parameters:
      aLinkedList - list of values, either numbers or Double[]/Integer[] arrays
      aDataType - data type of the list entries (DOUBLE, INT, DOUBLEARRAY or INTARRAY)
      i - index of the array element to use if the list contains arrays; -1 otherwise
      Returns:
      the average (Double or Integer), or null if the list is null or empty
    • SUM

      public static Number SUM(LinkedList aLinkedList, FWSimVariable.DATA_TYPE aDataType, int i)
      Calculates the sum of a list of values. null entries are skipped.
      Parameters:
      aLinkedList - list of values, either numbers or Double[]/Integer[] arrays
      aDataType - data type of the list entries (DOUBLE, INT, DOUBLEARRAY or INTARRAY)
      i - index of the array element to use if the list contains arrays; -1 otherwise
      Returns:
      the sum, or null if the list is null or empty
    • COUNT

      public static Integer COUNT(LinkedList aLinkedList)
      Counts the non-null entries of a list.
      Parameters:
      aLinkedList - list of values
      Returns:
      the number of non-null entries, or null if the list is null or empty
    • ADD

      public static Double[] ADD(Double[] aFirstObjectToCalculateWith, Double[] aSecondObjectToCalculateWith)
      Adds two arrays element by element. The result is written into the first array.
      Parameters:
      aFirstObjectToCalculateWith - first summand; modified in place and returned
      aSecondObjectToCalculateWith - second summand; must be at least as long as the first array
      Returns:
      the first array, now containing the element-wise sums
    • ADD

      public static Integer[] ADD(Integer[] aFirstObjectToCalculateWith, Integer[] aSecondObjectToCalculateWith)
      Adds two arrays element by element. The result is written into the first array.
      Parameters:
      aFirstObjectToCalculateWith - first summand; modified in place and returned
      aSecondObjectToCalculateWith - second summand; must be at least as long as the first array
      Returns:
      the first array, now containing the element-wise sums
    • MULTIPLY

      public static Double[] MULTIPLY(Double[] aFirstObjectToCalculateWith, Object aSecondObjectToCalculateWith)
      Multiplies an array either by a scalar or element by element by a second array. The result is written into the first array.
      Parameters:
      aFirstObjectToCalculateWith - array to multiply; modified in place and returned
      aSecondObjectToCalculateWith - a Number (scalar factor) or a Double[] of at least the same length; other types leave the array unchanged
      Returns:
      the first array, now containing the products
    • MULTIPLY

      public static Integer[] MULTIPLY(Integer[] aFirstObjectToCalculateWith, Object aSecondObjectToCalculateWith)
      Multiplies an array either by a scalar or element by element by a second array. The result is written into the first array. A scalar factor is truncated to an integer before multiplying.
      Parameters:
      aFirstObjectToCalculateWith - array to multiply; modified in place and returned
      aSecondObjectToCalculateWith - a Number (scalar factor) or an Integer[] of at least the same length; other types leave the array unchanged
      Returns:
      the first array, now containing the products
    • GaussIntegrationWeight

      public static double GaussIntegrationWeight(int n, int i)
      Returns a weight for Gauss-Legendre integration.
      Parameters:
      n - number of integration points (1 to 5)
      i - zero-based index of the weight (0 to n-1)
      Returns:
      the weight, or 0 if n or i is out of range
    • GaussIntegrationX

      public static double GaussIntegrationX(int n, int i)
      Returns an integration point (x-value in [-1, 1]) for Gauss-Legendre integration.
      Parameters:
      n - number of integration points (1 to 5)
      i - zero-based index of the point (0 to n-1)
      Returns:
      the x-value, or 0 if n or i is out of range
    • calulateLinearRegression

      @Deprecated public static void calulateLinearRegression(int ind, double[] x, double[] y, FWSimVariable<Double> aRegressionM, FWSimVariable<Double> aRegressionB)
      Calculates a least-squares linear regression y = m * x + b and stores slope and intercept in the given variables. Range errors while setting the values are logged, not thrown.
      Parameters:
      ind - number of data points to use from x and y
      x - x-values
      y - y-values
      aRegressionM - receives the calculated slope m
      aRegressionB - receives the calculated intercept b
    • calculateLinearRegression

      public static void calculateLinearRegression(int ind, double[] x, double[] y, FWSimVariable<Double> aRegressionM, FWSimVariable<Double> aRegressionB)
      Calculates a least-squares linear regression y = m * x + b and stores slope and intercept in the given variables. Range errors while setting the values are logged, not thrown.
      Parameters:
      ind - number of data points to use from x and y
      x - x-values
      y - y-values
      aRegressionM - receives the calculated slope m
      aRegressionB - receives the calculated intercept b