List of usage examples for org.apache.commons.math3.exception NumberIsTooLargeException NumberIsTooLargeException
public NumberIsTooLargeException(Number wrong, Number max, boolean boundIsAllowed)
From source file:lirmm.inria.fr.math.BigSparseRealMatrix.java
/** * Build a sparse matrix with the supplied i and column dimensions. * * @param rowDimension Number of rows of the matrix. * @param columnDimension Number of columns of the matrix. * @throws NotStrictlyPositiveException if i or column dimension is not * positive./* w ww.j a va 2s.c om*/ * @throws NumberIsTooLargeException if the total number of entries of the * matrix is larger than {@code Integer.MAX_VALUE}. */ public BigSparseRealMatrix(int rowDimension, int columnDimension) throws NotStrictlyPositiveException, NumberIsTooLargeException { super(rowDimension, columnDimension); long lRow = rowDimension; long lCol = columnDimension; if (lRow * lCol >= Long.MAX_VALUE) { throw new NumberIsTooLargeException(lRow * lCol, Long.MAX_VALUE, false); } this.rows = rowDimension; this.columns = columnDimension; this.entries = new OpenLongToDoubleHashMap(0.0); }
From source file:de.tuberlin.uebb.jbop.example.DSCompilerOnlyCompose.java
@Override public int getPartialDerivativeIndex(final int... orders) throws DimensionMismatchException, NumberIsTooLargeException { // safety check if (orders.length != parameters) { throw new DimensionMismatchException(orders.length, parameters); }//w ww.j a v a2 s .c o m int index = 0; int m = order; int ordersSum = 0; for (int i = parameters - 1; i >= 0; --i) { // derivative order for current free parameter int derivativeOrder = orders[i]; // safety check ordersSum += derivativeOrder; if (ordersSum > order) { throw new NumberIsTooLargeException(ordersSum, order, true); } while (derivativeOrder-- > 0) { // as long as we differentiate according to current free parameter, // we have to skip the value part and dive into the derivative part // so we add the size of the value part to the base index index += sizes[i][m--]; } } return index; }
From source file:de.tuberlin.uebb.jbop.example.DSCompiler.java
@Override @Optimizable/*from w w w . j a v a2s . co m*/ public int getPartialDerivativeIndex(final int... orders) throws DimensionMismatchException, NumberIsTooLargeException { // safety check if (orders.length != parameters) { throw new DimensionMismatchException(orders.length, parameters); } int index = 0; int m = order; int ordersSum = 0; for (int i = parameters - 1; i >= 0; --i) { // derivative order for current free parameter int derivativeOrder = orders[i]; // safety check ordersSum += derivativeOrder; if (ordersSum > order) { throw new NumberIsTooLargeException(ordersSum, order, true); } while (derivativeOrder-- > 0) { // as long as we differentiate according to current free parameter, // we have to skip the value part and dive into the derivative part // so we add the size of the value part to the base index index += sizes[i][m--]; } } return index; }
From source file:de.tuberlin.uebb.jbop.example.DerivativeStructure.java
/** * Build an instance representing a variable. * <p>/*w ww . ja v a2 s .com*/ * Instances built using this constructor are considered to be the free variables with respect to which differentials * are computed. As such, their differential with respect to themselves is +1. * </p> * * @param parameters * number of free parameters * @param order * derivation order * @param index * index of the variable (from 0 to {@code parameters - 1}) * @param value * value of the variable * @throws NumberIsTooLargeException * if {@code index >= parameters}. * @see #DerivativeStructure(int, int, double) */ public DerivativeStructure(final int parameters, final int order, final int index, final double value) throws NumberIsTooLargeException { this(parameters, order, value); if (index >= parameters) { throw new NumberIsTooLargeException(index, parameters, false); } if (order > 0) { // the derivative of the variable with respect to itself is 1. data[DSCompilerFactory.getCompiler(index, order).getSize()] = 1.0; } }
From source file:de.tuberlin.uebb.jbop.example.DerivativeStructureOnlyCompose.java
/** * Build an instance representing a variable. * <p>//w w w . j a va2s.c o m * Instances built using this constructor are considered to be the free variables with respect to which differentials * are computed. As such, their differential with respect to themselves is +1. * </p> * * @param parameters * number of free parameters * @param order * derivation order * @param index * index of the variable (from 0 to {@code parameters - 1}) * @param value * value of the variable * @throws NumberIsTooLargeException * if {@code index >= parameters}. * @see #DerivativeStructure(int, int, double) */ public DerivativeStructureOnlyCompose(final int parameters, final int order, final int index, final double value) throws NumberIsTooLargeException { this(parameters, order, value); if (index >= parameters) { throw new NumberIsTooLargeException(index, parameters, false); } if (order > 0) { // the derivative of the variable with respect to itself is 1. data[DSCompilerFactoryOnlyCompose.getCompiler(index, order).getSize()] = 1.0; } }
From source file:eu.tsp.sal.NPointCrossover.java
/** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome//from w ww . j av a2 s . com * @param second the second chromosome * @return the pair of new chromosomes that resulted from the crossover * @throws DimensionMismatchException if the length of the two chromosomes is different * @throws NumberIsTooLargeException if the number of crossoverPoints is too large for the actual chromosomes */ private ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second) throws DimensionMismatchException, NumberIsTooLargeException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } if (crossoverPoints >= length) { throw new NumberIsTooLargeException(crossoverPoints, length, false); } // array representations of the parents final List<T> parent1Rep = first.getRepresentation(); final List<T> parent2Rep = second.getRepresentation(); // and of the children final List<T> child1Rep = new ArrayList<T>(length); final List<T> child2Rep = new ArrayList<T>(length); final RandomGenerator random = SensorGeneticAlgorithm.getRandomGenerator(); List<T> c1 = child1Rep; List<T> c2 = child2Rep; int remainingPoints = crossoverPoints; int lastIndex = 0; for (int i = 0; i < crossoverPoints; i++, remainingPoints--) { // select the next crossover point at random final int crossoverIndex = 1 + lastIndex + random.nextInt(length - lastIndex - remainingPoints); // copy the current segment for (int j = lastIndex; j < crossoverIndex; j++) { c1.add(parent1Rep.get(j)); c2.add(parent2Rep.get(j)); } // swap the children for the next segment List<T> tmp = c1; c1 = c2; c2 = tmp; lastIndex = crossoverIndex; } // copy the last segment for (int j = lastIndex; j < length; j++) { c1.add(parent1Rep.get(j)); c2.add(parent2Rep.get(j)); } return new ChromosomePair(first.newFixedLengthChromosome(child1Rep), second.newFixedLengthChromosome(child2Rep)); }
From source file:gedi.util.math.stat.distributions.NormalMixtureDistribution.java
public static NormalMixtureDistribution init(final double[] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException { if (numComponents == 1) return new NormalMixtureDistribution(new NormalDistribution[] { new NormalDistribution(new Mean().evaluate(data), new StandardDeviation().evaluate(data)) }, new double[] { 1 }); if (data.length < 2) { throw new NotStrictlyPositiveException(data.length); }//from w w w . j a v a2 s . c o m if (numComponents < 1) { throw new NumberIsTooSmallException(numComponents, 2, true); } if (numComponents > data.length) { throw new NumberIsTooLargeException(numComponents, data.length, true); } final int numRows = data.length; double[] sortedData = data.clone(); Arrays.sort(sortedData); // components of mixture model to be created double[] mixing = new double[numComponents]; NormalDistribution[] comp = new NormalDistribution[numComponents]; // create a component based on data in each bin for (int k = 0; k < numComponents; k++) { // minimum index (inclusive) from sorted data for this bin final int minIndex = (k * numRows) / numComponents; // maximum index (exclusive) from sorted data for this bin final int maxIndex = Math.min(numRows, ((k + 1) * numRows) / numComponents); double m = new Mean().evaluate(sortedData, minIndex, maxIndex - minIndex); double sd = new StandardDeviation().evaluate(sortedData, minIndex, maxIndex - minIndex); mixing[k] = 1d / numComponents; comp[k] = new NormalDistribution(m, sd); } return new NormalMixtureDistribution(comp, mixing); }
From source file:de.tuberlin.uebb.jbop.example.DSCompilerFactory.java
/** * Get the index of a partial derivative in an array. * /*from w w w .ja va2 s .co m*/ * @param parameters * number of free parameters * @param order * derivation order * @param sizes * sizes array * @param orders * derivation orders with respect to each parameter * (the lenght of this array must match the number of parameters) * @return index of the partial derivative * @throws NumberIsTooLargeException * if sum of derivation orders is larger * than the instance limits */ public static int getPartialDerivativeIndex(final int parameters, final int order, final int[][] sizes, final int... orders) throws NumberIsTooLargeException { // the value is obtained by diving into the recursive Dan Kalman's structure // this is theorem 2 of his paper, with recursion replaced by iteration int index = 0; int m = order; int ordersSum = 0; for (int i = parameters - 1; i >= 0; --i) { // derivative order for current free parameter int derivativeOrder = orders[i]; // safety check ordersSum += derivativeOrder; if (ordersSum > order) { throw new NumberIsTooLargeException(ordersSum, order, true); } while (derivativeOrder-- > 0) { // as long as we differentiate according to current free parameter, // we have to skip the value part and dive into the derivative part // so we add the size of the value part to the base index index += sizes[i][m--]; } } return index; }
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/*** * Creates {@code H} of size {@code m x m} as described in [1] (see above). * * @param d statistic/*w w w . j a va2s.c o m*/ * @param n sample size * @return H matrix * @throws NumberIsTooLargeException if fractional part is greater than 1 * @throws FractionConversionException if algorithm fails to convert {@code h} to a * {@link org.apache.commons.math3.fraction.BigFraction} in expressing {@code d} as \((k * - h) / m\) for integer {@code k, m} and \(0 <= h < 1\). */ private FieldMatrix<BigFraction> createExactH(double d, int n) throws NumberIsTooLargeException, FractionConversionException { final int k = (int) Math.ceil(n * d); final int m = 2 * k - 1; final double hDouble = k - n * d; if (hDouble >= 1) { throw new NumberIsTooLargeException(hDouble, 1.0, false); } BigFraction h = null; try { h = new BigFraction(hDouble, 1.0e-20, 10000); } catch (final FractionConversionException e1) { try { h = new BigFraction(hDouble, 1.0e-10, 10000); } catch (final FractionConversionException e2) { h = new BigFraction(hDouble, 1.0e-5, 10000); } } final BigFraction[][] Hdata = new BigFraction[m][m]; /* * Start by filling everything with either 0 or 1. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (i - j + 1 < 0) { Hdata[i][j] = BigFraction.ZERO; } else { Hdata[i][j] = BigFraction.ONE; } } } /* * Setting up power-array to avoid calculating the same value twice: hPowers[0] = h^1 ... * hPowers[m-1] = h^m */ final BigFraction[] hPowers = new BigFraction[m]; hPowers[0] = h; for (int i = 1; i < m; ++i) { hPowers[i] = h.multiply(hPowers[i - 1]); } /* * First column and last row has special values (each other reversed). */ for (int i = 0; i < m; ++i) { Hdata[i][0] = Hdata[i][0].subtract(hPowers[i]); Hdata[m - 1][i] = Hdata[m - 1][i].subtract(hPowers[m - i - 1]); } /* * [1] states: "For 1/2 < h < 1 the bottom left element of the matrix should be (1 - 2*h^m + * (2h - 1)^m )/m!" Since 0 <= h < 1, then if h > 1/2 is sufficient to check: */ if (h.compareTo(BigFraction.ONE_HALF) == 1) { Hdata[m - 1][0] = Hdata[m - 1][0].add(h.multiply(2).subtract(1).pow(m)); } /* * Aside from the first column and last row, the (i, j)-th element is 1/(i - j + 1)! if i - * j + 1 >= 0, else 0. 1's and 0's are already put, so only division with (i - j + 1)! is * needed in the elements that have 1's. There is no need to calculate (i - j + 1)! and then * divide - small steps avoid overflows. Note that i - j + 1 > 0 <=> i + 1 > j instead of * j'ing all the way to m. Also note that it is started at g = 2 because dividing by 1 isn't * really necessary. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < i + 1; ++j) { if (i - j + 1 > 0) { for (int g = 2; g <= i - j + 1; ++g) { Hdata[i][j] = Hdata[i][j].divide(g); } } } } return new Array2DRowFieldMatrix<BigFraction>(BigFractionField.getInstance(), Hdata); }
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/*** * Creates {@code H} of size {@code m x m} as described in [1] (see above) * using double-precision./*from w w w . ja va 2s . c om*/ * * @param d statistic * @param n sample size * @return H matrix * @throws NumberIsTooLargeException if fractional part is greater than 1 */ private RealMatrix createRoundedH(double d, int n) throws NumberIsTooLargeException { final int k = (int) Math.ceil(n * d); final int m = 2 * k - 1; final double h = k - n * d; if (h >= 1) { throw new NumberIsTooLargeException(h, 1.0, false); } final double[][] Hdata = new double[m][m]; /* * Start by filling everything with either 0 or 1. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (i - j + 1 < 0) { Hdata[i][j] = 0; } else { Hdata[i][j] = 1; } } } /* * Setting up power-array to avoid calculating the same value twice: hPowers[0] = h^1 ... * hPowers[m-1] = h^m */ final double[] hPowers = new double[m]; hPowers[0] = h; for (int i = 1; i < m; ++i) { hPowers[i] = h * hPowers[i - 1]; } /* * First column and last row has special values (each other reversed). */ for (int i = 0; i < m; ++i) { Hdata[i][0] = Hdata[i][0] - hPowers[i]; Hdata[m - 1][i] -= hPowers[m - i - 1]; } /* * [1] states: "For 1/2 < h < 1 the bottom left element of the matrix should be (1 - 2*h^m + * (2h - 1)^m )/m!" Since 0 <= h < 1, then if h > 1/2 is sufficient to check: */ if (Double.compare(h, 0.5) > 0) { Hdata[m - 1][0] += FastMath.pow(2 * h - 1, m); } /* * Aside from the first column and last row, the (i, j)-th element is 1/(i - j + 1)! if i - * j + 1 >= 0, else 0. 1's and 0's are already put, so only division with (i - j + 1)! is * needed in the elements that have 1's. There is no need to calculate (i - j + 1)! and then * divide - small steps avoid overflows. Note that i - j + 1 > 0 <=> i + 1 > j instead of * j'ing all the way to m. Also note that it is started at g = 2 because dividing by 1 isn't * really necessary. */ for (int i = 0; i < m; ++i) { for (int j = 0; j < i + 1; ++j) { if (i - j + 1 > 0) { for (int g = 2; g <= i - j + 1; ++g) { Hdata[i][j] /= g; } } } } return MatrixUtils.createRealMatrix(Hdata); }