Rev 45 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
44 | daniel-mar | 1 | package de.viathinksoft.marschall.raumplan.formula; |
2 | |||
3 | public class FormulaProbe { |
||
4 | |||
48 | daniel-mar | 5 | protected static double round(double value, int decimalPlace) { |
6 | if (value < 0) { |
||
7 | // positive value only. |
||
8 | return -round(-value, decimalPlace); |
||
9 | } |
||
45 | daniel-mar | 10 | |
48 | daniel-mar | 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; |
||
45 | daniel-mar | 20 | } |
21 | |||
48 | daniel-mar | 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); |
||
45 | daniel-mar | 28 | } |
29 | |||
48 | daniel-mar | 30 | protected static boolean nearlyEqual(double a, double b) { |
31 | return Math.abs(Math.abs(a) - Math.abs(b)) < 0.0000000000001; |
||
32 | } |
||
33 | |||
34 | protected static double pow(double x, double y) { |
||
35 | return Math.pow(x, y); |
||
36 | } |
||
37 | |||
38 | protected static double sqrt(double x) { |
||
39 | return Math.pow(x, 1.0 / 2.0); |
||
40 | } |
||
41 | |||
42 | protected static double sqrt3(double x) { |
||
43 | if (x >= 0) |
||
44 | return Math.pow(x, 1.0 / 3.0); |
||
45 | else |
||
46 | return -Math.pow(-x, 1.0 / 3.0); |
||
47 | } |
||
48 | |||
49 | // Seite 1 |
||
50 | |||
51 | public static double g_star() { |
||
52 | return (double) (3 - sqrt(5)) / 2; |
||
53 | } |
||
54 | |||
55 | public static double b_star() { |
||
56 | return sqrt3(0.5 + sqrt(31.0 / 108.0)) |
||
57 | + sqrt3(0.5 - sqrt(31.0 / 108.0)); |
||
58 | } |
||
59 | |||
60 | public static double w2(double b, double g) { |
||
61 | if (nearlyEqual(b, b_star())) { |
||
62 | System.out.println("FATAL: w2(b*) is Lambda!"); |
||
63 | return Double.NaN; |
||
64 | } |
||
65 | |||
66 | if (!((g <= g_star()) && (b > b_star()) || (g >= g_star()) |
||
67 | && (b < b_star()))) { |
||
68 | System.out.println("w ist nicht definiert für b=" + b + "; g=" + g); |
||
69 | return Double.NaN; |
||
70 | } |
||
71 | |||
72 | return (double) ((1 - b) * (g + 1) * (pow(g, 2) - 3 * g + 1)) |
||
73 | / (2 * (1 - g) * (pow(b, 3) + b - 1)); |
||
74 | } |
||
75 | |||
76 | public static double K2(double b, double g, double w) { |
||
77 | // Vor Umstellung |
||
78 | double res = (double) (3 - g) / (1 - g) - g + pow(g, 2) - 4 + 2 * w |
||
79 | * (b / (1 - b) - b - pow(b, 2) - 1); |
||
80 | |||
81 | // Nach Umstellen |
||
82 | double res2 = (double) (-(1 - b) * (g + 1) * (pow(g, 2) - 3 * g + 1) + 2 |
||
83 | * w * (1 - g) * (pow(b, 3) + b - 1)) |
||
84 | / ((1 - b) * (1 - g)); |
||
85 | |||
86 | if (!nearlyEqual(res, res2)) { |
||
87 | System.out.println("Fatal in K2"); |
||
88 | System.out.println(res); |
||
89 | System.out.println(res2); |
||
90 | System.exit(1); |
||
91 | } |
||
92 | |||
93 | return res; |
||
94 | |||
95 | } |
||
96 | |||
97 | // Seite 2 |
||
98 | |||
99 | public static double X(double b, double g) { |
||
100 | if (nearlyEqual(b, b_star())) { |
||
101 | System.out.println("FATAL: X(b,g) may not have b*"); |
||
102 | return Double.NaN; |
||
103 | } |
||
104 | |||
105 | return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4)) - 2 |
||
106 | * pow(g, 3) * (pow(b, 3) + b - 1); |
||
107 | } |
||
108 | |||
109 | public static double X_star(double b, double g) { |
||
110 | return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4)) |
||
111 | * (pow(b, 3) + b - 1) - 2 * pow(g, 3) |
||
112 | * pow((pow(b, 3) + b - 1), 2); |
||
113 | } |
||
114 | |||
115 | public static double w23(double b, double g) { |
||
116 | if (nearlyEqual(b, b_star())) |
||
117 | return w3(b, g); |
||
118 | |||
119 | boolean dec1 = nearlyEqual(X(b, g), 0.0); |
||
120 | boolean dec2 = nearlyEqual(w3(b, g), w2(b, g)); |
||
121 | |||
122 | if (dec1 != dec2) { |
||
123 | System.out.println("FATAL: X(b,g) ist falsch"); |
||
124 | System.exit(1); |
||
125 | return Double.NaN; |
||
126 | } |
||
127 | |||
128 | if (!dec1) { |
||
129 | System.out.println("w23 ist nicht definiert für b=" + b + "; g=" |
||
130 | + g); |
||
131 | return Double.NaN; |
||
132 | } else { |
||
133 | return w3(b, g); // == w2(b,g) |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public static double w3(double b, double g) { |
||
138 | return (double) (pow(g, 3) * (1 - b)) / ((1 - g) * (1 - b + pow(b, 4))); |
||
139 | } |
||
140 | |||
141 | public static double K3(double b, double g, double w) { |
||
142 | // Vor Umstellung |
||
143 | double res = (1.0 / (1 - g)) - g - pow(g, 2) - 1 - w + b * w |
||
144 | * ((1.0 / (1 - b)) - b - pow(b, 2) - 1); |
||
145 | |||
146 | // Nach Umstellen |
||
147 | double res2 = (double) ((1 - b) * (pow(g, 3)) - w * (1 - b) * (1 - g) + pow( |
||
148 | b, 4) |
||
149 | * w * (1 - g)) |
||
150 | / ((1 - b) * (1 - g)); |
||
151 | |||
152 | if (!nearlyEqual(res, res2)) { |
||
153 | System.out.println("Fatal in K3"); |
||
154 | System.out.println(res); |
||
155 | System.out.println(res2); |
||
156 | System.exit(1); |
||
157 | } |
||
158 | |||
159 | return res; |
||
160 | } |
||
161 | |||
162 | public static double w_star() { |
||
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)))) |
||
164 | / (8 * (1 - (double) (3 - sqrt(5)) / 2) * (1 |
||
165 | - sqrt3(0.5 + sqrt(31.0 / 108.0)) |
||
166 | - sqrt3(0.5 - sqrt(31.0 / 108.0)) + pow( |
||
167 | sqrt3(0.5 + sqrt(31.0 / 108.0)) |
||
168 | + sqrt3(0.5 - sqrt(31.0 / 108.0)), 4))); |
||
169 | |||
170 | if (!nearlyEqual(res, w3(b_star(), g_star()))) { |
||
171 | System.out.println("Self test for w_star() failed!"); |
||
172 | System.exit(1); |
||
173 | return Double.NaN; |
||
174 | } |
||
175 | |||
176 | return res; |
||
177 | } |
||
178 | |||
45 | daniel-mar | 179 | /** |
44 | daniel-mar | 180 | * @param args |
181 | */ |
||
182 | public static void main(String[] args) { |
||
48 | daniel-mar | 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? |
||
44 | daniel-mar | 192 | |
48 | daniel-mar | 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()))); |
||
45 | daniel-mar | 218 | |
48 | daniel-mar | 219 | // w23test(0.2, 0.7); |
220 | w23test(b_star(), g_star()); |
||
44 | daniel-mar | 221 | } |
48 | daniel-mar | 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 | |||
44 | daniel-mar | 232 | } |