001: /*************************************************************************
002: * *
003: * 1) This source code file, in unmodified form, and compiled classes *
004: * derived from it can be used and distributed without restriction, *
005: * including for commercial use. (Attribution is not required *
006: * but is appreciated.) *
007: * *
008: * 2) Modified versions of this file can be made and distributed *
009: * provided: the modified versions are put into a Java package *
010: * different from the original package, edu.hws; modified *
011: * versions are distributed under the same terms as the original; *
012: * and the modifications are documented in comments. (Modification *
013: * here does not include simply making subclasses that belong to *
014: * a package other than edu.hws, which can be done without any *
015: * restriction.) *
016: * *
017: * David J. Eck *
018: * Department of Mathematics and Computer Science *
019: * Hobart and William Smith Colleges *
020: * Geneva, New York 14456, USA *
021: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
022: * *
023: *************************************************************************/package edu.hws.jcm.data;
024:
025: /**
026: * This class exists to associate standard functions, such as sin and abs, with
027: * their names. Note that the functions are actually implemented in the
028: * ExprsssionProgram class, where they are only represented by numerical operation
029: * codes. An object of type StandardFunction contains a name and the operation
030: * code of the associated standard function. A static routine, standardFunctionName,
031: * gives the name associated with each operation code.
032: */
033: public class StandardFunction implements MathObject {
034:
035: private String name; // The name of this standard function.
036: private int code; // The operation code for this function, from class ExpressionProgram.
037:
038: /**
039: * Create a StandardFunction object to represent the standard
040: * function with the given operation code, where opCode is one
041: * of the codes for standard functions defined in class ExpressionProgram.
042: * The name is the one associated with the opCode by the
043: * static function standardFunctionName() in this class. An error
044: * will occur if opCode is not one of the valid standard function
045: * operation codes.
046: */
047: public StandardFunction(int opCode) {
048: this (standardFunctionName(opCode), opCode);
049: }
050:
051: /**
052: * Create a StandardFunction object to represent the standard
053: * function with the given operation code, where opCode is one
054: * of the codes for stadard functions defined in class ExpressionProgram.
055: * Use the specified name for the standard function. This allows you
056: * to make alternative names, such as "log" instead of "log10". An error
057: * will occur if opCode is not one of the valid standard function
058: * operation codes.
059: */
060: public StandardFunction(String name, int opCode) {
061: setName(name);
062: code = opCode;
063: }
064:
065: /**
066: * Return the operation code for this standard function.
067: */
068: public int getOpCode() {
069: return code;
070: }
071:
072: //----------------------- Methods from interface MathObject --------------------
073:
074: /**
075: * Return the name of this StandardFunction oject.
076: */
077: public String getName() {
078: return name;
079: }
080:
081: /**
082: * Change the name of this StandardFunction. This shouldn't be done
083: * if this object is registered with a Parser.
084: */
085: public void setName(String name) {
086: this .name = name;
087: }
088:
089: //-------------------------------------------------------------------------------
090:
091: /**
092: * Return the usual name for the standard function with the
093: * specified opCode. The opcodes are defined in the ExpressionProgram class.
094: * Will throw an IllegalArgumentException if the specified oPcode is
095: * not the opcode for any standard function.
096: */
097: public static String standardFunctionName(int opCode) {
098: switch (opCode) {
099: case ExpressionProgram.SIN:
100: return "sin";
101: case ExpressionProgram.COS:
102: return "cos";
103: case ExpressionProgram.TAN:
104: return "tan";
105: case ExpressionProgram.COT:
106: return "cot";
107: case ExpressionProgram.SEC:
108: return "sec";
109: case ExpressionProgram.CSC:
110: return "csc";
111: case ExpressionProgram.ARCSIN:
112: return "arcsin";
113: case ExpressionProgram.ARCCOS:
114: return "arccos";
115: case ExpressionProgram.ARCTAN:
116: return "arctan";
117: case ExpressionProgram.ABS:
118: return "abs";
119: case ExpressionProgram.SQRT:
120: return "sqrt";
121: case ExpressionProgram.EXP:
122: return "exp";
123: case ExpressionProgram.LN:
124: return "ln";
125: case ExpressionProgram.LOG2:
126: return "log2";
127: case ExpressionProgram.LOG10:
128: return "log10";
129: case ExpressionProgram.TRUNC:
130: return "trunc";
131: case ExpressionProgram.ROUND:
132: return "round";
133: case ExpressionProgram.FLOOR:
134: return "floor";
135: case ExpressionProgram.CEILING:
136: return "ceiling";
137: case ExpressionProgram.CUBERT:
138: return "cubert";
139: default:
140: throw new IllegalArgumentException(
141: "Internal Error: Unknown standard function code.");
142: }
143: }
144:
145: }
|