- All Implemented Interfaces:
DoubleUnaryOperator,RealValuedDomainOps,RealValuedFunctOps,RealValuedFunctVAOps,VADomainOps
- Direct Known Subclasses:
BezierPolynomial,BSpline,CubicSpline,LeastSquaresFit,Polynomial
This is intended for cases in which a function should be passed as an argument.
A subclass will typically override one or more of the methods valueAt, derivAt, and secondDerivAt to provide the values for a function, its first derivative, and its second derivative. For any that are not available, an UnsupportedOperationException will be thrown.
The class also provides scripting-language support. If a Scripting
context is named scripting, the following EMCAScript
code will implement a sin function and its derivatives:
Alternatively, one may use the following code where the functions defining the derivatives are provided by name:importClass(org.bzdev.RealValuedFunction); .... // assume ourObject is a Java class with a method setFunction // that takes a RealValuedFunction as its argument. funct = new RealValuedFunction(scripting, {valueAt: function(x) {return Math.sin(x);}, derivAt: function(x) {return Math.cos(x);}, secondDerivAt: function(x) {return -Math.cos(x);}}); ourObject.setFunction(funct);
importClass(org.bzdev.RealValuedFunction); ... function f(x) {return Math.sin(x);} function fp(x) {return Math.cos(x);} function fpp(x) {return -Math.sin(x);} ... // assume ourObject is a Java class with a method setFunction // that takes a RealValuedFunction as its argument. funct = new RealValuedFunction(scripting, "f", "fp", "fpp"); ourObject.setFunction(funct);
-
Nested Class Summary
Nested classes/interfaces inherited from class org.bzdev.math.RealValuedFunctionVA
RealValuedFunctionVA.Linear -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RealValuedFunctionProvide an instance of a real-valued function that just returns its argument. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.RealValuedFunction(RealValuedFunctOps function) Constructor given a function.RealValuedFunction(RealValuedFunctOps function, RealValuedFunctOps functionp, RealValuedFunctOps functionpp) Constructor given a function and its derivatives.RealValuedFunction(ScriptingContext scriptingContext, Object object) Constructor when the function is provided by a script object.RealValuedFunction(ScriptingContext scriptingContext, String fname, String fpname, String fppname) Constructor when the function is provided by a script. -
Method Summary
Modifier and TypeMethodDescriptionderiv()Get a function that computes the first derivative of this real-valued function.deriv(int n) Get a function that computes this function's nth derivative.doublederivAt(double arg) Evaluate the function's first derivative.final doublederivAt(int i, double... args) Evaluate the partial derivative $\frac{\partial f}{\partial x_i}$ for a function f(x0,x1, ...).booleanDetermine if the domain maximum is in the domain.final booleandomainMaxClosed(int i) Determine if the domain maximum for the ith argument, when the arguments are in the domain of the function, is in the function's domain.booleanDetermine if the domain minimum is in the domain.final booleandomainMinClosed(int i) Determine if the domain minimum for the ith argument, when the arguments are in the domain of the function, is in the function's domain.doubleGet the maximum value in the domain of the function.final doublegetDomainMax(int i) Get the least upper bound of the ith argument when the arguments are in the domain of the function.doubleGet the minimum value in the domain of the function.final doublegetDomainMin(int i) Get the greatest lower bound of the ith argument when the arguments are in the domain of the function.booleanisInDomain(double x) Determine if an argument is within the domain of a function.final booleanisInDomain(double... args) Determine if a point is within the domain of this function.Get a function that computes the second derivative of this real-valued function.doublesecondDerivAt(double arg) Evaluate the function's second derivative.final doublesecondDerivAt(int i, int j, double... args) Evaluate the partial derivative $\frac{\partial^2 f}{\partial x_i \partial z_j}$ for a function f(x0,x1, ...).doublevalueAt(double arg) Call the function.final doublevalueAt(double... args) Call the function.Methods inherited from class org.bzdev.math.RealValuedFunctionVA
jacobian, jacobian, maxArgLength, minArgLengthMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.bzdev.math.RealValuedFunctOps
andThen, andThen, applyAsDouble, compose, compose, maxArgLength, minArgLength
-
Field Details
-
xFunction
Provide an instance of a real-valued function that just returns its argument. I.e., a function defined by f(x) = x.Note: this is a common case for curve fitting when the a function y(x) is generated and one wants to create a an instance of SplinePath2D for plotting.
-
-
Constructor Details
-
RealValuedFunction
public RealValuedFunction()Constructor. -
RealValuedFunction
Constructor given a function. This allows a lambda expression to be used to implement a real-valued function.- Parameters:
function- the function itself
-
RealValuedFunction
public RealValuedFunction(RealValuedFunctOps function, RealValuedFunctOps functionp, RealValuedFunctOps functionpp) Constructor given a function and its derivatives. This allows lambda expressions to be used to implement a real-valued function.- Parameters:
function- the function itselffunctionp- the first derivative of the functionfunctionpp- the second derivative of the function
-
RealValuedFunction
Constructor when the function is provided by a script object. The parameter 'object' is expected to be either an instance of RealValuedFunction or an object defined by a scripting language with methods named "valueAt", "derivAt" and "secondDerivAt". Each of these three methods takes a number as its argument and return a number.- Parameters:
scriptingContext- the scripting contextobject- an object from a scripting environment
-
RealValuedFunction
public RealValuedFunction(ScriptingContext scriptingContext, String fname, String fpname, String fppname) Constructor when the function is provided by a script. The script is expected to define up to three functions, indicated by their names.- Parameters:
scriptingContext- the scripting contextfname- the name of a function; null if the valueAt method is not supportedfpname- the name of a function providing the desired function's first derivative; null if the derivAt method is not supportedfppname- the name of a function providing the desired function's second derivative; null if the secondDerivAt method is not supported
-
-
Method Details
-
getDomainMin
Description copied from class:RealValuedFunctionVAGet the greatest lower bound of the ith argument when the arguments are in the domain of the function. The implementation will either return the most negative double-precision number or the result of calling a method named getDomainMin (with the same arguments as this method) provided by an object created by a scripting language. If an object created via a scripting language is not passed to a constructor, and a different value is appropriate, this method should be overridden.- Specified by:
getDomainMinin interfaceVADomainOps- Overrides:
getDomainMinin classRealValuedFunctionVA- Parameters:
i- the index determining the argument for which this method applies (0th, 1st, ...)- Returns:
- the minimum value
- Throws:
IllegalArgumentException- the argument is out of rangeIllegalStateException- the function was not fully initialized.
-
getDomainMin
Get the minimum value in the domain of the function.- Specified by:
getDomainMinin interfaceRealValuedDomainOps- Returns:
- the minimum value
- Throws:
IllegalStateException- the function was not fully initialized.
-
getDomainMax
Description copied from class:RealValuedFunctionVAGet the least upper bound of the ith argument when the arguments are in the domain of the function. The implementation will either return the largest double-precision number or the result of calling a method named getDomainMax (with the same arguments as this method) provided by an object created by a scripting language. If an object created via a scripting language is not passed to a constructor, and a different value is appropriate, this method should be overridden.- Specified by:
getDomainMaxin interfaceVADomainOps- Overrides:
getDomainMaxin classRealValuedFunctionVA- Parameters:
i- the index determining the argument for which this method applies (0th, 1st, ...)- Returns:
- the maximum value
- Throws:
IllegalArgumentException- the argument is out of rangeIllegalStateException- the function was not fully initialized.
-
getDomainMax
Description copied from interface:RealValuedDomainOpsGet the maximum value in the domain of the function.- Specified by:
getDomainMaxin interfaceRealValuedDomainOps- Returns:
- the maximum value
- Throws:
IllegalStateException- the function was not fully initialized.
-
domainMinClosed
Description copied from class:RealValuedFunctionVADetermine if the domain minimum for the ith argument, when the arguments are in the domain of the function, is in the function's domain. The implementation will either return true or the result of calling a method named getDomainMinClosed (with the same arguments as this method) provided by an object created by a scripting language. If an object created via a scripting language is not passed to a constructor, and a different value is appropriate, this method should be overridden.- Specified by:
domainMinClosedin interfaceVADomainOps- Overrides:
domainMinClosedin classRealValuedFunctionVA- Parameters:
i- the index determining the argument for which this method applies (0th, 1st, ...)- Returns:
- true if the domain minimum is in the domain; false if it is the greatest lower bound for the domain
- Throws:
IllegalArgumentException- the argument is out of rangeIllegalStateException- the function was not fully initialized.
-
domainMinClosed
Description copied from interface:RealValuedDomainOpsDetermine if the domain minimum is in the domain.- Specified by:
domainMinClosedin interfaceRealValuedDomainOps- Returns:
- true if the domain minimum is in the domain; false if it is the greatest lower bound for the domain
- Throws:
IllegalStateException- the function was not fully initialized.
-
domainMaxClosed
Description copied from class:RealValuedFunctionVADetermine if the domain maximum for the ith argument, when the arguments are in the domain of the function, is in the function's domain. The implementation will either return true or the result of calling a method named getDomainMaxClosed (with the same arguments as this method) provided by an object created by a scripting language. If an object created via a scripting language is not passed to a constructor, and a different value is appropriate, this method should be overridden.- Specified by:
domainMaxClosedin interfaceVADomainOps- Overrides:
domainMaxClosedin classRealValuedFunctionVA- Parameters:
i- the index determining the argument for which this method applies (0th, 1st, ...)- Returns:
- true if the domain maximum is in the domain; false if it is the least upper bound for the domain
- Throws:
IllegalArgumentException- the argument is out of rangeIllegalStateException- the function was not fully initialized.
-
domainMaxClosed
Description copied from interface:RealValuedDomainOpsDetermine if the domain maximum is in the domain.- Specified by:
domainMaxClosedin interfaceRealValuedDomainOps- Returns:
- true if the domain maximum is in the domain; false if it is the least upper bound for the domain
- Throws:
IllegalStateException- the function was not fully initialized.
-
isInDomain
public final boolean isInDomain(double... args) throws UnsupportedOperationException, IllegalArgumentException, IllegalStateException Description copied from class:RealValuedFunctionVADetermine if a point is within the domain of this function.The default behavior of this method assumes the domain is a rectangular region and uses the methods
RealValuedFunctionVA.getDomainMin(int),RealValuedFunctionVA.getDomainMax(int),RealValuedFunctionVA.domainMinClosed(int),RealValuedFunctionVA.domainMaxClosed(int), to determine if the arguments represent a point in the functions domain. If the domain is not rectangular with each side either in or not in the domain, then this method must be overridden. If it is not possible with a reasonable amount of computation to determine that a point is in the domain, an UnsupportedOperationException may be thrown. If this exception is thrown, it should be thrown regardless of the arguments.- Specified by:
isInDomainin interfaceVADomainOps- Overrides:
isInDomainin classRealValuedFunctionVA- Parameters:
args- the arguments (x0,x1,...) giving the coordinates of a point- Returns:
- true if the point (x0,x1,...) is in this function's domain; false otherwise
- Throws:
UnsupportedOperationException- domain membership could not be determined.IllegalArgumentException- an argument is out of rangeIllegalStateException- the function was not fully initialized.
-
isInDomain
Description copied from interface:RealValuedDomainOpsDetermine if an argument is within the domain of a function.- Specified by:
isInDomainin interfaceRealValuedDomainOps- Parameters:
x- a value to test- Returns:
- true if x is in this function's domain; false otherwise
- Throws:
UnsupportedOperationException- domain membership could not be determined.IllegalStateException- the function was not fully initialized.
-
valueAt
public final double valueAt(double... args) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException Description copied from class:RealValuedFunctionVACall the function.- Specified by:
valueAtin interfaceRealValuedFunctOps- Specified by:
valueAtin interfaceRealValuedFunctVAOps- Overrides:
valueAtin classRealValuedFunctionVA- Parameters:
args- the function's arguments- Returns:
- the value of the function for the given arguments
- Throws:
IllegalArgumentException- the function's argument(s) were out of rangeUnsupportedOperationException- the operation is not supported.IllegalStateException- the function was not fully initialized.
-
valueAt
public double valueAt(double arg) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException Call the function.- Specified by:
valueAtin interfaceRealValuedFunctOps- Parameters:
arg- the function's argument- Returns:
- the value of the function for the given argument
- Throws:
IllegalArgumentException- the function's argument was out of rangeUnsupportedOperationException- the operation is not supported.IllegalStateException- the function was not fully initialized.
-
derivAt
public final double derivAt(int i, double... args) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException Description copied from class:RealValuedFunctionVAEvaluate the partial derivative $\frac{\partial f}{\partial x_i}$ for a function f(x0,x1, ...).- Overrides:
derivAtin classRealValuedFunctionVA- Parameters:
i- the index indicating that the partial derivative is computed for the ith argument, numbered from zeroargs- the function f's arguments- Returns:
- the value of the partial derivative for the given argument
- Throws:
IllegalArgumentException- the function's argument(s) were out of rangeUnsupportedOperationException- the operation is not supported.IllegalStateException- the function was not fully initialized.
-
deriv
Get a function that computes this function's nth derivative. The value of n must be 0 (for the function itself), 1 for the first derivative, or 2 for the second derivative.If this real-valued function does not support an nth derivative, the returned function's valueAt method will throw a
UnsupportedOperationException.- Parameters:
n- 1 for the first derivative; 2 for the second derivative; 0 for the function itself- Returns:
- a function providing the derivative of this function
- Throws:
IllegalArgumentException- n is not 0, 1, or 2.IllegalStateException- the function was not fully initialized.
-
deriv
Get a function that computes the first derivative of this real-valued function. If this real-valued function does not support a first derivative, the returned function's valueAt method will throw aUnsupportedOperationException.- Returns:
- the first derivative
-
secondDeriv
Get a function that computes the second derivative of this real-valued function. If this real-valued function does not support a second derivative, the returned function's valueAt method will throw aUnsupportedOperationException.- Returns:
- the second derivative
-
derivAt
Evaluate the function's first derivative.- Parameters:
arg- the function's argument- Returns:
- the value of the function for the given argument
- Throws:
IllegalArgumentException- the function's argument was out of rangeUnsupportedOperationException- the operation is not supported.
-
secondDerivAt
public final double secondDerivAt(int i, int j, double... args) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException Description copied from class:RealValuedFunctionVAEvaluate the partial derivative $\frac{\partial^2 f}{\partial x_i \partial z_j}$ for a function f(x0,x1, ...).- Overrides:
secondDerivAtin classRealValuedFunctionVA- Parameters:
i- the index indicating that the partial derivative is computed for the ith argument, numbered from 0j- the index indicating that the partial derivative is computed for the jth argument, numbered from 0args- the function f's arguments- Returns:
- the value of the partial derivative $\frac{\partial^2 f}{\partial x_i \partial z_j}$ for the given arguments x0, x1, ...
- Throws:
IllegalArgumentException- the function's arguments were out of rangeUnsupportedOperationException- the operation is not supported.IllegalStateException- the function was not fully initialized.
-
secondDerivAt
public double secondDerivAt(double arg) throws IllegalArgumentException, UnsupportedOperationException, IllegalStateException Evaluate the function's second derivative.- Parameters:
arg- the function's argument- Returns:
- the value of the function for the given argument
- Throws:
IllegalArgumentException- the function's argument was out of rangeUnsupportedOperationException- the operation is not supported.IllegalStateException- the function was not fully initialized.
-