Class SpecialMath


  • final class SpecialMath
    extends java.lang.Object
    Special math functions.
    Since:
    1.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static double TWO_POW_M12
      2^-12.
      private static double TWO_POW_M20
      2^-20.
      private static double TWO_POW_M53
      2^-53.
      private static double TWO_POW_M6
      2^-6.
      private static double X_HIGH
      High threshold to use log1p(x) - x.
      private static double X_LOW
      Low threshold to use log1p(x) - x.
      private static double X_MIN
      Minimum x for log1pmx(x).
    • Constructor Summary

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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static double log1pmx​(double x)
      Returns log(1 + x) - x.
      private static double log1pmxSmall​(double x, double a)
      Returns log(1 + x) - x.
      • Methods inherited from class java.lang.Object

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

      • SpecialMath

        private SpecialMath()
        Private constructor.
    • Method Detail

      • log1pmx

        static double log1pmx​(double x)
        Returns log(1 + x) - x. This function is accurate when x -> 0.

        This function uses a Taylor series expansion when x is small (|x| < 0.01):

         ln(1 + x) - x = -x^2/2 + x^3/3 - x^4/4 + ...
         

        or around 0 (-0.791 <= x <= 1):

         ln(x + a) = ln(a) + 2 [z + z^3/3 + z^5/5 + z^7/7 + ... ]
        
         z = x / (2a + x)
         

        For a = 1:

         ln(x + 1) - x = -x + 2 [z + z^3/3 + z^5/5 + z^7/7 + ... ]
                       = z * (-x + 2z^2 [ 1/3 + z^2/5 + z^4/7 + ... ])
         

        The code is based on the log1pmx documentation for the R DPQ package with addition of the direct Taylor series for tiny x.

        See Abramowitz, M. and Stegun, I. A. (1972) Handbook of Mathematical Functions. New York: Dover. Formulas 4.1.24 and 4.2.29, p.68. Wikipedia: Abramowitz_and_Stegun provides links to the full text which is in public domain.

        Parameters:
        x - Value x
        Returns:
        log(1 + x) - x
      • log1pmxSmall

        private static double log1pmxSmall​(double x,
                                           double a)
        Returns log(1 + x) - x. This function is accurate when x -> 0.

        This function uses a Taylor series expansion when x is small (|x| < 0.01):

         ln(1 + x) - x = -x^2/2 + x^3/3 - x^4/4 + ...
         

        No loop iterations are used as the series is directly expanded for a set number of terms based on the absolute value of x.

        Parameters:
        x - Value x (assumed to be small)
        a - Absolute value of x
        Returns:
        log(1 + x) - x