AnyLogic
Expand
Font size
All Implemented Interfaces:
Serializable

public class TableFunction
extends Object
implements Serializable
Table function enables the user to define functions by giving a number of (argument, value) pairs, i.e. a number of base points on a plot. Supports various interpolation types. A call of get(x) will return a (possibly, interpolated) value of the function. A number of behaviors are supported for the case x is out of original argument range.
In case the function only supports discrete values (that were provided as the argument set), and no interpolation is allowed, a call of get(x) with x not matching any argument entry would result in exception thrown.
Author:
AnyLogic North America, LLC https://anylogic.com
See Also:
Serialized Form

Nested Class Summary

Modifier and TypeClassDescription
static enum TableFunction.InterpolationType 
static enum TableFunction.OutOfRangeAction 

Field Summary

Modifier and TypeFieldDescription
static final TableFunction.InterpolationTypeINTERPOLATION_APPROXIMATION 
static final TableFunction.InterpolationTypeINTERPOLATION_LINEAR 
static final TableFunction.InterpolationTypeINTERPOLATION_NONE 
static final TableFunction.InterpolationTypeINTERPOLATION_SPLINE 
static final TableFunction.InterpolationTypeINTERPOLATION_STEP 
static final TableFunction.OutOfRangeActionOUTOFRANGE_CUSTOM 
static final TableFunction.OutOfRangeActionOUTOFRANGE_ERROR 
static final TableFunction.OutOfRangeActionOUTOFRANGE_EXTRAPOLATE 
static final TableFunction.OutOfRangeActionOUTOFRANGE_NEAREST 
static final TableFunction.OutOfRangeActionOUTOFRANGE_REPEAT 

Constructor Summary

ConstructorDescription
TableFunction(double[] arguments, double[] values, TableFunction.InterpolationType interpolationtype, int approximationOrder, TableFunction.OutOfRangeAction outofrangeaction, double outofrangevalue)
Creates a new table function with the given arguments, values, interpolation type and out of range behavior type.
TableFunction(double[] arguments, double[] values, TableFunction.InterpolationType interpolationtype, TableFunction.OutOfRangeAction outofrangeaction, double outofrangevalue)

Method Summary

