Subversion Repositories distributed

Rev

Rev 48 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. package de.viathinksoft.marschall.raumplan.formula;
  2.  
  3. public class FormulaProbe {
  4.  
  5.         private static boolean nearlyEqual(double a, double b) {
  6.                 return Math.abs(Math.abs(a) - Math.abs(b)) < 0.0000000000001;
  7.         }
  8.  
  9.         private static double pow(double x, double y) {
  10.                 return Math.pow(x, y);
  11.         }
  12.  
  13.         private static double sqrt(double x) {
  14.                 return Math.pow(x, 1.0 / 2.0);
  15.         }
  16.  
  17.         private static double sqrt3(double x) {
  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.  
  35.         public static double w2(double b, double g) throws InvalidValException {
  36.                 if (nearlyEqual(b, b_star())) {
  37.                         System.out.println("FATAL: w2(b*) is Lambda!");
  38.                         throw new InvalidValException();
  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);
  44.                         throw new InvalidValException();
  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.  
  51.         public static double K2(double b, double g, double w)
  52.                         throws SelfTestException {
  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);
  66.                         throw new SelfTestException();
  67.                 }
  68.  
  69.                 return res;
  70.  
  71.         }
  72.  
  73.         // Seite 2
  74.  
  75.         public static double X(double b, double g) throws InvalidValException {
  76.                 if (nearlyEqual(b, b_star())) {
  77.                         System.out.println("FATAL: X(b,g) may not have b*");
  78.                         throw new InvalidValException();
  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.  
  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 {
  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");
  105.                         throw new SelfTestException();
  106.                 }
  107.  
  108.                 if (!dec1) {
  109.                         System.out.println("w23 ist nicht definiert für b=" + b + "; g="
  110.                                         + g);
  111.                         throw new InvalidValException();
  112.                 } else {
  113.                         return w3(b, g); // == w2(b,g)
  114.                 }
  115.         }
  116.  
  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)));
  129.         }
  130.  
  131.         public static double K3(double b, double g, double w)
  132.                         throws SelfTestException {
  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);
  147.                         throw new SelfTestException();
  148.                 }
  149.  
  150.                 return res;
  151.         }
  152.  
  153.         public static double w_star() throws SelfTestException, InvalidValException {
  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))
  157.                                                 - sqrt3(0.5 - sqrt(31.0 / 108.0)) - pow(
  158.                                                 sqrt3(0.5 + sqrt(31.0 / 108.0))
  159.                                                                 + sqrt3(0.5 - sqrt(31.0 / 108.0)), 4)));
  160.                 double res2 = w3(b_star(), g_star());
  161.  
  162.                 if (!nearlyEqual(res, res2)) {
  163.                         System.out.println("Self test for w_star() failed!");
  164.                         System.out.println(res);
  165.                         System.out.println(res2);
  166.                         throw new SelfTestException();
  167.                 }
  168.  
  169.                 return res;
  170.         }
  171.  
  172. }
  173.