org.apache.commons.math4.analysis

## Class FunctionUtils

• public class FunctionUtils
extends Object
Utilities for manipulating function objects.
Since:
3.0
• ### Method Summary

All Methods
Modifier and Type Method and Description
static UnivariateDifferentiableFunction add(UnivariateDifferentiableFunction... f)
static UnivariateFunction add(UnivariateFunction... f)
static MultivariateFunction collector(BivariateFunction combiner, double initialValue)
Returns a MultivariateFunction h(x[]) defined by
static MultivariateFunction collector(BivariateFunction combiner, UnivariateFunction f, double initialValue)
Returns a MultivariateFunction h(x[]) defined by
static UnivariateFunction combine(BivariateFunction combiner, UnivariateFunction f, UnivariateFunction g)
Returns the univariate function h(x) = combiner(f(x), g(x)).
static UnivariateDifferentiableFunction compose(UnivariateDifferentiableFunction... f)
Composes functions.
static UnivariateFunction compose(UnivariateFunction... f)
Composes functions.
static MultivariateFunction derivative(MultivariateDifferentiableFunction f, int[] orders)
static UnivariateFunction derivative(UnivariateDifferentiableFunction f, int order)
static UnivariateFunction fix1stArgument(BivariateFunction f, double fixed)
Creates a unary function by fixing the first argument of a binary function.
static UnivariateFunction fix2ndArgument(BivariateFunction f, double fixed)
Creates a unary function by fixing the second argument of a binary function.
static UnivariateDifferentiableFunction multiply(UnivariateDifferentiableFunction... f)
Multiplies functions.
static UnivariateFunction multiply(UnivariateFunction... f)
Multiplies functions.
static double[] sample(UnivariateFunction f, double min, double max, int n)
Samples the specified univariate real function on the specified interval.
static MultivariateDifferentiableFunction toDifferentiable(MultivariateFunction f, MultivariateVectorFunction gradient)
static UnivariateDifferentiableFunction toDifferentiable(UnivariateFunction f, UnivariateFunction... derivatives)
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Method Detail

• #### compose

public static UnivariateFunction compose(UnivariateFunction... f)
Composes functions.

The functions in the argument list are composed sequentially, in the given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).

Parameters:
f - List of functions.
Returns:
the composite function.
• #### compose

public static UnivariateDifferentiableFunction compose(UnivariateDifferentiableFunction... f)
Composes functions.

The functions in the argument list are composed sequentially, in the given order. For example, compose(f1,f2,f3) acts like f1(f2(f3(x))).

Parameters:
f - List of functions.
Returns:
the composite function.
Since:
3.1

public static UnivariateFunction add(UnivariateFunction... f)
Parameters:
f - List of functions.
Returns:
a function that computes the sum of the functions.

public static UnivariateDifferentiableFunction add(UnivariateDifferentiableFunction... f)
Parameters:
f - List of functions.
Returns:
a function that computes the sum of the functions.
Since:
3.1
• #### multiply

public static UnivariateFunction multiply(UnivariateFunction... f)
Multiplies functions.
Parameters:
f - List of functions.
Returns:
a function that computes the product of the functions.
• #### multiply

public static UnivariateDifferentiableFunction multiply(UnivariateDifferentiableFunction... f)
Multiplies functions.
Parameters:
f - List of functions.
Returns:
a function that computes the product of the functions.
Since:
3.1
• #### combine

public static UnivariateFunction combine(BivariateFunction combiner,
UnivariateFunction f,
UnivariateFunction g)
Returns the univariate function h(x) = combiner(f(x), g(x)).
Parameters:
combiner - Combiner function.
f - Function.
g - Function.
Returns:
the composite function.
• #### collector

public static MultivariateFunction collector(BivariateFunction combiner,
UnivariateFunction f,
double initialValue)
Returns a MultivariateFunction h(x[]) defined by
 
h(x[]) = combiner(...combiner(combiner(initialValue,f(x[0])),f(x[1]))...),f(x[x.length-1]))

Parameters:
combiner - Combiner function.
f - Function.
initialValue - Initial value.
Returns:
a collector function.
• #### collector

