io.yields.math.concepts.operator.Volume.java Source code

Java tutorial

Introduction

Here is the source code for io.yields.math.concepts.operator.Volume.java

Source

/*
 * Copyright 2015 by Yields.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.yields.math.concepts.operator;

import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.integration.RombergIntegrator;
import org.apache.commons.math3.analysis.integration.UnivariateIntegrator;

import java.util.function.Function;

/**
 * calculates the volume between two curves
 */
public class Volume {

    private Function<Double, Double> lowerCurve, upperCurve;
    private double min, max;
    private final UnivariateIntegrator integrator;

    public Volume() {
        this.integrator = new RombergIntegrator();
    }

    public Volume between(Function<Double, Double> lowerCurve, Function<Double, Double> upperCurve) {
        this.lowerCurve = lowerCurve;
        this.upperCurve = upperCurve;
        return this;
    }

    public Volume withDomain(double min, double max) {
        this.min = min;
        this.max = max;
        return this;
    }

    public double compute() {
        UnivariateFunction function = x -> Math.abs(lowerCurve.apply(x) - upperCurve.apply(x));
        return integrator.integrate(CurveLength.MAX_EVAL, function, min, max);
    }

}