Java Code Examples for org.apache.commons.math3.util.FastMath#rint()

The following examples show how to use org.apache.commons.math3.util.FastMath#rint() . 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: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Perform remainder of two derivative structures.
 * @param lhs array holding left hand side of remainder
 * @param lhsOffset offset of the left hand side in its array
 * @param rhs array right hand side of remainder
 * @param rhsOffset offset of the right hand side in its array
 * @param result array where result must be stored (it may be
 * one of the input arrays)
 * @param resultOffset offset of the result in its array
 */
public void remainder(final double[] lhs, final int lhsOffset,
                      final double[] rhs, final int rhsOffset,
                      final double[] result, final int resultOffset) {

    // compute k such that lhs % rhs = lhs - k rhs
    final double rem = FastMath.IEEEremainder(lhs[lhsOffset], rhs[rhsOffset]);
    final double k   = FastMath.rint((lhs[lhsOffset] - rem) / rhs[rhsOffset]);

    // set up value
    result[resultOffset] = rem;

    // set up partial derivatives
    for (int i = 1; i < getSize(); ++i) {
        result[resultOffset + i] = lhs[lhsOffset + i] - k * rhs[rhsOffset + i];
    }

}
 
Example 2
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Perform remainder of two derivative structures.
 * @param lhs array holding left hand side of remainder
 * @param lhsOffset offset of the left hand side in its array
 * @param rhs array right hand side of remainder
 * @param rhsOffset offset of the right hand side in its array
 * @param result array where result must be stored (it may be
 * one of the input arrays)
 * @param resultOffset offset of the result in its array
 */
public void remainder(final double[] lhs, final int lhsOffset,
                      final double[] rhs, final int rhsOffset,
                      final double[] result, final int resultOffset) {

    // compute k such that lhs % rhs = lhs - k rhs
    final double rem = FastMath.IEEEremainder(lhs[lhsOffset], rhs[rhsOffset]);
    final double k   = FastMath.rint((lhs[lhsOffset] - rem) / rhs[rhsOffset]);

    // set up value
    result[resultOffset] = rem;

    // set up partial derivatives
    for (int i = 1; i < getSize(); ++i) {
        result[resultOffset + i] = lhs[lhsOffset + i] - k * rhs[rhsOffset + i];
    }

}
 
Example 3
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Perform remainder of two derivative structures.
 * @param lhs array holding left hand side of remainder
 * @param lhsOffset offset of the left hand side in its array
 * @param rhs array right hand side of remainder
 * @param rhsOffset offset of the right hand side in its array
 * @param result array where result must be stored (it may be
 * one of the input arrays)
 * @param resultOffset offset of the result in its array
 */
public void remainder(final double[] lhs, final int lhsOffset,
                      final double[] rhs, final int rhsOffset,
                      final double[] result, final int resultOffset) {

    // compute k such that lhs % rhs = lhs - k rhs
    final double rem = FastMath.IEEEremainder(lhs[lhsOffset], rhs[rhsOffset]);
    final double k   = FastMath.rint((lhs[lhsOffset] - rem) / rhs[rhsOffset]);

    // set up value
    result[resultOffset] = rem;

    // set up partial derivatives
    for (int i = 1; i < getSize(); ++i) {
        result[resultOffset + i] = lhs[lhsOffset + i] - k * rhs[rhsOffset + i];
    }

}
 
Example 4
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Perform remainder of two derivative structures.
 * @param lhs array holding left hand side of remainder
 * @param lhsOffset offset of the left hand side in its array
 * @param rhs array right hand side of remainder
 * @param rhsOffset offset of the right hand side in its array
 * @param result array where result must be stored (it may be
 * one of the input arrays)
 * @param resultOffset offset of the result in its array
 */
