Class TMath

java.lang.Object
edu.cmu.tetrad.util.TMath

public final class TMath extends Object
Unified math interface for Tetrad.

Allows switching between Math, StrictMath, and Math implementations from a single place for experimentation and benchmarking.

If IMPL is static final the JVM will constant-fold the switch and inline the selected implementation.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Enumerates the available implementation strategies for mathematical computations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    The mathematical constant e, which is the base of the natural logarithm.
    static final double
    A constant holding the value of π (pi), the ratio of a circle's circumference to its diameter.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    abs(double x)
    Computes the absolute value of the given double using the specified implementation.
    static float
    abs(float x)
    Computes the absolute value of the given float using the specified implementation.
    static int
    abs(int x)
    Computes the absolute value of the given integer using the specified implementation.
    static long
    abs(long x)
    Computes the absolute value of the given long integer using the specified implementation.
    static double
    acos(double x)
    Calculates the arc cosine (inverse cosine) of a given value.
    static double
    asin(double x)
    Computes the arc sine (inverse sine) of the given value.
    static double
    atan(double x)
    Computes the arc tangent of a value.
    static double
    atan2(double y, double x)
    Computes the angle θ (in radians) between the positive x-axis of a plane and the point (x, y) on it.
    static double
    cbrt(double x)
    Computes the cube root of a given double value using the specified implementation strategy.
    static double
    ceil(double x)
    Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
    static double
    copySign(double magnitude, double sign)
    Returns the first floating-point argument with the sign of the second floating-point argument.
    static double
    cos(double x)
    Computes the cosine of a given angle in radians.
    static double
    cosh(double x)
    Computes the hyperbolic cosine of a given value.
    static double
    exp(double x)
    Computes the absolute value of a double value using the specified implementation.
    static double
    expm1(double x)
    Computes the natural logarithm of a double value using the specified implementation.
    static double
    floor(double x)
    Computes the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
    static int
    getExponent(double d)
    Returns the unbiased exponent used in the representation of the floating-point value.
    static double
    hypot(double x, double y)
    Calculates the square root of the sum of the squares of two arguments without intermediate overflow or underflow.
    static double
    log(double x)
    Computes the base-2 logarithm of a double value using the specified implementation.
    static double
    log10(double x)
    Computes the base-10 logarithm of a double value using the specified implementation.
    static double
    log1p(double x)
    Computes the logarithm of a double value using the specified implementation.
    static double
    max(double a, double b)
    Computes the maximum of two double values using the specified implementation.
    static float
    max(float a, float b)
    Computes the maximum of two float values using the specified implementation.
    static int
    max(int a, int b)
    Computes the maximum of two integers using the specified implementation.
    static long
    max(long a, long b)
    Computes the maximum of two long integers using the specified implementation.
    static double
    min(double a, double b)
    Computes the minimum of two double values using the specified implementation.
    static float
    min(float a, float b)
    Computes the minimum of two float values using the specified implementation.
    static int
    min(int a, int b)
    Computes the minimum of two integer values using the specified implementation.
    static long
    min(long a, long b)
    Computes the minimum of two long integers using the specified implementation.
    static double
    nextAfter(double start, double direction)
    Returns the floating-point number that is adjacent to the first argument in the direction of the second argument.
    static double
    nextDown(double x)
    Returns the floating-point value adjacent to the argument in the direction of negative infinity.
    static double
    nextUp(double x)
    Returns the floating-point value adjacent to the input in the direction of positive infinity.
    static double
    pow(double a, double b)
    Computes the value of the first argument raised to the power of the second argument using the specified internal implementation.
    static double
    Generates a random double value between 0.0 (inclusive) and 1.0 (exclusive).
    static double
    rint(double x)
    Returns the closest value to the argument, with ties rounding to the even neighbor.
    static long
    round(double x)
    Rounds the specified floating-point value to the nearest long value.
    static int
    round(float x)
    Rounds the given floating-point value to the nearest integer.
    static double
    scalb(double d, int scaleFactor)
    Returns a floating-point value obtained by scaling the specified floating-point value `d` by an integer power of two specified by `scaleFactor`.
    static double
    signum(double d)
    Calculates the signum function of the specified double value.
    static double
    sin(double x)
    Computes the trigonometric sine of an angle given in radians.
    static double
    sinh(double x)
    Computes the hyperbolic sine of a given angle in radians.
    static double
    sqrt(double x)
    Computes the square root of the given double value using the specified implementation.
    static double
    tan(double x)
    Computes the trigonometric tangent of an angle.
    static double
    tanh(double x)
    Computes the hyperbolic tangent of a given value.
    static double
    toDegrees(double x)
    Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
    static double
    toRadians(double x)
    Converts an angle measured in degrees to an approximately equivalent angle in radians.
    static double
    ulp(double x)
    Returns the size of an ulp (unit in the last place) of the argument.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PI

      public static final double PI
      A constant holding the value of π (pi), the ratio of a circle's circumference to its diameter. This value is approximately 3.14159.
      See Also:
    • E

      public static final double E
      The mathematical constant e, which is the base of the natural logarithm. This constant is approximately equal to 2.718281828459045 and is defined in Math.E.
      See Also:
  • Method Details

    • abs

      public static int abs(int x)
      Computes the absolute value of the given integer using the specified implementation.
      Parameters:
      x - the integer value for which the absolute value is to be computed
      Returns:
      the absolute value of the input integer
    • abs

      public static long abs(long x)
      Computes the absolute value of the given long integer using the specified implementation.
      Parameters:
      x - the long integer value for which the absolute value is to be computed
      Returns:
      the absolute value of the input long integer
    • abs

      public static float abs(float x)
      Computes the absolute value of the given float using the specified implementation.
      Parameters:
      x - the float value for which the absolute value is to be computed
      Returns:
      the absolute value of the input float
    • abs

      public static double abs(double x)
      Computes the absolute value of the given double using the specified implementation.
      Parameters:
      x - the double value for which the absolute value is to be computed
      Returns:
      the absolute value of the input double
    • max

      public static int max(int a, int b)
      Computes the maximum of two integers using the specified implementation.
      Parameters:
      a - the first integer value
      b - the second integer value
      Returns:
      the maximum of the two input integers
    • max

      public static long max(long a, long b)
      Computes the maximum of two long integers using the specified implementation.
      Parameters:
      a - the first long integer value
      b - the second long integer value
      Returns:
      the maximum of the two input long integers
    • max

      public static float max(float a, float b)
      Computes the maximum of two float values using the specified implementation.
      Parameters:
      a - the first float value
      b - the second float value
      Returns:
      the maximum of the two input float values
    • max

      public static double max(double a, double b)
      Computes the maximum of two double values using the specified implementation.
      Parameters:
      a - the first double value
      b - the second double value
      Returns:
      the maximum of the two input double values
    • min

      public static int min(int a, int b)
      Computes the minimum of two integer values using the specified implementation.
      Parameters:
      a - the first integer value
      b - the second integer value
      Returns:
      the minimum of the two input integer values
    • min

      public static long min(long a, long b)
      Computes the minimum of two long integers using the specified implementation.
      Parameters:
      a - the first long integer value
      b - the second long integer value
      Returns:
      the minimum of the two input long integers
    • min

      public static float min(float a, float b)
      Computes the minimum of two float values using the specified implementation.
      Parameters:
      a - the first float value
      b - the second float value
      Returns:
      the minimum of the two input float values
    • min

      public static double min(double a, double b)
      Computes the minimum of two double values using the specified implementation.
      Parameters:
      a - the first double value
      b - the second double value
      Returns:
      the minimum of the two input double values
    • exp

      public static double exp(double x)
      Computes the absolute value of a double value using the specified implementation.
      Parameters:
      x - the input double value
      Returns:
      the absolute value of the input double value
    • expm1

      public static double expm1(double x)
      Computes the natural logarithm of a double value using the specified implementation.
      Parameters:
      x - the input double value
      Returns:
      the natural logarithm of the input double value
    • log

      public static double log(double x)
      Computes the base-2 logarithm of a double value using the specified implementation.
      Parameters:
      x - the input double value
      Returns:
      the base-2 logarithm of the input double value
    • log10

      public static double log10(double x)
      Computes the base-10 logarithm of a double value using the specified implementation.
      Parameters:
      x - the input double value
      Returns:
      the base-10 logarithm of the input double value
    • log1p

      public static double log1p(double x)
      Computes the logarithm of a double value using the specified implementation.
      Parameters:
      x - the input double value
      Returns:
      the logarithm of the input double value
    • pow

      public static double pow(double a, double b)
      Computes the value of the first argument raised to the power of the second argument using the specified internal implementation.
      Parameters:
      a - the base value
      b - the exponent value
      Returns:
      the result of raising the base value to the power of the exponent
    • sqrt

      public static double sqrt(double x)
      Computes the square root of the given double value using the specified implementation. Depending on the underlying implementation, this method may use StrictMath.sqrt, FastMath.sqrt, or Math.sqrt to calculate the result.
      Parameters:
      x - the double value for which the square root is to be computed
      Returns:
      the square root of the input value or NaN if the input value is negative
    • cbrt

      public static double cbrt(double x)
      Computes the cube root of a given double value using the specified implementation strategy.
      Parameters:
      x - the value for which the cube root is to be calculated
      Returns:
      the cube root of the input value
    • hypot

      public static double hypot(double x, double y)
      Calculates the square root of the sum of the squares of two arguments without intermediate overflow or underflow.
      Parameters:
      x - the first value
      y - the second value
      Returns:
      the square root of (x² + y²)
    • sin

      public static double sin(double x)
      Computes the trigonometric sine of an angle given in radians.
      Parameters:
      x - the angle in radians for which the sine value is to be calculated
      Returns:
      the sine of the specified angle
    • cos

      public static double cos(double x)
      Computes the cosine of a given angle in radians.
      Parameters:
      x - the angle in radians for which the cosine is to be calculated
      Returns:
      the cosine of the specified angle
    • tan

      public static double tan(double x)
      Computes the trigonometric tangent of an angle.
      Parameters:
      x - The angle, in radians, for which the tangent is to be calculated.
      Returns:
      The tangent of the specified angle.
    • asin

      public static double asin(double x)
      Computes the arc sine (inverse sine) of the given value. The returned angle is in radians and lies in the range -π/2 through π/2.
      Parameters:
      x - the value whose arc sine is to be computed, must be in the range -1 to 1
      Returns:
      the arc sine of the argument, in radians
    • acos

      public static double acos(double x)
      Calculates the arc cosine (inverse cosine) of a given value. The method returns the angle in radians whose cosine is the specified value.
      Parameters:
      x - the value whose arc cosine is to be calculated. The value must be within the range -1.0 to 1.0, inclusive.
      Returns:
      the arc cosine of the specified value, in the range 0.0 through π (inclusive).
    • atan

      public static double atan(double x)
      Computes the arc tangent of a value. This method returns the angle in radians whose tangent is the given value.
      Parameters:
      x - the value whose arc tangent is to be calculated.
      Returns:
      the arc tangent of the specified value, in radians.
    • atan2

      public static double atan2(double y, double x)
      Computes the angle θ (in radians) between the positive x-axis of a plane and the point (x, y) on it. The angle is measured counterclockwise from the positive x-axis, and it is within the range of -π to π.
      Parameters:
      y - the ordinate (y-coordinate) of the point
      x - the abscissa (x-coordinate) of the point
      Returns:
      the angle θ in radians between the positive x-axis and the line segment from the origin to the point (x, y)
    • sinh

      public static double sinh(double x)
      Computes the hyperbolic sine of a given angle in radians.
      Parameters:
      x - the angle in radians for which the hyperbolic sine is to be computed
      Returns:
      the hyperbolic sine of the specified angle
    • cosh

      public static double cosh(double x)
      Computes the hyperbolic cosine of a given value.
      Parameters:
      x - the value for which the hyperbolic cosine is to be computed
      Returns:
      the hyperbolic cosine of the specified value
    • tanh

      public static double tanh(double x)
      Computes the hyperbolic tangent of a given value. The method delegates the computation to one of the available implementations depending on the current setting (STRICT, FAST, or default).
      Parameters:
      x - the value for which the hyperbolic tangent is to be computed
      Returns:
      the hyperbolic tangent of the specified value
    • floor

      public static double floor(double x)
      Computes the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
      Parameters:
      x - The value to be floored.
      Returns:
      The largest double value less than or equal to x that is equal to a mathematical integer.
    • ceil

      public static double ceil(double x)
      Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
      Parameters:
      x - the value to be rounded up to the nearest integer
      Returns:
      the smallest double value that is greater than or equal to x and is equal to a mathematical integer
    • rint

      public static double rint(double x)
      Returns the closest value to the argument, with ties rounding to the even neighbor. The method behavior depends on the implementation mode being used.
      Parameters:
      x - the value to be rounded to the nearest integer value
      Returns:
      the value nearest to the argument, with ties rounded to the even neighbor
    • round

      public static long round(double x)
      Rounds the specified floating-point value to the nearest long value. The rounding method depends on the current implementation mode: STRICT, FAST, or the default.
      Parameters:
      x - the double value to be rounded
      Returns:
      the value of the argument rounded to the nearest long value
    • round

      public static int round(float x)
      Rounds the given floating-point value to the nearest integer. The rounding behavior depends on the implementation strategy.
      Parameters:
      x - the floating-point value to be rounded
      Returns:
      the rounded integer value
    • toRadians

      public static double toRadians(double x)
      Converts an angle measured in degrees to an approximately equivalent angle in radians. This method provides flexibility in selecting the underlying implementation for conversion.
      Parameters:
      x - the angle in degrees
      Returns:
      the angle in radians
    • toDegrees

      public static double toDegrees(double x)
      Converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally performed by multiplying the input value by the factor of 180 divided by π.
      Parameters:
      x - the angle in radians to be converted to degrees
      Returns:
      the converted angle in degrees
    • ulp

      public static double ulp(double x)
      Returns the size of an ulp (unit in the last place) of the argument. The ulp of a floating-point value is the positive distance between this floating-point value and the next larger value representable in the same precision.
      Parameters:
      x - the floating-point value whose ulp is to be determined
      Returns:
      the size of an ulp of the argument
    • nextUp

      public static double nextUp(double x)
      Returns the floating-point value adjacent to the input in the direction of positive infinity.
      Parameters:
      x - the floating-point value for which the next higher value is to be returned
      Returns:
      the smallest floating-point value that is greater than x and representable as a double
    • nextDown

      public static double nextDown(double x)
      Returns the floating-point value adjacent to the argument in the direction of negative infinity.
      Parameters:
      x - the floating-point value whose adjacent value in the direction of negative infinity is to be returned
      Returns:
      the adjacent floating-point value in the direction of negative infinity
    • nextAfter

      public static double nextAfter(double start, double direction)
      Returns the floating-point number that is adjacent to the first argument in the direction of the second argument. This method determines the result based on the current implementation strategy.
      Parameters:
      start - the starting floating-point value.
      direction - the floating-point value indicating the direction of the adjacent value.
      Returns:
      the floating-point number adjacent to start in the direction of direction.
    • scalb

      public static double scalb(double d, int scaleFactor)
      Returns a floating-point value obtained by scaling the specified floating-point value `d` by an integer power of two specified by `scaleFactor`. This operation multiplies `d` by 2 to the power of `scaleFactor`.
      Parameters:
      d - the floating-point value to be scaled
      scaleFactor - the power of two by which to scale the floating-point value
      Returns:
      the scaled floating-point value, which is `d * 2^scaleFactor`
    • getExponent

      public static int getExponent(double d)
      Returns the unbiased exponent used in the representation of the floating-point value. The method delegates the computation to different implementations depending on the configured mode.
      Parameters:
      d - the floating-point value whose exponent is to be extracted
      Returns:
      the unbiased exponent of the given floating-point value
    • signum

      public static double signum(double d)
      Calculates the signum function of the specified double value. The signum function returns: - 1.0 if the input is positive. - -1.0 if the input is negative. - 0.0 if the input is zero.
      Parameters:
      d - the input value whose signum is to be computed
      Returns:
      the signum of the input value as a double, which can be 1.0, -1.0, or 0.0
    • random

      public static double random()
      Generates a random double value between 0.0 (inclusive) and 1.0 (exclusive). The method selected for generating the random value depends on the implementation mode: STRICT, FAST, or the default Math.random().
      Returns:
      a pseudorandom double greater than or equal to 0.0 and less than 1.0
    • copySign

      public static double copySign(double magnitude, double sign)
      Returns the first floating-point argument with the sign of the second floating-point argument. This method ensures the sign of the result matches the sign of the second argument, while the magnitude matches the first argument.
      Parameters:
      magnitude - the value whose magnitude is to be returned
      sign - the value whose sign is to be assigned to the result
      Returns:
      a floating-point value with the magnitude of magnitude and the sign of sign