Subversion Repositories distributed

Rev

Rev 48 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 48 Rev 49
Line 1... Line 1...
1
package de.viathinksoft.marschall.raumplan.formula;
1
package de.viathinksoft.marschall.raumplan.formula;
2
 
2
 
3
public class FormulaProbe {
3
public class FormulaProbe {
4
 
4
 
5
        protected static double round(double value, int decimalPlace) {
-
 
6
                if (value < 0) {
-
 
7
                        // positive value only.
-
 
8
                        return -round(-value, decimalPlace);
-
 
9
                }
-
 
10
 
-
 
11
                double power_of_ten = 1;
-
 
12
                // floating point arithmetic can be very tricky.
-
 
13
                // that's why I introduce a "fudge factor"
-
 
14
                double fudge_factor = 0.05;
-
 
15
                while (decimalPlace-- > 0) {
-
 
16
                        power_of_ten *= 10.0d;
-
 
17
                        fudge_factor /= 10.0d;
-
 
18
                }
-
 
19
                return Math.round((value + fudge_factor) * power_of_ten) / power_of_ten;
-
 
20
        }
-
 
21
 
-
 
22
        protected static String roundu(double value) {
-
 
23
                if (Math.abs(value) < 0.000000000000001) {
-
 
24
                        // return "<0.000000000000001";
-
 
25
                        return "0";
-
 
26
                }
-
 
27
                return "" + round(value, 17);
-
 
28
        }
-
 
29
 
-
 
30
        protected static boolean nearlyEqual(double a, double b) {
5
        private static boolean nearlyEqual(double a, double b) {
31
                return Math.abs(Math.abs(a) - Math.abs(b)) < 0.0000000000001;
6
                return Math.abs(Math.abs(a) - Math.abs(b)) < 0.0000000000001;
32
        }
7
        }
33
 
8
 
34
        protected static double pow(double x, double y) {
9
        private static double pow(double x, double y) {
35
                return Math.pow(x, y);
10
                return Math.pow(x, y);
36
        }
11
        }
37
 
12
 
38
        protected static double sqrt(double x) {
13
        private static double sqrt(double x) {
39
                return Math.pow(x, 1.0 / 2.0);
14
                return Math.pow(x, 1.0 / 2.0);
40
        }
15
        }
41
 
16
 
42
        protected static double sqrt3(double x) {
17
        private static double sqrt3(double x) {
43
                if (x >= 0)
18
                if (x >= 0)
44
                        return Math.pow(x, 1.0 / 3.0);
19
                        return Math.pow(x, 1.0 / 3.0);
45
                else
20
                else
46
                        return -Math.pow(-x, 1.0 / 3.0);
21
                        return -Math.pow(-x, 1.0 / 3.0);
47
        }
22
        }
Line 55... Line 30...
55
        public static double b_star() {
30
        public static double b_star() {
56
                return sqrt3(0.5 + sqrt(31.0 / 108.0))
31
                return sqrt3(0.5 + sqrt(31.0 / 108.0))
57
                                + sqrt3(0.5 - sqrt(31.0 / 108.0));
32
                                + sqrt3(0.5 - sqrt(31.0 / 108.0));
58
        }
33
        }
59
 
34
 
60
        public static double w2(double b, double g) {
35
        public static double w2(double b, double g) throws InvalidValException {
61
                if (nearlyEqual(b, b_star())) {
36
                if (nearlyEqual(b, b_star())) {
62
                        System.out.println("FATAL: w2(b*) is Lambda!");
37
                        System.out.println("FATAL: w2(b*) is Lambda!");
63
                        return Double.NaN;
38
                        throw new InvalidValException();
64
                }
39
                }
65
 
40
 
66
                if (!((g <= g_star()) && (b > b_star()) || (g >= g_star())
41
                if (!((g <= g_star()) && (b > b_star()) || (g >= g_star())
67
                                && (b < b_star()))) {
42
                                && (b < b_star()))) {
68
                        System.out.println("w ist nicht definiert für b=" + b + "; g=" + g);
43
                        System.out.println("w ist nicht definiert für b=" + b + "; g=" + g);
69
                        return Double.NaN;
44
                        throw new InvalidValException();
70
                }
45
                }
71
 
46
 
72
                return (double) ((1 - b) * (g + 1) * (pow(g, 2) - 3 * g + 1))
47
                return (double) ((1 - b) * (g + 1) * (pow(g, 2) - 3 * g + 1))
73
                                / (2 * (1 - g) * (pow(b, 3) + b - 1));
48
                                / (2 * (1 - g) * (pow(b, 3) + b - 1));
74
        }
49
        }
75
 
50
 
76
        public static double K2(double b, double g, double w) {
51
        public static double K2(double b, double g, double w)
-
 
52
                        throws SelfTestException {
77
                // Vor Umstellung
53
                // Vor Umstellung
78
                double res = (double) (3 - g) / (1 - g) - g + pow(g, 2) - 4 + 2 * w
54
                double res = (double) (3 - g) / (1 - g) - g + pow(g, 2) - 4 + 2 * w
79
                                * (b / (1 - b) - b - pow(b, 2) - 1);
55
                                * (b / (1 - b) - b - pow(b, 2) - 1);
80
 
56
 
81
                // Nach Umstellen
57
                // Nach Umstellen
Line 85... Line 61...
85
 
61
 
86
                if (!nearlyEqual(res, res2)) {
62
                if (!nearlyEqual(res, res2)) {
87
                        System.out.println("Fatal in K2");
63
                        System.out.println("Fatal in K2");
88
                        System.out.println(res);
64
                        System.out.println(res);
89
                        System.out.println(res2);
65
                        System.out.println(res2);
90
                        System.exit(1);
66
                        throw new SelfTestException();
91
                }
67
                }
92
 
68
 
93
                return res;
69
                return res;
94
 
70
 
95
        }
71
        }
96
 
72
 
97
        // Seite 2
73
        // Seite 2
98
 
74
 
99
        public static double X(double b, double g) {
75
        public static double X(double b, double g) throws InvalidValException {
100
                if (nearlyEqual(b, b_star())) {
76
                if (nearlyEqual(b, b_star())) {
101
                        System.out.println("FATAL: X(b,g) may not have b*");
77
                        System.out.println("FATAL: X(b,g) may not have b*");
102
                        return Double.NaN;
78
                        throw new InvalidValException();
103
                }
79
                }
104
 
80
 
105
                return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4)) - 2
81
                return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4)) - 2
