package umontreal.iro.lecuyer.functions;



/**
 * Represents a function computing the average of several functions.
 * Let 
 * f0(x),…, fn-1(x) be a set of n functions.
 * This function represents the average 
 * 

*
* f (x) = $\displaystyle {\frac{1}{n}}$∑i=0n-1fi(x). *

* */ public class AverageMathFunction implements MathFunction , MathFunctionWithFirstDerivative, MathFunctionWithDerivative, MathFunctionWithIntegral { private MathFunction[] func; /** * Constructs a function computing the average * of the functions in the array func. * * @param func the array of functions to average. * * */ public AverageMathFunction (MathFunction... func) { if (func == null) throw new NullPointerException(); this.func = func.clone (); } /** * Returns the functions being averaged. * * @return the averaged functions. * */ public MathFunction[] getFunctions() { return func.clone (); } public double evaluate (double x) { double sum = 0; for (final MathFunction fi : func) sum += fi.evaluate (x); return sum / func.length; } public double derivative (double x, int n) { if (n < 0) throw new IllegalArgumentException ("n must be greater than or equal to 0"); if (n == 0) return evaluate (x); double sum = 0; for (final MathFunction fi : func) sum += MathFunctionUtil.derivative (fi, x, n); return sum / func.length; } public double derivative (double x) { double sum = 0; for (final MathFunction fi : func) sum += MathFunctionUtil.derivative (fi, x); return sum / func.length; } public double integral (double a, double b) { double sum = 0; for (final MathFunction fi : func) sum += MathFunctionUtil.integral (fi, a, b); return sum / func.length; } }

Ads help maintain this website.