-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.java
267 lines (221 loc) · 6 KB
/
Function.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Function.java
//
// Rahul Simha
// Spring 2008
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.text.*;
public class Function {
String name = "Func";
String xLabel = "x";
Vector<Point2D.Double> points = new Vector<Point2D.Double>();
double minX, maxX, minY, maxY;
public Function (String name)
{
if (name != null)
this.name = name;
}
public Function (String name, String xLabel)
{
if (name != null)
this.name = name;
if (xLabel != null)
this.xLabel = xLabel;
}
public String getName ()
{
return name;
}
public void add (double x, double y)
{
// Find right place for it.
int k = points.size();
for (int i=0; i<points.size(); i++) {
Point2D.Double p = points.get(i);
if (p.x == x) {
// Duplicate: error
System.out.println ("ERROR in Function.add(x=" + x + "," + y + "): x already exists with p.y=" + p.y);
return;
}
if (p.x > x) {
// Insert at i.
k = i;
break;
}
}
points.add (k, new Point2D.Double(x,y));
// Update max and min.
if (x < minX) {
minX = x;
}
if (x > maxX) {
maxX = x;
}
if (y < minY) {
minY = y;
}
if (y > maxY) {
maxY = y;
}
}
public double get (double x)
{
for (int i=0; i<points.size(); i++) {
Point2D.Double p = points.get(i);
if (p.x == x) {
return p.y;
}
else if (p.x > x) {
if (i == 0) {
// Already less than leftmost.
return p.y;
}
else {
// It's between p.x and previous p[i-1].x
Point2D.Double q = points.get(i-1);
// Now interpolate.
double y = q.y + (x - q.x) * (p.y - q.y) / (p.x - q.x);
// Note: divide by zero can't happen because we
// ensure x values can't be duplicated.
return y;
}
}
}
Point2D.Double p = points.get (points.size()-1);
return p.y;
}
public int getNumValues ()
{
return points.size();
}
public double maxX ()
{
return maxX;
}
public double minX ()
{
return minX;
}
public double maxY ()
{
return maxY;
}
public double minY ()
{
return minY;
}
public double distance (Function F, int numPoints)
{
// First compute the common domain.
double leftX = Math.max (minX, F.minX());
double rightX = Math.min (maxX, F.maxX());
if (leftX >= rightX) {
// Not possible.
return 0;
}
double interval = (rightX - leftX) / numPoints;
double sum = 0;
for (double x=leftX; x<=rightX; x+=interval) {
sum = sum + interval*Math.abs (get(x) - F.get(x));
}
return sum;
}
public String toString ()
{
// First, set format string for each of x and y.
if ((points == null) || (points.size() == 0)) {
return "Function " + name + " has no points";
}
String str = name + " (X:[" + format(minX) + "," + format(maxX) + "] Y:["
+ format(minY) + "," + format(maxY) + "])\n";
for (int i=0; i<points.size(); i++) {
Point2D.Double p = points.get(i);
str += " " + format(p.x) + " " + format(p.y) + "\n";
}
return str;
}
String format (double x)
{
if (x == 0) {
return "0.0";
}
double y = Math.abs(x);
int k = (int) y;
String s = "#.##";
int count = 0;
while (k < 1) {
s += "#";
y = y * 10;
k = (int) y;
count ++;
}
DecimalFormat df = null;
if (count > 8) {
df = new DecimalFormat ("#.##E0");
}
else {
df = new DecimalFormat (s);
}
return df.format (x);
}
public void show ()
{
SimplePlotPanel.makePlotFrame (points, name, 600, 480);
}
public static void show (Function ... fSet)
{
ArrayList<Vector> pointSets = new ArrayList<Vector> ();
ArrayList<String> names = new ArrayList<String> ();
for (Function F: fSet) {
pointSets.add (F.points);
names.add (F.name);
}
SimplePlotPanel.makePlotFrame (pointSets, names, fSet[0].xLabel, 600, 480);
}
public static void show (String title, Function ... fSet)
{
ArrayList<Vector> pointSets = new ArrayList<Vector> ();
ArrayList<String> names = new ArrayList<String> ();
for (Function F: fSet) {
pointSets.add (F.points);
names.add (F.name);
}
SimplePlotPanel.makePlotFrame (title, pointSets, names, fSet[0].xLabel, 600, 480);
}
public static void main (String[] argv)
{
//test1();
test2 ();
}
static void test1 ()
{
Function F = new Function ("Test");
F.add (1, 2);
F.add (2, 4);
F.add (3, 6);
System.out.println ("F(1.5)=" + F.get(1.5) + " F(2.2)=" + F.get(2.2) + " F(3.0)=" + F.get(3.0) + " F(5)=" + F.get(5.0));
F.show ();
}
static void test2 ()
{
Function F = new Function ("Test1");
for (double x=0; x<=10; x+=0.1) {
F.add (x, 3*x+5);
}
Function G = new Function ("Test2");
for (double x=0; x<=10; x+=0.1) {
G.add (x, 4*x+5);
}
Function zero = new Function ("Zero");
for (double x=0; x<=10; x+=0.1) {
zero.add (x, 0);
}
double d = F.distance (G, 100);
System.out.println ("dist=" + d);
double d2 = F.distance (G, 1000);
System.out.println ("dist2=" + d2);
double d3 = F.distance (zero, 1000);
System.out.println ("dist3=" + d3);
}
}