Class RealValuedFunction

java.lang.Object
org.bzdev.math.RealValuedFunctionVA
org.bzdev.math.RealValuedFunction
All Implemented Interfaces:
DoubleUnaryOperator, RealValuedDomainOps, RealValuedFunctOps, RealValuedFunctVAOps, VADomainOps
Direct Known Subclasses:
BezierPolynomial, BSpline, CubicSpline, LeastSquaresFit, Polynomial

public class RealValuedFunction extends RealValuedFunctionVA implements RealValuedDomainOps, RealValuedFunctOps
Class defining a real-valued function with a real argument.

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:


     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);
 
Alternatively, one may use the following code where the functions defining the derivatives are provided by name:

     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);
 
  • Field Details

    • xFunction

      public static final RealValuedFunction 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

      public RealValuedFunction(RealValuedFunctOps function)
      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 itself
      functionp - the first derivative of the function
      functionpp - the second derivative of the function
    • RealValuedFunction

      public RealValuedFunction(ScriptingContext scriptingContext, Object object)
      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 context
      object - 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 context
      fname - the name of a function; null if the valueAt method is not supported
      fpname - the name of a function providing the desired function's first derivative; null if the derivAt method is not supported
      fppname - the name of a function providing the desired function's second derivative; null if the secondDerivAt method is not supported
  • Method Details