public static MultivariateFunction collector(BivariateFunction combiner,
double initialValue)
Returns a MultivariateFunction h(x[]) defined by
 
h(x[]) = combiner(...combiner(combiner(initialValue,x[0]),x[1])...),x[x.length-1])

Parameters:
combiner - Combiner function.
initialValue - Initial value.
Returns:
a collector function.
• #### fix1stArgument

public static UnivariateFunction fix1stArgument(BivariateFunction f,
double fixed)
Creates a unary function by fixing the first argument of a binary function.
Parameters:
f - Binary function.
fixed - value to which the first argument of f is set.
Returns:
the unary function h(x) = f(fixed, x)
• #### fix2ndArgument

public static UnivariateFunction fix2ndArgument(BivariateFunction f,
double fixed)
Creates a unary function by fixing the second argument of a binary function.
Parameters:
f - Binary function.
fixed - value to which the second argument of f is set.
Returns:
the unary function h(x) = f(x, fixed)
• #### sample

public static double[] sample(UnivariateFunction f,
double min,
double max,
int n)
throws NumberIsTooLargeException,
NotStrictlyPositiveException
Samples the specified univariate real function on the specified interval.

The interval is divided equally into n sections and sample points are taken from min to max - (max - min) / n; therefore f is not sampled at the upper bound max.

Parameters:
f - Function to be sampled
min - Lower bound of the interval (included).
max - Upper bound of the interval (excluded).
n - Number of sample points.
Returns:
the array of samples.
Throws:
NumberIsTooLargeException - if the lower bound min is greater than, or equal to the upper bound max.
NotStrictlyPositiveException - if the number of sample points n is negative.
• #### toDifferentiable

public static UnivariateDifferentiableFunction toDifferentiable(UnivariateFunction f,
UnivariateFunction... derivatives)
Convert regular functions to UnivariateDifferentiableFunction.

This method handle the case with one free parameter and several derivatives. For the case with several free parameters and only first order derivatives, see toDifferentiable(MultivariateFunction, MultivariateVectorFunction). There are no direct support for intermediate cases, with several free parameters and order 2 or more derivatives, as is would be difficult to specify all the cross derivatives.

Note that the derivatives are expected to be computed only with respect to the raw parameter x of the base function, i.e. they are df/dx, df2/dx2, ... Even if the built function is later used in a composition like f(sin(t)), the provided derivatives should not apply the composition with sine and its derivatives by themselves. The composition will be done automatically here and the result will properly contain f(sin(t)), df(sin(t))/dt, df2(sin(t))/dt2 despite the provided derivatives functions know nothing about the sine function.

Parameters:
f - base function f(x)
derivatives - derivatives of the base function, in increasing differentiation order
Returns:
a differentiable function with value and all specified derivatives
toDifferentiable(MultivariateFunction, MultivariateVectorFunction), derivative(UnivariateDifferentiableFunction, int)
• #### toDifferentiable

public static MultivariateDifferentiableFunction toDifferentiable(MultivariateFunction f,
MultivariateVectorFunction gradient)
Convert regular functions to MultivariateDifferentiableFunction.

This method handle the case with several free parameters and only first order derivatives. For the case with one free parameter and several derivatives, see toDifferentiable(UnivariateFunction, UnivariateFunction...). There are no direct support for intermediate cases, with several free parameters and order 2 or more derivatives, as is would be difficult to specify all the cross derivatives.

Note that the gradient is expected to be computed only with respect to the raw parameter x of the base function, i.e. it is df/dx1, df/dx2, ... Even if the built function is later used in a composition like f(sin(t), cos(t)), the provided gradient should not apply the composition with sine or cosine and their derivative by itself. The composition will be done automatically here and the result will properly contain f(sin(t), cos(t)), df(sin(t), cos(t))/dt despite the provided derivatives functions know nothing about the sine or cosine functions.

Parameters:
f - base function f(x)
gradient - gradient of the base function
Returns:
a differentiable function with value and gradient
toDifferentiable(UnivariateFunction, UnivariateFunction...), derivative(MultivariateDifferentiableFunction, int[])