Class FunctionUtils
- java.lang.Object
-
- org.hipparchus.analysis.FunctionUtils
-
public class FunctionUtils extends Object
Utilities for manipulating function objects.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static UnivariateDifferentiableFunction
add(UnivariateDifferentiableFunction... f)
Adds functions.static UnivariateFunction
add(UnivariateFunction... f)
Adds functions.static MultivariateFunction
collector(BivariateFunction combiner, double initialValue)
Returns a MultivariateFunction h(x[]) defined bystatic MultivariateFunction
collector(BivariateFunction combiner, UnivariateFunction f, double initialValue)
Returns a MultivariateFunction h(x[]) defined bystatic UnivariateFunction
combine(BivariateFunction combiner, UnivariateFunction f, UnivariateFunction g)
Returns the univariate functionh(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)
Convert anMultivariateDifferentiableFunction
to anMultivariateFunction
computing nth order derivative.static UnivariateFunction
derivative(UnivariateDifferentiableFunction f, int order)
Convert anUnivariateDifferentiableFunction
to anUnivariateFunction
computing nth order derivative.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)
Convert regular functions toMultivariateDifferentiableFunction
.static UnivariateDifferentiableFunction
toDifferentiable(UnivariateFunction f, UnivariateFunction... derivatives)
Convert regular functions toUnivariateDifferentiableFunction
.
-
-
-
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.
-
add
public static UnivariateFunction add(UnivariateFunction... f)
Adds functions.- Parameters:
f
- List of functions.- Returns:
- a function that computes the sum of the functions.
-
add
public static UnivariateDifferentiableFunction add(UnivariateDifferentiableFunction... f)
Adds functions.- Parameters:
f
- List of functions.- Returns:
- a function that computes the sum of the functions.
-
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.
-
combine
public static UnivariateFunction combine(BivariateFunction combiner, UnivariateFunction f, UnivariateFunction g)
Returns the univariate functionh(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 byh(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 byh(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 off
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 off
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 MathIllegalArgumentException
Samples the specified univariate real function on the specified interval.The interval is divided equally into
n
sections and sample points are taken frommin
tomax - (max - min) / n
; thereforef
is not sampled at the upper boundmax
.- Parameters:
f
- Function to be sampledmin
- Lower bound of the interval (included).max
- Upper bound of the interval (excluded).n
- Number of sample points.- Returns:
- the array of samples.
- Throws:
MathIllegalArgumentException
- if the lower boundmin
is greater than, or equal to the upper boundmax
.MathIllegalArgumentException
- if the number of sample pointsn
is negative.
-
toDifferentiable
public static UnivariateDifferentiableFunction toDifferentiable(UnivariateFunction f, UnivariateFunction... derivatives)
Convert regular functions toUnivariateDifferentiableFunction
.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
- See Also:
toDifferentiable(MultivariateFunction, MultivariateVectorFunction)
,derivative(UnivariateDifferentiableFunction, int)
-
toDifferentiable
public static MultivariateDifferentiableFunction toDifferentiable(MultivariateFunction f, MultivariateVectorFunction gradient)
Convert regular functions toMultivariateDifferentiableFunction
.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
- See Also:
toDifferentiable(UnivariateFunction, UnivariateFunction...)
,derivative(MultivariateDifferentiableFunction, int[])
-
derivative
public static UnivariateFunction derivative(UnivariateDifferentiableFunction f, int order)
Convert anUnivariateDifferentiableFunction
to anUnivariateFunction
computing nth order derivative.This converter is only a convenience method. Beware computing only one derivative does not save any computation as the original function will really be called under the hood. The derivative will be extracted from the full
DerivativeStructure
result.- Parameters:
f
- original function, with value and all its derivativesorder
- of the derivative to extract- Returns:
- function computing the derivative at required order
- See Also:
derivative(MultivariateDifferentiableFunction, int[])
,toDifferentiable(UnivariateFunction, UnivariateFunction...)
-
derivative
public static MultivariateFunction derivative(MultivariateDifferentiableFunction f, int[] orders)
Convert anMultivariateDifferentiableFunction
to anMultivariateFunction
computing nth order derivative.This converter is only a convenience method. Beware computing only one derivative does not save any computation as the original function will really be called under the hood. The derivative will be extracted from the full
DerivativeStructure
result.- Parameters:
f
- original function, with value and all its derivativesorders
- of the derivative to extract, for each free parameters- Returns:
- function computing the derivative at required order
- See Also:
derivative(UnivariateDifferentiableFunction, int)
,toDifferentiable(MultivariateFunction, MultivariateVectorFunction)
-
-