public void remainder(final double[] lhs, final int lhsOffset,
                      final double[] rhs, final int rhsOffset,
                      final double[] result, final int resultOffset) {

    // compute k such that lhs % rhs = lhs - k rhs
    final double rem = FastMath.IEEEremainder(lhs[lhsOffset], rhs[rhsOffset]);
    final double k   = FastMath.rint((lhs[lhsOffset] - rem) / rhs[rhsOffset]);

    // set up value
    result[resultOffset] = rem;

    // set up partial derivatives
    for (int i = 1; i < getSize(); ++i) {
        result[resultOffset + i] = lhs[lhsOffset + i] - k * rhs[rhsOffset + i];
    }

}
 
Example 5
Source File: DSCompiler.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** Perform remainder of two derivative structures.
 * @param lhs array holding left hand side of remainder
 * @param lhsOffset offset of the left hand side in its array
 * @param rhs array right hand side of remainder
 * @param rhsOffset offset of the right hand side in its array
 * @param result array where result must be stored (it may be
 * one of the input arrays)
 * @param resultOffset offset of the result in its array
 */
public void remainder(final double[] lhs, final int lhsOffset,
                      final double[] rhs, final int rhsOffset,
                      final double[] result, final int resultOffset) {

    // compute k such that lhs % rhs = lhs - k rhs
    final double rem = lhs[lhsOffset] % rhs[rhsOffset];
    final double k   = FastMath.rint((lhs[lhsOffset] - rem) / rhs[rhsOffset]);

    // set up value
    result[resultOffset] = rem;

    // set up partial derivatives
    for (int i = 1; i < getSize(); ++i) {
        result[resultOffset + i] = lhs[lhsOffset + i] - k * rhs[rhsOffset + i];
    }

}
 
Example 6
Source File: Rint.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.rint(x);
}
 
Example 7
Source File: ContinuousOutputModel.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Set the time of the interpolated point.
 * <p>This method should <strong>not</strong> be called before the
 * integration is over because some internal variables are set only
 * once the last step has been handled.</p>
 * <p>Setting the time outside of the integration interval is now
 * allowed (it was not allowed up to version 5.9 of Mantissa), but
 * should be used with care since the accuracy of the interpolator
 * will probably be very poor far from this interval. This allowance
 * has been added to simplify implementation of search algorithms
 * near the interval endpoints.</p>
 * @param time time of the interpolated point
 */
public void setInterpolatedTime(final double time) {

    // initialize the search with the complete steps table
    int iMin = 0;
    final StepInterpolator sMin = steps.get(iMin);
    double tMin = 0.5 * (sMin.getPreviousTime() + sMin.getCurrentTime());

    int iMax = steps.size() - 1;
    final StepInterpolator sMax = steps.get(iMax);
    double tMax = 0.5 * (sMax.getPreviousTime() + sMax.getCurrentTime());

    // handle points outside of the integration interval
    // or in the first and last step
    if (locatePoint(time, sMin) <= 0) {
      index = iMin;
      sMin.setInterpolatedTime(time);
      return;
    }
    if (locatePoint(time, sMax) >= 0) {
      index = iMax;
      sMax.setInterpolatedTime(time);
      return;
    }

    // reduction of the table slice size
    while (iMax - iMin > 5) {

      // use the last estimated index as the splitting index
      final StepInterpolator si = steps.get(index);
      final int location = locatePoint(time, si);
      if (location < 0) {
        iMax = index;
        tMax = 0.5 * (si.getPreviousTime() + si.getCurrentTime());
      } else if (location > 0) {
        iMin = index;
        tMin = 0.5 * (si.getPreviousTime() + si.getCurrentTime());
      } else {
        // we have found the target step, no need to continue searching
        si.setInterpolatedTime(time);
        return;
      }

      // compute a new estimate of the index in the reduced table slice
      final int iMed = (iMin + iMax) / 2;
      final StepInterpolator sMed = steps.get(iMed);
      final double tMed = 0.5 * (sMed.getPreviousTime() + sMed.getCurrentTime());

      if ((FastMath.abs(tMed - tMin) < 1e-6) || (FastMath.abs(tMax - tMed) < 1e-6)) {
        // too close to the bounds, we estimate using a simple dichotomy
        index = iMed;
      } else {
        // estimate the index using a reverse quadratic polynom
        // (reverse means we have i = P(t), thus allowing to simply
        // compute index = P(time) rather than solving a quadratic equation)
        final double d12 = tMax - tMed;
        final double d23 = tMed - tMin;
        final double d13 = tMax - tMin;
        final double dt1 = time - tMax;
        final double dt2 = time - tMed;
        final double dt3 = time - tMin;
        final double iLagrange = ((dt2 * dt3 * d23) * iMax -
                                  (dt1 * dt3 * d13) * iMed +
                                  (dt1 * dt2 * d12) * iMin) /
                                 (d12 * d23 * d13);
        index = (int) FastMath.rint(iLagrange);
      }

      // force the next size reduction to be at least one tenth
      final int low  = FastMath.max(iMin + 1, (9 * iMin + iMax) / 10);
      final int high = FastMath.min(iMax - 1, (iMin + 9 * iMax) / 10);
      if (index < low) {
        index = low;
      } else if (index > high) {
        index = high;
      }

    }

    // now the table slice is very small, we perform an iterative search
    index = iMin;
    while ((index <= iMax) && (locatePoint(time, steps.get(index)) > 0)) {
      ++index;
    }

    steps.get(index).setInterpolatedTime(time);

}
 
