forked from yannrichet/jmathplot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBase.java
More file actions
265 lines (223 loc) · 6.95 KB
/
Base.java
File metadata and controls
265 lines (223 loc) · 6.95 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
package org.math.plot.plotObjects;
import org.math.plot.utils.FastMath;
/**
* BSD License
*
* @author Yann RICHET
* Changed on 6/13/2014 by Jerry Dietrich
* Contact info ballooninternet@cox.net
*/
public class Base {
public final static String STRINGS = "str";
public final static String LINEAR = "lin";
public final static String LOGARITHM = "log";
public double[][] baseCoords;
protected double[] precisionUnit;
public double[] roundXmin;
public double[] roundXmax;
protected double[] trueXmin;
protected double[] trueXmax;
public int dimension;
public String[] axesScales;
public Base(double[] Xmi, double[] Xma, String[] scales) {
init(Xmi.length);
trueXmin = Xmi;
trueXmax = Xma;
dimension = trueXmin.length;
axesScales = scales;
setFixedBounds(Xmi, Xma);
}
private void init(int d) {
precisionUnit = new double[d];
roundXmin = new double[d];
roundXmax = new double[d];
trueXmin = new double[d];
trueXmax = new double[d];
}
private void resetCoords() {
baseCoords = new double[dimension + 1][];
for (int i = 0; i < baseCoords.length; i++) {
baseCoords[i] = (double[]) (roundXmin.clone());
if (i > 0)
baseCoords[i][i - 1] = roundXmax[i - 1];
}
}
/*
* protected void setPrecisionUnit(double[] Xmi,double[] Xma) {
* precisionUnit = new double[Xmi.length]; for (int i = 0; i <
* precisionUnit.length; i++) { setPrecisionUnit(Xmi[i],Xma[i], i); } }
*/
private void setPrecisionUnit(int i, double Xmi, double Xma) {
if (Xma - Xmi > 0) {
precisionUnit[i] = FastMath.pow(10, FastMath.floor(FastMath.log(Xma - Xmi) / FastMath.log(10)));
} else {
precisionUnit[i] = 1;
}
// System.out.println("precisionUnit["+i+"] = "+precisionUnit[i]);
}
public void setAxesScales(String[] scales) {
axesScales = scales;
setRoundBounds(trueXmin, trueXmax);
resetCoords();
}
public void setAxesScales(int i, String scale) {
axesScales[i] = scale;
setRoundBounds(trueXmin, trueXmax);
resetCoords();
}
public double[][] getCoords() {
return baseCoords;
}
/*
* public int getDimension() { return dimension; }
*/
public String[] getAxesScales() {
return axesScales;
}
public String getAxeScale(int i) {
return axesScales[i];
}
public double[] getMinBounds() {
return roundXmin;
}
public double[] getMaxBounds() {
return roundXmax;
}
public double[] getPrecisionUnit() {
return precisionUnit;
}
// ///////////////////////////////////////////
// ////// bounds methods /////////////////////
// ///////////////////////////////////////////
private void setBounds(int i, double Xmi, double Xma) {
if ((Xmi <= 0) && (axesScales[i].equalsIgnoreCase(LOGARITHM))) {
Xmi = 1.0;
}
if ((Xma <= 0) && (axesScales[i].equalsIgnoreCase(LOGARITHM))) {
Xma = 1.0;
}
if (Xmi == Xma) {
Xmi = Xma - 1;
}
if (Xmi > Xma) {
throw new IllegalArgumentException("Error while bounding dimension " + (i + 1) + " : min " + Xmi + " must be < to max " + Xma);
}
roundXmin[i] = Xmi;
roundXmax[i] = Xma;
resetCoords();
}
/*
* private void setBounds(double[] Xmi, double[] Xma) { for (int i = 0; i <
* Xmi.length; i++) { setBounds(i, Xmi[i], Xma[i]); } }
*/
public void setFixedBounds(int i, double Xmi, double Xma) {
setPrecisionUnit(i, Xmi, Xma);
setBounds(i, Xmi, Xma);
}
public void setFixedBounds(double[] Xmi, double[] Xma) {
for (int i = 0; i < Xmi.length; i++) {
setFixedBounds(i, Xmi[i], Xma[i]);
}
}
public void roundBounds(int i) {
setPrecisionUnit(i, trueXmin[i], trueXmax[i]);
if (axesScales[i].equalsIgnoreCase(LOGARITHM)) {
setBounds(i, FastMath.pow(10, FastMath.floor(FastMath.log(trueXmin[i]) / FastMath.log(10))), FastMath.pow(10, FastMath.ceil(FastMath.log(trueXmax[i]) / FastMath.log(10))));
} else if (axesScales[i].equalsIgnoreCase(LINEAR)||axesScales[i].equalsIgnoreCase(STRINGS)) {
if (roundXmin[i] <= roundXmax[i])
setBounds(i, precisionUnit[i] * (FastMath.floor(trueXmin[i] / precisionUnit[i])), precisionUnit[i] * (FastMath.ceil(trueXmax[i] / precisionUnit[i])));
else
setBounds(i, precisionUnit[i] * (FastMath.ceil(trueXmax[i] / precisionUnit[i])), precisionUnit[i] * (FastMath.floor(trueXmin[i] / precisionUnit[i])));
}
/*
* System.out.println("precisionUnit[" + i + "]=" + precisionUnit[i]);
* System.out.println("trueXmin["+i+"]="+trueXmin[i]);
* System.out.println("trueXmax["+i+"]="+trueXmax[i]);
* System.out.println("roundXmin["+i+"]="+roundXmin[i]);
* System.out.println("roundXmax["+i+"]="+roundXmax[i]);
*
* System.out.println("Xmi=" + trueXmin[i] + " Xma=" + trueXmax[i]);
* System.out.println( " -> precisionUnit[i] * (Math.floor(Xmi /
* precisionUnit[i]))=" + precisionUnit[i] * (Math.floor(trueXmin[i] /
* precisionUnit[i]))); System.out.println( " -> precisionUnit[i] *
* (Math.ceil(Xma / precisionUnit[i]))=" + precisionUnit[i] *
* (ceil(trueXmax[i] / precisionUnit[i])));
*/
}
public void setRoundBounds(int i, double Xmi, double Xma) {
trueXmin[i] = Xmi;
trueXmax[i] = Xma;
roundBounds(i);
}
public void setRoundBounds(double[] Xmi, double[] Xma) {
for (int i = 0; i < Xmi.length; i++) {
trueXmin[i] = Xmi[i];
trueXmax[i] = Xma[i];
roundBounds(i);
}
}
public boolean includeInBounds(int dim, double XY) {
boolean changed = false;
for (int i = 0; i < roundXmin.length; i++) {
if (i == dim) {
if (XY < trueXmin[i]) {
trueXmin[i] = XY;
changed = true;
}
}
}
for (int i = 0; i < roundXmax.length; i++) {
if (i == dim) {
if (XY > trueXmax[i]) {
trueXmax[i] = XY;
changed = true;
}
}
}
if (changed) {
roundBounds(dim);
}
return changed;
}
public boolean includeInBounds(double[] XY) {
boolean changed = false;
for (int i = 0; i < roundXmin.length; i++) {
if (XY[i] < trueXmin[i]) {
trueXmin[i] = XY[i];
changed = true;
}
}
for (int i = 0; i < roundXmax.length; i++) {
if (XY[i] > trueXmax[i]) {
trueXmax[i] = XY[i];
changed = true;
}
}
if (changed) {
setRoundBounds(trueXmin, trueXmax);
}
return changed;
}
// ///////////////////////////////////////////
// ////// other public methods ///////////////
// ///////////////////////////////////////////
public boolean authorizedLogScale(int i) {
// System.out.println("Xmin[" + i + "] = " + roundXmin[i]);
if (roundXmin[i] > 0) {
return true;
} else {
return false;
}
}
public String toString() {
StringBuffer s = new StringBuffer();
for (int i = 0; i < baseCoords.length; i++) {
s.append("[");
for (int j = 0; j < baseCoords[i].length; j++)
s.append(baseCoords[i][j] + ",");
s.deleteCharAt(s.length() - 1);
s.append("]");
}
return s.toString();
}
}