Java Code Examples for org.apache.commons.math3.util.MathUtils#checkNotNull()

The following examples show how to use org.apache.commons.math3.util.MathUtils#checkNotNull() . 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: BaseAbstractUnivariateIntegrator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prepare for computation.
 * Subclasses must call this method if they override any of the
 * {@code solve} methods.
 *
 * @param maxEval Maximum number of evaluations.
 * @param f the integrand function
 * @param lower the min bound for the interval
 * @param upper the upper bound for the interval
 * @throws NullArgumentException if {@code f} is {@code null}.
 * @throws MathIllegalArgumentException if {@code min >= max}.
 */
protected void setup(final int maxEval,
                     final UnivariateFunction f,
                     final double lower, final double upper)
    throws NullArgumentException, MathIllegalArgumentException {

    // Checks.
    MathUtils.checkNotNull(f);
    UnivariateSolverUtils.verifyInterval(lower, upper);

    // Reset.
    min = lower;
    max = upper;
    function = f;
    evaluations.setMaximalCount(maxEval);
    evaluations.resetCount();
    iterations.resetCount();

}
 
Example 2
Source File: SymmLQ.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param x not meaningful in this implementation; should not be considered
 * as an initial guess (<a href="#initguess">more</a>)
 * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is
 * {@code true}, and {@code a} is not self-adjoint
 * @throws IllConditionedOperatorException if {@code a} is ill-conditioned
 */
@Override
public RealVector solve(final RealLinearOperator a, final RealVector b,
    final RealVector x) throws NullArgumentException,
    NonSquareOperatorException, DimensionMismatchException,
    NonSelfAdjointOperatorException, IllConditionedOperatorException,
    MaxCountExceededException {
    MathUtils.checkNotNull(x);
    return solveInPlace(a, null, b, x.copy(), false, 0.);
}
 
Example 3
Source File: SynchronizedDescriptiveStatistics.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copies source to dest.
 * <p>Neither source nor dest can be null.</p>
 * <p>Acquires synchronization lock on source, then dest before copying.</p>
 *
 * @param source SynchronizedDescriptiveStatistics to copy
 * @param dest SynchronizedDescriptiveStatistics to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(SynchronizedDescriptiveStatistics source,
                        SynchronizedDescriptiveStatistics dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    synchronized (source) {
        synchronized (dest) {
            DescriptiveStatistics.copy(source, dest);
        }
    }
}
 
Example 4
Source File: Kurtosis.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copies source to dest.
 * <p>Neither source nor dest can be null.</p>
 *
 * @param source Kurtosis to copy
 * @param dest Kurtosis to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(Kurtosis source, Kurtosis dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    dest.moment = source.moment.copy();
    dest.incMoment = source.incMoment;
}
 
Example 5
Source File: ChiSquareTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Throws DimensionMismatchException if the input array is not rectangular.
 *
 * @param in array to be tested
 * @throws NullArgumentException if input array is null
 * @throws DimensionMismatchException if input array is not rectangular
 */
private void checkRectangular(final long[][] in)
    throws NullArgumentException, DimensionMismatchException {

    MathUtils.checkNotNull(in);
    for (int i = 1; i < in.length; i++) {
        if (in[i].length != in[0].length) {
            throw new DimensionMismatchException(
                    LocalizedFormats.DIFFERENT_ROWS_LENGTHS,
                    in[i].length, in[0].length);
        }
    }

}
 
Example 6
Source File: Percentile.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a Percentile with the specific quantile value,
 * {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
 *
 * @param quantile the quantile to be computed
 * @param estimationType one of the percentile {@link EstimationType  estimation types}
 * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
 * @param kthSelector a {@link KthSelector} to use for pivoting during search
 * @throws MathIllegalArgumentException if p is not within (0,100]
 * @throws NullArgumentException if type or NaNStrategy passed is null
 */
protected Percentile(final double quantile,
                     final EstimationType estimationType,
                     final NaNStrategy nanStrategy,
                     final KthSelector kthSelector)
    throws MathIllegalArgumentException {
    setQuantile(quantile);
    cachedPivots = null;
    MathUtils.checkNotNull(estimationType);
    MathUtils.checkNotNull(nanStrategy);
    MathUtils.checkNotNull(kthSelector);
    this.estimationType = estimationType;
    this.nanStrategy = nanStrategy;
    this.kthSelector = kthSelector;
}
 
Example 7
Source File: SparseFieldVector.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc}
 * @exception NullArgumentException if d is null
 */
