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

The following examples show how to use org.apache.commons.math.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: PolynomialFunction.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the coefficients of the derivative of the polynomial with the given coefficients.
 *
 * @param coefficients Coefficients of the polynomial to differentiate.
 * @return the coefficients of the derivative or {@code null} if coefficients has length 1.
 * @throws NoDataException if {@code coefficients} is empty.
 * @throws NullArgumentException if {@code coefficients} is {@code null}.
 */
protected static double[] differentiate(double[] coefficients)
    throws NullArgumentException, NoDataException {
    MathUtils.checkNotNull(coefficients);
    int n = coefficients.length;
    if (n == 0) {
        throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
    }
    if (n == 1) {
        return new double[]{0};
    }
    double[] result = new double[n - 1];
    for (int i = n - 1; i > 0; i--) {
        result[i - 1] = i * coefficients[i];
    }
    return result;
}
 
Example 2
Source File: Percentile.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 Percentile to copy
 * @param dest Percentile to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(Percentile source, Percentile dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    if (source.cachedPivots != null) {
        System.arraycopy(source.cachedPivots, 0, dest.cachedPivots, 0, source.cachedPivots.length);
    }
    dest.quantile = source.quantile;
}
 
Example 3
Source File: Sum.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 Sum to copy
 * @param dest Sum to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(Sum source, Sum dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    dest.n = source.n;
    dest.value = source.value;
}
 
Example 4
Source File: Cardumen_00112_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Copies source to dest.
 * <p>Neither source nor dest can be null.</p>
 *
 * @param source Variance to copy
 * @param dest Variance to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(Variance source, Variance dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    dest.moment = source.moment.copy();
    dest.isBiasCorrected = source.isBiasCorrected;
    dest.incMoment = source.incMoment;
}
 
Example 5
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 6
Source File: SemiVariance.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 SemiVariance to copy
 * @param dest SemiVariance to copy to
 * @throws NullArgumentException if either source or dest is null
 */
public static void copy(final SemiVariance source, SemiVariance dest)
    throws NullArgumentException {
    MathUtils.checkNotNull(source);
    MathUtils.checkNotNull(dest);
    dest.setData(source.getDataRef());
    dest.biasCorrected = source.biasCorrected;
    dest.varianceDirection = source.varianceDirection;
}
 
Example 7
Source File: BigFraction_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Create a {@link BigFraction} given the numerator and denominator as
 * {@code BigInteger}. The {@link BigFraction} is reduced to lowest terms.
 *
 * @param num the numerator, must not be {@code null}.
 * @param den the denominator, must not be {@code null}.
 * @throws ZeroException if the denominator is zero.
 * @throws NullArgumentException if either of the arguments is null
 */
public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
        throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
        numerator   = BigInteger.ZERO;
        denominator = BigInteger.ONE;
    } else {

        // reduce numerator and denominator by greatest common denominator
        final BigInteger gcd = num.gcd(den);
        if (BigInteger.ONE.compareTo(gcd) < 0) {
            num = num.divide(gcd);
            den = den.divide(gcd);
        }

        // move sign to numerator
        if (BigInteger.ZERO.compareTo(den) > 0) {
            num = num.negate();
            den = den.negate();
        }

        // store the values in the final fields
        numerator   = num;
        denominator = den;

    }
}
 
