Example usage for org.apache.commons.math3.analysis DifferentiableUnivariateFunction derivative

List of usage examples for org.apache.commons.math3.analysis DifferentiableUnivariateFunction derivative

Introduction

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

Prototype

UnivariateFunction derivative();

Source Link

Document

Returns the derivative of the function

Usage

From source file:org.apache.solr.client.solrj.io.eval.DerivativeEvaluator.java

@Override
public Object doWork(Object value) throws IOException {
    if (null == value) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }/*from w  ww . j  a  v a 2  s .c o  m*/

    if (!(value instanceof VectorFunction)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a FunctionVector",
                toExpression(constructingFactory), value.getClass().getSimpleName()));
    }

    VectorFunction vectorFunction = (VectorFunction) value;
    if (!(vectorFunction.getFunction() instanceof DifferentiableUnivariateFunction)) {
        throw new IOException("Cannot evaluate derivative from parameter.");
    }

    DifferentiableUnivariateFunction func = (DifferentiableUnivariateFunction) vectorFunction.getFunction();
    double[] x = (double[]) vectorFunction.getFromContext("x");
    UnivariateFunction derfunc = func.derivative();
    double[] dvalues = new double[x.length];
    for (int i = 0; i < x.length; i++) {
        dvalues[i] = derfunc.value(x[i]);
    }

    VectorFunction vf = new VectorFunction(derfunc, dvalues);
    vf.addToContext("x", x);
    vf.addToContext("y", vectorFunction.getFromContext("y"));

    return vf;
}