public FieldVector<T> append(T d) throws NullArgumentException {
    MathUtils.checkNotNull(d);
    FieldVector<T> res = new SparseFieldVector<T>(this, 1);
    res.setEntry(virtualSize, d);
    return res;
 }
 
Example 8
Source File: SymmLQ.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is
 * {@code true}, and {@code a} or {@code m} is not self-adjoint
 * @throws NonPositiveDefiniteOperatorException if {@code m} is not
 * positive definite
 * @throws IllConditionedOperatorException if {@code a} is ill-conditioned
 */
@Override
public RealVector solve(final RealLinearOperator a,
    final RealLinearOperator m, final RealVector b) throws
    NullArgumentException, NonSquareOperatorException,
    DimensionMismatchException, MaxCountExceededException,
    NonSelfAdjointOperatorException, NonPositiveDefiniteOperatorException,
    IllConditionedOperatorException {
    MathUtils.checkNotNull(a);
    final RealVector x = new ArrayRealVector(a.getColumnDimension());
    return solveInPlace(a, m, b, x, false, 0.);
}
 
Example 9
Source File: SumOfLogs.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copies source to dest.
 * <p>Neither source nor dest can be null.</p>
 *
 * @param source SumOfLogs to copy
 * @param dest SumOfLogs to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(SumOfLogs source, SumOfLogs dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    dest.n = source.n;
    dest.value = source.value;
}
 
