-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathDataFunction.java
More file actions
336 lines (305 loc) · 10.2 KB
/
DataFunction.java
File metadata and controls
336 lines (305 loc) · 10.2 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.opensourcephysics.controls.XML;
import org.opensourcephysics.controls.XMLControl;
import org.opensourcephysics.controls.XMLLoader;
import org.opensourcephysics.numerics.ParsedMultiVarFunction;
import org.opensourcephysics.numerics.ParserException;
import org.opensourcephysics.tools.ToolsRes;
/**
* This is a dataset whose values are determined by a multivariable function
* evaluated with input constants and linked datasets.
*
* @author Douglas Brown
*/
public class DataFunction extends Dataset {
// instance fields
DatasetManager inputData;
ParsedMultiVarFunction function;
String functionString;
String inputString; // recent attempted function string, successful or not
int varCount;
ArrayList<double[]> data = new ArrayList<double[]>();
/**
* Constructs a DataFunction for the specified input data.
*
* @param input the input data
*/
public DataFunction(DatasetManager input) {
inputData = input;
String name = ToolsRes.getString("DataFunction.DefaultName"); //$NON-NLS-1$
setXYColumnNames(input.getDataset(0).getXColumnName(), name);
setXColumnVisible(false);
setExpression(""); //$NON-NLS-1$
}
/**
* Constructs a DataFunction for the specified input data, name and expression.
*
* @param input the input data
* @param name
* @param expression
*/
public DataFunction(DatasetManager input, String name, String expression) {
inputData = input;
setXYColumnNames(input.getDataset(0).getXColumnName(), name);
setXColumnVisible(false);
setExpression(expression);
}
/**
* Sets the expression.
*
* @param e the expression string
*/
public void setExpression(String e) {
// set the variable count for refresh purposes
varCount = getVarCount();
try {
function = new ParsedMultiVarFunction(e, getVarNames());
functionString = e;
inputString = e;
refreshFunctionData();
} catch(ParserException ex) {
setExpression("0"); //$NON-NLS-1$
inputString = e; // unsuccessful string
refreshFunctionData();
}
}
/**
* Gets the expression.
*
* @return the expression string
*/
public String getExpression() {
return functionString;
}
/**
* Sets the y-column name (ie the function name).
*
* @param name the name
*/
public void setYColumnName(String name) {
if((name==null)||name.equals("")) { //$NON-NLS-1$
return;
}
setXYColumnNames(getXColumnName(), name);
}
/**
* Gets the current input string. If the last attempt to set the function
* was unsuccessful, this is different from the function string.
*
* @return the input string
*/
public String getInputString() {
return inputString;
}
/**
* Refreshes the data points.
*/
public void refreshFunctionData() {
super.clear();
if(function==null) {
return;
}
// watch for change in inputs
if(varCount!=getVarCount()) {
setExpression(inputString);
return;
}
double[][] data = getFunctionData();
if(data.length==0) {
return;
}
double[] fData = new double[data.length];
int len = data[0].length;
for(int n = 0; n<len; n++) { // number of data values for each variable
for(int i = 0; i<data.length; i++) { // number of variables
if(n<data[i].length) {
fData[i] = data[i][n];
} else {
fData[i] = Double.NaN;
}
}
double val = Double.NaN;
if(!"0".equals(functionString)||"0".equals(inputString)) { //$NON-NLS-1$ //$NON-NLS-2$
val = function.evaluate(fData);
if (function.evaluatedToNaN()) {
val = Double.NaN;
}
// String[] names = getVarNames();
// for(int i = 0; i<names.length; i++) {
// if(expressionContainsName(names[i]) && Double.isNaN(fData[i])) {
// val = Double.NaN;
// }
// }
}
super.append(fData[0], val);
}
}
/**
* @return the name of this DataFunction
*/
public String toString() {
return getYColumnName();
}
/**
* Overrides Dataset methods. DatasetFunction manages its own data.
*/
public void append(double x, double y) {
/** empty block */
}
public void append(double x, double y, double dx, double dy) {
/** empty block */
}
public void append(double[] x, double[] y) {
/** empty block */
}
public void append(double[] x, double[] y, double[] dx, double[] dy) {
/** empty block */
}
public void clear() {
/** empty block */
}
//______________________________ private methods ___________________________
// returns arrays of values
private double[][] getFunctionData() {
int length = 0;
data.clear();
Iterator<Dataset> it = inputData.getDatasets().iterator();
while(it.hasNext()) {
Dataset dataset = it.next();
if(dataset==this) {
continue;
}
// add linked variable (x-column) first
if(data.isEmpty()) {
double[] xPoints = dataset.getXPoints();
length = xPoints.length;
data.add(xPoints);
}
// add y-columns
data.add(dataset.getYPoints());
}
String[] names = inputData.getConstantNames();
for(String next: names) {
double[] points = new double[length];
double val = inputData.getConstantValue(next);
for(int i = 0; i<length; i++) {
points[i] = val;
}
data.add(points);
}
return data.toArray(new double[0][0]);
}
private int getVarCount() {
ArrayList<Dataset> list = inputData.getDatasets();
int count = list.contains(this) ? list.size() : list.size()+1;
return count+inputData.getConstantNames().length;
// return count+inputData.getProperties().size();
}
private String[] getVarNames() {
List<String> names = new ArrayList<String>();
Iterator<Dataset> it = inputData.getDatasets().iterator();
while(it.hasNext()) {
Dataset dataset = it.next();
if(dataset==this) {
continue;
}
String name = null;
if(names.isEmpty()) {
name = TeXParser.removeSubscripting(dataset.getXColumnName());
names.add(name);
}
name = TeXParser.removeSubscripting(dataset.getYColumnName());
names.add(name);
}
for (String name: inputData.getConstantNames()) {
names.add(name);
}
// names.addAll(inputData.getProperties().keySet());
return names.toArray(new String[0]);
}
/**
* Returns the XML.ObjectLoader for this class.
*
* @return the object loader
*/
public static XML.ObjectLoader getLoader() {
return new Loader();
}
/**
* A class to save and load DataFunction data in an XMLControl.
*/
protected static class Loader extends XMLLoader {
public void saveObject(XMLControl control, Object obj) {
DataFunction data = (DataFunction) obj;
control.setValue("function_name", data.getYColumnName()); //$NON-NLS-1$
control.setValue("function", data.getInputString()); //$NON-NLS-1$
// save Dataset properties for loading into Datasets
XML.getLoader(Dataset.class).saveObject(control, obj);
}
public Object createObject(XMLControl control) {
// must be instantiated with DatasetManager
return null;
}
public Object loadObject(XMLControl control, Object obj) {
DataFunction data = (DataFunction) obj;
data.setYColumnName(control.getString("function_name")); //$NON-NLS-1$
data.setExpression(control.getString("function")); //$NON-NLS-1$
data.setID(control.getInt("datasetID")); //$NON-NLS-1$
// load Dataset display properties
if(control.getPropertyNames().contains("marker_shape")) { //$NON-NLS-1$
data.setMarkerShape(control.getInt("marker_shape")); //$NON-NLS-1$
}
if(control.getPropertyNames().contains("marker_size")) { //$NON-NLS-1$
data.setMarkerSize(control.getInt("marker_size")); //$NON-NLS-1$
}
data.setSorted(control.getBoolean("sorted")); //$NON-NLS-1$
data.setConnected(control.getBoolean("connected")); //$NON-NLS-1$
Color color = (Color) control.getObject("line_color"); //$NON-NLS-1$
if(color!=null) {
data.setLineColor(color);
}
Color fill = (Color) control.getObject("fill_color"); //$NON-NLS-1$
color = (Color) control.getObject("edge_color"); //$NON-NLS-1$
if(fill!=null) {
if(color!=null) {
data.setMarkerColor(fill, color);
} else {
data.setMarkerColor(fill);
}
}
return obj;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/