106
                                * pow(g, 3) * (pow(b, 3) + b - 1);
82
                                * pow(g, 3) * (pow(b, 3) + b - 1);
107
        }
83
        }
Line 110... Line 86...
110
                return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4))
86
                return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4))
111
                                * (pow(b, 3) + b - 1) - 2 * pow(g, 3)
87
                                * (pow(b, 3) + b - 1) - 2 * pow(g, 3)
112
                                * pow((pow(b, 3) + b - 1), 2);
88
                                * pow((pow(b, 3) + b - 1), 2);
113
        }
89
        }
114
 
90
 
-
 
91
        public static double b_star_star() {
-
 
92
                return 0.724492; // TODO
-
 
93
        }
-
 
94
 
115
        public static double w23(double b, double g) {
95
        public static double w23(double b, double g) throws SelfTestException,
-
 
96
                        InvalidValException {
116
                if (nearlyEqual(b, b_star()))
97
                if (nearlyEqual(b, b_star()))
117
                        return w3(b, g);
98
                        return w3(b, g);
118
 
99
 
119
                boolean dec1 = nearlyEqual(X(b, g), 0.0);
100
                boolean dec1 = nearlyEqual(X(b, g), 0.0);
120
                boolean dec2 = nearlyEqual(w3(b, g), w2(b, g));
101
                boolean dec2 = nearlyEqual(w3(b, g), w2(b, g));
121
 
102
 
122
                if (dec1 != dec2) {
103
                if (dec1 != dec2) {
123
                        System.out.println("FATAL: X(b,g) ist falsch");
104
                        System.out.println("FATAL: X(b,g) ist falsch");
124
                        System.exit(1);
105
                        throw new SelfTestException();
125
                        return Double.NaN;
-
 
126
                }
106
                }
127
 
107
 
128
                if (!dec1) {
108
                if (!dec1) {
129
                        System.out.println("w23 ist nicht definiert für b=" + b + "; g="
109
                        System.out.println("w23 ist nicht definiert für b=" + b + "; g="
130
                                        + g);
110
                                        + g);
131
                        return Double.NaN;
111
                        throw new InvalidValException();
132
                } else {
112
                } else {
133
                        return w3(b, g); // == w2(b,g)
113
                        return w3(b, g); // == w2(b,g)
134
                }
114
                }
135
        }
115
        }
