/** * 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); }
/** {@inheritDoc} */ public double value(double x, double y) { return FastMath.min(x, y); } }
/** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @param from Initial index of the range to be copied, inclusive. * @param to Final index of the range to be copied, exclusive. (This index may lie outside the array.) * @return the copied array. */ public static double[] copyOfRange(double[] source, int from, int to) { final int len = to - from; final double[] output = new double[len]; System.arraycopy(source, from, output, 0, FastMath.min(len, source.length - from)); return output; }
/** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @param len Number of entries to copy. If smaller then the source * length, the copy will be truncated, if larger it will padded with * zeroes. * @return the copied array. */ public static double[] copyOf(double[] source, int len) { final double[] output = new double[len]; System.arraycopy(source, 0, output, 0, FastMath.min(len, source.length)); return output; }
/** * Creates a copy of the {@code source} array. * * @param source Array to be copied. * @param len Number of entries to copy. If smaller then the source * length, the copy will be truncated, if larger it will padded with * zeroes. * @return the copied array. */ public static int[] copyOf(int[] source, int len) { final int[] output = new int[len]; System.arraycopy(source, 0, output, 0, FastMath.min(len, source.length)); return output; }
/** Decompose matrix. * @param matrix transposed matrix * @since 3.2 */ protected void decompose(double[][] matrix) { for (int minor = 0; minor < FastMath.min(matrix.length, matrix[0].length); minor++) { performHouseholderReflection(minor, matrix); } }
/** Convert an index from one (parameters, order) structure to another. * @param index index of a partial derivative in source derivative structure * @param srcP number of free parameters in source derivative structure * @param srcDerivativesIndirection derivatives indirection array for the source * derivative structure * @param destP number of free parameters in destination derivative structure * @param destO derivation order in destination derivative structure * @param destSizes sizes array for the destination derivative structure * @return index of the partial derivative with the <em>same</em> characteristics * in destination derivative structure * @throws NumberIsTooLargeException if order is too large */ private static int convertIndex(final int index, final int srcP, final int[][] srcDerivativesIndirection, final int destP, final int destO, final int[][] destSizes) throws NumberIsTooLargeException { int[] orders = new int[destP]; System.arraycopy(srcDerivativesIndirection[index], 0, orders, 0, FastMath.min(srcP, destP)); return getPartialDerivativeIndex(destP, destO, destSizes, orders); }
/** * Returns the Householder reflector vectors. * <p>H is a lower trapezoidal matrix whose columns represent * each successive Householder reflector vector. This matrix is used * to compute Q.</p> * @return a matrix containing the Householder reflector vectors */ public RealMatrix getH() { if (cachedH == null) { final int n = qrt.length; final int m = qrt[0].length; double[][] ha = new double[m][n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < FastMath.min(i + 1, n); ++j) { ha[i][j] = qrt[j][i] / -rDiag[j]; } } cachedH = MatrixUtils.createRealMatrix(ha); } // return the cached matrix return cachedH; }
/** {@inheritDoc} */ @Override public void setSeed(int[] seed) { if (seed == null) { setSeed(System.currentTimeMillis() + System.identityHashCode(this)); return; } final int seedLen = seed.length; final int rslLen = rsl.length; System.arraycopy(seed, 0, rsl, 0, FastMath.min(seedLen, rslLen)); if (seedLen < rslLen) { for (int j = seedLen; j < rslLen; j++) { long k = rsl[j - seedLen]; rsl[j] = (int) (0x6c078965L * (k ^ k >> 30) + j & 0xffffffffL); } } initState(); }
/** * Returns the index of the bin to which the given value belongs * * @param value the value whose bin we are trying to find * @return the index of the bin containing the value */ private int findBin(double value) { return FastMath.min( FastMath.max((int) FastMath.ceil((value - min) / delta) - 1, 0), binCount - 1); }
/** Compute step grow/shrink factor according to normalized error. * @param error normalized error of the current step * @return grow/shrink factor for next step */ protected double computeStepGrowShrinkFactor(final double error) { return FastMath.min(maxGrowth, FastMath.max(minReduction, safety * FastMath.pow(error, exp))); }
/** * {@inheritDoc} * * For number of successes {@code m} and sample size {@code n}, the upper * bound of the support is {@code min(m, n)}. * * @return upper bound of the support */ public int getSupportUpperBound() { return FastMath.min(getNumberOfSuccesses(), getSampleSize()); }
/** * Multiply the instance by a polynomial. * * @param p Polynomial to multiply by. * @return a new polynomial equal to this times {@code p} */ public PolynomialFunction multiply(final PolynomialFunction p) { double[] newCoefficients = new double[coefficients.length + p.coefficients.length - 1]; for (int i = 0; i < newCoefficients.length; ++i) { newCoefficients[i] = 0.0; for (int j = FastMath.max(0, i + 1 - p.coefficients.length); j < FastMath.min(coefficients.length, i + 1); ++j) { newCoefficients[i] += coefficients[j] * p.coefficients[i-j]; } } return new PolynomialFunction(newCoefficients); }
/** {@inheritDoc} */ @Override public T walkInRowOrder(final FieldMatrixChangingVisitor<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) { block[k] = visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); }
/** {@inheritDoc} */ @Override public double walkInRowOrder(final RealMatrixPreservingVisitor 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) { visitor.visit(p, q, block[k]); ++k; } } } } return visitor.end(); }
/** * Returns one sample using Cheng's sampling algorithm. * @param random random generator to use * @param alpha distribution first shape parameter * @param beta distribution second shape parameter * @return sampled value */ static double sample(RandomGenerator random, final double alpha, final double beta) { final double a = FastMath.min(alpha, beta); final double b = FastMath.max(alpha, beta); if (a > 1) { return algorithmBB(random, alpha, a, b); } else { return algorithmBC(random, alpha, b, a); } }
/** * Calculates the QR-decomposition of the given matrix. * * @param matrix The matrix to decompose. * @param threshold Singularity threshold. */ public QRDecomposition(RealMatrix matrix, double threshold) { this.threshold = threshold; final int m = matrix.getRowDimension(); final int n = matrix.getColumnDimension(); qrt = matrix.transpose().getData(); rDiag = new double[FastMath.min(m, n)]; cachedQ = null; cachedQT = null; cachedR = null; cachedH = null; decompose(qrt); }
/** * Build the transformation to bi-diagonal shape of a matrix. * @param matrix the matrix to transform. */ BiDiagonalTransformer(RealMatrix matrix) { final int m = matrix.getRowDimension(); final int n = matrix.getColumnDimension(); final int p = FastMath.min(m, n); householderVectors = matrix.getData(); main = new double[p]; secondary = new double[p - 1]; cachedU = null; cachedB = null; cachedV = null; // transform matrix if (m >= n) { transformToUpperBiDiagonal(); } else { transformToLowerBiDiagonal(); } }
/** {@inheritDoc} */ public DerivativeStructure value(final DerivativeStructure t) throws MathIllegalArgumentException { // check we can achieve the requested derivation order with the sample if (t.getOrder() >= nbPoints) { throw new NumberIsTooLargeException(t.getOrder(), nbPoints, false); } // compute sample position, trying to be centered if possible final double t0 = FastMath.max(FastMath.min(t.getValue(), tMax), tMin) - halfSampleSpan; // compute sample points final double[] y = new double[nbPoints]; for (int i = 0; i < nbPoints; ++i) { y[i] = function.value(t0 + i * stepSize); } // evaluate derivatives return evaluate(t, t0, y); }