- 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 RealValuedFunction
Provide 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.double
derivAt
(double arg) Evaluate the function's first derivative.final double
derivAt
(int i, double... args) Evaluate the partial derivative $\frac{\partial f}{\partial x_i}$ for a function f(x0,x1, ...).boolean
Determine if the domain maximum is in the domain.final boolean
domainMaxClosed
(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.boolean
Determine if the domain minimum is in the domain.final boolean
domainMinClosed
(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.double
Get the maximum value in the domain of the function.final double
getDomainMax
(int i) Get the least upper bound of the ith argument when the arguments are in the domain of the function.double
Get the minimum value in the domain of the function.final double
getDomainMin
(int i) Get the greatest lower bound of the ith argument when the arguments are in the domain of the function.boolean
isInDomain
(double x) Determine if an argument is within the domain of a function.final boolean
isInDomain
(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.double
secondDerivAt
(double arg) Evaluate the function's second derivative.final double
secondDerivAt
(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, ...).double
valueAt
(double arg) Call the function.final double
valueAt
(double... args) Call the function.Methods inherited from class org.bzdev.math.RealValuedFunctionVA
jacobian, jacobian, maxArgLength, minArgLength
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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:RealValuedFunctionVA
Get 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:
getDomainMin
in interfaceVADomainOps
- Overrides:
getDomainMin
in 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:
getDomainMin
in interfaceRealValuedDomainOps
- Returns:
- the minimum value
- Throws:
IllegalStateException
- the function was not fully initialized.
-
getDomainMax
Description copied from class:RealValuedFunctionVA
Get 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:
getDomainMax
in interfaceVADomainOps
- Overrides:
getDomainMax
in 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:RealValuedDomainOps
Get the maximum value in the domain of the function.- Specified by:
getDomainMax
in interfaceRealValuedDomainOps
- Returns:
- the maximum value
- Throws:
IllegalStateException
- the function was not fully initialized.
-
domainMinClosed
Description copied from class:RealValuedFunctionVA
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. 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:
domainMinClosed
in interfaceVADomainOps
- Overrides:
domainMinClosed
in 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:RealValuedDomainOps
Determine if the domain minimum is in the domain.- Specified by:
domainMinClosed
in 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:RealValuedFunctionVA
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. 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:
domainMaxClosed
in interfaceVADomainOps
- Overrides:
domainMaxClosed
in 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:RealValuedDomainOps
Determine if the domain maximum is in the domain.- Specified by:
domainMaxClosed
in 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:RealValuedFunctionVA
Determine 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:
isInDomain
in interfaceVADomainOps
- Overrides:
isInDomain
in 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:RealValuedDomainOps
Determine if an argument is within the domain of a function.- Specified by:
isInDomain
in 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:RealValuedFunctionVA
Call the function.- Specified by:
valueAt
in interfaceRealValuedFunctOps
- Specified by:
valueAt
in interfaceRealValuedFunctVAOps
- Overrides:
valueAt
in 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:
valueAt
in 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:RealValuedFunctionVA
Evaluate the partial derivative $\frac{\partial f}{\partial x_i}$ for a function f(x0,x1, ...).- Overrides:
derivAt
in 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:RealValuedFunctionVA
Evaluate the partial derivative $\frac{\partial^2 f}{\partial x_i \partial z_j}$ for a function f(x0,x1, ...).- Overrides:
secondDerivAt
in 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.
-