136
 
116
 
137
        public static double w3(double b, double g) {
117
        public static double w3(double b, double g) throws InvalidValException {
-
 
118
                if (nearlyEqual(b, b_star_star())) {
138
                return (double) (pow(g, 3) * (1 - b)) / ((1 - g) * (1 - b + pow(b, 4)));
119
                        System.out.println("FATAL: w3(b**) is Lambda!");
-
 
120
                        throw new InvalidValException();
139
        }
121
                }
140
 
122
 
-
 
123
                if (b > b_star_star()) {
-
 
124
                        System.out.println("FATAL: w3(b B b**) < 0!");
-
 
125
                        throw new InvalidValException();
-
 
126
                }
-
 
127
 
-
 
128
                return (double) (pow(g, 3) * (1 - b)) / ((1 - g) * (1 - b - pow(b, 4)));
-
 
129
        }
-
 
130
 
141
        public static double K3(double b, double g, double w) {
131
        public static double K3(double b, double g, double w)
-
 
132
                        throws SelfTestException {
142
                // Vor Umstellung
133
                // Vor Umstellung
143
                double res = (1.0 / (1 - g)) - g - pow(g, 2) - 1 - w + b * w
134
                double res = (1.0 / (1 - g)) - g - pow(g, 2) - 1 - w + b * w
144
                                * ((1.0 / (1 - b)) - b - pow(b, 2) - 1);
135
                                * ((1.0 / (1 - b)) - b - pow(b, 2) - 1);
145
 
136
 
146
                // Nach Umstellen
137
                // Nach Umstellen
Line 151... Line 142...
151
 
142
 
152
                if (!nearlyEqual(res, res2)) {
143
                if (!nearlyEqual(res, res2)) {
153
                        System.out.println("Fatal in K3");
144
                        System.out.println("Fatal in K3");
154
                        System.out.println(res);
145
                        System.out.println(res);
155
                        System.out.println(res2);
146
                        System.out.println(res2);
156
                        System.exit(1);
147
                        throw new SelfTestException();
157
                }
148
                }
158
 
149
 
159
                return res;
150
                return res;
160
        }
151
        }
161
 
152
 
162
        public static double w_star() {
153
        public static double w_star() throws SelfTestException, InvalidValException {
163
                double res = (double) (pow(3 - sqrt(5), 3) * (1 - sqrt3(0.5 + sqrt(31.0 / 108.0)) - sqrt3(0.5 - sqrt(31.0 / 108.0))))
154
                double res = (double) (pow(3 - sqrt(5), 3) * (1 - sqrt3(0.5 + sqrt(31.0 / 108.0)) - sqrt3(0.5 - sqrt(31.0 / 108.0))))
164
                                / (8 * (1 - (double) (3 - sqrt(5)) / 2) * (1
155
                                / (8 * (1 - (double) (3 - sqrt(5)) / 2) * (1
165
                                                - sqrt3(0.5 + sqrt(31.0 / 108.0))
156
                                                - sqrt3(0.5 + sqrt(31.0 / 108.0))
166
                                                - sqrt3(0.5 - sqrt(31.0 / 108.0)) + pow(
157
                                                - sqrt3(0.5 - sqrt(31.0 / 108.0)) - pow(
167
                                                sqrt3(0.5 + sqrt(31.0 / 108.0))
158
                                                sqrt3(0.5 + sqrt(31.0 / 108.0))
168
                                                                + sqrt3(0.5 - sqrt(31.0 / 108.0)), 4)));
159
                                                                + sqrt3(0.5 - sqrt(31.0 / 108.0)), 4)));
-
 
160
                double res2 = w3(b_star(), g_star());
169
 
161
 
170
                if (!nearlyEqual(res, w3(b_star(), g_star()))) {
162
                if (!nearlyEqual(res, res2)) {
171
                        System.out.println("Self test for w_star() failed!");
163
                        System.out.println("Self test for w_star() failed!");
172
                        System.exit(1);
164
                        System.out.println(res);
173
                        return Double.NaN;
165
                        System.out.println(res2);
-
 
166
                        throw new SelfTestException();
174
                }
167
                }
175
 
168
 
176
                return res;
169
                return res;
177
        }
170
        }
