Example usage for org.apache.commons.math3.analysis UnivariateFunction value

List of usage examples for org.apache.commons.math3.analysis UnivariateFunction value

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis UnivariateFunction value.

Prototype

double value(double x);

Source Link

Document

Compute the value of the function.

Usage

From source file:org.moeaframework.examples.gp.regression.SymbolicRegression.java

/**
 * Constructs a new symbolic regression problem for approximating the
 * given function./*from w  w  w .  ja  va  2 s  . c o  m*/
 * 
 * @param function the actual function implementation
 * @param lowerBound the lower bound for comparing the actual and
 *        approximate functions
 * @param upperBound the upper bound for comparing the actual and
 *        approximate functions
 * @param steps the number of comparisons made between the actual and
 *        approximate functions
 */
public SymbolicRegression(UnivariateFunction function, double lowerBound, double upperBound, int steps) {
    super(1, 1);
    this.function = function;
    this.lowerBound = lowerBound;
    this.upperBound = upperBound;
    this.steps = steps;

    // setup the default rules
    symbol = "x";
    rules = new Rules();
    rules.add(new Add());
    rules.add(new Multiply());
    rules.add(new Subtract());
    rules.add(new Divide());
    rules.add(new Sin());
    rules.add(new Cos());
    rules.add(new Exp());
    rules.add(new Log());
    rules.add(new Get(Number.class, symbol));
    rules.setReturnType(Number.class);
    rules.setMaxVariationDepth(10);

    // cache the function's x and y values
    x = new double[steps];
    y = new double[steps];

    for (int i = 0; i < steps; i++) {
        x[i] = lowerBound + (i / (steps - 1.0)) * (upperBound - lowerBound);
        y[i] = function.value(x[i]);
    }
}

From source file:org.orekit.propagation.events.EventState.java

/** Evaluate the impact of the proposed step on the event detector.
 * @param interpolator step interpolator for the proposed step
 * @return true if the event detector triggers an event before
 * the end of the proposed step (this implies the step should be
 * rejected)//from   w w  w  .j  av a2s. c om
 * @exception OrekitException if the switching function
 * cannot be evaluated
 * @exception TooManyEvaluationsException if an event cannot be located
 * @exception NoBracketingException if bracketing cannot be performed
 */
