01: /*************************************************************************
02: * *
03: * 1) This source code file, in unmodified form, and compiled classes *
04: * derived from it can be used and distributed without restriction, *
05: * including for commercial use. (Attribution is not required *
06: * but is appreciated.) *
07: * *
08: * 2) Modified versions of this file can be made and distributed *
09: * provided: the modified versions are put into a Java package *
10: * different from the original package, edu.hws; modified *
11: * versions are distributed under the same terms as the original; *
12: * and the modifications are documented in comments. (Modification *
13: * here does not include simply making subclasses that belong to *
14: * a package other than edu.hws, which can be done without any *
15: * restriction.) *
16: * *
17: * David J. Eck *
18: * Department of Mathematics and Computer Science *
19: * Hobart and William Smith Colleges *
20: * Geneva, New York 14456, USA *
21: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
22: * *
23: *************************************************************************/package edu.hws.jcm.data;
24:
25: /**
26: * An object of type Cases stores a list of "case values" that is generated
27: * while an expression is being evaluated using the routine Expression.getValuesWithCases().
28: * This information can be used as a heuristic (i.e. a fudge) to help detect
29: * a possible discontinuity between two evaluations of the expression. Suppose
30: * that the expression is evaluated twice, with some change of variable values
31: * between the two evaluations. If the variables' values are not changed too much,
32: * and if the Cases objects generated by the two evaluations are equal (as determined
33: * by the "equals" method defined in this class), then it is likely that
34: * there is no discontinuity. (However, this is not perfect. The discontinuity
35: * in 1/x^2 won't be detected since the case value generated by 1/f(x) only
36: * checks the sign of f(x), and the denominator of 1/x^2 is positive on both
37: * sides of x=0. If you want to be more paranoid, check both the expression
38: * and its derivative.) (I really don't like this very much, but it can be used to draw
39: * pretty good graphs in general.)
40: */
41: public class Cases {
42: private int[] cases = new int[1]; // Array of values that have been added with addCase(value).
43: private int caseCt; // Number of items stored in cases array.
44:
45: /**
46: * Remove all the cases that have been added with addCase().
47: * This makes it possible to reuse this object in another
48: * call to Expression.getValueWithCases().
49: */
50: public void clear() {
51: caseCt = 0;
52: }
53:
54: /**
55: * Add a new case value to the list stored in this object.
56: */
57: public void addCase(int value) {
58: if (caseCt == cases.length) {
59: int[] temp = new int[2 * caseCt];
60: System.arraycopy(cases, 0, temp, 0, caseCt);
61: cases = temp;
62: }
63: cases[caseCt++] = value;
64: }
65:
66: /**
67: * Test whether c contains exactly the same list of case
68: * values as this Cases object does.
69: */
70: public boolean equals(Cases c) {
71: if (c.caseCt != caseCt)
72: return false;
73: for (int i = 0; i < caseCt; i++)
74: if (c.cases[i] != cases[i])
75: return false;
76: return true;
77: }
78:
79: } // end class Cases
|