Class MathTools

java.lang.Object
org.sm.smtools.math.MathTools

public final class MathTools
extends java.lang.Object
The MathTools class offers some basic useful mathematical operations.

All methods in this class are static, so they should be invoked as:

   ... = MathTools.method(...);
 
Note that this class cannot be subclassed!
Version:
11/11/2019
Author:
Sven Maerivoet
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static class  MathTools.EKernelType
    The different kernel types.
  • Field Summary

    Fields
    Modifier and Type Field Description
    static int kNrOfDoubleDecimals
    The number of decimals in a double, equal to log10(2^(#bits_mantissa)) with #bits_mantissa = #bits_total - #bits_exponent - #bits_sign.
  • Method Summary

    Modifier and Type Method Description
    static double atan​(double x, double y)
    Calculates the arc tangent of the two doubles.
    static double clip​(double value, double minimum, double maximum)
    Clips a double between two extrema.
    static int clip​(int value, int minimum, int maximum)
    Clips an int between two extrema.
    static double convertBToGB​(long nrOfBytes)
    Converts a number of bytes to gigabytes (1 GB = 1000^3 B).
    static double convertBToGiB​(long nrOfBytes)
    Converts a number of bytes to gibibytes (1 GiB = 1024^3 B).
    static double convertBTokB​(long nrOfBytes)
    Converts a number of bytes to kilobytes (1 kB = 1000 B).
    static double convertBToKiB​(long nrOfBytes)
    Converts a number of bytes to kibibytes (1 KiB = 1024 B).
    static double convertBToMB​(long nrOfBytes)
    Converts a number of bytes to megabytes (1 MB = 1000^2 B).
    static double convertBToMiB​(long nrOfBytes)
    Converts a number of bytes to mebibytes (1 MiB = 1024^2 B).
    static double convertBToTB​(long nrOfBytes)
    Converts a number of bytes to terabytes (1 GB = 1000^4 B).
    static double convertBToTiB​(long nrOfBytes)
    Converts a number of bytes to tebibytes (1 TiB = 1024^4 B).
    static double cube​(double x)
    Calculates the cube of a double.
    static double deg2rad​(double degrees)
    Converts degrees to radians.
    static double fac​(double n)
    Calculates the factorial of a double.
    static double facApprox​(double n)
    Calculates the approximated factorial of a double (using Stirling's formula).
    static Extrema findExtrema​(double[] x)
    Finds all local extreme values (and their indices) in an array.
    static double findMaximum​(double[] x)
    Searches incrementally for the maximum value in an array.
    static int findMaximum​(int[] x)
    Searches incrementally for the maximum value in an array.
    static double findMinimum​(double[] x)
    Searches incrementally for the minimum value in an array.
    static int findMinimum​(int[] x)
    Searches incrementally for the minimum value in an array.
    static void forcePartialOrder​(java.awt.geom.Point2D.Double p1, java.awt.geom.Point2D.Double p2)
    Forces a partial order on the components of two points p1 and p2 such that (x1,y1) ≤ (x2,y2).
    static double frac​(double x)
    Calculates the fractional part of a double.
    static double getEllipseCircumference​(double a, double b)
    Returns the approximated circumference of an ellipse.
    static double getEllipseEccentricity​(double a, double b)
    Returns the eccentricity of an ellipse.
    static double getGreatCircleDistance​(double latitude1, double longitude1, double latitude2, double longitude2)
    Calculates the great circle distance (using the haversine formula) between two specified points.
    static double getKernel​(double u, MathTools.EKernelType kernelType)
    Provides a kernel.
    static FunctionLookupTable getKernelSmoother​(FunctionLookupTable functionLookupTable, MathTools.EKernelType kernelType, double bandwidth, int nrOfSupportPoints)
    Performs kernel smoothing on a 1D function specified by lookup tables for in the (X,Y) plane.
    static LatLongPosition getLatitudeLongitudeDisplacement​(double latitude, double longitude, double latitudeOffset, double longitudeOffset)
    Calculates a (latitude,longitude) in a LatLongPosition object that is of a specified offset (expressed in meter) from a given point.
    static boolean isEven​(int n)
    Checks whether or not a number is even.
    static boolean isOdd​(int n)
    Checks whether or not a number is odd.
    static boolean isPrime​(long x)
    Returns whether or not a given long number is prime.
    static double logBase​(double x, double base)
    Takes a logarithm of a double in a custom base.
    static double map​(double value, double fromMinimum, double fromMaximum, double toMinimum, double toMaximum)
    Maps a double from one range to another.
    static double normalisedLinearInterpolation​(double value, double from, double to)
    Performs linear interpolation of a double with respect to two boundary values.
    static double quadr​(double x)
    Calculates the quadratic of a double.
    static double rad2deg​(double radians)
    Converts radians to degrees.
    static long round​(double x)
    Rounds a double to a long.
    static ArraySearchBounds searchArrayBounds​(double[] x, double xSearch)
    Determines the indices of the 2 values surrounding the searched value in an array of x.length elements, such that: x[fLowerBound] ≤ xSearch < x[fUpperBound]
    static double sinc​(double x)
    Calculates the unnormalised sinc (sinus cardinalis) function of a double.
    static double sincn​(double x)
    Calculates the normalised sinc (sinus cardinalis) function of a double.
    static double sqr​(double x)
    Calculates the square of a double.

    Methods inherited from class java.lang.Object

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

    • kNrOfDoubleDecimals

      public static final int kNrOfDoubleDecimals
      The number of decimals in a double, equal to log10(2^(#bits_mantissa)) with #bits_mantissa = #bits_total - #bits_exponent - #bits_sign.
  • Method Details

    • frac

      public static double frac​(double x)
      Calculates the fractional part of a double.
      Parameters:
      x - the double to calculate the fractional part of
      Returns:
      the fractional part of the double
    • sqr

      public static double sqr​(double x)
      Calculates the square of a double.
      Parameters:
      x - the double to be squared
      Returns:
      the squared double
      See Also:
      cube(double), quadr(double)
    • cube

      public static double cube​(double x)
      Calculates the cube of a double.
      Parameters:
      x - the double to be cubed
      Returns:
      the cubed double
      See Also:
      sqr(double), quadr(double)
    • quadr

      public static double quadr​(double x)
      Calculates the quadratic of a double.
      Parameters:
      x - the double to be quadrated
      Returns:
      the quadrated double
      See Also:
      sqr(double), cube(double)
    • fac

      public static double fac​(double n)
      Calculates the factorial of a double.
      Parameters:
      n - the double to calculate the factorial for
      Returns:
      the factorial of the specified double
      See Also:
      facApprox(double)
    • facApprox

      public static double facApprox​(double n)
      Calculates the approximated factorial of a double (using Stirling's formula).
      Parameters:
      n - the double to calculate the approximated factorial for
      Returns:
      the approximated factorial of the specified double
      See Also:
      fac(double)
    • atan

      public static double atan​(double x, double y)
      Calculates the arc tangent of the two doubles.

      Both doubles x and y are used as y / x (x is allowed to be zero). The arc tangent will be a positive angle, lying in [0,2*PI]. The following cases are considered:

      • x ≥ 0, y ≥ 0: 0 ≤ atan(x,y) ≤ PI/2
      • x ≤ 0, y ≥ 0: PI/2 ≤ atan(x,y) ≤ PI
      • x ≤ 0, y ≤ 0: PI ≤ atan(x,y) ≤ 3*PI/2
      • x ≥ 0, y ≤ 0: 3*PI/2 ≤ atan(x,y) ≤ 2*PI

      With these two special cases:

      • x = 0, y > 0: atan(x,y) = PI/2
      • x = 0, y < 0: atan(x,y) = 3*PI/2
      Parameters:
      x - the denominator of the double to calculate the arc tangent of
      y - the numerator of the double to calculate the arc tangent of
      Returns:
      the arc tangent of the doubles
    • sinc

      public static double sinc​(double x)
      Calculates the unnormalised sinc (sinus cardinalis) function of a double.
      Parameters:
      x - the double to calculate the unnormalised sinc of
      Returns:
      the unnormalised sinc of the double
    • sincn

      public static double sincn​(double x)
      Calculates the normalised sinc (sinus cardinalis) function of a double.
      Parameters:
      x - the double to calculate the normalised sinc of
      Returns:
      the normalised sinc of the double
    • deg2rad

      public static double deg2rad​(double degrees)
      Converts degrees to radians.
      Parameters:
      degrees - the number of degrees to convert
      Returns:
      the number of degrees converted to radians
      See Also:
      rad2deg(double)
    • rad2deg

      public static double rad2deg​(double radians)
      Converts radians to degrees.
      Parameters:
      radians - the radians to convert
      Returns:
      the radians converted to number of degrees
      See Also:
      deg2rad(double)
    • getGreatCircleDistance

      public static double getGreatCircleDistance​(double latitude1, double longitude1, double latitude2, double longitude2)
      Calculates the great circle distance (using the haversine formula) between two specified points.
      Parameters:
      latitude1 - -
      longitude1 - -
      latitude2 - -
      longitude2 - -
      Returns:
      the great circle distance [in m] between the two specified points
    • getLatitudeLongitudeDisplacement

      public static LatLongPosition getLatitudeLongitudeDisplacement​(double latitude, double longitude, double latitudeOffset, double longitudeOffset)
      Calculates a (latitude,longitude) in a LatLongPosition object that is of a specified offset (expressed in meter) from a given point.
      Parameters:
      latitude - -
      longitude - -
      latitudeOffset - the latitude offset [in m]
      longitudeOffset - the longitude offset [in m]
      Returns:
      a (latitude,longitude) in a LatLongPosition object at a specified offset from a given point
    • clip

      public static int clip​(int value, int minimum, int maximum)
      Clips an int between two extrema.
      Parameters:
      value - the int to clip between the two extrema
      minimum - the lower boundary to clip the int
      maximum - the upper boundary to clip the int
      Returns:
      the int clipped between the two extrema
      See Also:
      clip(double,double,double)
    • clip

      public static double clip​(double value, double minimum, double maximum)
      Clips a double between two extrema.
      Parameters:
      value - the double to clip between the two extrema
      minimum - the lower boundary to clip the double
      maximum - the upper boundary to clip the double
      Returns:
      the double clipped between the two extrema
      See Also:
      clip(int,int,int)
    • map

      public static double map​(double value, double fromMinimum, double fromMaximum, double toMinimum, double toMaximum)
      Maps a double from one range to another. The value is not constrained within the range.
      Parameters:
      value - the value to map from the current to the target range
      fromMinimum - the lower bound of the value's current range
      fromMaximum - the upper bound of the value's current range
      toMinimum - the lower bound of the value's target range
      toMaximum - the upper bound of the value's target range
      Returns:
      the value mapped from the current to the target range
    • forcePartialOrder

      public static void forcePartialOrder​(java.awt.geom.Point2D.Double p1, java.awt.geom.Point2D.Double p2)
      Forces a partial order on the components of two points p1 and p2 such that (x1,y1) ≤ (x2,y2).
      Parameters:
      p1 - the first point
      p2 - the second point
    • findMinimum

      public static double findMinimum​(double[] x)
      Searches incrementally for the minimum value in an array.
      Parameters:
      x - the array to search in
      Returns:
      the minimum value in the array
    • findMaximum

      public static double findMaximum​(double[] x)
      Searches incrementally for the maximum value in an array.
      Parameters:
      x - the array to search in
      Returns:
      the maximum value in the array
    • findMinimum

      public static int findMinimum​(int[] x)
      Searches incrementally for the minimum value in an array.
      Parameters:
      x - the array to search in
      Returns:
      the minimum value in the array
    • findMaximum

      public static int findMaximum​(int[] x)
      Searches incrementally for the maximum value in an array.
      Parameters:
      x - the array to search in
      Returns:
      the maximum value in the array
    • normalisedLinearInterpolation

      public static double normalisedLinearInterpolation​(double value, double from, double to)
      Performs linear interpolation of a double with respect to two boundary values.

      This method assumes that the specified value lies in the interval [0,1], with 0.0 corresponding to the lower boundary (from) and 1.0 to the upper boundary (to).

      Parameters:
      value - the double to interpolate linearly with respect to two boundary values
      from - the lower boundary to use for the linear interpolation (corresponding to value = 0.0)
      to - the upper boundary to use for the linear interpolation (corresponding to value = 1.0)
      Returns:
      the linear interpolation of the specified double between the two boundary values
    • searchArrayBounds

      public static ArraySearchBounds searchArrayBounds​(double[] x, double xSearch)
      Determines the indices of the 2 values surrounding the searched value in an array of x.length elements, such that: x[fLowerBound] ≤ xSearch < x[fUpperBound]

      There are 2 special cases:

      • xSearch < min(x) leads to fLowerBound = fUpperBound = 0
      • xSearch ≥ max(x) leads to fLowerBound = fUpperBound = x.length - 1

      Note that this method assumes that the elements in x are sorted!

      Parameters:
      x - an array of doubles containing the values
      xSearch - the value to search for
      Returns:
      an ArraySearchBounds object containing the indices of the 2 values surrounding the searched value
    • isEven

      public static boolean isEven​(int n)
      Checks whether or not a number is even.
      Parameters:
      n - the number to check
      Returns:
      true when n is even, false if it's odd
    • isOdd

      public static boolean isOdd​(int n)
      Checks whether or not a number is odd.
      Parameters:
      n - the number to check
      Returns:
      true when n is odd, false if it's even
    • convertBTokB

      public static double convertBTokB​(long nrOfBytes)
      Converts a number of bytes to kilobytes (1 kB = 1000 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of kilobytes corresponding to the number of bytes
    • convertBToKiB

      public static double convertBToKiB​(long nrOfBytes)
      Converts a number of bytes to kibibytes (1 KiB = 1024 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of kibibytes corresponding to the number of bytes
    • convertBToMB

      public static double convertBToMB​(long nrOfBytes)
      Converts a number of bytes to megabytes (1 MB = 1000^2 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of megabytes corresponding to the number of bytes
    • convertBToMiB

      public static double convertBToMiB​(long nrOfBytes)
      Converts a number of bytes to mebibytes (1 MiB = 1024^2 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of mebibytes corresponding to the number of bytes
    • convertBToGB

      public static double convertBToGB​(long nrOfBytes)
      Converts a number of bytes to gigabytes (1 GB = 1000^3 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of gigabytes corresponding to the number of bytes
    • convertBToGiB

      public static double convertBToGiB​(long nrOfBytes)
      Converts a number of bytes to gibibytes (1 GiB = 1024^3 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of gibibytes corresponding to the number of bytes
    • convertBToTB

      public static double convertBToTB​(long nrOfBytes)
      Converts a number of bytes to terabytes (1 GB = 1000^4 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of terabytes corresponding to the number of bytes
    • convertBToTiB

      public static double convertBToTiB​(long nrOfBytes)
      Converts a number of bytes to tebibytes (1 TiB = 1024^4 B).
      Parameters:
      nrOfBytes - the number of bytes to convert
      Returns:
      the number of tebibytes corresponding to the number of bytes
    • round

      public static long round​(double x)
      Rounds a double to a long.
      Parameters:
      x - the double to round
      Returns:
      a long representing the rounded double
    • logBase

      public static double logBase​(double x, double base)
      Takes a logarithm of a double in a custom base.
      Parameters:
      x - the double to take the logarithm of
      base - the base of the logarithm
      Returns:
      a double representing the calculated logarithm in the specified base
    • isPrime

      public static boolean isPrime​(long x)
      Returns whether or not a given long number is prime.
      Parameters:
      x - the long to test for primality
      Returns:
      true if the given long is prime, false otherwise
    • getEllipseCircumference

      public static double getEllipseCircumference​(double a, double b)
      Returns the approximated circumference of an ellipse.
      Parameters:
      a - the length of the major axis
      b - the length of the minor axis
      Returns:
      the approximated circumference of an ellipse
      See Also:
      infinite series approximation
    • getEllipseEccentricity

      public static double getEllipseEccentricity​(double a, double b)
      Returns the eccentricity of an ellipse.
      Parameters:
      a - the length of the major axis
      b - the length of the minor axis
      Returns:
      the eccentricity of an ellipse
    • findExtrema

      public static Extrema findExtrema​(double[] x)
      Finds all local extreme values (and their indices) in an array.
      Parameters:
      x - the sequence to find all local extreme values for
      Returns:
      all encountered local extreme values
    • getKernel

      public static double getKernel​(double u, MathTools.EKernelType kernelType)
      Provides a kernel.
      Parameters:
      u - the point where the kernel is to be evaluated
      kernelType - the type of kernel to use in the evaluation
      Returns:
      the kernel evaluated in u
    • getKernelSmoother

      public static FunctionLookupTable getKernelSmoother​(FunctionLookupTable functionLookupTable, MathTools.EKernelType kernelType, double bandwidth, int nrOfSupportPoints)
      Performs kernel smoothing on a 1D function specified by lookup tables for in the (X,Y) plane.
      Parameters:
      functionLookupTable - the lookup table for the specified 1D function
      kernelType - the type of kernel to use
      bandwidth - the bandwidth of the kernel
      nrOfSupportPoints - the number of (X,Y) values to use for the smoothened 1D function
      Returns:
      the smoothed data in a new lookup table