Package pulse.math

Class MathUtils


  • public class MathUtils
    extends Object
    A utility class containing some common methods potentially enhancing the performance of standard mathematical operations. Largely based on https://martin.ankerl.com
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static double EQUALS_TOLERANCE  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean approximatelyEquals​(double a, double b)
      Checks if two numbers are approximately equal by comparing the modulus of their difference to 1.0E-5.
      static double atanh​(double a)
      Calculate the hyperbolic arctangent (exact).
      static double fastAbs​(double a)
      A fast bitwise calculation of the absolute value.
      static double fastExp​(double val)
      Approximate calculation of exp(val).
      static double fastLog​(double val)
      A highly-approximate calculation of ln(val).
      static double fastPowGeneral​(double a, double b)
      Approximate calculation of ab.
      static long fastPowInt​(long x, int y)
      Rapid exponentiation where the base and the exponent are integer value.
      static double fastPowLoop​(double a, int b)
      Rapid calculation of b-th power of a, which is simply a repeated multiplication, in case of positive b, or a repeated division.
      static int fastPowSgn​(int n)
      Rapid calculation of (-1)n using a ternary conditional operator.
      static double fastTanh​(double a)
      A method for the approximate calculation of the hyperbolic tangent.
      static double linearExtrapolation​(double[] p1, double[] p2, double xStar)
      Performs linear extrapolation according to the rule y(xStar) = y1 + (xStar - x1)/(x2 - x1) * (y2 - y1)
    • Method Detail

      • approximatelyEquals

        public static boolean approximatelyEquals​(double a,
                                                  double b)
        Checks if two numbers are approximately equal by comparing the modulus of their difference to 1.0E-5.
        Parameters:
        a - a number
        b - another number
        Returns:
        true if numbers are approximately equal, false otherwise
      • fastTanh

        public static double fastTanh​(double a)
        A method for the approximate calculation of the hyperbolic tangent.
        Parameters:
        a - the argument of tanh(a).
        Returns:
        the approximate tanh(a) value.
      • atanh

        public static double atanh​(double a)
        Calculate the hyperbolic arctangent (exact).
        Parameters:
        a - the argument of atanh(a)
        Returns:
        the exact value of the atanh(a).
      • fastPowLoop

        public static double fastPowLoop​(double a,
                                         int b)
        Rapid calculation of b-th power of a, which is simply a repeated multiplication, in case of positive b, or a repeated division.
        Parameters:
        a - the base .
        b - the exponent.
        Returns:
        the exponentiation result.
      • fastPowSgn

        public static int fastPowSgn​(int n)
        Rapid calculation of (-1)n using a ternary conditional operator.
        Parameters:
        n - a positive integer number
        Returns:
        the result of exponentiation.
      • fastPowInt

        public static long fastPowInt​(long x,
                                      int y)
        Rapid exponentiation where the base and the exponent are integer value. Uses bitwise shiftiing.
        Parameters:
        x - the base
        y - the exponent
        Returns:
        result of the exponentiation
      • fastExp

        public static double fastExp​(double val)
        Approximate calculation of exp(val).
        Parameters:
        val - the argument of the exponent.
        Returns:
        the result.
      • fastLog

        public static double fastLog​(double val)
        A highly-approximate calculation of ln(val).
        Parameters:
        val - the argument of the natural logarithm.
        Returns:
        the result.
      • fastPowGeneral

        public static double fastPowGeneral​(double a,
                                            double b)
        Approximate calculation of ab.
        Parameters:
        a - the base (real)
        b - the exponent (real)
        Returns:
        the result of calculation
      • fastAbs

        public static double fastAbs​(double a)
        A fast bitwise calculation of the absolute value. Arguably faster than Math.abs.
        Parameters:
        a - the argument
        Returns:
        the calculation result
      • linearExtrapolation

        public static double linearExtrapolation​(double[] p1,
                                                 double[] p2,
                                                 double xStar)
        Performs linear extrapolation according to the rule y(xStar) = y1 + (xStar - x1)/(x2 - x1) * (y2 - y1)
        Parameters:
        p1 - point 1 (x1, y1)
        p2 - point 2 (x2, y2)
        xStar - interpolation x coordinate
        Returns: