- java.lang.Object
-
- org.chocosolver.util.tools.MathUtils
-
public final class MathUtils extends Object
various mathematics utilities. The functions do not exist in the basic math package Math.*- Since:
- 8 dec. 2008 version 2.0.1
- Version:
- 2.0.1
- Author:
- Charles Prud'homme, Arnaud Malapert
-
-
Field Summary
Fields Modifier and Type Field Description static double
ROUNDED_LOG_PRECISION
Precision for rounded logarithm.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
combinaison(int n, int p)
it computes the number of combinaison C_n^p.static int
divCeil(int x, int y)
Returns the smallest (closest to positive infinity)int
value that is greater or equal to a/b.static int
divFloor(int x, int y)
Returns the largest (closest to positive infinity)int
value that is less than or equal to a/b.static long
factorial(int n)
simple recursive version of factorialstatic boolean
isPowerOfTwo(int x)
static double
log(double value, double exponent)
static int
max(int[] values)
Returns the element with the greatest value in values.static int
max(int[][] values)
Returns the element with the greatest value in values.static int
min(int[] values)
Returns the element with the smallest value in values.static int
min(int[][] values)
Returns the element with the smallest value in values.static int
pow(int value, int exp)
Returns the value of the first argument raised to the power of the second argument.static double
roundedLog(double value, double exponent)
a rounded logarithm to avoid issues with jvm dependant math functionsstatic int
safeAdd(int x, int y)
Returns the sum of its arguments, returning eitherInteger.MAX_VALUE
if the result overflows anint
, orInteger.MIN_VALUE
if the result underflows anint
, .static int
safeCast(long x)
static int
safeMultiply(int x, int y)
Returns the product of its arguments, returning eitherInteger.MAX_VALUE
if the result overflows anint
, orInteger.MIN_VALUE
if the result underflows anint
, .static int
safeSubstract(int x, int y)
Returns the difference of its arguments, returning eitherInteger.MAX_VALUE
if the result overflows anint
, orInteger.MIN_VALUE
if the result underflows anint
, .static int
sum(int[] values)
Returns the sum of elements in values.static int
sum(int[][] values)
Retuns the sum of elements in values.static int
sum(int[] values, int begin, int end)
Returns the sum of elements in values from position begin (inclusive) to position end (exclusive).static int
sumFrom(int[] values, int begin)
Returns the sum of elements in values from position begin (inclusive) to values.length.static int
sumTo(int[] values, int end)
Returns the sum of elements in values from position 0 (inclusive) to position end (exclusive).
-
-
-
Field Detail
-
ROUNDED_LOG_PRECISION
public static final double ROUNDED_LOG_PRECISION
Precision for rounded logarithm.- See Also:
- Constant Field Values
-
-
Method Detail
-
factorial
public static long factorial(int n)
simple recursive version of factorial- Parameters:
n
- size of the suite- Returns:
- n!
-
combinaison
public static int combinaison(int n, int p)
it computes the number of combinaison C_n^p. The function is only recursive and do not use an array to store temporary results- Parameters:
n
- max cardinalityp
- sub cardinality- Returns:
- n among k combinations
-
isPowerOfTwo
public static boolean isPowerOfTwo(int x)
- Parameters:
x
- a value- Returns:
- true if x is power of 2.
-
pow
public static int pow(int value, int exp)
Returns the value of the first argument raised to the power of the second argument. SeeMath.pow(double, double)
for special cases.- Parameters:
value
-exp
-- Returns:
-
log
public static double log(double value, double exponent)
-
roundedLog
public static double roundedLog(double value, double exponent)
a rounded logarithm to avoid issues with jvm dependant math functions
-
sum
public static int sum(int[] values, int begin, int end)
Returns the sum of elements in values from position begin (inclusive) to position end (exclusive).- Parameters:
values
- array of intsbegin
- starting position (inclusive)end
- ending position (exclusive)- Returns:
- the sum of elements in values from position begin (inclusive) to position end (exclusive).
-
sumFrom
public static int sumFrom(int[] values, int begin)
Returns the sum of elements in values from position begin (inclusive) to values.length.- Parameters:
values
- array of intsbegin
- starting position (inclusive)- Returns:
- the sum of elements in values from position begin (inclusive) to values.length.
-
sumTo
public static int sumTo(int[] values, int end)
Returns the sum of elements in values from position 0 (inclusive) to position end (exclusive).- Parameters:
values
- array of intsend
- ending position (exclusive)- Returns:
- the sum of elements in values from position 0 (inclusive) to position end (exclusive).
-
sum
public static int sum(int[] values)
Returns the sum of elements in values.- Parameters:
values
- array of ints- Returns:
- the sum of elements in values.
-
sum
public static int sum(int[][] values)
Retuns the sum of elements in values.- Parameters:
values
- matrix of ints- Returns:
- the sum of elements in values.
-
max
public static int max(int[] values)
Returns the element with the greatest value in values.- Parameters:
values
- array of ints- Returns:
- the element with the greatest value in values.
-
max
public static int max(int[][] values)
Returns the element with the greatest value in values.- Parameters:
values
- array of ints- Returns:
- the element with the greatest value in values.
-
min
public static int min(int[] values)
Returns the element with the smallest value in values.- Parameters:
values
- array of ints- Returns:
- the element with the smallest value in values.
-
min
public static int min(int[][] values)
Returns the element with the smallest value in values.- Parameters:
values
- array of ints- Returns:
- the element with the smallest value in values.
-
divFloor
public static int divFloor(int x, int y)
Returns the largest (closest to positive infinity)int
value that is less than or equal to a/b. Adapted fromMath.floorDiv(int, int)
.- Parameters:
x
- the dividendy
- the divisor- Returns:
- the largest (closest to positive infinity)
int
value that is less than or equal to a/b.
-
divCeil
public static int divCeil(int x, int y)
Returns the smallest (closest to positive infinity)int
value that is greater or equal to a/b. Adapted fromMath.floorDiv(int, int)
.- Parameters:
x
- the dividendy
- the divisor- Returns:
- the smallest (closest to positive infinity)
int
value that is greater or equal to a/b.
-
safeAdd
public static int safeAdd(int x, int y)
Returns the sum of its arguments, returning eitherInteger.MAX_VALUE
if the result overflows anint
, orInteger.MIN_VALUE
if the result underflows anint
, .- Parameters:
x
- the first valuey
- the second value- Returns:
- the result
-
safeSubstract
public static int safeSubstract(int x, int y)
Returns the difference of its arguments, returning eitherInteger.MAX_VALUE
if the result overflows anint
, orInteger.MIN_VALUE
if the result underflows anint
, .- Parameters:
x
- the first valuey
- the second value- Returns:
- the result
-
safeMultiply
public static int safeMultiply(int x, int y)
Returns the product of its arguments, returning eitherInteger.MAX_VALUE
if the result overflows anint
, orInteger.MIN_VALUE
if the result underflows anint
, .- Parameters:
x
- the first valuey
- the second value- Returns:
- the result
-
safeCast
public static int safeCast(long x)
- Parameters:
x
- long to cast- Returns:
- the closest int value when safe casting a long into an int
-
-