public boolean evaluateStep(final OrekitStepInterpolator interpolator)
        throws OrekitException, TooManyEvaluationsException, NoBracketingException {

    try {

        final double convergence = detector.getThreshold();
        final int maxIterationcount = detector.getMaxIterationCount();
        if (forward ^ interpolator.isForward()) {
            forward = !forward;
            pendingEvent = false;
            pendingEventTime = null;
            previousEventTime = null;
        }
        final AbsoluteDate t1 = interpolator.getCurrentDate();
        final double dt = t1.durationFrom(t0);
        if (FastMath.abs(dt) < convergence) {
            // we cannot do anything on such a small step, don't trigger any events
            return false;
        }
        final int n = FastMath.max(1, (int) FastMath.ceil(FastMath.abs(dt) / detector.getMaxCheckInterval()));
        final double h = dt / n;

        final UnivariateFunction f = new UnivariateFunction() {
            public double value(final double t) throws LocalWrapperException {
                try {
                    interpolator.setInterpolatedDate(t0.shiftedBy(t));
                    return g(interpolator.getInterpolatedState());
                } catch (OrekitException oe) {
                    throw new LocalWrapperException(oe);
                }
            }
        };

        final BracketingNthOrderBrentSolver solver = new BracketingNthOrderBrentSolver(convergence, 5);

        AbsoluteDate ta = t0;
        double ga = g0;
        for (int i = 0; i < n; ++i) {

            // evaluate detector value at the end of the substep
            final AbsoluteDate tb = t0.shiftedBy((i + 1) * h);
            interpolator.setInterpolatedDate(tb);
            final double gb = g(interpolator.getInterpolatedState());

            // check events occurrence
            if (g0Positive ^ (gb >= 0)) {
                // there is a sign change: an event is expected during this step

                // variation direction, with respect to the integration direction
                increasing = gb >= ga;

                // find the event time making sure we select a solution just at or past the exact root
                final double dtA = ta.durationFrom(t0);
                final double dtB = tb.durationFrom(t0);
                final double dtRoot = forward
                        ? solver.solve(maxIterationcount, f, dtA, dtB, AllowedSolution.RIGHT_SIDE)
                        : solver.solve(maxIterationcount, f, dtB, dtA, AllowedSolution.LEFT_SIDE);
                final AbsoluteDate root = t0.shiftedBy(dtRoot);

                if ((previousEventTime != null) && (FastMath.abs(root.durationFrom(ta)) <= convergence)
                        && (FastMath.abs(root.durationFrom(previousEventTime)) <= convergence)) {
                    // we have either found nothing or found (again ?) a past event,
                    // retry the substep excluding this value, and taking care to have the
                    // required sign in case the g function is noisy around its zero and
                    // crosses the axis several times
                    do {
                        ta = forward ? ta.shiftedBy(convergence) : ta.shiftedBy(-convergence);
                        ga = f.value(ta.durationFrom(t0));
                    } while ((g0Positive ^ (ga >= 0)) && (forward ^ (ta.compareTo(tb) >= 0)));

                    if (forward ^ (ta.compareTo(tb) >= 0)) {
                        // we were able to skip this spurious root
                        --i;
                    } else {
                        // we can't avoid this root before the end of the step,
                        // we have to handle it despite it is close to the former one
                        // maybe we have two very close roots
                        pendingEventTime = root;
                        pendingEvent = true;
                        return true;
                    }

                } else if ((previousEventTime == null)
                        || (FastMath.abs(previousEventTime.durationFrom(root)) > convergence)) {
                    pendingEventTime = root;
                    pendingEvent = true;
                    return true;
                } else {
                    // no sign change: there is no event for now
                    ta = tb;
                    ga = gb;
                }

            } else {
                // no sign change: there is no event for now
                ta = tb;
                ga = gb;
            }

        }

        // no event during the whole step
        pendingEvent = false;
        pendingEventTime = null;
        return false;

    } catch (LocalWrapperException lwe) {
        throw lwe.getWrappedException();
    }

}

From source file:uk.ac.diamond.scisoft.analysis.dataset.function.Interpolation1D.java

public static Dataset interpolate(IDataset oldx, IDataset oldy, IDataset newx,
        UnivariateInterpolator interpolator) {

    //TODO more sanity checks on inputs

    DoubleDataset dx = (DoubleDataset) DatasetUtils.cast(oldx, Dataset.FLOAT64);
    DoubleDataset dy = (DoubleDataset) DatasetUtils.cast(oldy, Dataset.FLOAT64);

    boolean sorted = true;
    double maxtest = dx.getDouble(0);
    int count = dx.getSize();
    for (int i = 1; i < count; i++) {
        if (maxtest > dx.getDouble(i)) {
            sorted = false;/*  www  .  j  a  v  a  2s. c om*/
            break;
        }
        maxtest = dx.getDouble(i);
    }

    double[] sortedx = null;
    double[] sortedy = null;

    if (!sorted) {
        IntegerDataset sIdx = getIndiciesOfSorted(dx);
        sortedx = new double[dx.getData().length];
        sortedy = new double[dy.getData().length];

        for (int i = 0; i < sIdx.getSize(); i++) {
            sortedx[i] = dx.getDouble(sIdx.get(i));
            sortedy[i] = dy.getDouble(sIdx.get(i));
        }
    } else {
        sortedx = dx.getData();
        sortedy = dy.getData();
    }

    UnivariateFunction func = interpolator.interpolate(sortedx, sortedy);

    Dataset newy = DatasetFactory.zeros(newx.getShape(), Dataset.FLOAT64);
    newy.setName(oldy.getName() + "_interpolated");
    count = newy.getSize();
    double val = 0;
    for (int i = 0; i < count; i++) {

        try {
            val = func.value(newx.getDouble(i));
        } catch (OutOfRangeException e) {
            val = 0;
        }

        newy.set(val, i);
    }

    return newy;
}