Example 8
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 9
Source File: Math_37_Complex_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Returns a {@code Complex} whose value is {@code this * factor}.
 * Implements preliminary checks for {@code NaN} and infinity followed by
 * the definitional formula:
 * <pre>
 *  <code>
 *   (a + bi)(c + di) = (ac - bd) + (ad + bc)i
 *  </code>
 * </pre>
 * Returns {@link #NaN} if either {@code this} or {@code factor} has one or
 * more {@code NaN} parts.
 * <br/>
 * Returns {@link #INF} if neither {@code this} nor {@code factor} has one
 * or more {@code NaN} parts and if either {@code this} or {@code factor}
 * has one or more infinite parts (same result is returned regardless of
 * the sign of the components).
 * <br/>
 * Returns finite values in components of the result per the definitional
 * formula in all remaining cases.
 *
 * @param  factor value to be multiplied by this {@code Complex}.
 * @return {@code this * factor}.
 * @throws NullArgumentException if {@code factor} is {@code null}.
 */
public Complex multiply(Complex factor)
    throws NullArgumentException {
    MathUtils.checkNotNull(factor);
    if (isNaN || factor.isNaN) {
        return NaN;
    }
    if (Double.isInfinite(real) ||
        Double.isInfinite(imaginary) ||
        Double.isInfinite(factor.real) ||
        Double.isInfinite(factor.imaginary)) {
        // we don't use isInfinite() to avoid testing for NaN again
        return INF;
    }
    return createComplex(real * factor.real - imaginary * factor.imaginary,
                         real * factor.imaginary + imaginary * factor.real);
}
 
Example 10
Source File: Math_47_Complex_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Returns a {@code Complex} whose value is
 * {@code (this + addend)}.
 * Uses the definitional formula
 * <pre>
 *  <code>
 *   (a + bi) + (c + di) = (a+c) + (b+d)i
 *  </code>
 * </pre>
 * <br/>
 * If either {@code this} or {@code addend} has a {@code NaN} value in
 * either part, {@link #NaN} is returned; otherwise {@code Infinite}
 * and {@code NaN} values are returned in the parts of the result
 * according to the rules for {@link java.lang.Double} arithmetic.
 *
 * @param  addend Value to be added to this {@code Complex}.
 * @return {@code this + addend}.
 * @throws NullArgumentException if {@code addend} is {@code null}.
 */
public Complex add(Complex addend) throws NullArgumentException {
    MathUtils.checkNotNull(addend);
    if (isNaN || addend.isNaN) {
        return NaN;
    }

    return createComplex(real + addend.getReal(),
                         imaginary + addend.getImaginary());
}
 
Example 11
Source File: Arja_0035_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Return the product of this complex number and the given complex number.
 * <p>
 * Implements preliminary checks for NaN and infinity followed by
 * the definitional formula:
 * <pre><code>
 * (a + bi)(c + di) = (ac - bd) + (ad + bc)i
 * </code></pre>
 * </p>
 * <p>
 * Returns {@link #NaN} if either this or <code>rhs</code> has one or more
 * NaN parts.
 * </p>
 * Returns {@link #INF} if neither this nor <code>rhs</code> has one or more
 * NaN parts and if either this or <code>rhs</code> has one or more
 * infinite parts (same result is returned regardless of the sign of the
 * components).
 * </p>
 * <p>
 * Returns finite values in components of the result per the
 * definitional formula in all remaining cases.
 *  </p>
 *
 * @param rhs the other complex number
 * @return the complex number product
 * @throws NullArgumentException if <code>rhs</code> is null
 */
public Complex multiply(Complex rhs)
    throws NullArgumentException {
    MathUtils.checkNotNull(rhs);
    if (isNaN || rhs.isNaN) {
        return NaN;
    }
    if (Double.isInfinite(real) || Double.isInfinite(imaginary) ||
        Double.isInfinite(rhs.real)|| Double.isInfinite(rhs.imaginary)) {
        // we don't use Complex.isInfinite() to avoid testing for NaN again
        return INF;
    }
    return createComplex(real * rhs.real - imaginary * rhs.imaginary,
            real * rhs.imaginary + imaginary * rhs.real);
}
 
Example 12
Source File: Math_46_Complex_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Returns a {@code Complex} whose value is
 * {@code (this - subtrahend)}.
 * Uses the definitional formula
 * <pre>
 *  <code>
 *   (a + bi) - (c + di) = (a-c) + (b-d)i
 *  </code>
 * </pre>
 * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part,
 * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are
 * returned in the parts of the result according to the rules for
 * {@link java.lang.Double} arithmetic.
 *
 * @param  subtrahend value to be subtracted from this {@code Complex}.
 * @return {@code this - subtrahend}.
 * @throws NullArgumentException if {@code subtrahend} is {@code null}.
 */
public Complex subtract(Complex subtrahend)
    throws NullArgumentException {
    MathUtils.checkNotNull(subtrahend);
    if (isNaN || subtrahend.isNaN) {
        return NaN;
    }

    return createComplex(real - subtrahend.getReal(),
                         imaginary - subtrahend.getImaginary());
}
 
Example 13
Source File: JGenProg2017_0065_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Return the sum of this complex number and the given complex number.
 * <p>
 * Uses the definitional formula
 * <pre>
 * (a + bi) + (c + di) = (a+c) + (b+d)i
 * </pre></p>
 * <p>
 * If either this or <code>rhs</code> has a NaN value in either part,
 * {@link #NaN} is returned; otherwise Infinite and NaN values are
 * returned in the parts of the result according to the rules for
 * {@link java.lang.Double} arithmetic.</p>
 *
 * @param rhs the other complex number
 * @return the complex number sum
 * @throws NullArgumentException if <code>rhs</code> is null
 */
public Complex add(Complex rhs)
    throws NullArgumentException {
    MathUtils.checkNotNull(rhs);
    return createComplex(real + rhs.getReal(),
        imaginary + rhs.getImaginary());
}
 
Example 14
Source File: Arja_0035_t.java    From coming with MIT License 3 votes vote down vote up
/**
 * Return the difference between this complex number and the given complex
 * number.
  * <p>
 * Uses the definitional formula
 * <pre>
 * (a + bi) - (c + di) = (a-c) + (b-d)i
 * </pre></p>
 * <p>
 * If either this or <code>rhs</code> has a NaN value in either part,
 * {@link #NaN} is returned; otherwise infinite and NaN values are
 * returned in the parts of the result according to the rules for
 * {@link java.lang.Double} arithmetic. </p>
 *
 * @param rhs the other complex number
 * @return the complex number difference
 * @throws NullArgumentException if <code>rhs</code> is null
 */
public Complex subtract(Complex rhs)
    throws NullArgumentException {
    MathUtils.checkNotNull(rhs);
    if (isNaN || rhs.isNaN) {
        return NaN;
    }

    return createComplex(real - rhs.getReal(),
        imaginary - rhs.getImaginary());
}
 
Example 15
Source File: Complex.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a {@code Complex} whose value is
 * {@code (this - subtrahend)}.
 * Uses the definitional formula
 * <pre>
 *  <code>
 *   (a + bi) - (c + di) = (a-c) + (b-d)i
 *  </code>
 * </pre>
 * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part,
 * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are
 * returned in the parts of the result according to the rules for
 * {@link java.lang.Double} arithmetic.
 *
 * @param  subtrahend value to be subtracted from this {@code Complex}.
 * @return {@code this - subtrahend}.
 * @throws NullArgumentException if {@code subtrahend} is {@code null}.
 */
public Complex subtract(Complex subtrahend)
    throws NullArgumentException {
    MathUtils.checkNotNull(subtrahend);
    if (isNaN || subtrahend.isNaN) {
        return NaN;
    }

    return createComplex(real - subtrahend.getReal(),
                         imaginary - subtrahend.getImaginary());
}
 
Example 16
Source File: Cardumen_00218_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Returns a {@code Complex} whose value is
 * {@code (this - subtrahend)}.
 * Uses the definitional formula
 * <pre>
 *  <code>
 *   (a + bi) - (c + di) = (a-c) + (b-d)i
 *  </code>
 * </pre>
 * If either {@code this} or {@code subtrahend} has a {@code NaN]} value in either part,
 * {@link #NaN} is returned; otherwise infinite and {@code NaN} values are
 * returned in the parts of the result according to the rules for
 * {@link java.lang.Double} arithmetic.
 *
 * @param  subtrahend value to be subtracted from this {@code Complex}.
 * @return {@code this - subtrahend}.
 * @throws NullArgumentException if {@code subtrahend} is {@code null}.
 */
public Complex subtract(Complex subtrahend)
    throws NullArgumentException {
    MathUtils.checkNotNull(subtrahend);
    if (isNaN || subtrahend.isNaN) {
        return NaN;
    }

    return createComplex(real - subtrahend.getReal(),
                         imaginary - subtrahend.getImaginary());
}
 
Example 17
Source File: Math_47_Complex_s.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 18
Source File: Math_46_Complex_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 19
Source File: Math_36_BigFraction_s.java    From coming with MIT License 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 20
Source File: Arja_0035_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</code>.
 * <p>
 * Implements the formula: <pre>
 * <code> y<sup>x</sup> = exp(x&middot;log(y))</code></pre>
 * where <code>exp</code> and <code>log</code> are {@link #exp} and
 * {@link #log}, respectively.</p>
 * <p>
 * Returns {@link Complex#NaN} if either real or imaginary part of the
 * input argument is <code>NaN</code> or infinite, or if <code>y</code>
 * equals {@link Complex#ZERO}.</p>
 *
 * @param x the exponent.
 * @return <code>this</code><sup><code>x</code></sup>
 * @throws NullArgumentException if x is null
 * @since 1.2
 */
public Complex pow(Complex x)
    throws NullArgumentException {
    MathUtils.checkNotNull(x);
    return this.log().multiply(x).exp();
}