[ main | data | awt | draw ]

JCM Version 1.0
Package edu.hws.jcm.functions



This page discusses the more useful Java classes in the package edu.hws.jcm.functions, which is part of the JCM project. The full API can be found in the JavaDoc documentation. You should understand this package to be unfinished, since there are some features that should be added in the future.


class SummationParser

If you add a SummationParser object to a Parser, then that Parser will understand summation notation in the format "sum(i,n,m,term)" where the first parameter is the summation variable, the next two parameters give the lower and upper limits of the summation, and the fourth parameter is an arbitrary expression that generally will refer to the summation variable as well as other variables. All you have to do is say "parser.add(new SummationParser());". This is meant partly as a nice example of how parsers can be extended.

class ExpressionFunction

Represents a named function defined by an expression. This function is a ParserExtension and so can be added to a parser. The parser will then understand the function when it is used in an expression. For example, if you say "parser.add(new ExprssionFunction("sinh","(e^x-e^(-x))/2");", then the parser can parse expressions that use the function "sinh". The default constructor, used here, defines a function of x. In the constructor "ExpressionFunction(String,String[],definition)", the second parameter is an array of strings that give the names of the arguments of the function. For example: "new ExpressionFunction("max", new String[] {"a","b"}, "(a>b)?a:b");". It's possible to allow other variables besides the arguments in the definition. To do this, you have to provide a parser that already knows about those variables as a fourth parameter to the constructor.

class WrapperFunction

This is a cute litte class that represents a Function that is just a wrapper for another Function. All the calls to methods in the Function interface are delegated to the Function that is contained in a WrapperFunction. The wrapped function can be set in the constructor and can be set or changed later by calling the setFunction(Function) method. Derivatives of WrapperFunctions are computed in a way that makes sure that they are updated when the wrapped function is changed. The idea is that you can use the WrapperFunction for graphing, in expressions, etc. When you change the definition of the Wrapper function, all the other objects that depend on it will also change automatically.

You can give a WrapperFunction a name and add it to a Parser so that it can be used in expressions. This can be useful since an arbitrary Function can't be added to a Parser. However, you could wrap it in a WrapperFunction, give it a name, and then add it to a Parser.

class TableFunction and class TableFunctionGraph

A TableFunction is a function that is defined by a set of (x,y) coordinate pairs. Between the coordinates, the values of the function are interpolated. By default, a smooth function is defined using cubic interpolation. A TableFunction can be given a name so that it can be added to a parser and used in expressions. The default constructor makes a TableFunction that initially has no points and that uses cubic interpolation. The interpolation method can be specified as a parameter to the constructor or can be specified later using the setStyle(int) method. The style is given by one of the constants: TableFunction.SMOOTH (the default), TableFunction.PIECEWISE_LINEAR (linear interpolation between points), TableFunction.STEP (a step function where the value of the function is taken from the nearest point in the table), TableFunction.STEP_LEFT (the value at x is taken from the point to the left of x), or TableFunction.STEP_RIGHT (the value comes from the next point to the right).

Add a point to the table by calling addPoint(double,double). Use addPoints(double[],double[]) to add points whose x-coordinates are taken from the first array and y-coordinates from the second array. You can quickly add a bunch of points with y-coordinate zero and with x-coordinates spread out over a specified range. Use addIntervals(int,double,double). The first parameter gives the number of intervals into which the range is to be divided. The next two parameters give the range of x-coordinates.

Set the y-coordinate of the i-th point in the table with setY(int,double). Get the y-coordinate of the i-th point with getY(int), and get the x-coordinate with getX(i). The getPointCount() method returns the number of points. Points are always arranges so that their x-coordinates are in increasing order. The ordering does not depend on the order in which points are added to the table.

Since a TableFunction is a function, you can evaluate it at an x-value by calling getVal(double[]), where the parameter is an array of length 1. For convenience, getVal(double) is also defined. You can call derivative(1) to get the derivative function.

A TableFuntionGraph is just a Drawable object that can display a TableFunction as a graph. The points of the Table are shown as disks that can be dragged up and down to modify the function. You can turn off this interactivity by calling setInteractive(false). You can arrange for a Computable to be called when the user modifies the graph by calling the setOnDrag(Computable) method. Since a Controller is a Computable, you can pass a Controller to this method. The compute() method of the Computable will be called repeatedly as the user drags the point. The TableFunction to be graphed can be specified in a constructor. It can be set or changed later by calling the setFunction(TableFunction) method. The color of the graph, which is magenta by default, can be set by calling the setColor(Color) method.

class TableFunctionInput

A TableFunctionInput is a rather large panel that lets the user create and edit TableFunctions. I haven't used this class in any of my examples. See the JavaDoc documentation for information.


[ main | data | awt | draw ]