Package edu.hws.jcm.functions
Class TableFunction
- java.lang.Object
-
- edu.hws.jcm.functions.FunctionParserExtension
-
- edu.hws.jcm.functions.TableFunction
-
- All Implemented Interfaces:
ExpressionCommand
,Function
,MathObject
,ParserExtension
,java.io.Serializable
public class TableFunction extends FunctionParserExtension
A TableFunction is a function that is specified by a table of (x,y)-points. Values are interpolated between the specified x-values. This can be done in several differnt ways; the method that is used is controlled by the "Style" property. Since a TableFunction extends FunctionParserExtension, a TableFunction can be added to a Parser (provided it has a name), and it can then be used in expressions parsed by that parser. Note that this class is meant to be used for functions that are defined by a fairly small number of points, since each function evaluation involves a linear search through the list of x-values of the defining points.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
PIECEWISE_LINEAR
If the style of the function is set to PIECEWISE_LINEAR, then linear interpolation is used to find the value of the functions for x-values between the points that define the function.static int
SMOOTH
If the style of the function is set to SMOOTH, then cubic interpolation is used to find the value of the functions for x-values between the points that define the function.static int
STEP
If the style of the function is set to STEP, then the function is piecewise constant, and the value of the function at x is taken from the nearest point in the list of points that define the function.static int
STEP_LEFT
If the style of the function is set to STEP_LEFT, then the function is piecewise constant, and the value of the function at x is taken from the nearest point to the left in the list of points that define the function.static int
STEP_RIGHT
If the style of the function is set to STEP_RIGHT, then the function is piecewise constant, and the value of the function at x is taken from the nearest point to the right in the list of points that define the function.-
Fields inherited from class edu.hws.jcm.functions.FunctionParserExtension
name
-
-
Constructor Summary
Constructors Constructor Description TableFunction()
Create a TableFunction with SMOOTH style and no points.TableFunction(int sytle)
Create a TableFunction with specified style and no points.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addIntervals(int intervals, double xmin, double xmax)
Add points to the table.int
addPoint(double x, double y)
Add a point with the specified x and y coordinates.void
addPoints(double[] xCoords, double[] yCoords)
Add points to the table.void
apply(StackOfDouble stack, Cases cases)
Override method apply() from interface FunctionParserExtension, to handle cases properly.void
copyDataFrom(TableFunction source)
Copy data from another TableFunction, except that the name of the funcion is not duplicated.boolean
dependsOn(Variable wrt)
Returns false.Function
derivative(int wrt)
Compute the derivative of this function.Function
derivative(Variable wrt)
Returns null.int
findPoint(double x)
If there is a point in the list with x-coordinate x, then this function returns the index of that point in the list (where the index of the first point is zero).int
getArity()
Returns the arity of the function, which is 1.int
getPointCount()
Gets the number of points in the table.int
getStyle()
Get the style of this TableFunction, which specifies how values are interpolated between points on the curve.double
getVal(double x)
Get the value of the function at x, using interpolation if x lies between two x-coordinates in the list of points that define the function.double
getVal(double[] params)
Get the value of the function at the specified parameter value.double
getValueWithCases(double[] params, Cases cases)
Get the value of the function at the specified parameter value.double
getX(int i)
Get the x-coordinate in the i-th point, where the first point is number zero.double
getY(int i)
Get the y-coordinate in the i-th point, where the first point is number zero.void
removeAllPoints()
Remove all points.void
removePointAt(int i)
Removes the i-th point from the list of points.void
setStyle(int style)
Set the style of this TableFunction, to specify how values are interpolated between points on the curve.void
setY(int i, double y)
Set the y-coordinate in the i-th point to y, where the first point is number zero.-
Methods inherited from class edu.hws.jcm.functions.FunctionParserExtension
appendOutputString, compileDerivative, doParse, extent, getName, setName, setParensCanBeOptional
-
-
-
-
Field Detail
-
SMOOTH
public static final int SMOOTH
If the style of the function is set to SMOOTH, then cubic interpolation is used to find the value of the functions for x-values between the points that define the function.- See Also:
- Constant Field Values
-
PIECEWISE_LINEAR
public static final int PIECEWISE_LINEAR
If the style of the function is set to PIECEWISE_LINEAR, then linear interpolation is used to find the value of the functions for x-values between the points that define the function.- See Also:
- Constant Field Values
-
STEP
public static final int STEP
If the style of the function is set to STEP, then the function is piecewise constant, and the value of the function at x is taken from the nearest point in the list of points that define the function.- See Also:
- Constant Field Values
-
STEP_LEFT
public static final int STEP_LEFT
If the style of the function is set to STEP_LEFT, then the function is piecewise constant, and the value of the function at x is taken from the nearest point to the left in the list of points that define the function.- See Also:
- Constant Field Values
-
STEP_RIGHT
public static final int STEP_RIGHT
If the style of the function is set to STEP_RIGHT, then the function is piecewise constant, and the value of the function at x is taken from the nearest point to the right in the list of points that define the function.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TableFunction
public TableFunction()
Create a TableFunction with SMOOTH style and no points.
-
TableFunction
public TableFunction(int sytle)
Create a TableFunction with specified style and no points.- Parameters:
style
- The style for the function: SMOOTH, PIECEWISE_LINEAR, STEP, STEP_LEFT, or STEP_RIGHT.
-
-
Method Detail
-
copyDataFrom
public void copyDataFrom(TableFunction source)
Copy data from another TableFunction, except that the name of the funcion is not duplicated. The new TableFunction is nameless.
-
setStyle
public void setStyle(int style)
Set the style of this TableFunction, to specify how values are interpolated between points on the curve.- Parameters:
style
- One of the style constants SMOOTH, PIECEWISE_LINEAR, STEP, STEP_LEFT, STEP_RIGHT. Other values are ignored.
-
getStyle
public int getStyle()
Get the style of this TableFunction, which specifies how values are interpolated between points on the curve.- Returns:
- The style of this TableFunction. This is one of the constants SMOOTH, PIECEWISE_LINEAR, STEP, STEP_LEFT, or STEP_RIGHT.
-
addPoints
public void addPoints(double[] xCoords, double[] yCoords)
Add points to the table. The x-coordinates of the points are taken from the xCoords array. The y-coordinate for the i-th point is yCoords[i], if an i-th position exists in this array. Otherwise, the y-coordinate is is zero. (Note that if xCoords[i] duplicates an x-value already in the table, then no new point is added but the corresponging y-value is changed.)- Parameters:
xCoords
- A list of x-coordinates to be added to the table. If this is null, then nothing is done.yCoords
- The value of yCoords[i], if it exists, is the y-coordinate corresponding to xCoords[i]. Otherwise, the y-coordinate is undefined. This can be null, in which case all y-coordinates are zero.
-
addIntervals
public void addIntervals(int intervals, double xmin, double xmax)
Add points to the table. The number of points added is intervals + 1. The x-coordinates are evenly spaced between xmin and xmax. The y-coordinates are zero.- Parameters:
intervals
- The number of intervals. The number of points added is intervals + 1. The value should be at least 1. If not, nothing is done.xmin
- The minimim x-coordinate for added points.xmax
- The maximum x-coodinate for added points. Should be greater than xmin, for efficiency, but no error occurs if it is not.
-
addPoint
public int addPoint(double x, double y)
Add a point with the specified x and y coordinates. If a point with the given x coordinate already exists in the table, then no new point is added, but the associated y-value is changed. (If x is Double.NaN, then no change is made and the return value is -1.)- Parameters:
x
- The x-coordinate of the point to be added or modified.y
- The y-coordinate of the point.- Returns:
- the position of the point in the list of points, where the first point is at position zero.
-
getPointCount
public int getPointCount()
Gets the number of points in the table.
-
getX
public double getX(int i)
Get the x-coordinate in the i-th point, where the first point is number zero. Throws an IllegalArgumentException if i is less than zero or greater than or equal to the number of points.
-
getY
public double getY(int i)
Get the y-coordinate in the i-th point, where the first point is number zero. Throws an IllegalArgumentException if i is less than zero or greater than or equal to the number of points.
-
setY
public void setY(int i, double y)
Set the y-coordinate in the i-th point to y, where the first point is number zero. Throws an IllegalArgumentException if i is less than zero or greater than or equal to the number of points.
-
findPoint
public int findPoint(double x)
If there is a point in the list with x-coordinate x, then this function returns the index of that point in the list (where the index of the first point is zero). If there is no such point, then -1 is returned.
-
removePointAt
public void removePointAt(int i)
Removes the i-th point from the list of points. Throws an IllegalArgumentException if i is less than zero or greater than or equal to the number of points.
-
removeAllPoints
public void removeAllPoints()
Remove all points. The resulting function is undefined everywhere.
-
getVal
public double getVal(double x)
Get the value of the function at x, using interpolation if x lies between two x-coordinates in the list of points that define the function. If x is outside the range of x-coords in the table, the value of the function is Double.NaN.
-
getValueWithCases
public double getValueWithCases(double[] params, Cases cases)
Get the value of the function at the specified parameter value.
-
getVal
public double getVal(double[] params)
Get the value of the function at the specified parameter value.
-
derivative
public Function derivative(int wrt)
Compute the derivative of this function. The value of the parameter, wrt, must be 1 or an IllegalArguemntException will be thrown.
-
derivative
public Function derivative(Variable wrt)
Returns null. It really should be the constant function zero, but I don't expect this ever to be called. Since dependsOn(wrt) returns false, it will never be called within the JCM system.
-
dependsOn
public boolean dependsOn(Variable wrt)
Returns false.
-
getArity
public int getArity()
Returns the arity of the function, which is 1.
-
apply
public void apply(StackOfDouble stack, Cases cases)
Override method apply() from interface FunctionParserExtension, to handle cases properly. Not meant to be called directly.- Specified by:
apply
in interfaceExpressionCommand
- Overrides:
apply
in classFunctionParserExtension
- Parameters:
stack
- contains results of previous commands in the program.cases
- if non-null, any case information generated during evaluation should be recorded here.
-
-