JCM Version 1.0
This page discusses the more useful Java classes in the package edu.hws.jcm.data, which is part of the JCM project. The full API can be found in the JavaDoc documentation.
A Parser can parse strings that represent mathematical expressions. The principal method in the class is parse(String), which returns the object of type Expression that corresponds to the mathematical expression specified in the string. This method can throw a ParseError if there is a syntax error in the string. The method add(MathObject) is also essential. It adds things like Variables and Constants to the parser, so that they can be used in expressions.
A parser can be configured with several options, which can be specified when it is contstructed or added later by calling the addOptions(int) method. (See the JavaDoc documentation for a list of possible options.) By default, the BOOLEANS and STANDARD_FUNCTIONS options are on, which makes it possible to use conditional expressions such as "(x<l0)?-x:x" and standard functions such as sin() and cos() in expressions. Other than that, the most useful options are OPTIONAL_STARS, which makes the * operater optional in multiplications, and FACTORIAL, which allows the factorial operator as in "n!". Options can be or'ed together. For example:
parser = new Parser(STANDARD_FUNCTIONS | OPTIONAL_STARS | OPTIONAL_PARENS);
Every parser understands the mathematical operators +, -, *, /, ^, and **. (Both ^ and ** stand for exponentiation.) It also understands "pi" and "e" as names for the mathematical constants.
A Value object has a real number value, which can be retrieved by calling its getVal() method. This is the only method defined in the Value interface. Value objects are used in many places in JCM where a numerical value is needed. For example, the minimum and maximum allowed values on a VariableSlider are set as Value objects.
interface Expression and class ExpressionProgram
An Expression object is a type of Value that represents a mathematical expression. Since it is a value object, it has a getVal() method that can be used to retrieve its value. This value can, of course, change when the values of variables or other objects in the expression change. The Expression class includes the method derivative(Variable) which computes the derivative of the expression with respect to the given Variable and returns it as another Expression object.
An ExpressionProgram is a concrete implementation of the Expression interface. When a Parser parses a string, it actually produces an ExpressionProgram object. The derivative(Variable) method in this class actually returns an ExpressionProgram object. Ordinarily, you don't need to worry about the ExpressionProgram class. You just need to think in terms of Expressions. However, you should know that when used with an ExpressionProgram, the toString() method returns a valid string representation of the expression, which could be passed to a Parser to reconstruct the expression. If the ExpressionProgram was actually produced by a Parser, then the original string that was parsed to produce the expression is returned. Otherwise, a not-necessarily-very-pretty string representation is computed. This at least gives you a way to get a string representation of the derivative of an expression with respect to some Variable x: expression.derivative(x).toString()
A Constant object is just an Expression (and therefore a Value) that has a constant value. It can also have a name, and if it has a name it can be added to a Parser and used in expressions. (The standard Constant named "pi" is handled in this way.) Constants are most useful in contexts where a Value object is required, but you just want to specify a number. For example, "new Constant(5)" is a Value object with constant value 5.
Variable is defined as a subclass of Constant, with the simple addition of a setVal(double) method that can be used to change the value of a Variable. Variables can, of course, be added to Parsers. If you want to work with expressions that use a variable named "x":, for example, you would typically say
Variable xVar = new Variable("x"); Parser parser = new Parser(); parser.add(xVar);
Later, if you have an expression, exp, that might depend on the variable x, you could get the derivative of that expression with respect to x by saying exp.derivative(xVar).
A function computes an output number from one or more input numbers. The Function interface defines the general behavior of functions in JCM. The getArity() method returns the number of arguments in the function. (For example, f(x) has arity 1 while g(s,t) has arity 2.) The method getVal(double) returns the output value of the function for the input values specified in the array. The length of the array should be equal to the arity of the function. To find the derivative of a function with respect to one of its arguments, call derivative(int). This method returns a Function. The int parameter should be at least 1 and no greater than the arity of the function. If the function f is a function of one argument, then f.derivative(1) computes its derivative function.
A SimpleFunction is a Function that is constructed from an Expression and a Variable or an array of Variables. All its methods are defined in the Function interface. For example, if exp is an expression which might depend on the variable xVar, then "new SimpleFunction(exp,xVar)" is the Function that you get by considering exp as a function of xVar. That is, the value of the function at an input number x is computed by assigning x to be the value of the variable xVar and then computing the value of exp.
The ValueMath class implements the Value interface. It is a convenience method for making Value objects that depend in simple ways on other Values. For example, if v1 and v2 are Values, then "new ValueMath(v1,v2,'+') represents the sum of v1 and v2 as a Value. You can apply the operator +, -, *, /, or ^ to two Values.
Another constructor takes a Function of one variable and a Value as inputs, and produces something that represents the result of evaluating the Function at the specified Value.