Class FastMathCalc


  • class FastMathCalc
    extends java.lang.Object
    Class used to compute the classical functions tables.
    Since:
    3.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static double[] FACT
      Factorial table, for Taylor series expansions.
      private static long HEX_40000000
      0x40000000 - used to split a double into two parts, both with the low order bits cleared.
      private static double[][] LN_SPLIT_COEF
      Coefficients for slowLog.
      private static java.lang.String TABLE_END_DECL
      Table end declaration.
      private static java.lang.String TABLE_START_DECL
      Table start declaration.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FastMathCalc()
      Private Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void buildSinCosTables​(double[] SINE_TABLE_A, double[] SINE_TABLE_B, double[] COSINE_TABLE_A, double[] COSINE_TABLE_B, int SINE_TABLE_LEN, double[] TANGENT_TABLE_A, double[] TANGENT_TABLE_B)
      Build the sine and cosine tables.
      private static void checkLen​(int expectedLen, int actual)
      Check two lengths are equal.
      (package private) static double expint​(int p, double[] result)
      Compute exp(p) for a integer p in extended precision.
      (package private) static java.lang.String format​(double d)
      Format a double.
      (package private) static void printarray​(java.io.PrintStream out, java.lang.String name, int expectedLen, double[] array)
      Print an array.
      (package private) static void printarray​(java.io.PrintStream out, java.lang.String name, int expectedLen, double[][] array2d)
      Print an array.
      private static void quadMult​(double[] a, double[] b, double[] result)
      Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.
      private static void resplit​(double[] a)
      Recompute a split.
      (package private) static double slowCos​(double x, double[] result)
      For x between 0 and pi/4 compute cosine using Talor series cos(x) = 1 - x^2/2! + x^4/4! ...
      (package private) static double slowexp​(double x, double[] result)
      For x between 0 and 1, returns exp(x), uses extended precision
      (package private) static double[] slowLog​(double xi)
      xi in the range of [1, 2].
      (package private) static double slowSin​(double x, double[] result)
      For x between 0 and pi/4 compute sine using Taylor expansion: sin(x) = x - x^3/3! + x^5/5! - x^7/7! ...
      private static void split​(double d, double[] split)
      Compute split[0], split[1] such that their sum is equal to d, and split[0] has its 30 least significant bits as zero.
      private static void splitAdd​(double[] a, double[] b, double[] ans)
      Add two numbers in split form.
      private static void splitMult​(double[] a, double[] b, double[] ans)
      Multiply two numbers in split form.
      (package private) static void splitReciprocal​(double[] in, double[] result)
      Compute the reciprocal of in.
      • Methods inherited from class java.lang.Object

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

      • HEX_40000000

        private static final long HEX_40000000
        0x40000000 - used to split a double into two parts, both with the low order bits cleared. Equivalent to 2^30.
        See Also:
        Constant Field Values
      • FACT

        private static final double[] FACT
        Factorial table, for Taylor series expansions. 0!, 1!, 2!, ... 19!
      • LN_SPLIT_COEF

        private static final double[][] LN_SPLIT_COEF
        Coefficients for slowLog.
      • TABLE_START_DECL

        private static final java.lang.String TABLE_START_DECL
        Table start declaration.
        See Also:
        Constant Field Values
      • TABLE_END_DECL

        private static final java.lang.String TABLE_END_DECL
        Table end declaration.
        See Also:
        Constant Field Values
    • Constructor Detail

      • FastMathCalc

        private FastMathCalc()
        Private Constructor.
    • Method Detail

      • buildSinCosTables

        private static void buildSinCosTables​(double[] SINE_TABLE_A,
                                              double[] SINE_TABLE_B,
                                              double[] COSINE_TABLE_A,
                                              double[] COSINE_TABLE_B,
                                              int SINE_TABLE_LEN,
                                              double[] TANGENT_TABLE_A,
                                              double[] TANGENT_TABLE_B)
        Build the sine and cosine tables.
        Parameters:
        SINE_TABLE_A - table of the most significant part of the sines
        SINE_TABLE_B - table of the least significant part of the sines
        COSINE_TABLE_A - table of the most significant part of the cosines
        COSINE_TABLE_B - table of the most significant part of the cosines
        SINE_TABLE_LEN - length of the tables
        TANGENT_TABLE_A - table of the most significant part of the tangents
        TANGENT_TABLE_B - table of the most significant part of the tangents
      • slowCos

        static double slowCos​(double x,
                              double[] result)
        For x between 0 and pi/4 compute cosine using Talor series cos(x) = 1 - x^2/2! + x^4/4! ...
        Parameters:
        x - number from which cosine is requested
        result - placeholder where to put the result in extended precision (may be null)
        Returns:
        cos(x)
      • slowSin

        static double slowSin​(double x,
                              double[] result)
        For x between 0 and pi/4 compute sine using Taylor expansion: sin(x) = x - x^3/3! + x^5/5! - x^7/7! ...
        Parameters:
        x - number from which sine is requested
        result - placeholder where to put the result in extended precision (may be null)
        Returns:
        sin(x)
      • slowexp

        static double slowexp​(double x,
                              double[] result)
        For x between 0 and 1, returns exp(x), uses extended precision
        Parameters:
        x - argument of exponential
        result - placeholder where to place exp(x) split in two terms for extra precision (i.e. exp(x) = result[0] + result[1]
        Returns:
        exp(x)
      • split

        private static void split​(double d,
                                  double[] split)
        Compute split[0], split[1] such that their sum is equal to d, and split[0] has its 30 least significant bits as zero.
        Parameters:
        d - number to split
        split - placeholder where to place the result
      • resplit

        private static void resplit​(double[] a)
        Recompute a split.
        Parameters:
        a - input/out array containing the split, changed on output
      • splitMult

        private static void splitMult​(double[] a,
                                      double[] b,
                                      double[] ans)
        Multiply two numbers in split form.
        Parameters:
        a - first term of multiplication
        b - second term of multiplication
        ans - placeholder where to put the result
      • splitAdd

        private static void splitAdd​(double[] a,
                                     double[] b,
                                     double[] ans)
        Add two numbers in split form.
        Parameters:
        a - first term of addition
        b - second term of addition
        ans - placeholder where to put the result
      • splitReciprocal

        static void splitReciprocal​(double[] in,
                                    double[] result)
        Compute the reciprocal of in. Use the following algorithm. in = c + d. want to find x + y such that x+y = 1/(c+d) and x is much larger than y and x has several zero bits on the right. Set b = 1/(2^22), a = 1 - b. Thus (a+b) = 1. Use following identity to compute (a+b)/(c+d) (a+b)/(c+d) = a/c + (bc - ad) / (c^2 + cd) set x = a/c and y = (bc - ad) / (c^2 + cd) This will be close to the right answer, but there will be some rounding in the calculation of X. So by carefully computing 1 - (c+d)(x+y) we can compute an error and add that back in. This is done carefully so that terms of similar size are subtracted first.
        Parameters:
        in - initial number, in split form
        result - placeholder where to put the result
      • quadMult

        private static void quadMult​(double[] a,
                                     double[] b,
                                     double[] result)
        Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.
        Parameters:
        a - first term of the multiplication
        b - second term of the multiplication
        result - placeholder where to put the result
      • expint

        static double expint​(int p,
                             double[] result)
        Compute exp(p) for a integer p in extended precision.
        Parameters:
        p - integer whose exponential is requested
        result - placeholder where to put the result in extended precision
        Returns:
        exp(p) in standard precision (equal to result[0] + result[1])
      • slowLog

        static double[] slowLog​(double xi)
        xi in the range of [1, 2]. 3 5 7 x+1 / x x x \ ln ----- = 2 * | x + ---- + ---- + ---- + ... | 1-x \ 3 5 7 / So, compute a Remez approximation of the following function ln ((sqrt(x)+1)/(1-sqrt(x))) / x This will be an even function with only positive coefficents. x is in the range [0 - 1/3]. Transform xi for input to the above function by setting x = (xi-1)/(xi+1). Input to the polynomial is x^2, then the result is multiplied by x.
        Parameters:
        xi - number from which log is requested
        Returns:
        log(xi)
      • printarray

        static void printarray​(java.io.PrintStream out,
                               java.lang.String name,
                               int expectedLen,
                               double[][] array2d)
        Print an array.
        Parameters:
        out - text output stream where output should be printed
        name - array name
        expectedLen - expected length of the array
        array2d - array data
      • printarray

        static void printarray​(java.io.PrintStream out,
                               java.lang.String name,
                               int expectedLen,
                               double[] array)
        Print an array.
        Parameters:
        out - text output stream where output should be printed
        name - array name
        expectedLen - expected length of the array
        array - array data
      • format

        static java.lang.String format​(double d)
        Format a double.
        Parameters:
        d - double number to format
        Returns:
        formatted number
      • checkLen

        private static void checkLen​(int expectedLen,
                                     int actual)
                              throws DimensionMismatchException
        Check two lengths are equal.
        Parameters:
        expectedLen - expected length
        actual - actual length
        Throws:
        DimensionMismatchException - if the two lengths are not equal