178
 
171
 
179
        /**
-
 
180
         * @param args
-
 
181
         */
-
 
182
        public static void main(String[] args) {
-
 
183
                // vereinfachte K2 prüfen
-
 
184
                // vereinfachte K3 prüfen (???)
-
 
185
                // Die Formel w2 prüfen
-
 
186
                // Die Formel w3 prüfen
-
 
187
                // Die Formeln w2,3 numerisch prüfen: Ist 2D=3D=0?
-
 
188
                // für b=b*
-
 
189
                // für b!=b*
-
 
190
                // b* g* lambda prüfen: Ist 2D=3D=0
-
 
191
                // b* g* [irgendwas] prüfen: ist 2D=0 und 3D!=0?
-
 
192
 
-
 
193
                System.out.println("b* = " + roundu(b_star()));
-
 
194
                System.out.println("g* = " + roundu(g_star()));
-
 
195
                System.out.println("w* = " + roundu(w_star()));
-
 
196
                System.out.println("w23(b*, g*) = " + roundu(w23(b_star(), g_star())));
-
 
197
                System.out.println("w3(b*, g*) = " + roundu(w3(b_star(), g_star())));
-
 
198
                System.out.println("K2(.5 .5 .5) = " + roundu(K2(0.5, 0.5, 0.5)));
-
 
199
                System.out.println("K3(.5 .5 .5) = " + roundu(K3(0.5, 0.5, 0.5)));
-
 
200
                System.out.println("K2(0 .5 .375) = " + roundu(K2(0.0, 0.5, 0.375)));
-
 
201
                System.out.println("K3(0 .5 .375) = " + roundu(K3(0.0, 0.5, 0.375)));
-
 
202
                System.out.println("K2(b*, g*, 0.0) = "
-
 
203
                                + roundu(K2(b_star(), g_star(), 0.0)));
-
 
204
                System.out.println("K2(b*, g*, 0.1) = "
-
 
205
                                + roundu(K2(b_star(), g_star(), 0.1)));
-
 
206
                System.out.println("K2(b*, g*, 0.3) = "
-
 
207
                                + roundu(K2(b_star(), g_star(), 0.3)));
-
 
208
                System.out.println("K2(b*, g*, 0.5) = "
-
 
209
                                + roundu(K2(b_star(), g_star(), 0.5)));
-
 
210
                System.out.println("K2(b*, g*, 0.7) = "
-
 
211
                                + roundu(K2(b_star(), g_star(), 0.7)));
-
 
212
                System.out.println("K2(b*, g*, 0.99) = "
-
 
213
                                + roundu(K2(b_star(), g_star(), 0.99)));
-
 
214
                System.out.println("K2(b*, g*, w*) = "
-
 
215
                                + roundu(K2(b_star(), g_star(), w_star())));
-
 
216
                System.out.println("K3(b*, g*, w*) = "
-
 
217
                                + roundu(K3(b_star(), g_star(), w_star())));
-
 
218
 
-
 
219
                // w23test(0.2, 0.7);
-
 
220
                w23test(b_star(), g_star());
-
 
221
        }
-
 
222
 
-
 
223
        protected static void w23test(double b, double g) {
-
 
224
                double w = w23(b, g);
-
 
225
                System.out.println("w23(" + b + " " + g + ") = " + roundu(w));
-
 
226
                System.out.println("K2(" + b + " " + g + " " + w + ") = "
-
 
227
                                + roundu(K2(b, g, w)));
-
 
228
                System.out.println("K3(" + b + " " + g + " " + w + ") = "
-
 
229
                                + roundu(K3(b, g, w)));
-
 
230
        }
-
 
231
 
-
 
232
}
172
}