Class MiscMathsFunctions
java.lang.Object
solvers.utilities.MiscMathsFunctions
This class implements several basic math functions, low-level fast memory
copies, matrix type changes, memory usage calculations and printing operations.
Its methods are used throughout the program, therefore it was best to gather
them all here.
- Author:
- Michail Makaronidis, 2010
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidCopy a source array to a destination one.static voidCopy a source array to a destination one.static intbinomialCoefficient(int n, int k) This method calculates the binomial coefficient C(n, k) ("n choose k")static BigRationalcomputeFactorial(int n) Computes the factorial for a given number and returns the result as a BigRational.static BigDecimalcomputeFactorialAsBigDecimal(int n) Computes the factorial for a given number and returns the result as a BigDecimal.static Map<Integer, BigRational> computeFactorials(int n) Computes the factorials up to a given number and returns them as a Map<Integer,BigRational>.static Map<Integer, BigDecimal> Computes the factorials up to a given number and returns them as a Map<Integer,BigDecimal>.static BigRational[]createBigRationalArray(int[] s) Transforms an array of ints to an array of BigRationals.static BigRational[][]createBigRationalArray(int[][] d) Transforms an array of ints to an array of BigRationals.static voiddotprintMatrix(BigRational[][] A) static BigDecimalexp(double p, MathContext mc) static booleanisPowerOf2(int n) Checks if specified number is power of 2 using bitwise operators.static BigDecimallog(BigDecimal N, MathContext mc) static doublelogGamma(double x) Uses Lanczos approximation formula to compute the LogGamma Function ref: https://introcs.cs.princeton.edu/java/91float/Gamma.java.htmlstatic BigRational[]matrixVectorMultiply(BigRational[][] A, BigRational[] v) Multiplies a two-dimensional matrix of BigRationals with a one-dimensional one.static BigRational[]matrixVectorMultiplyJ(BigRational[][] A, BigRational[] v) static doublemax(double[] arr) static voidprintMatrix(int[] A) Prints a 1-dimensional matrix.static voidprintMatrix(int[][] A) Prints a 2-dimensional matrixstatic voidprintMatrix(BigRational[] A) Prints a 1-dimensional matrix.static voidprintMatrix(BigRational[][] A) Prints a 2-dimensional matrixstatic voidPrints a 1-dimensional matrix of BigRationals as doubles.static voidprintPrettyMatrix(BigRational[][] A) Prints a 2-dimensional matrix of BigRationals as doubles.static doublesum(double[] arr) static intsum(int[] arr)
-
Method Details
-
isPowerOf2
public static boolean isPowerOf2(int n) Checks if specified number is power of 2 using bitwise operators.- Parameters:
n- The number to check.- Returns:
- Boolean indicating if n is a power of 2.
-
binomialCoefficient
public static int binomialCoefficient(int n, int k) This method calculates the binomial coefficient C(n, k) ("n choose k")- Parameters:
n- The first numberk- The second number- Returns:
- The binomial coefficient
-
computeFactorials
Computes the factorials up to a given number and returns them as a Map<Integer,BigRational>.- Parameters:
n- The number up to which the factorials are computed- Returns:
- The Map<Integer,BigRational> containing the computed values
-
computeFactorialsAsBigDecimal
Computes the factorials up to a given number and returns them as a Map<Integer,BigDecimal>.- Parameters:
n- The number up to which the factorials are computed- Returns:
- The Map<Integer,BigDecimal> containing the computed values
-
computeFactorial
Computes the factorial for a given number and returns the result as a BigRational.- Parameters:
n- The number to calculate the factorial for.- Returns:
- The result as a BigRational.
-
computeFactorialAsBigDecimal
Computes the factorial for a given number and returns the result as a BigDecimal.- Parameters:
n- The number to calculate the factorial for.- Returns:
- The result as a BigDecimal.
-
printMatrix
public static void printMatrix(int[][] A) Prints a 2-dimensional matrix- Parameters:
A- A 2-dimensional matrix of ints
-
printMatrix
Prints a 2-dimensional matrix- Parameters:
A- A 2-dimensional matrix of BigRationals
-
printPrettyMatrix
Prints a 2-dimensional matrix of BigRationals as doubles.- Parameters:
A- A 2-dimensional matrix of BigRationals
-
printMatrix
public static void printMatrix(int[] A) Prints a 1-dimensional matrix.- Parameters:
A- A 1-dimensional matrix of ints
-
printMatrix
Prints a 1-dimensional matrix.- Parameters:
A- A 1-dimensional matrix of BigRationals
-
printPrettyMatrix
Prints a 1-dimensional matrix of BigRationals as doubles.- Parameters:
A- A 1-dimensional matrix of BigRationals
-
createBigRationalArray
Transforms an array of ints to an array of BigRationals.- Parameters:
s- The source array of ints- Returns:
- The resulting array of BigRationals
-
createBigRationalArray
Transforms an array of ints to an array of BigRationals.- Parameters:
d- The source array of ints- Returns:
- The resulting array of BigRationals
-
matrixVectorMultiply
Multiplies a two-dimensional matrix of BigRationals with a one-dimensional one. It does not perform a generic multiplication, but it rather checks for linear system inconsistencies as well.- Parameters:
A- The two-dimensional matrixv- The one-dimensional matrix- Returns:
- The multiplication result
-
matrixVectorMultiplyJ
-
arrayCopy
-
arrayCopy
-
dotprintMatrix
- Parameters:
A- A 2-dimensional matrix of BigRationals
-
max
public static double max(double[] arr) -
sum
public static double sum(double[] arr) -
sum
public static int sum(int[] arr) -
exp
- Parameters:
p- Exponent of the power to be computedmc- MathContext object for the final precision- Returns:
- BigDecimal representation of exp(p)
- Throws:
InternalErrorException
-
log
- Parameters:
N- BigDecimal of which the logarithm is computedmc- MathContext object for the final precision- Returns:
- BigDecimal representation of log(N)
-
logGamma
public static double logGamma(double x) Uses Lanczos approximation formula to compute the LogGamma Function ref: https://introcs.cs.princeton.edu/java/91float/Gamma.java.html
-