function
Class BasicScalarFunction

java.lang.Object
  extended by function.BasicScalarFunction
All Implemented Interfaces:
Computable, ScalarFunction
Direct Known Subclasses:
BasicDifferentiableScalarFunction, CaffeToScalarFunction, Hartman3, Hartman6, Shekel5, TestFunction1D, VariableDomainScalarFunction, WaveDragAxisymmetric.AreaRule, WaveDragAxisymmetric.Panair, Woods

public abstract class BasicScalarFunction
extends java.lang.Object
implements ScalarFunction

Abstract class to implement most of the functionality of the ScalarFunction interface. Subclasses need to do two things: 1. Put a static block at the very top of the class (it's important to have it at the very top) with a single line, DEFAULT_BOUNDS = <value>, and 2. Implement at least the quickCompute(double[]) method.

The default bounds for each problem are initialized to plus-minus infinity. For many functions, finite bounds have are significant: calling these functions with values outside the bounds may result in the compute code crashing, or spitting out garbage. In such cases, users should override the makeBounds() method by adding commands to set upper and lower bounds for the static (class-level) variable DEFAULT_BOUNDS. The compute(double[]) method protects against this by first checking whether values are within bounds (values actually on the boundary is considered valid), and only calls the compute routine with valid inputs. Invalid inputs get a predetermined value stored in VALUE_OUTSIDE_BOUNDS. This value can also be overriden by adding a line to the static block.

For improved speed, users can override the quickCompute(DoubleMatrix2D, DoubleMatrix1D) method and exploit optimized operations from DoubleMatrix2D. Optimized bounds checking, though, may not be possible, and as such, the compute(DoubleMatrix2D, DoubleMatrix1D) method checks bounds row-by-row. Calling the compute(double[][], double[]) is even slower owing to dimension-checking on every input. This needs to be done because double[][] arrays in Java can be ragged. This calls for careful overriding of the quickCompute(DoubleMatrix2D, DoubleMatrix1D) method, to ensure that subclasses 'do the right thing' for DEFAULT_BOUNDS checking and correction. Unfortunately, the colt libraries do not provide methods that return arrays of indices of rows/elements matching some criterion.

Author:
dgorur

Field Summary
protected static Hypercube DEFAULT_BOUNDS
           
protected static int DEFAULT_DIM
           
protected static double VALUE_OUTSIDE_BOUNDS
           
 
Constructor Summary
BasicScalarFunction()
          Default constructor.
 
Method Summary
protected  void checkDimensions(double[] x)
          Checks the validity of the given array as an input.
 double compute(double[] x)
          Computes the output corresponding to the given input.
 void compute(double[][] xArr, double[] outputs)
          Computes the outputs for the given inputs, and stores the result in the supplied array.
 void compute(double[][] x, double[][] y)
          Computes the outputs corresponding to the given set of inputs and stores the results in the given array, which cannot be null.
 void compute(double[] x, double[] y)
          Computes the outputs corresponding to the given input, and stores the result in the supplied array.
 double compute(cern.colt.matrix.DoubleMatrix1D xVec)
          Computes the output corresponding to the given input.
 cern.colt.matrix.DoubleMatrix1D compute(cern.colt.matrix.DoubleMatrix2D xMat, cern.colt.matrix.DoubleMatrix1D outputs)
          Computes the outputs for the given inputs, and stores the result in the given vector.
 Hypercube getBounds()
          Returns a Hypercube specifying the domain of this Computable.
 int getInputDimension()
          Returns the number of dimensions accepted by this Computable.
 int getOutputDimension()
          Returns the output dimension for this Computable.
 void init()
          Initialization method.
 boolean isWithinBounds(double[] x)
          Returns true if the given point is within bounds.
protected static void makeBounds()
           
abstract  double quickCompute(double[] x)
          Compute method without dimension checking.
 void quickCompute(cern.colt.matrix.DoubleMatrix2D xMat, cern.colt.matrix.DoubleMatrix1D outputs)
          Compute method without dimension checking, operating on DoubleMatrix2D and exploiting optimized operations therein.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DIM

protected static int DEFAULT_DIM

VALUE_OUTSIDE_BOUNDS

protected static double VALUE_OUTSIDE_BOUNDS

DEFAULT_BOUNDS

protected static Hypercube DEFAULT_BOUNDS
Constructor Detail

BasicScalarFunction

public BasicScalarFunction()
Default constructor.

Method Detail

makeBounds

protected static void makeBounds()

compute

public double compute(double[] x)
Description copied from interface: ScalarFunction
Computes the output corresponding to the given input.

Specified by:
compute in interface ScalarFunction
Parameters:
x - the given input.
Returns:
the output corresponding to x.

compute

public void compute(double[] x,
                    double[] y)
Description copied from interface: Computable
Computes the outputs corresponding to the given input, and stores the result in the supplied array.

Specified by:
compute in interface Computable
Parameters:
x - the given input.
y - array to hold the result.

compute

public void compute(double[][] xArr,
                    double[] outputs)
Description copied from interface: ScalarFunction
Computes the outputs for the given inputs, and stores the result in the supplied array.

Specified by:
compute in interface ScalarFunction
Parameters:
xArr - array of inputs, row-wise.
outputs - array to hold the result, cannot be null.

compute

public void compute(double[][] x,
                    double[][] y)
Description copied from interface: Computable
Computes the outputs corresponding to the given set of inputs and stores the results in the given array, which cannot be null.

Specified by:
compute in interface Computable
Parameters:
x - the given inputs.
y - array to hold the result.

compute

public double compute(cern.colt.matrix.DoubleMatrix1D xVec)
Description copied from interface: ScalarFunction
Computes the output corresponding to the given input.

Specified by:
compute in interface ScalarFunction
Parameters:
xVec - the given input.
Returns:
the output corresponding to the given input.

compute

public cern.colt.matrix.DoubleMatrix1D compute(cern.colt.matrix.DoubleMatrix2D xMat,
                                               cern.colt.matrix.DoubleMatrix1D outputs)
Description copied from interface: ScalarFunction
Computes the outputs for the given inputs, and stores the result in the given vector.

Specified by:
compute in interface ScalarFunction
Parameters:
xMat - a matrix of inputs, specified row-wise.
outputs - vector to hold the result, cannot be null.
Returns:
the output vector, for convenience.

getBounds

public Hypercube getBounds()
Description copied from interface: Computable
Returns a Hypercube specifying the domain of this Computable.

Specified by:
getBounds in interface Computable
Returns:
the domain of this ScalarFunction.

getInputDimension

public int getInputDimension()
Description copied from interface: Computable
Returns the number of dimensions accepted by this Computable.

Specified by:
getInputDimension in interface Computable
Returns:
the number of dimensions.

getOutputDimension

public int getOutputDimension()
Description copied from interface: Computable
Returns the output dimension for this Computable.

Specified by:
getOutputDimension in interface Computable
Returns:
the number of output dimensions.

init

public void init()
Initialization method.

Specified by:
init in interface ScalarFunction

isWithinBounds

public boolean isWithinBounds(double[] x)
Description copied from interface: Computable
Returns true if the given point is within bounds.

Specified by:
isWithinBounds in interface Computable
Parameters:
x - the given point.
Returns:
true if within bounds.

quickCompute

public abstract double quickCompute(double[] x)
Compute method without dimension checking. All subclasses must implement this method: it is the core method that defines the function.

Specified by:
quickCompute in interface ScalarFunction
Parameters:
x - the given input.
Returns:
the output corresponding to the given input.

quickCompute

public void quickCompute(cern.colt.matrix.DoubleMatrix2D xMat,
                         cern.colt.matrix.DoubleMatrix1D outputs)
Compute method without dimension checking, operating on DoubleMatrix2D and exploiting optimized operations therein.

Specified by:
quickCompute in interface ScalarFunction
Parameters:
xMat - given matrix of inputs.
outputs - vector to store the result.

checkDimensions

protected void checkDimensions(double[] x)
                        throws java.lang.IllegalArgumentException
Checks the validity of the given array as an input.

Parameters:
x - the given input.
Throws:
java.lang.IllegalArgumentException - if the size of x is different from the dimensionality of this ScalarFunction.