Rev 48 | 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 | |||
49 | daniel-mar | 5 | private static boolean nearlyEqual(double a, double b) { |
48 | daniel-mar | 6 | return Math.abs(Math.abs(a) - Math.abs(b)) < 0.0000000000001; |
7 | } |
||
8 | |||
49 | daniel-mar | 9 | private static double pow(double x, double y) { |
48 | daniel-mar | 10 | return Math.pow(x, y); |
11 | } |
||
12 | |||
49 | daniel-mar | 13 | private static double sqrt(double x) { |
48 | daniel-mar | 14 | return Math.pow(x, 1.0 / 2.0); |
15 | } |
||
16 | |||
49 | daniel-mar | 17 | private static double sqrt3(double x) { |
48 | daniel-mar | 18 | if (x >= 0) |
19 | return Math.pow(x, 1.0 / 3.0); |
||
20 | else |
||
21 | return -Math.pow(-x, 1.0 / 3.0); |
||
22 | } |
||
23 | |||
24 | // Seite 1 |
||
25 | |||
26 | public static double g_star() { |
||
27 | return (double) (3 - sqrt(5)) / 2; |
||
28 | } |
||
29 | |||
30 | public static double b_star() { |
||
31 | return sqrt3(0.5 + sqrt(31.0 / 108.0)) |
||
32 | + sqrt3(0.5 - sqrt(31.0 / 108.0)); |
||
33 | } |
||
34 | |||
49 | daniel-mar | 35 | public static double w2(double b, double g) throws InvalidValException { |
48 | daniel-mar | 36 | if (nearlyEqual(b, b_star())) { |
37 | System.out.println("FATAL: w2(b*) is Lambda!"); |
||
49 | daniel-mar | 38 | throw new InvalidValException(); |
48 | daniel-mar | 39 | } |
40 | |||
41 | if (!((g <= g_star()) && (b > b_star()) || (g >= g_star()) |
||
42 | && (b < b_star()))) { |
||
43 | System.out.println("w ist nicht definiert für b=" + b + "; g=" + g); |
||
49 | daniel-mar | 44 | throw new InvalidValException(); |
48 | daniel-mar | 45 | } |
46 | |||
47 | return (double) ((1 - b) * (g + 1) * (pow(g, 2) - 3 * g + 1)) |
||
48 | / (2 * (1 - g) * (pow(b, 3) + b - 1)); |
||
49 | } |
||
50 | |||
49 | daniel-mar | 51 | public static double K2(double b, double g, double w) |
52 | throws SelfTestException { |
||
48 | daniel-mar | 53 | // Vor Umstellung |
54 | double res = (double) (3 - g) / (1 - g) - g + pow(g, 2) - 4 + 2 * w |
||
55 | * (b / (1 - b) - b - pow(b, 2) - 1); |
||
56 | |||
57 | // Nach Umstellen |
||
58 | double res2 = (double) (-(1 - b) * (g + 1) * (pow(g, 2) - 3 * g + 1) + 2 |
||
59 | * w * (1 - g) * (pow(b, 3) + b - 1)) |
||
60 | / ((1 - b) * (1 - g)); |
||
61 | |||
62 | if (!nearlyEqual(res, res2)) { |
||
63 | System.out.println("Fatal in K2"); |
||
64 | System.out.println(res); |
||
65 | System.out.println(res2); |
||
49 | daniel-mar | 66 | throw new SelfTestException(); |
48 | daniel-mar | 67 | } |
68 | |||
69 | return res; |
||
70 | |||
71 | } |
||
72 | |||
73 | // Seite 2 |
||
74 | |||
49 | daniel-mar | 75 | public static double X(double b, double g) throws InvalidValException { |
48 | daniel-mar | 76 | if (nearlyEqual(b, b_star())) { |
77 | System.out.println("FATAL: X(b,g) may not have b*"); |
||
49 | daniel-mar | 78 | throw new InvalidValException(); |
48 | daniel-mar | 79 | } |
80 | |||
81 | return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4)) - 2 |
||
82 | * pow(g, 3) * (pow(b, 3) + b - 1); |
||
83 | } |
||
84 | |||
85 | public static double X_star(double b, double g) { |
||
86 | return (g + 1) * (pow(g, 2) - 3 * g + 1) * (1 - b + pow(b, 4)) |
||
87 | * (pow(b, 3) + b - 1) - 2 * pow(g, 3) |
||
88 | * pow((pow(b, 3) + b - 1), 2); |
||
89 | } |
||
90 | |||
49 | daniel-mar | 91 | public static double b_star_star() { |
92 | return 0.724492; // TODO |
||
93 | } |
||
94 | |||
95 | public static double w23(double b, double g) throws SelfTestException, |
||
96 | InvalidValException { |
||
48 | daniel-mar | 97 | if (nearlyEqual(b, b_star())) |
98 | return w3(b, g); |
||
99 | |||
100 | boolean dec1 = nearlyEqual(X(b, g), 0.0); |
||
101 | boolean dec2 = nearlyEqual(w3(b, g), w2(b, g)); |
||
102 | |||
103 | if (dec1 != dec2) { |
||
104 | System.out.println("FATAL: X(b,g) ist falsch"); |
||
49 | daniel-mar | 105 | throw new SelfTestException(); |
48 | daniel-mar | 106 | } |
107 | |||
108 | if (!dec1) { |
||
109 | System.out.println("w23 ist nicht definiert für b=" + b + "; g=" |
||
110 | + g); |
||
49 | daniel-mar | 111 | throw new InvalidValException(); |
48 | daniel-mar | 112 | } else { |
113 | return w3(b, g); // == w2(b,g) |
||
114 | } |
||
115 | } |
||
116 | |||
49 | daniel-mar | 117 | public static double w3(double b, double g) throws InvalidValException { |
118 | if (nearlyEqual(b, b_star_star())) { |
||
119 | System.out.println("FATAL: w3(b**) is Lambda!"); |
||
120 | throw new InvalidValException(); |
||
121 | } |
||
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))); |
||
48 | daniel-mar | 129 | } |
130 | |||
49 | daniel-mar | 131 | public static double K3(double b, double g, double w) |
132 | throws SelfTestException { |
||
48 | daniel-mar | 133 | // Vor Umstellung |
134 | double res = (1.0 / (1 - g)) - g - pow(g, 2) - 1 - w + b * w |
||
135 | * ((1.0 / (1 - b)) - b - pow(b, 2) - 1); |
||
136 | |||
137 | // Nach Umstellen |
||
138 | double res2 = (double) ((1 - b) * (pow(g, 3)) - w * (1 - b) * (1 - g) + pow( |
||
139 | b, 4) |
||
140 | * w * (1 - g)) |
||
141 | / ((1 - b) * (1 - g)); |
||
142 | |||
143 | if (!nearlyEqual(res, res2)) { |
||
144 | System.out.println("Fatal in K3"); |
||
145 | System.out.println(res); |
||
146 | System.out.println(res2); |
||
49 | daniel-mar | 147 | throw new SelfTestException(); |
48 | daniel-mar | 148 | } |
149 | |||
150 | return res; |
||
151 | } |
||
152 | |||
49 | daniel-mar | 153 | public static double w_star() throws SelfTestException, InvalidValException { |
48 | daniel-mar | 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)))) |
155 | / (8 * (1 - (double) (3 - sqrt(5)) / 2) * (1 |
||
156 | - sqrt3(0.5 + sqrt(31.0 / 108.0)) |
||
49 | daniel-mar | 157 | - sqrt3(0.5 - sqrt(31.0 / 108.0)) - pow( |
48 | daniel-mar | 158 | sqrt3(0.5 + sqrt(31.0 / 108.0)) |
159 | + sqrt3(0.5 - sqrt(31.0 / 108.0)), 4))); |
||
49 | daniel-mar | 160 | double res2 = w3(b_star(), g_star()); |
48 | daniel-mar | 161 | |
49 | daniel-mar | 162 | if (!nearlyEqual(res, res2)) { |
48 | daniel-mar | 163 | System.out.println("Self test for w_star() failed!"); |
49 | daniel-mar | 164 | System.out.println(res); |
165 | System.out.println(res2); |
||
166 | throw new SelfTestException(); |
||
48 | daniel-mar | 167 | } |
168 | |||
169 | return res; |
||
170 | } |
||
171 | |||
44 | daniel-mar | 172 | } |