Modifier and TypeMethodDescription
CustomDistributionAbstract<Double>createCustomDistribution()
Constructs a custom distribution from the TableFunction.
CustomDistributionAbstract<Double>createCustomDistribution(Random random)
Constructs a custom distribution from the TableFunction.
doubleget(double x)
Returns the table function value corresponding to the given argument, subject to the currently set interpolation type and out of range handling.
intgetApproximationOrder()
The method returns the currently set order of the approximation polynomial used in the approximation mode.
double[]getArguments()
Returns the (sorted) array of arguments.
TableFunction.InterpolationTypegetInterpolationType()
Returns the current interpolation type of the table function.
intgetLength()
Returns the number of entries (rows) in the table function.
doublegetNextArgument(double x)
Returns the value of the nearest argument that is strictly greater than x, respecting the possible table repeating.
TableFunction.OutOfRangeActiongetOutOfRangeAction()
Returns the current type of action performed for out of range arguments.
doublegetOutOfRangeDefaultValue()
Returns the value that is returned by get(x) in case x is out of range when out of range action type is OUTOFRANGE_CUSTOM.
double[]getValues()
Returns the array of values corresponding to the (sorted) array of arguments.
intindexOf(double x)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Finds the interval between the two points on the argument axis [a,b[ where x belongs to and returns the index of the left point a.
voidsetApproximationOrder(int order)
This method sets order of the approximation polynomial for the INTERPOLATION_APPROXIMATION mode.
voidsetArgumentsAndValues(double[] arguments, double[] values)
Sets the new argument and value arrays for the table function.
voidsetInterpolationType(TableFunction.InterpolationType type)
Sets the new interpolation type for the table function.
voidsetOutOfRangeAction(TableFunction.OutOfRangeAction action)
Sets the new action performed in case the argument provided in get() is out of range.
voidsetOutOfRangeDefaultValue(double value)
Sets the value to be returned by get(x) in case x is out of range AND out of range action type is OUTOFRANGE_CUSTOM.
StringtoString()
Returns the textual representation of the table function.

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Details

INTERPOLATION_NONE

public static final TableFunction.InterpolationType INTERPOLATION_NONE

INTERPOLATION_STEP

public static final TableFunction.InterpolationType INTERPOLATION_STEP

INTERPOLATION_LINEAR

public static final TableFunction.InterpolationType INTERPOLATION_LINEAR

INTERPOLATION_SPLINE

public static final TableFunction.InterpolationType INTERPOLATION_SPLINE

INTERPOLATION_APPROXIMATION

public static final TableFunction.InterpolationType INTERPOLATION_APPROXIMATION

OUTOFRANGE_ERROR

public static final TableFunction.OutOfRangeAction OUTOFRANGE_ERROR

OUTOFRANGE_NEAREST

public static final TableFunction.OutOfRangeAction OUTOFRANGE_NEAREST

OUTOFRANGE_CUSTOM

public static final TableFunction.OutOfRangeAction OUTOFRANGE_CUSTOM

OUTOFRANGE_REPEAT

public static final TableFunction.OutOfRangeAction OUTOFRANGE_REPEAT

OUTOFRANGE_EXTRAPOLATE

public static final TableFunction.OutOfRangeAction OUTOFRANGE_EXTRAPOLATE

Constructor Details

TableFunction

@Deprecated
public TableFunction(double[] arguments,
 double[] values,
 TableFunction.InterpolationType interpolationtype,
 TableFunction.OutOfRangeAction outofrangeaction,
 double outofrangevalue)
Creates a new table function with the given arguments, values, interpolation type and out of range behavior type. Note that arrays are not copied; they are used by reference and can be modified.
Parameters:
arguments - the array of arguments (2 or more - recommended, no duplicates, can be sorted or not)
values - the array of values (same size as arguments)
interpolationtype - the interpolation type
outofrangeaction - the type of action performed for out of range arguments in get()
outofrangevalue - the value returned if argument is out of range

TableFunction

public TableFunction(double[] arguments,
 double[] values,
 TableFunction.InterpolationType interpolationtype,
 int approximationOrder,
 TableFunction.OutOfRangeAction outofrangeaction,
 double outofrangevalue)
Creates a new table function with the given arguments, values, interpolation type and out of range behavior type. Note that arrays are not copied; they are used by reference and can be modified.
Parameters:
arguments - the array of arguments (2 or more - recommended, no duplicates, can be sorted or not)
values - the array of values (same size as arguments)
interpolationtype - the interpolation type
approximationOrder - the order (1, 2, 3...) of the approximation polynomial if the INTERPOLATION_APPROXIMATION mode is selected
outofrangeaction - the type of action performed for out of range arguments in get()
outofrangevalue - the value returned if argument is out of range

Method Details

get

public double get(double x)
Returns the table function value corresponding to the given argument, subject to the currently set interpolation type and out of range handling.
Parameters:
x - the argument
Returns:
the table function value

setArgumentsAndValues

public void setArgumentsAndValues(double[] arguments,
 double[] values)
Sets the new argument and value arrays for the table function. The following checks are performed: arrays must be of the same length, there cannot be duplicate arguments. The arguments will be sorted if they are not
Parameters:
arguments - the array of arguments
values - the array of values

getArguments

public double[] getArguments()
Returns the (sorted) array of arguments.
Returns:
the array of arguments

getValues

public double[] getValues()
Returns the array of values corresponding to the (sorted) array of arguments.
Returns:
the array of values

getLength

public int getLength()
Returns the number of entries (rows) in the table function.
Returns:
the number of entries (rows) in the table function

getNextArgument

public double getNextArgument(double x)
Returns the value of the nearest argument that is strictly greater than x, respecting the possible table repeating. If the table is not repeating and there is no such argument, returns +infinity.
Returns:
the index of the nearest argument that is greater than x, or +infinity

setInterpolationType

public void setInterpolationType(TableFunction.InterpolationType type)
Sets the new interpolation type for the table function. Valid types are: INTERPOLATION_NONE, INTERPOLATION_STEP, INTERPOLATION_LINEAR, INTERPOLATION_SPLINE, INTERPOLATION_APPROXIMATION.
In case of approximation please ensure that correct approximation order is specified (using setApproximationOrder(int) or in the constructor) before calling this method
Parameters:
type - the new interpolation type

setApproximationOrder

public void setApproximationOrder(int order)
This method sets order of the approximation polynomial for the INTERPOLATION_APPROXIMATION mode. The method doesn't change interpolation type (see setInterpolationType(InterpolationType))
Parameters:
order - order of the approximation (polynomial order)

getApproximationOrder

public int getApproximationOrder()
The method returns the currently set order of the approximation polynomial used in the approximation mode.
Returns:
order of the approximation polynomial

getInterpolationType

public TableFunction.InterpolationType getInterpolationType()
Returns the current interpolation type of the table function.
Returns:
the current interpolation type

setOutOfRangeAction

public void setOutOfRangeAction(TableFunction.OutOfRangeAction action)
Sets the new action performed in case the argument provided in get() is out of range. The valid actions are:
OUTOFRANGE_ERROR - raise error (throw exception)
OUTOFRANGE_NEAREST - use nearest value
OUTOFRANGE_CUSTOM - use specific value
OUTOFRANGE_REPEAT - treat the table as repeated pattern
In case interpolation type is INTERPOLATION_NONE, the only possible behavior is to raise error.
Parameters:
action - the type of action performed for out of range arguments

getOutOfRangeAction

public TableFunction.OutOfRangeAction getOutOfRangeAction()
Returns the current type of action performed for out of range arguments.
Returns:
the current type of action performed for out of range arguments

setOutOfRangeDefaultValue

public void setOutOfRangeDefaultValue(double value)
Sets the value to be returned by get(x) in case x is out of range AND out of range action type is OUTOFRANGE_CUSTOM.
Parameters:
value - the value to be returned by get(x) in case x is out of range

getOutOfRangeDefaultValue

public double getOutOfRangeDefaultValue()
Returns the value that is returned by get(x) in case x is out of range when out of range action type is OUTOFRANGE_CUSTOM.
Returns:
the value that is returned by get(x) in case x is out of range

toString

public String toString()
Returns the textual representation of the table function.
Overrides:
toString in class Object
Returns:
the textual representation of the table function

indexOf

@AnyLogicInternalAPI
public int indexOf(double x)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Finds the interval between the two points on the argument axis [a,b[ where x belongs to and returns the index of the left point a. The index of the last point is returned only if x equals the last point. If x isn't in the range, nearest index is returned

createCustomDistribution

public CustomDistributionAbstract<Double> createCustomDistribution()
Constructs a custom distribution from the TableFunction. Takes table function's interpolation type and check if it can be supported.
The random number generator is not set after this constructor and must then be provided in each call of get( Random ) method.
Returns:
CustomDistribution matching tableFunction

createCustomDistribution

public CustomDistributionAbstract<Double> createCustomDistribution(Random random)
Constructs a custom distribution from the TableFunction. Takes table function's interpolation type and check if it can be supported.
The random number generator is set after this constructor, so you can use both get() and get( Random ) methods. The default RNG can be accessed as getDefaultRandomGenerator() if called from an Agent or an Experiment.
Parameters:
random - the random number generator that will be used
Returns:
CustomDistribution matching tableFunction