Example 8
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc}
 * @since 3.2
 */
public DerivativeStructure rint() {
    return new DerivativeStructure(compiler.getFreeParameters(),
                                   compiler.getOrder(),
                                   FastMath.rint(data[0]));
}
 
Example 9
Source File: Rint.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.rint(x);
}
 
Example 10
Source File: Rint.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.rint(x);
}
 
Example 11
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** Get the whole number that is the nearest to the instance, or the even one if x is exactly half way between two integers.
 * @return a double number r such that r is an integer r - 0.5 <= this <= r + 0.5
 */
public DerivativeStructure rint() {
    return new DerivativeStructure(compiler.getFreeParameters(),
                                   compiler.getOrder(),
                                   FastMath.rint(data[0]));
}
 
Example 12
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc}
 * @since 3.2
 */
public DerivativeStructure rint() {
    return new DerivativeStructure(compiler.getFreeParameters(),
                                   compiler.getOrder(),
                                   FastMath.rint(data[0]));
}
 
Example 13
Source File: DerivativeStructure.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc}
 * @since 3.2
 */
public DerivativeStructure rint() {
    return new DerivativeStructure(compiler.getFreeParameters(),
                                   compiler.getOrder(),
                                   FastMath.rint(data[0]));
}
 
Example 14
Source File: Rint.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/** {@inheritDoc} */
public double value(double x) {
    return FastMath.rint(x);
}
 
Example 15
Source File: Percentile.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected double index(final double p, final int length) {
    final double minLimit = 1d/2 / length;
    return Double.compare(p, minLimit) <= 0 ?
            0 : FastMath.rint(length * p);
}
 
Example 16
Source File: Percentile.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected double index(final double p, final int length) {
    final double minLimit = 1d/2 / length;
    return Double.compare(p, minLimit) <= 0 ?
            0 : FastMath.rint(length * p);
}
 
Example 17
Source File: NeighbourhoodSizeFunctionFactory.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an exponential decay {@link NeighbourhoodSizeFunction function}.
 * It will compute <code>a e<sup>-x / b</sup></code>,
 * where {@code x} is the (integer) independent variable and
 * <ul>
 *  <li><code>a = initValue</code>
 *  <li><code>b = -numCall / ln(valueAtNumCall / initValue)</code>
 * </ul>
 *
 * @param initValue Initial value, i.e.
 * {@link NeighbourhoodSizeFunction#value(long) value(0)}.
 * @param valueAtNumCall Value of the function at {@code numCall}.
 * @param numCall Argument for which the function returns
 * {@code valueAtNumCall}.
 * @return the neighbourhood size function.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code initValue <= 0}.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code valueAtNumCall <= 0}.
 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
 * if {@code valueAtNumCall >= initValue}.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code numCall <= 0}.
 */
