nr
Class ScalarFunction

java.lang.Object
  extended by nr.ScalarFunction

public abstract class ScalarFunction
extends java.lang.Object

A functionoid designed to implement a scalar function of a vector

Author:
Daniel Wachsstock

Constructor Summary
ScalarFunction()
           
 
Method Summary
abstract  double eval(Vec x)
          a scalar function of a vector.
 Vec grad(Vec x)
          Calculates the gradient for this function.
 Vec grad(Vec x, Vec g)
          Calculates the gradient for this function.
 double gradCost()
          The computational cost of evaluating the gradient.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScalarFunction

public ScalarFunction()
Method Detail

eval

public abstract double eval(Vec x)
a scalar function of a vector.

Parameters:
x - the independent variable
Returns:
y

gradCost

public double gradCost()
The computational cost of evaluating the gradient. Some algorithms make decisions on how hard a calculation is, so this function should return the ratio of computer time in calling grad(nr.Vec, nr.Vec) to eval(nr.Vec). For the most part this can be ignored. This will usually depend on the size of the input vector, so is unknowable to a generic implementation, so the default implementation throws an Error. If it is needed, it must be implemented (how to get the necessary information is up to you).

For the default gradient, gradCost = 2*x.size() (grad calls eval twice for every element of the input vector)

Returns:
the relative cost of evaluating the Jacobian

grad

public Vec grad(Vec x,
                Vec g)
Calculates the gradient for this function. if f = eval(x) and j = grad(x), then j[i] = df/dx[j]. In the default implementation, it is calculated by symmetric finite differencing, as in Numerical Recipes eq. 5.7.7. This involves 2*x.size() calls to eval.

Parameters:
x - the independent variable
g - the the gradient of eval. If grad == null, a new Vec will be created
Returns:
g

grad

public Vec grad(Vec x)
Calculates the gradient for this function. Should have the same result as (Vec, Vec) with g == null. The default implementation does just that.

Parameters:
x - the independent variable
Returns:
the jacobian