/** * Add two long integers, checking for overflow. * * @param a an addend * @param b an addend * @return the sum <code>a+b</code> * @throws org.matheclipse.parser.client.math.MathException if the result * can not be represented as an long * @since 1.2 */ public static long addAndCheck( long a, long b ) { return addAndCheck( a, b, "overflow: add" ); }
/** * Returns a <code>double</code> representation of the * <a href="http://mathworld.wolfram.com/BinomialCoefficient.html"> Binomial * Coefficient</a>, "<code>n choose k</code>", the number of * <code>k</code>-element subsets that can be selected from an * <code>n</code>-element set. * * <Strong>Preconditions</strong>: * <ul> * <li><code>0 <= k <= n </code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li>The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which all coefficients are < * Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, * Double.POSITIVE_INFINITY is returned</li> * </ul> * * @param n the size of the set * @param k the size of the subsets to be counted * @return <code>n choose k</code> * @throws java.lang.IllegalArgumentException if preconditions are not met. */ public static double binomialCoefficientDouble( final int n, final int k ) { return Math.floor( Math.exp( binomialCoefficientLog( n, k ) ) + 0.5 ); }
/** * Returns true iff both arguments are null or have same dimensions and all * their elements are {@link #equals(double,double) equals}. * * @param x first array * @param y second array * @return true if the values are both null or have same dimension and equal * elements * @since 1.2 */ public static boolean equals( double[] x, double[] y ) { if ( (x == null) || (y == null) ) { return !((x == null) ^ (y == null)); } if ( x.length != y.length ) { return false; } for ( int i = 0; i < x.length; ++i ) { if ( !equals( x[i], y[i] ) ) { return false; } } return true; }
/** * Returns the least common multiple between two integer values. * * @param a the first integer value. * @param b the second integer value. * @return the least common multiple between a and b. * @throws org.matheclipse.parser.client.math.MathException if the lcm is * too large to store as an int * @since 1.1 */ public static int lcm( int a, int b ) { return Math.abs( mulAndCheck( a / gcd( a, b ), b ) ); }
@Override public int hashCode() { return MathUtils.hash(value);// Double.doubleToLongBits(value); // return (int)(bits ^ (bits >>> 32)); }
return createComplex( Math.abs( imaginary ) / (2.0 * t), MathUtils.indicator( imaginary ) * t );
/** * Returns n!. Shorthand for <code>n</code> <a * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers <code>1,...,n</code> as a <code>double</code>. * <p> * <Strong>Preconditions</strong>: * <ul> * <li> <code>n >= 0</code> (otherwise <code>IllegalArgumentException</code> * is thrown)</li> * <li> The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which <code>n!</code> < * Double.MAX_VALUE</code> is 170. If the computed value exceeds * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned</li> * </ul> * </p> * * @param n * argument * @return <code>n!</code> * @throws IllegalArgumentException * if n < 0 */ public static double factorialDouble(final int n) { if (n < 0) { throw new IllegalArgumentException("must have n >= 0 for n!"); } return Math.floor(Math.exp(factorialLog(n)) + 0.5); }
long result = Math.round(factorialDouble(n)); if (result == Long.MAX_VALUE) { throw new MathException("result too large to represent in a long integer");
long result = Math.round( binomialCoefficientDouble( n, k ) ); if ( result == Long.MAX_VALUE ) { throw new MathException( "result too large to represent in a long integer" );
/** * Returns the least common multiple between two integer values. * * @param a * the first integer value. * @param b * the second integer value. * @return the least common multiple between a and b. * @throws MathException * if the lcm is too large to store as an int * @since 1.1 */ public static int lcm(int a, int b) { return Math.abs(mulAndCheck(a / gcd(a, b), b)); }
public int hashCode() { return MathUtils.hash(value);// Double.doubleToLongBits(value); // return (int)(bits ^ (bits >>> 32)); }
return createComplex(t, imaginary / (2.0 * t)); } else { return createComplex(Math.abs(imaginary) / (2.0 * t), MathUtils.indicator(imaginary) * t);
/** * Returns n!. Shorthand for <code>n</code> * <a href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the * product of the numbers <code>1,...,n</code> as a <code>double</code>. * * <Strong>Preconditions</strong>: * <ul> * <li><code>n >= 0</code> (otherwise * <code>IllegalArgumentException</code> is thrown)</li> * <li>The result is small enough to fit into a <code>double</code>. The * largest value of <code>n</code> for which <code>n! < * Double.MAX_VALUE</code> is 170. If the computed value exceeds * Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned</li> * </ul> * * @param n argument * @return <code>n!</code> * @throws java.lang.IllegalArgumentException if n < 0 */ public static double factorialDouble( final int n ) { if ( n < 0 ) { throw new IllegalArgumentException( "must have n >= 0 for n!" ); } return Math.floor( Math.exp( factorialLog( n ) ) + 0.5 ); }
long result = Math.round(factorialDouble(n)); if (result == Long.MAX_VALUE) { throw new MathException("result too large to represent in a long integer");
long result = Math.round(binomialCoefficientDouble(n, k)); if (result == Long.MAX_VALUE) { throw new MathException("result too large to represent in a long integer");
/** * Returns the least common multiple between two integer values. * * @param a * the first integer value. * @param b * the second integer value. * @return the least common multiple between a and b. * @throws MathException * if the lcm is too large to store as an int * @since 1.1 */ public static int lcm(int a, int b) { return Math.abs(mulAndCheck(a / gcd(a, b), b)); }
/** * Hash code. * * @return the int */ @Override public int hashCode() { return MathUtils.hash( value );// Double.doubleToLongBits(value); // return (int)(bits ^ (bits >>> 32)); }