Example 10
Source File: BlockFieldMatrix.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void setSubMatrix(final T[][] subMatrix, final int row, final int column) {
    // 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 11
Source File: ArrayFieldVector.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Construct a vector by appending one vector to another vector.
 * This constructor needs at least one non-empty array to retrieve
 * the field from its first element. This implies it cannot build
 * 0 length vectors. To build vectors from any size, one should
 * use the {@link #ArrayFieldVector(Field, FieldElement[], FieldElement[])}
 * constructor.
 *
 * @param v1 First vector (will be put in front of the new vector).
 * @param v2 Second vector (will be put at back of the new vector).
 * @throws NullArgumentException if {@code v1} or {@code v2} is
 * {@code null}.
 * @throws ZeroException if both arrays are empty.
 * @see #ArrayFieldVector(Field, FieldElement[], FieldElement[])
 */
public ArrayFieldVector(T[] v1, T[] v2)
        throws NullArgumentException, ZeroException {
    MathUtils.checkNotNull(v1);
    MathUtils.checkNotNull(v2);
    if (v1.length + v2.length == 0) {
        throw new ZeroException(LocalizedFormats.VECTOR_MUST_HAVE_AT_LEAST_ONE_ELEMENT);
    }
    data = MathArrays.buildArray(v1[0].getField(), v1.length + v2.length);
    System.arraycopy(v1, 0, data, 0, v1.length);
    System.arraycopy(v2, 0, data, v1.length, v2.length);
    field = data[0].getField();
}
 
Example 12
Source File: MultivariateFunctionPenaltyAdapter.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Simple constructor.
 * <p>
 * When the optimizer provided points are out of range, the value of the
 * penalty function will be used instead of the value of the underlying
 * function. In order for this penalty to be effective in rejecting this
 * point during the optimization process, the penalty function value should
 * be defined with care. This value is computed as:
 * <pre>
 *   penalty(point) = offset + &sum;<sub>i</sub>[scale[i] * &radic;|point[i]-boundary[i]|]
 * </pre>
 * where indices i correspond to all the components that violates their boundaries.
 * </p>
 * <p>
 * So when attempting a function minimization, offset should be larger than
 * the maximum expected value of the underlying function and scale components
 * should all be positive. When attempting a function maximization, offset
 * should be lesser than the minimum expected value of the underlying function
 * and scale components should all be negative.
 * minimization, and lesser than the minimum expected value of the underlying
 * function when attempting maximization.
 * </p>
 * <p>
 * These choices for the penalty function have two properties. First, all out
 * of range points will return a function value that is worse than the value
 * returned by any in range point. Second, the penalty is worse for large
 * boundaries violation than for small violations, so the optimizer has an hint
 * about the direction in which it should search for acceptable points.
 * </p>
 * @param bounded bounded function
 * @param lower lower bounds for each element of the input parameters array
 * (some elements may be set to {@code Double.NEGATIVE_INFINITY} for
 * unbounded values)
 * @param upper upper bounds for each element of the input parameters array
 * (some elements may be set to {@code Double.POSITIVE_INFINITY} for
 * unbounded values)
 * @param offset base offset of the penalty function
 * @param scale scale of the penalty function
 * @exception DimensionMismatchException if lower bounds, upper bounds and
 * scales are not consistent, either according to dimension or to bounadary
 * values
 */
public MultivariateFunctionPenaltyAdapter(final MultivariateFunction bounded,
                                          final double[] lower, final double[] upper,
                                          final double offset, final double[] scale) {

    // safety checks
    MathUtils.checkNotNull(lower);
    MathUtils.checkNotNull(upper);
    MathUtils.checkNotNull(scale);
    if (lower.length != upper.length) {
        throw new DimensionMismatchException(lower.length, upper.length);
    }
    if (lower.length != scale.length) {
        throw new DimensionMismatchException(lower.length, scale.length);
    }
    for (int i = 0; i < lower.length; ++i) {
        // note the following test is written in such a way it also fails for NaN
        if (!(upper[i] >= lower[i])) {
            throw new NumberIsTooSmallException(upper[i], lower[i], true);
        }
    }

    this.bounded = bounded;
    this.lower   = lower.clone();
    this.upper   = upper.clone();
    this.offset  = offset;
    this.scale   = scale.clone();
}
 
Example 13
Source File: PreconditionedIterativeLinearSolver.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns an estimate of the solution to the linear system A &middot; x =
 * b.
 *
 * @param a the linear operator A of the system
 * @param m the preconditioner, M (can be {@code null})
 * @param b the right-hand side vector
 * @return a new vector containing the solution
 * @throws NullArgumentException if one of the parameters is {@code null}
 * @throws NonSquareOperatorException if {@code a} or {@code m} is not
 * square
 * @throws DimensionMismatchException if {@code m} or {@code b} have
 * dimensions inconsistent with {@code a}
 * @throws MaxCountExceededException at exhaustion of the iteration count,
 * unless a custom
 * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback}
 * has been set at construction of the {@link IterationManager}
 */
public RealVector solve(RealLinearOperator a, RealLinearOperator m,
    RealVector b) throws NullArgumentException, NonSquareOperatorException,
    DimensionMismatchException, MaxCountExceededException {
    MathUtils.checkNotNull(a);
    final RealVector x = new ArrayRealVector(a.getColumnDimension());
    return solveInPlace(a, m, b, x);
}
 
Example 14
Source File: PreconditionedIterativeLinearSolver.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns an estimate of the solution to the linear system A &middot; x =
 * b.
 *
 * @param a the linear operator A of the system
 * @param m the preconditioner, M (can be {@code null})
 * @param b the right-hand side vector
 * @param x0 the initial guess of the solution
 * @return a new vector containing the solution
 * @throws NullArgumentException if one of the parameters is {@code null}
 * @throws NonSquareOperatorException if {@code a} or {@code m} is not
 * square
 * @throws DimensionMismatchException if {@code m}, {@code b} or
 * {@code x0} have dimensions inconsistent with {@code a}
 * @throws MaxCountExceededException at exhaustion of the iteration count,
 * unless a custom
 * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback}
 * has been set at construction of the {@link IterationManager}
 */
public RealVector solve(final RealLinearOperator a,
    final RealLinearOperator m, final RealVector b, final RealVector x0)
    throws NullArgumentException, NonSquareOperatorException,
    DimensionMismatchException, MaxCountExceededException {
    MathUtils.checkNotNull(x0);
    return solveInPlace(a, m, b, x0.copy());
}
 
Example 15
Source File: SecondMoment.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Copies source to dest.
 * <p>Neither source nor dest can be null.</p>
 *
 * @param source SecondMoment to copy
 * @param dest SecondMoment to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(SecondMoment source, SecondMoment dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    FirstMoment.copy(source, dest);
    dest.m2 = source.m2;
}
 
Example 16
Source File: GeometricMean.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Copies source to dest.
 * <p>Neither source nor dest can be null.</p>
 *
 * @param source GeometricMean to copy
 * @param dest GeometricMean to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(GeometricMean source, GeometricMean dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    dest.sumOfLogs = source.sumOfLogs.copy();
}
 
