Package dev.nm.analysis.function
Class FunctionOps
- java.lang.Object
-
- dev.nm.analysis.function.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 doublecombination(int n, int k)Compute the combination function or binomial coefficient.static doubledotProduct(double[] x1, double[] x2)\(x_1 \cdot x_2\)static longdotProduct(long[] x1, long[] x2)\(x_1 \cdot x_2\)static doublefactorial(int n)n!static intgcd(int a, int b)Calculates the greatest common divisor of integer a and integer b.static intlcm(int a, int b)Calculates the least common multiple of integer a and integer b.static doublelinearInterpolate(double x, double x0, double y0, double x1, double y1)Linear interpolation between two points.static longmod(long x, long m)Compute the positive modulus of a number.static longmodpow(long base, long exponent, long modulus)be mod mstatic doublepermutation(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 bexponent- positive integer emodulus- positive integer m- Returns:
- be mod m
- Throws:
IllegalArgumentException- if not all inputs are positive integers- See Also:
- Wikipedia: Modular exponentiation
- Wikipedia: Right-to-left binary method
- "Bruce Schneier, "p. 244," Applied Cryptography, 2e, ISBN 0-471-11709-9."
-
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 integerm- 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- alongarrayx2- alongarray- 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- adoublearrayx2- adoublearray- 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 setk- 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 setk- 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- xx0- x0y0- y0x1- x1y1- 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 integerb- 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 integerb- an integer- Returns:
- the least common multiple of a and b
- See Also:
- Wikipedia: Greatest common divisor
-
-