Rev 45 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 45 | Rev 48 | ||
---|---|---|---|
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 | private static final double PRECIOUS = 1000; |
5 | protected static double round(double value, int decimalPlace) { |
- | 6 | if (value < 0) { |
|
- | 7 | // positive value only. |
|
- | 8 | return -round(-value, decimalPlace); |
|
- | 9 | } |
|
6 | 10 | ||
7 | /** |
- | |
8 | * Kollissionsradius für 2D-Raum |
11 | double power_of_ten = 1; |
9 | * |
- | |
10 | * @param b |
12 | // floating point arithmetic can be very tricky. |
11 | * Korridorverkleinerungsfaktor |
13 | // that's why I introduce a "fudge factor" |
12 | * @param g |
14 | double fudge_factor = 0.05; |
13 | * Raumverkleinerungsfaktor |
15 | while (decimalPlace-- > 0) { |
14 | * @param w |
16 | power_of_ten *= 10.0d; |
15 | * Verhältnis des initialen Korridors mit dem Ursprungsraum |
- | |
16 | * @return =0 für Berührung, <0 für Abstand, > für Kollission |
17 | fudge_factor /= 10.0d; |
17 | */ |
18 | } |
18 | private static double calc_2d(double b, double g, double w) { |
- | |
19 | return (3 - g) / (1 - g) - g + Math.pow(g, 2) - 4 + 2 * w |
19 | return Math.round((value + fudge_factor) * power_of_ten) / power_of_ten; |
20 | * (b / (1 - b) - b - Math.pow(b, 2) - 1); |
- | |
21 | } |
20 | } |
22 | 21 | ||
23 | /** |
- | |
24 | * Kollissionsradius für 3D-Raum |
22 | protected static String roundu(double value) { |
25 | * |
- | |
26 | * @param b |
- | |
27 | * Korridorverkleinerungsfaktor |
23 | if (Math.abs(value) < 0.000000000000001) { |
28 | * @param g |
- | |
29 | * Raumverkleinerungsfaktor |
24 | // return "<0.000000000000001"; |
30 | * @param w |
25 | return "0"; |
31 | * Verhältnis des initialen Korridors mit dem Ursprungsraum |
- | |
32 | * @return =0 für Berührung, <0 für Abstand, > für Kollission |
- | |
33 | */ |
26 | } |
34 | private static double calc_3d(double b, double g, double w) { |
- | |
35 | return w * b * (1 / (1 - b) - 1 - b - Math.pow(b, 2)) + 1 / (1 - g) - 1 |
- | |
36 | - g - Math.pow(g, 2) - w; |
27 | return "" + round(value, 17); |
37 | } |
28 | } |
38 | 29 | ||
39 | /** |
- | |
40 | * Findet 2D=0 Punkte |
30 | protected static boolean nearlyEqual(double a, double b) { |
- | 31 | return Math.abs(Math.abs(a) - Math.abs(b)) < 0.0000000000001; |
|
41 | * |
32 | } |
42 | * @param args |
- | |
43 | */ |
33 | |
44 | public static void main(String[] args) { |
34 | protected static double pow(double x, double y) { |
45 | for (int bi = 0; bi < PRECIOUS; bi++) { |
- | |
46 | for (int gi = 0; gi < PRECIOUS; gi++) { |
- | |
47 | for (int wi = 0; wi < PRECIOUS; wi++) { |
- | |
48 | double b = (double) bi / PRECIOUS; |
35 | return Math.pow(x, y); |
- | 36 | } |
|
- | 37 | ||
49 | double g = (double) gi / PRECIOUS; |
38 | protected static double sqrt(double x) { |
50 | double w = (double) wi / PRECIOUS; |
39 | return Math.pow(x, 1.0 / 2.0); |
51 | double r2d = calc_2d(b, g, w); |
- | |
52 | double r3d = calc_3d(b, g, w); |
- | |
- | 40 | } |
|
53 | 41 | ||
54 | if ((Math.abs(r2d) == 0) /* || (Math.abs(r3d) == 0) */) { |
42 | protected static double sqrt3(double x) { |
- | 43 | if (x >= 0) |
|
55 | System.out.println("(b=" + b + ", g=" + g + ", w=" + w |
44 | return Math.pow(x, 1.0 / 3.0); |
- | 45 | else |
|
56 | + ") = " + r2d + " (2D), " + r3d + " (3D)"); |
46 | return -Math.pow(-x, 1.0 / 3.0); |
57 | } |
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; |
|
58 | } |
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); |
|
59 | } |
91 | } |
- | 92 | ||
- | 93 | return res; |
|
- | 94 | ||
60 | } |
95 | } |
61 | 96 | ||
- | 97 | // Seite 2 |
|
- | 98 | ||
- | 99 | public static double X(double b, double g) { |
|
- | 100 | if (nearlyEqual(b, b_star())) { |
|
62 | System.out.println("Beendet"); |
101 | System.out.println("FATAL: X(b,g) may not have b*"); |
- | 102 | return Double.NaN; |
|
63 | } |
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 | ||
- | 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 | ||
64 | } |
232 | } |