Class MathUtils
- java.lang.Object
-
- org.matheclipse.parser.client.math.MathUtils
-
public final class MathUtils extends java.lang.ObjectSome useful additions to the built-in functions inMath.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static intaddAndCheck(int x, int y)Add two integers, checking for overflow.static longaddAndCheck(long a, long b)Add two long integers, checking for overflow.static longbinomialCoefficient(int n, int k)Returns an exact representation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.static doublebinomialCoefficientDouble(int n, int k)Returns adoublerepresentation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.static doublebinomialCoefficientLog(int n, int k)Returns the naturallogof the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.static doublecosh(double x)Returns the hyperbolic cosine of x.static booleanequals(double[] x, double[] y)Returns true iff both arguments are null or have same dimensions and all their elements areequalsstatic booleanequals(double x, double y)Returns true iff both arguments are NaN or neither is NaN and they are equalstatic longfactorial(int n)Returns n!.static doublefactorialDouble(int n)Returns n!.static doublefactorialLog(int n)Returns the natural logarithm of n!.static intgcd(int u, int v)Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations.static inthash(double value)Returns an integer hash code representing the given double value.static inthash(double[] value)Returns an integer hash code representing the given double array value.static byteindicator(byte x)For a byte value x, this method returns (byte)(+1) if x >= 0 and (byte)(-1) if x < 0.static doubleindicator(double x)For a double precision value x, this method returns +1.0 if x >= 0 and -1.0 if x < 0.static floatindicator(float x)For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x < 0.static intindicator(int x)For an int value x, this method returns +1 if x >= 0 and -1 if x < 0.static longindicator(long x)For a long value x, this method returns +1L if x >= 0 and -1L if x < 0.static shortindicator(short x)For a short value x, this method returns (short)(+1) if x >= 0 and (short)(-1) if x < 0.static intlcm(int a, int b)Returns the least common multiple between two integer values.static doublelog(double base, double x)static intmulAndCheck(int x, int y)Multiply two integers, checking for overflow.static longmulAndCheck(long a, long b)Multiply two long integers, checking for overflow.static doublenormalizeAngle(double a, double center)Normalize an angle in a 2&pi wide interval around a center value.static bytesign(byte x)Returns the sign for byte valuex.static doublesign(double x)Returns the sign for double precisionx.static floatsign(float x)Returns the sign for float valuex.static intsign(int x)Returns the sign for int valuex.static longsign(long x)Returns the sign for long valuex.static shortsign(short x)Returns the sign for short valuex.static doublesinh(double x)Returns the hyperbolic sine of x.static intsubAndCheck(int x, int y)Subtract two integers, checking for overflow.static longsubAndCheck(long a, long b)Subtract two long integers, checking for overflow.
-
-
-
Method Detail
-
addAndCheck
public static int addAndCheck(int x, int y)Add two integers, checking for overflow.- Parameters:
x- an addendy- an addend- Returns:
- the sum
x+y - Throws:
MathException- if the result can not be represented as an int- Since:
- 1.1
-
addAndCheck
public static long addAndCheck(long a, long b)Add two long integers, checking for overflow.- Parameters:
a- an addendb- an addend- Returns:
- the sum
a+b - Throws:
MathException- if the result can not be represented as an long- Since:
- 1.2
-
binomialCoefficient
public static long binomialCoefficient(int n, int k)Returns an exact representation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
long. The largest value ofnfor which all coefficients are< Long.MAX_VALUEis 66. If the computed value exceedsLong.MAX_VALUEanArithMeticExceptionis thrown.
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
java.lang.IllegalArgumentException- if preconditions are not met.MathException- if the result is too large to be represented by a long integer.
-
-
binomialCoefficientDouble
public static double binomialCoefficientDouble(int n, int k)Returns adoublerepresentation of the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
double. The largest value ofnfor which all coefficients are < Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
java.lang.IllegalArgumentException- if preconditions are not met.
-
-
binomialCoefficientLog
public static double binomialCoefficientLog(int n, int k)Returns the naturallogof the Binomial Coefficient, "n choose k", the number ofk-element subsets that can be selected from ann-element set.Preconditions:
-
0 <= k <= n(otherwiseIllegalArgumentExceptionis thrown)
- Parameters:
n- the size of the setk- the size of the subsets to be counted- Returns:
n choose k- Throws:
java.lang.IllegalArgumentException- if preconditions are not met.
-
-
cosh
public static double cosh(double x)
Returns the hyperbolic cosine of x.- Parameters:
x- double value for which to find the hyperbolic cosine- Returns:
- hyperbolic cosine of x
-
equals
public static boolean equals(double x, double y)Returns true iff both arguments are NaN or neither is NaN and they are equal- Parameters:
x- first valuey- second value- Returns:
- true if the values are equal or both are NaN
-
equals
public static boolean equals(double[] x, double[] y)Returns true iff both arguments are null or have same dimensions and all their elements areequals- Parameters:
x- first arrayy- second array- Returns:
- true if the values are both null or have same dimension and equal elements
- Since:
- 1.2
-
factorial
public static long factorial(int n)
Returns n!. Shorthand fornFactorial, the product of the numbers1,...,n.Preconditions:
-
n >= 0(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
long. The largest value ofnfor whichn!< Long.MAX_VALUE is 20. If the computed value exceedsLong.MAX_VALUEanArithMeticExceptionis thrown.
- Parameters:
n- argument- Returns:
n!- Throws:
MathException- if the result is too large to be represented by a long integer.java.lang.IllegalArgumentException- if n < 0
-
-
factorialDouble
public static double factorialDouble(int n)
Returns n!. Shorthand fornFactorial, the product of the numbers1,...,nas adouble.Preconditions:
-
n >= 0(otherwiseIllegalArgumentExceptionis thrown) - The result is small enough to fit into a
double. The largest value ofnfor whichn!< Double.MAX_VALUE is 170. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned
- Parameters:
n- argument- Returns:
n!- Throws:
java.lang.IllegalArgumentException- if n < 0
-
-
factorialLog
public static double factorialLog(int n)
Returns the natural logarithm of n!.Preconditions:
-
n >= 0(otherwiseIllegalArgumentExceptionis thrown)
- Parameters:
n- argument- Returns:
n!- Throws:
java.lang.IllegalArgumentException- if preconditions are not met.
-
-
gcd
public static int gcd(int u, int v)Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961).
- Parameters:
u- a non-zero numberv- a non-zero number- Returns:
- the greatest common divisor, never zero
- Since:
- 1.1
-
hash
public static int hash(double value)
Returns an integer hash code representing the given double value.- Parameters:
value- the value to be hashed- Returns:
- the hash code
-
hash
public static int hash(double[] value)
Returns an integer hash code representing the given double array value.- Parameters:
value- the value to be hashed (may be null)- Returns:
- the hash code
- Since:
- 1.2
-
indicator
public static byte indicator(byte x)
For a byte value x, this method returns (byte)(+1) if x >= 0 and (byte)(-1) if x < 0.- Parameters:
x- the value, a byte- Returns:
- (byte)(+1) or (byte)(-1), depending on the sign of x
-
indicator
public static double indicator(double x)
For a double precision value x, this method returns +1.0 if x >= 0 and -1.0 if x < 0. ReturnsNaNifxisNaN.- Parameters:
x- the value, a double- Returns:
- +1.0 or -1.0, depending on the sign of x
-
indicator
public static float indicator(float x)
For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x < 0. ReturnsNaNifxisNaN.- Parameters:
x- the value, a float- Returns:
- +1.0F or -1.0F, depending on the sign of x
-
indicator
public static int indicator(int x)
For an int value x, this method returns +1 if x >= 0 and -1 if x < 0.- Parameters:
x- the value, an int- Returns:
- +1 or -1, depending on the sign of x
-
indicator
public static long indicator(long x)
For a long value x, this method returns +1L if x >= 0 and -1L if x < 0.- Parameters:
x- the value, a long- Returns:
- +1L or -1L, depending on the sign of x
-
indicator
public static short indicator(short x)
For a short value x, this method returns (short)(+1) if x >= 0 and (short)(-1) if x < 0.- Parameters:
x- the value, a short- Returns:
- (short)(+1) or (short)(-1), depending on the sign of x
-
lcm
public static int lcm(int a, int b)Returns the least common multiple between two integer values.- Parameters:
a- the first integer value.b- the second integer value.- Returns:
- the least common multiple between a and b.
- Throws:
MathException- if the lcm is too large to store as an int- Since:
- 1.1
-
log
public static double log(double base, double x)Returns the logarithm for base
bofx.Returns
NaNif either argument is negative. Ifbaseis 0 andxis positive, 0 is returned. Ifbaseis positive andxis 0,Double.NEGATIVE_INFINITYis returned. If both arguments are 0, the result isNaN.- Parameters:
base- the base of the logarithm, must be greater than 0x- argument, must be greater than 0- Returns:
- the value of the logarithm - the number y such that base^y = x.
- Since:
- 1.2
-
mulAndCheck
public static int mulAndCheck(int x, int y)Multiply two integers, checking for overflow.- Parameters:
x- a factory- a factor- Returns:
- the product
x*y - Throws:
MathException- if the result can not be represented as an int- Since:
- 1.1
-
mulAndCheck
public static long mulAndCheck(long a, long b)Multiply two long integers, checking for overflow.- Parameters:
a- first valueb- second value- Returns:
- the product
a * b - Throws:
MathException- if the result can not be represented as an long- Since:
- 1.2
-
normalizeAngle
public static double normalizeAngle(double a, double center)Normalize an angle in a 2&pi wide interval around a center value.This method has three main uses:
- normalize an angle between 0 and 2π:
a = MathUtils.normalizeAngle(a, Math.PI); - normalize an angle between -π and +π
a = MathUtils.normalizeAngle(a, 0.0); - compute the angle between two defining angular positions:
angle = MathUtils.normalizeAngle(end, start) - start;
Note that due to numerical accuracy and since π cannot be represented exactly, the result interval is closed, it cannot be half-closed as would be more satisfactory in a purely mathematical view.
- Parameters:
a- angle to normalizecenter- center of the desired 2π interval for the result- Returns:
- a-2kπ with integer k and center-π <= a-2kπ <= center+π
- Since:
- 1.2
- normalize an angle between 0 and 2π:
-
sign
public static byte sign(byte x)
Returns the sign for byte valuex.For a byte value x, this method returns (byte)(+1) if x > 0, (byte)(0) if x = 0, and (byte)(-1) if x < 0.
- Parameters:
x- the value, a byte- Returns:
- (byte)(+1), (byte)(0), or (byte)(-1), depending on the sign of x
-
sign
public static double sign(double x)
Returns the sign for double precisionx.For a double value
x, this method returns+1.0ifx > 0,0.0ifx = 0.0, and-1.0ifx < 0. ReturnsNaNifxisNaN.- Parameters:
x- the value, a double- Returns:
- +1.0, 0.0, or -1.0, depending on the sign of x
-
sign
public static float sign(float x)
Returns the sign for float valuex.For a float value x, this method returns +1.0F if x > 0, 0.0F if x = 0.0F, and -1.0F if x < 0. Returns
NaNifxisNaN.- Parameters:
x- the value, a float- Returns:
- +1.0F, 0.0F, or -1.0F, depending on the sign of x
-
sign
public static int sign(int x)
Returns the sign for int valuex.For an int value x, this method returns +1 if x > 0, 0 if x = 0, and -1 if x < 0.
- Parameters:
x- the value, an int- Returns:
- +1, 0, or -1, depending on the sign of x
-
sign
public static long sign(long x)
Returns the sign for long valuex.For a long value x, this method returns +1L if x > 0, 0L if x = 0, and -1L if x < 0.
- Parameters:
x- the value, a long- Returns:
- +1L, 0L, or -1L, depending on the sign of x
-
sign
public static short sign(short x)
Returns the sign for short valuex.For a short value x, this method returns (short)(+1) if x > 0, (short)(0) if x = 0, and (short)(-1) if x < 0.
- Parameters:
x- the value, a short- Returns:
- (short)(+1), (short)(0), or (short)(-1), depending on the sign of x
-
sinh
public static double sinh(double x)
Returns the hyperbolic sine of x.- Parameters:
x- double value for which to find the hyperbolic sine- Returns:
- hyperbolic sine of x
-
subAndCheck
public static int subAndCheck(int x, int y)Subtract two integers, checking for overflow.- Parameters:
x- the minuendy- the subtrahend- Returns:
- the difference
x-y - Throws:
MathException- if the result can not be represented as an int- Since:
- 1.1
-
subAndCheck
public static long subAndCheck(long a, long b)Subtract two long integers, checking for overflow.- Parameters:
a- first valueb- second value- Returns:
- the difference
a-b - Throws:
MathException- if the result can not be represented as an long- Since:
- 1.2
-
-
DMelt 3.0 © DataMelt by jWork.ORG