Java Code Examples for org.apache.commons.math3.util.ArithmeticUtils#mulAndCheck()

The following examples show how to use org.apache.commons.math3.util.ArithmeticUtils#mulAndCheck() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PolynomialExpansionMapper.java    From Alink with Apache License 2.0 5 votes vote down vote up
/**
 * calculate the length of the expended polynomial.
 *
 * @param num the item number of the input polynomial.
 * @param degree the degree of the polynomial.
 * @return the polynomial size.
 */
@VisibleForTesting
static int getPolySize(int num, int degree) {
	if (num == 0) {
		return 1;
	}
	if (num == 1 || degree == 1) {
		return num + degree;
	}
	if (degree > num) {
		return getPolySize(degree, num);
	}
	long res = 1;
	int i = num + 1;
	int j;
	if (num + degree < CONSTANT) {
		for (j = 1; j <= degree; ++j) {
			res = res * i / j;
			++i;
		}
	} else {
		int depth;
		for (j = 1; j <= degree; ++j) {
			depth = ArithmeticUtils.gcd(i, j);
			res = ArithmeticUtils.mulAndCheck(res / (j / depth), i / depth);
			++i;
		}
	}
	if (res > Integer.MAX_VALUE) {
		throw new IllegalArgumentException("The expended polynomial size is too large.");
	}
	return (int) res;

}
 
Example 2
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = CombinatoricsUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = CombinatoricsUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = CombinatoricsUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 3
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 4
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 5
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = CombinatoricsUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = CombinatoricsUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = CombinatoricsUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 6
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 7
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 8
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = ArithmeticUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = ArithmeticUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}
 
Example 9
Source File: InverseHilbertMatrix.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the {@code (i, j)} entry of the inverse Hilbert matrix. Exact
 * arithmetic is used; in case of overflow, an exception is thrown.
 *
 * @param i Row index (starts at 0).
 * @param j Column index (starts at 0).
 * @return The coefficient of the inverse Hilbert matrix.
 */
public long getEntry(final int i, final int j) {
    long val = i + j + 1;
    long aux = CombinatoricsUtils.binomialCoefficient(n + i, n - j - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = CombinatoricsUtils.binomialCoefficient(n + j, n - i - 1);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    aux = CombinatoricsUtils.binomialCoefficient(i + j, i);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    val = ArithmeticUtils.mulAndCheck(val, aux);
    return ((i + j) & 1) == 0 ? val : -val;
}