org.apache.commons.math.util.MathUtils Java Examples
The following examples show how to use
org.apache.commons.math.util.MathUtils.
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: NonMonotonousSequenceException.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Construct the exception. * * @param wrong Value that did not match the requirements. * @param previous Previous value in the sequence. * @param index Index of the value that did not match the requirements. * @param direction Strictly positive for a sequence required to be * increasing, negative (or zero) for a decreasing sequence. * @param strict Whether the sequence must be strictly increasing or * decreasing. */ public NonMonotonousSequenceException(Number wrong, Number previous, int index, MathUtils.OrderDirection direction, boolean strict) { super(direction == MathUtils.OrderDirection.INCREASING ? (strict ? LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE : LocalizedFormats.NOT_INCREASING_SEQUENCE) : (strict ? LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE : LocalizedFormats.NOT_DECREASING_SEQUENCE), wrong, previous, index, index - 1); this.direction = direction; this.strict = strict; this.index = index; this.previous = previous; }
Example #2
Source File: AbstractRealMatrix.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Computes a hashcode for the matrix. * * @return hashcode for matrix */ @Override public int hashCode() { int ret = 7; final int nRows = getRowDimension(); final int nCols = getColumnDimension(); ret = ret * 31 + nRows; ret = ret * 31 + nCols; for (int row = 0; row < nRows; ++row) { for (int col = 0; col < nCols; ++col) { ret = ret * 31 + (11 * (row+1) + 17 * (col+1)) * MathUtils.hash(getEntry(row, col)); } } return ret; }
Example #3
Source File: EigenDecompositionImplTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Verifies operation on indefinite matrix */ public void testZeroDivide() { RealMatrix indefinite = MatrixUtils.createRealMatrix(new double [][] { { 0.0, 1.0, -1.0 }, { 1.0, 1.0, 0.0 }, { -1.0,0.0, 1.0 } }); EigenDecomposition ed = new EigenDecompositionImpl(indefinite, MathUtils.SAFE_MIN); checkEigenValues((new double[] {2, 1, -1}), ed, 1E-12); double isqrt3 = 1/Math.sqrt(3.0); checkEigenVector((new double[] {isqrt3,isqrt3,-isqrt3}), ed, 1E-12); double isqrt2 = 1/Math.sqrt(2.0); checkEigenVector((new double[] {0.0,-isqrt2,-isqrt2}), ed, 1E-12); double isqrt6 = 1/Math.sqrt(6.0); checkEigenVector((new double[] {2*isqrt6,-isqrt6,isqrt6}), ed, 1E-12); }
Example #4
Source File: jMutRepair_0015_s.java From coming with MIT License | 6 votes |
/** * Returns the row with the minimum ratio as given by the minimum ratio test (MRT). * @param tableau simple tableau for the problem * @param col the column to test the ratio of. See {@link #getPivotColumn(SimplexTableau)} * @return row with the minimum ratio */ private Integer getPivotRow(final int col, final SimplexTableau tableau) { double minRatio = Double.MAX_VALUE; Integer minRatioPos = null; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double rhs = tableau.getEntry(i, tableau.getWidth() - 1); final double entry = tableau.getEntry(i, col); if (MathUtils.compareTo(entry, 0, epsilon) >= 0) { final double ratio = rhs / entry; if (ratio < minRatio) { minRatio = ratio; minRatioPos = i; } } } return minRatioPos; }
Example #5
Source File: EigenDecompositionImpl.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Check if a matrix is symmetric. * @param matrix * matrix to check * @return true if matrix is symmetric */ private boolean isSymmetric(final RealMatrix matrix) { final int rows = matrix.getRowDimension(); final int columns = matrix.getColumnDimension(); final double eps = 10 * rows * columns * MathUtils.EPSILON; for (int i = 0; i < rows; ++i) { for (int j = i + 1; j < columns; ++j) { final double mij = matrix.getEntry(i, j); final double mji = matrix.getEntry(j, i); if (Math.abs(mij - mji) > (Math.max(Math.abs(mij), Math .abs(mji)) * eps)) { return false; } } } return true; }
Example #6
Source File: Arja_00171_t.java From coming with MIT License | 6 votes |
/** * Check if a matrix is symmetric. * @param matrix matrix to check * @return true if matrix is symmetric */ private boolean isSymmetric(final RealMatrix matrix) { final int rows = matrix.getRowDimension(); final int columns = matrix.getColumnDimension(); final double eps = 10 * rows * columns * MathUtils.EPSILON; for (int i = 0; i < rows; ++i) { for (int j = i + 1; j < columns; ++j) { final double mij = matrix.getEntry(i, j); final double mji = matrix.getEntry(j, i); if (Math.abs(mij - mji) > (Math.max(Math.abs(mij), Math.abs(mji)) * eps)) { return false; } } } return true; }
Example #7
Source File: HarmonicFitterTest.java From astor with GNU General Public License v2.0 | 6 votes |
@Test public void testNoError() throws OptimizationException { HarmonicFunction f = new HarmonicFunction(0.2, 3.4, 4.1); HarmonicFitter fitter = new HarmonicFitter(new LevenbergMarquardtOptimizer()); for (double x = 0.0; x < 1.3; x += 0.01) { fitter.addObservedPoint(1.0, x, f.value(x)); } HarmonicFunction fitted = fitter.fit(); assertEquals(f.getAmplitude(), fitted.getAmplitude(), 1.0e-13); assertEquals(f.getPulsation(), fitted.getPulsation(), 1.0e-13); assertEquals(f.getPhase(), MathUtils.normalizeAngle(fitted.getPhase(), f.getPhase()), 1.0e-13); for (double x = -1.0; x < 1.0; x += 0.01) { assertTrue(Math.abs(f.value(x) - fitted.value(x)) < 1.0e-13); } }
Example #8
Source File: Cardumen_00275_s.java From coming with MIT License | 6 votes |
/** * Returns the row with the minimum ratio as given by the minimum ratio test (MRT). * @param tableau simple tableau for the problem * @param col the column to test the ratio of. See {@link #getPivotColumn(SimplexTableau)} * @return row with the minimum ratio */ private Integer getPivotRow(final int col, final SimplexTableau tableau) { double minRatio = Double.MAX_VALUE; Integer minRatioPos = null; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { final double rhs = tableau.getEntry(i, tableau.getWidth() - 1); final double entry = tableau.getEntry(i, col); if (MathUtils.compareTo(entry, 0, epsilon) >= 0) { final double ratio = rhs / entry; if (ratio < minRatio) { minRatio = ratio; minRatioPos = i; } } } return minRatioPos; }
Example #9
Source File: DescriptiveStatisticsTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void checkremoval(DescriptiveStatistics dstat, int wsize, double mean1, double mean2, double mean3) { dstat.setWindowSize(wsize); dstat.clear(); for (int i = 1 ; i <= 6 ; ++i) { dstat.addValue(i); } assertTrue(MathUtils.equals(mean1, dstat.getMean())); dstat.replaceMostRecentValue(0); assertTrue(MathUtils.equals(mean2, dstat.getMean())); dstat.removeMostRecentValue(); assertTrue(MathUtils.equals(mean3, dstat.getMean())); }
Example #10
Source File: VarianceTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testWeightedVariance() { Variance variance = new Variance(); assertEquals(expectedWeightedValue(), variance.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance()); // All weights = 1 -> weighted variance = unweighted variance assertEquals(expectedValue(), variance.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance()); // All weights the same -> when weights are normalized to sum to the length of the values array, // weighted variance = unweighted value assertEquals(expectedValue(), variance.evaluate(testArray, MathUtils.normalizeArray(identicalWeightsArray, testArray.length), 0, testArray.length), getTolerance()); }
Example #11
Source File: SummaryStatistics.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Returns true iff <code>object</code> is a * <code>SummaryStatistics</code> instance and all statistics have the * same values as this. * @param object the object to test equality against. * @return true if object equals this */ @Override public boolean equals(Object object) { if (object == this) { return true; } if (object instanceof SummaryStatistics == false) { return false; } SummaryStatistics stat = (SummaryStatistics)object; return MathUtils.equals(stat.getGeometricMean(), getGeometricMean()) && MathUtils.equals(stat.getMax(), getMax()) && MathUtils.equals(stat.getMean(), getMean()) && MathUtils.equals(stat.getMin(), getMin()) && MathUtils.equals(stat.getN(), getN()) && MathUtils.equals(stat.getSum(), getSum()) && MathUtils.equals(stat.getSumsq(), getSumsq()) && MathUtils.equals(stat.getVariance(), getVariance()); }
Example #12
Source File: SaddlePointExpansion.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Compute the PMF for a binomial distribution using the saddle point * expansion. * * @param x the value at which the probability is evaluated. * @param n the number of trials. * @param p the probability of success. * @param q the probability of failure (1 - p). * @return log(p(x)). */ static double logBinomialProbability(int x, int n, double p, double q) { double ret; if (x == 0) { if (p < 0.1) { ret = -getDeviancePart(n, n * q) - n * p; } else { ret = n * FastMath.log(q); } } else if (x == n) { if (q < 0.1) { ret = -getDeviancePart(n, n * p) - n * q; } else { ret = n * FastMath.log(p); } } else { ret = getStirlingError(n) - getStirlingError(x) - getStirlingError(n - x) - getDeviancePart(x, n * p) - getDeviancePart(n - x, n * q); double f = (MathUtils.TWO_PI * x * (n - x)) / n; ret = -0.5 * FastMath.log(f) + ret; } return ret; }
Example #13
Source File: Cardumen_00185_s.java From coming with MIT License | 6 votes |
/** * Returns the row with the minimum ratio as given by the minimum ratio test (MRT). * @param tableau simple tableau for the problem * @param col the column to test the ratio of. See {@link #getPivotColumn()} * @return row with the minimum ratio */ private Integer getPivotRow(final int col, final SimplexTableau tableau) { double minRatio = Double.MAX_VALUE; Integer minRatioPos = null; for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { double rhs = tableau.getEntry(i, tableau.getWidth() - 1); if (MathUtils.compareTo(tableau.getEntry(i, col), 0, epsilon) >= 0) { double ratio = rhs / tableau.getEntry(i, col); if (ratio < minRatio) { minRatio = ratio; minRatioPos = i; } } } return minRatioPos; }
Example #14
Source File: Arja_0036_s.java From coming with MIT License | 6 votes |
/** * Create a counter. * * @param size Counter sizes (number of slots in each dimension). * @throws NotStrictlyPositiveException if one of the sizes is * negative or zero. */ public MultidimensionalCounter(int ... size) { dimension = size.length; this.size = MathUtils.copyOf(size); uniCounterOffset = new int[dimension]; last = dimension - 1; int tS = size[last]; for (int i = 0; i < last; i++) { int count = 1; for (int j = i + 1; j < dimension; j++) { count *= size[j]; } uniCounterOffset[i] = count; tS *= size[i]; } uniCounterOffset[last] = 0; if (tS <= 0) { throw new NotStrictlyPositiveException(tS); } totalSize = tS; }
Example #15
Source File: JGenProg2017_0071_t.java From coming with MIT License | 6 votes |
/** * Check if a matrix is symmetric. * @param matrix matrix to check * @return true if matrix is symmetric */ private boolean isSymmetric(final RealMatrix matrix) { final int rows = matrix.getRowDimension(); final int columns = matrix.getColumnDimension(); final double eps = 10 * rows * columns * MathUtils.EPSILON; for (int i = 0; i < rows; ++i) { for (int j = i + 1; j < columns; ++j) { final double mij = matrix.getEntry(i, j); final double mji = matrix.getEntry(j, i); if (Math.abs(mij - mji) > (Math.max(Math.abs(mij), Math.abs(mji)) * eps)) { return false; } } } return true; }
Example #16
Source File: VarianceTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testWeightedVariance() { Variance variance = new Variance(); assertEquals(expectedWeightedValue(), variance.evaluate(testArray, testWeightsArray, 0, testArray.length), getTolerance()); // All weights = 1 -> weighted variance = unweighted variance assertEquals(expectedValue(), variance.evaluate(testArray, unitWeightsArray, 0, testArray.length), getTolerance()); // All weights the same -> when weights are normalized to sum to the length of the values array, // weighted variance = unweighted value assertEquals(expectedValue(), variance.evaluate(testArray, MathUtils.normalizeArray(identicalWeightsArray, testArray.length), 0, testArray.length), getTolerance()); }
Example #17
Source File: Cardumen_00182_t.java From coming with MIT License | 6 votes |
/** * Solves Phase 1 of the Simplex method. * @param tableau simple tableau for the problem * @exception OptimizationException if the maximal number of iterations is * exceeded, or if the problem is found not to have a bounded solution, or * if there is no feasible solution */ protected void solvePhase1(final SimplexTableau tableau) throws OptimizationException { // make sure we're in Phase 1 if (tableau.getNumArtificialVariables() == 0) { return; } while (!isPhase1Solved(tableau)) { doIteration(tableau); } // if W is not zero then we have no feasible solution if (!MathUtils.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0, epsilon)) { throw new NoFeasibleSolutionException(); } }
Example #18
Source File: MultivariateSummaryStatistics.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns hash code based on values of statistics * * @return hash code */ @Override public int hashCode() { int result = 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getMax()); result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + MathUtils.hash(getN()); result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + MathUtils.hash(getSumSq()); result = result * 31 + MathUtils.hash(getSumLog()); result = result * 31 + getCovariance().hashCode(); return result; }
Example #19
Source File: EigenDecompositionImplTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testMath308() { double[] mainTridiagonal = { 22.330154644539597, 46.65485522478641, 17.393672330044705, 54.46687435351116, 80.17800767709437 }; double[] secondaryTridiagonal = { 13.04450406501361, -5.977590941539671, 2.9040909856707517, 7.1570352792841225 }; // the reference values have been computed using routine DSTEMR // from the fortran library LAPACK version 3.2.1 double[] refEigenValues = { 82.044413207204002, 53.456697699894512, 52.536278520113882, 18.847969733754262, 14.138204224043099 }; RealVector[] refEigenVectors = { new ArrayRealVector(new double[] { -0.000462690386766, -0.002118073109055, 0.011530080757413, 0.252322434584915, 0.967572088232592 }), new ArrayRealVector(new double[] { 0.314647769490148, 0.750806415553905, -0.167700312025760, -0.537092972407375, 0.143854968127780 }), new ArrayRealVector(new double[] { 0.222368839324646, 0.514921891363332, -0.021377019336614, 0.801196801016305, -0.207446991247740 }), new ArrayRealVector(new double[] { -0.713933751051495, 0.190582113553930, -0.671410443368332, 0.056056055955050, -0.006541576993581 }), new ArrayRealVector(new double[] { -0.584677060845929, 0.367177264979103, 0.721453187784497, -0.052971054621812, 0.005740715188257 }) }; EigenDecomposition decomposition = new EigenDecompositionImpl(mainTridiagonal, secondaryTridiagonal, MathUtils.SAFE_MIN); double[] eigenValues = decomposition.getRealEigenvalues(); for (int i = 0; i < refEigenValues.length; ++i) { assertEquals(refEigenValues[i], eigenValues[i], 1.0e-5); assertEquals(0, refEigenVectors[i].subtract(decomposition.getEigenvector(i)).getNorm(), 2.0e-7); } }
Example #20
Source File: Line.java From astor with GNU General Public License v2.0 | 5 votes |
/** Reset the instance as if built from a line and an angle. * @param p point belonging to the line * @param alpha angle of the line with respect to abscissa axis */ public void reset(final Vector2D p, final double alpha) { this.angle = MathUtils.normalizeAngle(alpha, FastMath.PI); cos = FastMath.cos(this.angle); sin = FastMath.sin(this.angle); originOffset = cos * p.getY() - sin * p.getX(); }
Example #21
Source File: Vector3D.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Get a hashCode for the 3D vector. * <p> * All NaN values have the same hash code.</p> * * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 642; } return 643 * (164 * MathUtils.hash(x) + 3 * MathUtils.hash(y) + MathUtils.hash(z)); }
Example #22
Source File: MultivariateSummaryStatistics.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Returns hash code based on values of statistics * * @return hash code */ @Override public int hashCode() { int result = 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getGeometricMean()); result = result * 31 + MathUtils.hash(getMax()); result = result * 31 + MathUtils.hash(getMean()); result = result * 31 + MathUtils.hash(getMin()); result = result * 31 + MathUtils.hash(getN()); result = result * 31 + MathUtils.hash(getSum()); result = result * 31 + MathUtils.hash(getSumSq()); result = result * 31 + MathUtils.hash(getSumLog()); result = result * 31 + getCovariance().hashCode(); return result; }
Example #23
Source File: PascalDistributionImpl.java From astor with GNU General Public License v2.0 | 5 votes |
/** * For this distribution, X, this method returns P(X = x). * @param x the value at which the PMF is evaluated * @return PMF for this distribution */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = MathUtils.binomialCoefficientDouble(x + getNumberOfSuccesses() - 1, getNumberOfSuccesses() - 1) * Math.pow(getProbabilityOfSuccess(), getNumberOfSuccesses()) * Math.pow(1.0 - getProbabilityOfSuccess(), x); } return ret; }
Example #24
Source File: PascalDistributionImpl.java From astor with GNU General Public License v2.0 | 5 votes |
/** * For this distribution, X, this method returns P(X = x). * @param x the value at which the PMF is evaluated * @return PMF for this distribution */ public double probability(int x) { double ret; if (x < 0) { ret = 0.0; } else { ret = MathUtils.binomialCoefficientDouble(x + getNumberOfSuccesses() - 1, getNumberOfSuccesses() - 1) * Math.pow(getProbabilityOfSuccess(), getNumberOfSuccesses()) * Math.pow(1.0 - getProbabilityOfSuccess(), x); } return ret; }
Example #25
Source File: Complex.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Get a hashCode for the complex number. * <p> * All NaN values have the same hash code.</p> * * @return a hash code value for this object */ @Override public int hashCode() { if (isNaN()) { return 7; } return 37 * (17 * MathUtils.hash(imaginary) + MathUtils.hash(real)); }
Example #26
Source File: Math_87_SimplexTableau_t.java From coming with MIT License | 5 votes |
/** * Checks whether the given column is basic. * @param col index of the column to check * @return the row that the variable is basic in. null if the column is not basic */ private Integer getBasicRow(final int col) { Integer row = null; for (int i = getNumObjectiveFunctions(); i < getHeight(); i++) { if (MathUtils.equals(getEntry(i, col), 1.0, epsilon) && (row == null)) { row = i; } else if (!MathUtils.equals(getEntry(i, col), 0.0, epsilon)) { return null; } } return row; }
Example #27
Source File: Cardumen_0063_s.java From coming with MIT License | 5 votes |
/** * Checks whether Phase 1 is solved. * @param tableau simple tableau for the problem * @return whether Phase 1 is solved */ private boolean isPhase1Solved(final SimplexTableau tableau) { if (tableau.getNumArtificialVariables() == 0) { return true; } for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) { if (MathUtils.compareTo(tableau.getEntry(0, i), 0, epsilon) < 0) { return false; } } return true; }
Example #28
Source File: PolynomialFunctionLagrangeForm.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Construct a Lagrange polynomial with the given abscissas and function * values. The order of interpolating points are not important. * <p> * The constructor makes copy of the input arrays and assigns them.</p> * * @param x interpolating points * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. * @throws org.apache.commons.math.exception.NonMonotonousSequenceException * if two abscissae have the same value. */ public PolynomialFunctionLagrangeForm(double x[], double y[]) { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(y, 0, this.y, 0, y.length); coefficientsComputed = false; if (!verifyInterpolationArray(x, y, false)) { MathUtils.sortInPlace(this.x, this.y); // Second check in case some abscissa is duplicated. verifyInterpolationArray(this.x, this.y, true); } }
Example #29
Source File: Math_47_Complex_t.java From coming with MIT License | 5 votes |
/** * Get a hashCode for the complex number. * Any {@code Double.NaN} value in real or imaginary part produces * the same hash code {@code 7}. * * @return a hash code value for this object. */ @Override public int hashCode() { if (isNaN) { return 7; } return 37 * (17 * MathUtils.hash(imaginary) + MathUtils.hash(real)); }
Example #30
Source File: Fraction.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Create a fraction given the numerator and denominator. The fraction is * reduced to lowest terms. * @param num the numerator. * @param den the denominator. * @throws ArithmeticException if the denominator is <code>zero</code> */ public Fraction(int num, int den) { if (den == 0) { throw MathRuntimeException.createArithmeticException( ZERO_DENOMINATOR_MESSAGE, num, den); } if (den < 0) { if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) { throw MathRuntimeException.createArithmeticException( OVERFLOW_MESSAGE, num, den); } num = -num; den = -den; } // reduce numerator and denominator by greatest common denominator. final int d = MathUtils.gcd(num, den); if (d > 1) { num /= d; den /= d; } // move sign to numerator. if (den < 0) { num = -num; den = -den; } this.numerator = num; this.denominator = den; }