|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectfunction.BasicScalarFunction
public abstract class BasicScalarFunction
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.
| 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 |
|---|
protected static int DEFAULT_DIM
protected static double VALUE_OUTSIDE_BOUNDS
protected static Hypercube DEFAULT_BOUNDS
| Constructor Detail |
|---|
public BasicScalarFunction()
| Method Detail |
|---|
protected static void makeBounds()
public double compute(double[] x)
ScalarFunction
compute in interface ScalarFunctionx - the given input.
x.
public void compute(double[] x,
double[] y)
Computable
compute in interface Computablex - the given input.y - array to hold the result.
public void compute(double[][] xArr,
double[] outputs)
ScalarFunction
compute in interface ScalarFunctionxArr - array of inputs, row-wise.outputs - array to hold the result, cannot be null.
public void compute(double[][] x,
double[][] y)
Computablenull.
compute in interface Computablex - the given inputs.y - array to hold the result.public double compute(cern.colt.matrix.DoubleMatrix1D xVec)
ScalarFunction
compute in interface ScalarFunctionxVec - the given input.
public cern.colt.matrix.DoubleMatrix1D compute(cern.colt.matrix.DoubleMatrix2D xMat,
cern.colt.matrix.DoubleMatrix1D outputs)
ScalarFunction
compute in interface ScalarFunctionxMat - a matrix of inputs, specified row-wise.outputs - vector to hold the result, cannot be null.
public Hypercube getBounds()
ComputableHypercube specifying the domain of this
Computable.
getBounds in interface ComputableScalarFunction.public int getInputDimension()
ComputableComputable.
getInputDimension in interface Computablepublic int getOutputDimension()
ComputableComputable.
getOutputDimension in interface Computablepublic void init()
init in interface ScalarFunctionpublic boolean isWithinBounds(double[] x)
Computabletrue if the given point is within bounds.
isWithinBounds in interface Computablex - the given point.
true if within bounds.public abstract double quickCompute(double[] x)
quickCompute in interface ScalarFunctionx - the given input.
public void quickCompute(cern.colt.matrix.DoubleMatrix2D xMat,
cern.colt.matrix.DoubleMatrix1D outputs)
DoubleMatrix2D and exploiting optimized operations therein.
quickCompute in interface ScalarFunctionxMat - given matrix of inputs.outputs - vector to store the result.
protected void checkDimensions(double[] x)
throws java.lang.IllegalArgumentException
x - the given input.
java.lang.IllegalArgumentException - if the size of x is different from the dimensionality of
this ScalarFunction.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||