Class FunctionOps


  • public class FunctionOps
    extends Object
    These are some commonly used mathematical functions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double combination​(int n, int k)
      Compute the combination function or binomial coefficient.
      static double dotProduct​(double[] x1, double[] x2)
      \(x_1 \cdot x_2\)
      static long dotProduct​(long[] x1, long[] x2)
      \(x_1 \cdot x_2\)
      static double factorial​(int n)
      n!
      static int gcd​(int a, int b)
      Calculates the greatest common divisor of integer a and integer b.
      static int lcm​(int a, int b)
      Calculates the least common multiple of integer a and integer b.
      static double linearInterpolate​(double x, double x0, double y0, double x1, double y1)
      Linear interpolation between two points.
      static long mod​(long x, long m)
      Compute the positive modulus of a number.
      static long modpow​(long base, long exponent, long modulus)
      be mod m
      static double permutation​(int n, int k)
      Compute the permutation function.
    • Method Detail

      • modpow

        public static long modpow​(long base,
                                  long exponent,
                                  long modulus)
        
         be mod m
         
        We use the fact that
        
         (a * b) mod m = ((a mod m) * (b mod m)) mod m
         
        This code may fail without being noticed for very large numbers because overflow in integer operations does not throw any exception in the JVM.
        Parameters:
        base - positive integer b
        exponent - positive integer e
        modulus - positive integer m
        Returns:
        be mod m
        Throws:
        IllegalArgumentException - if not all inputs are positive integers
        See Also:
      • mod

        public static long mod​(long x,
                               long m)
        Compute the positive modulus of a number. If x is positive, we return x mod m; otherwise, we return the smallest positive integer less than m, having the same modulo. For example,
        
         -1 mod 5 = 4
         
        Parameters:
        x - an integer
        m - the modulus
        Returns:
        x mod m
        See Also:
        Wikipedia: Modulo operation
      • dotProduct

        public static long dotProduct​(long[] x1,
                                      long[] x2)
        \(x_1 \cdot x_2\)

        This operation is called inner product when used in the context of vector space.

        Parameters:
        x1 - a long array
        x2 - a long array
        Returns:
        \(x_1 \cdot x_2\)
        See Also:
        Wikipedia: Dot product
      • dotProduct

        public static double dotProduct​(double[] x1,
                                        double[] x2)
        \(x_1 \cdot x_2\)

        This operation is called inner product when used in the context of vector space.

        Parameters:
        x1 - a double array
        x2 - a double array
        Returns:
        \(x_1 \cdot x_2\)
        See Also:
        Wikipedia: Dot product
      • factorial

        public static double factorial​(int n)
        n!
        Parameters:
        n - an integer
        Returns:
        n!
        See Also:
        Wikipedia: Factorial
      • combination

        public static double combination​(int n,
                                         int k)
        Compute the combination function or binomial coefficient. It is the number of subsets of size k in a larger set of n elements.
        Parameters:
        n - the size of the full set
        k - the size of a combination
        Returns:
        \(\frac{n!}{(n-k)! k!}\)
        See Also:
        Wikipedia: Combination
      • permutation

        public static double permutation​(int n,
                                         int k)
        Compute the permutation function. It is the number of size-k-permutations from a larger set of n elements.
        Parameters:
        n - the size of the full set
        k - the size of a permutation
        Returns:
        \(\frac{n!}{(n-k)!}\)
        See Also:
        Wikipedia: Permutation
      • linearInterpolate

        public static double linearInterpolate​(double x,
                                               double x0,
                                               double y0,
                                               double x1,
                                               double y1)
        Linear interpolation between two points.
        Parameters:
        x - x
        x0 - x0
        y0 - y0
        x1 - x1
        y1 - y1
        Returns:
        the linear interpolation between two points
        See Also:
        Wikipedia: Linear interpolation between two known points
      • gcd

        public static int gcd​(int a,
                              int b)
        Calculates the greatest common divisor of integer a and integer b.
        Parameters:
        a - an integer
        b - an integer
        Returns:
        the greatest common divisor of a and b
        See Also:
        Wikipedia: Greatest common divisor
      • lcm

        public static int lcm​(int a,
                              int b)
        Calculates the least common multiple of integer a and integer b.
        Parameters:
        a - an integer
        b - an integer
        Returns:
        the least common multiple of a and b
        See Also:
        Wikipedia: Greatest common divisor