Example 17
Source File: SymmLQ.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the solution to the system (A - shift &middot; I) &middot; x = b.
 * <p>
 * If the solution x is expected to contain a large multiple of {@code b}
 * (as in Rayleigh-quotient iteration), then better precision may be
 * achieved with {@code goodb} set to {@code true}.
 * </p>
 * <p>
 * {@code shift} should be zero if the system A &middot; x = b is to be
 * solved. Otherwise, it could be an approximation to an eigenvalue of A,
 * such as the Rayleigh quotient b<sup>T</sup> &middot; A &middot; b /
 * (b<sup>T</sup> &middot; b) corresponding to the vector b. If b is
 * sufficiently like an eigenvector corresponding to an eigenvalue near
 * shift, then the computed x may have very large components. When
 * normalized, x may be closer to an eigenvector than b.
 * </p>
 *
 * @param a the linear operator A of the system
 * @param b the right-hand side vector
 * @param goodb usually {@code false}, except if {@code x} is expected to
 * contain a large multiple of {@code b}
 * @param shift the amount to be subtracted to all diagonal elements of A
 * @return a reference to {@code x}
 * @throws NullArgumentException if one of the parameters is {@code null}
 * @throws NonSquareOperatorException if {@code a} is not square
 * @throws DimensionMismatchException if {@code b} has dimensions
 * inconsistent with {@code a}
 * @throws MaxCountExceededException at exhaustion of the iteration count,
 * unless a custom
 * {@link org.apache.commons.math3.util.Incrementor.MaxCountExceededCallback callback}
 * has been set at construction of the {@link IterationManager}
 * @throws NonSelfAdjointOperatorException if {@link #getCheck()} is
 * {@code true}, and {@code a} is not self-adjoint
 * @throws IllConditionedOperatorException if {@code a} is ill-conditioned
 */
public RealVector solve(final RealLinearOperator a, final RealVector b,
    final boolean goodb, final double shift) throws NullArgumentException,
    NonSquareOperatorException, DimensionMismatchException,
    NonSelfAdjointOperatorException, IllConditionedOperatorException,
    MaxCountExceededException {
    MathUtils.checkNotNull(a);
    final RealVector x = new ArrayRealVector(a.getColumnDimension());
    return solveInPlace(a, null, b, x, goodb, shift);
}
 
Example 18
Source File: BigFraction.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * <p>
 * Adds the value of this fraction to the passed {@link BigInteger},
 * returning the result in reduced form.
 * </p>
 *
 * @param bg
 *            the {@link BigInteger} to add, must'nt be <code>null</code>.
 * @return a <code>BigFraction</code> instance with the resulting values.
 * @throws NullArgumentException
 *             if the {@link BigInteger} is <code>null</code>.
 */
public BigFraction add(final BigInteger bg) throws NullArgumentException {
    MathUtils.checkNotNull(bg);
    return new BigFraction(numerator.add(denominator.multiply(bg)), denominator);
}
 
Example 19
Source File: Cardumen_00220_t.java    From coming with MIT License 2 votes vote down vote up
/**
 * Returns of value of this complex number raised to the power of {@code x}.
 * Implements the formula:
 * <pre>
 *  <code>
 *   y<sup>x</sup> = exp(x&middot;log(y))
 *  </code>
 * </pre>
 * where {@code exp} and {@code log} are {@link #exp} and
 * {@link #log}, respectively.
 * <br/>
 * Returns {@link Complex#NaN} if either real or imaginary part of the
 * input argument is {@code NaN} or infinite, or if {@code y}
 * equals {@link Complex#ZERO}.
 *
 * @param  x exponent to which this {@code Complex} is to be raised.
 * @return <code> this<sup>{@code x}</sup></code>.
 * @throws NullArgumentException if x is {@code null}.
 * @since 1.2
 */
public Complex pow(Complex x)
    throws NullArgumentException {
    MathUtils.checkNotNull(x);
    return this.log().multiply(x).exp();
}
 
Example 20
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Construct an ArrayDataAdapter from a double[] array
 *
 * @param in double[] array holding the data
 * @throws NullArgumentException if in is null
 */
public ArrayDataAdapter(double[] in) throws NullArgumentException {
    super();
    MathUtils.checkNotNull(in);
    inputArray = in;
}