Java Code Examples for org.apache.commons.math3.util.FastMath#min()
The following examples show how to use
org.apache.commons.math3.util.FastMath#min() .
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: BlockRealMatrix.java From astor with GNU General Public License v2.0 | 6 votes |
/** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixPreservingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); }
Example 2
Source File: BlockRealMatrix.java From astor with GNU General Public License v2.0 | 6 votes |
/** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); }
Example 3
Source File: BlockFieldMatrix.java From astor with GNU General Public License v2.0 | 6 votes |
/** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixPreservingVisitor<T> visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int p = pStart; p < pEnd; ++p) { for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final T[] block = blocks[iBlock * blockColumns + jBlock]; int k = (p - pStart) * jWidth; for (int q = qStart; q < qEnd; ++q) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); }
Example 4
Source File: BlockRealMatrix.java From astor with GNU General Public License v2.0 | 6 votes |
/** {@inheritDoc} */ @Override public double walkInOptimizedOrder(final RealMatrixChangingVisitor visitor) { visitor.start(rows, columns, 0, rows - 1, 0, columns - 1); int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final double[] block = blocks[blockIndex]; int k = 0; for (int p = pStart; p < pEnd; ++p) { for (int q = qStart; q < qEnd; ++q) { block[k] = visitor.visit(p, q, block[k]); ++k; } } ++blockIndex; } } return visitor.end(); }
Example 5
Source File: PolynomialFunction.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Add a polynomial to the instance. * * @param p Polynomial to add. * @return a new polynomial which is the sum of the instance and {@code p}. */ public PolynomialFunction add(final PolynomialFunction p) { // identify the lowest degree polynomial final int lowLength = FastMath.min(coefficients.length, p.coefficients.length); final int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] + p.coefficients[i]; } System.arraycopy((coefficients.length < p.coefficients.length) ? p.coefficients : coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); return new PolynomialFunction(newCoefficients); }
Example 6
Source File: BlockRealMatrix.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Create a data array in blocks layout. * <p> * This method can be used to create the array argument of the {@link * #BlockRealMatrix(int, int, double[][], boolean)} constructor. * </p> * @param rows Number of rows in the new matrix. * @param columns Number of columns in the new matrix. * @return a new data array in blocks layout. * @see #toBlocksLayout(double[][]) * @see #BlockRealMatrix(int, int, double[][], boolean) */ public static double[][] createBlocksLayout(final int rows, final int columns) { final int blockRows = (rows + BLOCK_SIZE - 1) / BLOCK_SIZE; final int blockColumns = (columns + BLOCK_SIZE - 1) / BLOCK_SIZE; final double[][] blocks = new double[blockRows * blockColumns][]; int blockIndex = 0; for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); final int iHeight = pEnd - pStart; for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); final int jWidth = qEnd - qStart; blocks[blockIndex] = new double[iHeight * jWidth]; ++blockIndex; } } return blocks; }
Example 7
Source File: PolynomialFunction.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Add a polynomial to the instance. * * @param p Polynomial to add. * @return a new polynomial which is the sum of the instance and {@code p}. */ public PolynomialFunction add(final PolynomialFunction p) { // identify the lowest degree polynomial final int lowLength = FastMath.min(coefficients.length, p.coefficients.length); final int highLength = FastMath.max(coefficients.length, p.coefficients.length); // build the coefficients array double[] newCoefficients = new double[highLength]; for (int i = 0; i < lowLength; ++i) { newCoefficients[i] = coefficients[i] + p.coefficients[i]; } System.arraycopy((coefficients.length < p.coefficients.length) ? p.coefficients : coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); return new PolynomialFunction(newCoefficients); }
Example 8
Source File: BlockFieldMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ @Override public FieldMatrix<T> transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockFieldMatrix<T> out = new BlockFieldMatrix<T>(getField(), nCols, nRows); // perform transpose block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < blockColumns; ++iBlock) { for (int jBlock = 0; jBlock < blockRows; ++jBlock) { // transpose current block final T[] outBlock = out.blocks[blockIndex]; final T[] tBlock = blocks[jBlock * blockColumns + iBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows); int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lInc = pEnd - pStart; int l = p - pStart; for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[l]; ++k; l+= lInc; } } // go to next block ++blockIndex; } } return out; }
Example 9
Source File: IterativeLegendreGaussIntegrator.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ @Override protected double doIntegrate() throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // Compute first estimate with a single step. double oldt = stage(1); int n = 2; while (true) { // Improve integral with a larger number of steps. final double t = stage(n); // Estimate the error. final double delta = FastMath.abs(t - oldt); final double limit = FastMath.max(getAbsoluteAccuracy(), getRelativeAccuracy() * (FastMath.abs(oldt) + FastMath.abs(t)) * 0.5); // check convergence if (iterations.getCount() + 1 >= getMinimalIterationCount() && delta <= limit) { return t; } // Prepare next iteration. final double ratio = FastMath.min(4, FastMath.pow(delta / limit, 0.5 / numberOfPoints)); n = FastMath.max((int) (ratio * n), n + 1); oldt = t; iterations.incrementCount(); } }
Example 10
Source File: BlockRealMatrix.java From astor with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ @Override public BlockRealMatrix transpose() { final int nRows = getRowDimension(); final int nCols = getColumnDimension(); final BlockRealMatrix out = new BlockRealMatrix(nCols, nRows); // perform transpose block-wise, to ensure good cache behavior int blockIndex = 0; for (int iBlock = 0; iBlock < blockColumns; ++iBlock) { for (int jBlock = 0; jBlock < blockRows; ++jBlock) { // transpose current block final double[] outBlock = out.blocks[blockIndex]; final double[] tBlock = blocks[jBlock * blockColumns + iBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, columns); final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, rows); int k = 0; for (int p = pStart; p < pEnd; ++p) { final int lInc = pEnd - pStart; int l = p - pStart; for (int q = qStart; q < qEnd; ++q) { outBlock[k] = tBlock[l]; ++k; l+= lInc; } } // go to next block ++blockIndex; } } return out; }
Example 11
Source File: BlockRealMatrix.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ @Override public double[] preMultiply(final double[] v) throws DimensionMismatchException { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final double[] out = new double[columns]; // perform multiplication block-wise, to ensure good cache behavior for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final double[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q = qStart; q < qEnd; ++q) { int k = q - qStart; double sum = 0; int p = pStart; while (p < pEnd - 3) { sum += block[k] * v[p] + block[k + jWidth] * v[p + 1] + block[k + jWidth2] * v[p + 2] + block[k + jWidth3] * v[p + 3]; k += jWidth4; p += 4; } while (p < pEnd) { sum += block[k] * v[p++]; k += jWidth; } out[q] += sum; } } } return out; }
Example 12
Source File: OrderedCrossover.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover. * * @param first the first chromosome * @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 */ protected ChromosomePair mate(final AbstractListChromosome<T> first, final AbstractListChromosome<T> second) throws DimensionMismatchException { final int length = first.getLength(); if (length != second.getLength()) { throw new DimensionMismatchException(second.getLength(), length); } // array representations of the parents final List<T> parent1Rep = first.getRepresentation(); final List<T> parent2Rep = second.getRepresentation(); // and of the children final List<T> child1 = new ArrayList<T>(length); final List<T> child2 = new ArrayList<T>(length); // sets of already inserted items for quick access final Set<T> child1Set = new HashSet<T>(length); final Set<T> child2Set = new HashSet<T>(length); final RandomGenerator random = GeneticAlgorithm.getRandomGenerator(); // choose random points, making sure that lb < ub. int a = random.nextInt(length); int b; do { b = random.nextInt(length); } while (a == b); // determine the lower and upper bounds final int lb = FastMath.min(a, b); final int ub = FastMath.max(a, b); // add the subLists that are between lb and ub child1.addAll(parent1Rep.subList(lb, ub + 1)); child1Set.addAll(child1); child2.addAll(parent2Rep.subList(lb, ub + 1)); child2Set.addAll(child2); // iterate over every item in the parents for (int i = 1; i <= length; i++) { final int idx = (ub + i) % length; // retrieve the current item in each parent final T item1 = parent1Rep.get(idx); final T item2 = parent2Rep.get(idx); // if the first child already contains the item in the second parent add it if (!child1Set.contains(item2)) { child1.add(item2); child1Set.add(item2); } // if the second child already contains the item in the first parent add it if (!child2Set.contains(item1)) { child2.add(item1); child2Set.add(item1); } } // rotate so that the original slice is in the same place as in the parents. Collections.rotate(child1, lb); Collections.rotate(child2, lb); return new ChromosomePair(first.newFixedLengthChromosome(child1), second.newFixedLengthChromosome(child2)); }
Example 13
Source File: SchurTransformer.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Perform a double QR step involving rows l:idx and columns m:n * * @param il the index of the small sub-diagonal element * @param im the start index for the QR step * @param iu the current eigenvalue index * @param shift shift information holder * @param hVec the initial houseHolder vector */ private void performDoubleQRStep(final int il, final int im, final int iu, final ShiftInfo shift, final double[] hVec) { final int n = matrixT.length; double p = hVec[0]; double q = hVec[1]; double r = hVec[2]; for (int k = im; k <= iu - 1; k++) { boolean notlast = k != (iu - 1); if (k != im) { p = matrixT[k][k - 1]; q = matrixT[k + 1][k - 1]; r = notlast ? matrixT[k + 2][k - 1] : 0.0; shift.x = FastMath.abs(p) + FastMath.abs(q) + FastMath.abs(r); if (Precision.equals(shift.x, 0.0, epsilon)) { continue; } p /= shift.x; q /= shift.x; r /= shift.x; } double s = FastMath.sqrt(p * p + q * q + r * r); if (p < 0.0) { s = -s; } if (s != 0.0) { if (k != im) { matrixT[k][k - 1] = -s * shift.x; } else if (il != im) { matrixT[k][k - 1] = -matrixT[k][k - 1]; } p += s; shift.x = p / s; shift.y = q / s; double z = r / s; q /= p; r /= p; // Row modification for (int j = k; j < n; j++) { p = matrixT[k][j] + q * matrixT[k + 1][j]; if (notlast) { p += r * matrixT[k + 2][j]; matrixT[k + 2][j] -= p * z; } matrixT[k][j] -= p * shift.x; matrixT[k + 1][j] -= p * shift.y; } // Column modification for (int i = 0; i <= FastMath.min(iu, k + 3); i++) { p = shift.x * matrixT[i][k] + shift.y * matrixT[i][k + 1]; if (notlast) { p += z * matrixT[i][k + 2]; matrixT[i][k + 2] -= p * r; } matrixT[i][k] -= p; matrixT[i][k + 1] -= p * q; } // Accumulate transformations final int high = matrixT.length - 1; for (int i = 0; i <= high; i++) { p = shift.x * matrixP[i][k] + shift.y * matrixP[i][k + 1]; if (notlast) { p += z * matrixP[i][k + 2]; matrixP[i][k + 2] -= p * r; } matrixP[i][k] -= p; matrixP[i][k + 1] -= p * q; } } // (s != 0) } // k loop // clean up pollution due to round-off errors for (int i = im + 2; i <= iu; i++) { matrixT[i][i-2] = 0.0; if (i > im + 2) { matrixT[i][i-3] = 0.0; } } }
Example 14
Source File: BlockFieldMatrix.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ @Override public T[] preMultiply(final T[] v) { if (v.length != rows) { throw new DimensionMismatchException(v.length, rows); } final T[] out = buildArray(getField(), columns); final T zero = getField().getZero(); // perform multiplication block-wise, to ensure good cache behavior for (int jBlock = 0; jBlock < blockColumns; ++jBlock) { final int jWidth = blockWidth(jBlock); final int jWidth2 = jWidth + jWidth; final int jWidth3 = jWidth2 + jWidth; final int jWidth4 = jWidth3 + jWidth; final int qStart = jBlock * BLOCK_SIZE; final int qEnd = FastMath.min(qStart + BLOCK_SIZE, columns); for (int iBlock = 0; iBlock < blockRows; ++iBlock) { final T[] block = blocks[iBlock * blockColumns + jBlock]; final int pStart = iBlock * BLOCK_SIZE; final int pEnd = FastMath.min(pStart + BLOCK_SIZE, rows); for (int q = qStart; q < qEnd; ++q) { int k = q - qStart; T sum = zero; int p = pStart; while (p < pEnd - 3) { sum = sum. add(block[k].multiply(v[p])). add(block[k + jWidth].multiply(v[p + 1])). add(block[k + jWidth2].multiply(v[p + 2])). add(block[k + jWidth3].multiply(v[p + 3])); k += jWidth4; p += 4; } while (p < pEnd) { sum = sum.add(block[k].multiply(v[p++])); k += jWidth; } out[q] = out[q].add(sum); } } } return out; }
Example 15
Source File: BlockFieldMatrix.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ @Override public void setSubMatrix(final T[][] subMatrix, final int row, final int column) throws DimensionMismatchException, OutOfRangeException, NoDataException, NullArgumentException { // safety checks MathUtils.checkNotNull(subMatrix); final int refLength = subMatrix[0].length; if (refLength == 0) { throw new NoDataException(LocalizedFormats.AT_LEAST_ONE_COLUMN); } final int endRow = row + subMatrix.length - 1; final int endColumn = column + refLength - 1; checkSubMatrixIndex(row, endRow, column, endColumn); for (final T[] subRow : subMatrix) { if (subRow.length != refLength) { throw new DimensionMismatchException(refLength, subRow.length); } } // compute blocks bounds final int blockStartRow = row / BLOCK_SIZE; final int blockEndRow = (endRow + BLOCK_SIZE) / BLOCK_SIZE; final int blockStartColumn = column / BLOCK_SIZE; final int blockEndColumn = (endColumn + BLOCK_SIZE) / BLOCK_SIZE; // perform copy block-wise, to ensure good cache behavior for (int iBlock = blockStartRow; iBlock < blockEndRow; ++iBlock) { final int iHeight = blockHeight(iBlock); final int firstRow = iBlock * BLOCK_SIZE; final int iStart = FastMath.max(row, firstRow); final int iEnd = FastMath.min(endRow + 1, firstRow + iHeight); for (int jBlock = blockStartColumn; jBlock < blockEndColumn; ++jBlock) { final int jWidth = blockWidth(jBlock); final int firstColumn = jBlock * BLOCK_SIZE; final int jStart = FastMath.max(column, firstColumn); final int jEnd = FastMath.min(endColumn + 1, firstColumn + jWidth); final int jLength = jEnd - jStart; // handle one block, row by row final T[] block = blocks[iBlock * blockColumns + jBlock]; for (int i = iStart; i < iEnd; ++i) { System.arraycopy(subMatrix[i - row], jStart - column, block, (i - firstRow) * jWidth + (jStart - firstColumn), jLength); } } } }
Example 16
Source File: QRDecomposition.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Returns the transpose of the matrix Q of the decomposition. * <p>Q is an orthogonal matrix</p> * @return the transpose of the Q matrix, Q<sup>T</sup> */ public RealMatrix getQT() { if (cachedQT == null) { // QT is supposed to be m x m final int n = qrt.length; final int m = qrt[0].length; double[][] qta = new double[m][m]; /* * Q = Q1 Q2 ... Q_m, so Q is formed by first constructing Q_m and then * applying the Householder transformations Q_(m-1),Q_(m-2),...,Q1 in * succession to the result */ for (int minor = m - 1; minor >= FastMath.min(m, n); minor--) { qta[minor][minor] = 1.0d; } for (int minor = FastMath.min(m, n)-1; minor >= 0; minor--){ final double[] qrtMinor = qrt[minor]; qta[minor][minor] = 1.0d; if (qrtMinor[minor] != 0.0) { for (int col = minor; col < m; col++) { double alpha = 0; for (int row = minor; row < m; row++) { alpha -= qta[col][row] * qrtMinor[row]; } alpha /= rDiag[minor] * qrtMinor[minor]; for (int row = minor; row < m; row++) { qta[col][row] += -alpha * qrtMinor[row]; } } } } cachedQT = MatrixUtils.createRealMatrix(qta); } // return the cached matrix return cachedQT; }
Example 17
Source File: Min.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x, double y) { return FastMath.min(x, y); }
Example 18
Source File: Percentile.java From astor with GNU General Public License v2.0 | 4 votes |
/** * Select the k<sup>th</sup> smallest element from work array * @param work work array (will be reorganized during the call) * @param pivotsHeap set of pivot index corresponding to elements that * are already at their sorted location, stored as an implicit heap * (i.e. a sorted binary tree stored in a flat array, where the * children of a node at index n are at indices 2n+1 for the left * child and 2n+2 for the right child, with 0-based indices) * @param k index of the desired element * @return k<sup>th</sup> smallest element */ private double select(final double[] work, final int[] pivotsHeap, final int k) { int begin = 0; int end = work.length; int node = 0; while (end - begin > MIN_SELECT_SIZE) { final int pivot; if ((node < pivotsHeap.length) && (pivotsHeap[node] >= 0)) { // the pivot has already been found in a previous call // and the array has already been partitioned around it pivot = pivotsHeap[node]; } else { // select a pivot and partition work array around it pivot = partition(work, begin, end, medianOf3(work, begin, end)); if (node < pivotsHeap.length) { pivotsHeap[node] = pivot; } } if (k == pivot) { // the pivot was exactly the element we wanted return work[k]; } else if (k < pivot) { // the element is in the left partition end = pivot; node = FastMath.min(2 * node + 1, pivotsHeap.length); // the min is here to avoid integer overflow } else { // the element is in the right partition begin = pivot + 1; node = FastMath.min(2 * node + 2, pivotsHeap.length); // the min is here to avoid integer overflow } } // the element is somewhere in the small sub-array // sort the sub-array using insertion sort insertionSort(work, begin, end); return work[k]; }
Example 19
Source File: Min.java From astor with GNU General Public License v2.0 | 4 votes |
/** {@inheritDoc} */ public double value(double x, double y) { return FastMath.min(x, y); }
Example 20
Source File: HypergeometricDistribution.java From astor with GNU General Public License v2.0 | 2 votes |
/** * Return the highest domain value for the given hypergeometric distribution * parameters. * * @param m Number of successes in the population. * @param k Sample size. * @return the highest domain value of the hypergeometric distribution. */ private int getUpperDomain(int m, int k) { return FastMath.min(k, m); }