public static NeighbourhoodSizeFunction exponentialDecay(final double initValue,
                                                         final double valueAtNumCall,
                                                         final long numCall) {
    return new NeighbourhoodSizeFunction() {
        /** DecayFunction. */
        private final ExponentialDecayFunction decay
            = new ExponentialDecayFunction(initValue, valueAtNumCall, numCall);

        /** {@inheritDoc} */
        public int value(long n) {
            return (int) FastMath.rint(decay.value(n));
        }
    };
}
 
Example 18
Source File: SparseGradient.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/** {@inheritDoc} */
public SparseGradient remainder(final SparseGradient a) {

    // compute k such that lhs % rhs = lhs - k rhs
    final double rem = FastMath.IEEEremainder(value, a.value);
    final double k   = FastMath.rint((value - rem) / a.value);

    return subtract(a.multiply(k));

}
 
Example 19
Source File: NeighbourhoodSizeFunctionFactory.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an sigmoid-like {@code NeighbourhoodSizeFunction function}.
 * The function {@code f} will have the following properties:
 * <ul>
 *  <li>{@code f(0) = initValue}</li>
 *  <li>{@code numCall} is the inflexion point</li>
 *  <li>{@code slope = f'(numCall)}</li>
 * </ul>
 *
 * @param initValue Initial value, i.e.
 * {@link NeighbourhoodSizeFunction#value(long) value(0)}.
 * @param slope Value of the function derivative at {@code numCall}.
 * @param numCall Inflexion point.
 * @return the neighbourhood size function.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code initValue <= 0}.
 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
 * if {@code slope >= 0}.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code numCall <= 0}.
 */
public static NeighbourhoodSizeFunction quasiSigmoidDecay(final double initValue,
                                                          final double slope,
                                                          final long numCall) {
    return new NeighbourhoodSizeFunction() {
        /** DecayFunction. */
        private final QuasiSigmoidDecayFunction decay
            = new QuasiSigmoidDecayFunction(initValue, slope, numCall);

        /** {@inheritDoc} */
        public int value(long n) {
            return (int) FastMath.rint(decay.value(n));
        }
    };
}
 
Example 20
Source File: NeighbourhoodSizeFunctionFactory.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates an exponential decay {@link NeighbourhoodSizeFunction function}.
 * It will compute <code>a e<sup>-x / b</sup></code>,
 * where {@code x} is the (integer) independent variable and
 * <ul>
 *  <li><code>a = initValue</code>
 *  <li><code>b = -numCall / ln(valueAtNumCall / initValue)</code>
 * </ul>
 *
 * @param initValue Initial value, i.e.
 * {@link NeighbourhoodSizeFunction#value(long) value(0)}.
 * @param valueAtNumCall Value of the function at {@code numCall}.
 * @param numCall Argument for which the function returns
 * {@code valueAtNumCall}.
 * @return the neighbourhood size function.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code initValue <= 0}.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code valueAtNumCall <= 0}.
 * @throws org.apache.commons.math3.exception.NumberIsTooLargeException
 * if {@code valueAtNumCall >= initValue}.
 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
 * if {@code numCall <= 0}.
 */
public static NeighbourhoodSizeFunction exponentialDecay(final double initValue,
                                                         final double valueAtNumCall,
                                                         final long numCall) {
    return new NeighbourhoodSizeFunction() {
        /** DecayFunction. */
        private final ExponentialDecayFunction decay
            = new ExponentialDecayFunction(initValue, valueAtNumCall, numCall);

        /** {@inheritDoc} */
        public int value(long n) {
            return (int) FastMath.rint(decay.value(n));
        }
    };
}