Advertisement
Guest User

zerawiel::Poly3.java

a guest
Jul 25th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package zerawiel;
  2.  
  3. import org.apache.commons.math3.complex.Complex;
  4.  
  5. public class Poly3 {
  6.     public Complex a,b,c,d;
  7.     public Complex x0,x1,x2;
  8.     public Complex p,q;
  9.    
  10.     /**
  11.      * Create a polynomial given coefficinents.
  12.      * @throws Exception
  13.      *
  14.      */
  15.     public Poly3(Complex a, Complex b, Complex c, Complex d) throws Exception {
  16.         this.a = a;
  17.         this.b = b;
  18.         this.c = c;
  19.         this.d = d;
  20.         if (a.equals(Complex.ZERO)) throw new Exception();
  21.     }
  22.    
  23.     // p = c/a-b*b/(3*a*a);
  24.     // q = 2*b*b*b/(27*a*a*a)+d/a-b*c/(3*a*a);
  25.     private void canonicalForm()
  26.     {
  27.         p = c.divide(a).subtract(b.multiply(b).divide(a.multiply(a).multiply(3)));
  28.         q = (b.multiply(b).multiply(b).multiply(2).divide(a.multiply(a).multiply(a).multiply(27)))
  29.             .add(d.divide(a)).subtract(b.multiply(c).divide(a.multiply(a).multiply(3)));
  30.     }
  31.    
  32.     //x = y - b/3a
  33.     private Complex ytox(Complex y)
  34.     {
  35.         return y.subtract(b.divide(a.multiply(3)));
  36.     }
  37.    
  38.     int find_m_from_eps(Complex z, Complex[] eps) throws Exception
  39.     {
  40.         for (int i=0; i<3; i++)
  41.         {
  42.             Complex r = z.subtract(eps[i]);
  43.             if (ComplexEx.getR(r)<1e-12)
  44.                 return i;
  45.         }
  46.         throw new Exception(); //it is not possible
  47.     }
  48.    
  49.     int find_l_for_m(int m) throws Exception
  50.     {
  51.         switch (m)
  52.         {
  53.         case 0: return 0;
  54.         case 1: return 2;
  55.         case 2: return 1;
  56.         default: throw new Exception(); //it is not possible           
  57.         }
  58.     }
  59.    
  60.     public void solve() throws Exception
  61.     {
  62.         canonicalForm();
  63.         if (ComplexEx.getR(p)<1e-12)
  64.         {
  65.             System.out.println("p=0");
  66.             Complex roots[] = ComplexEx.rootn(3, Complex.ZERO.subtract(q));
  67.             x0 = ytox(roots[0]);
  68.             x1 = ytox(roots[1]);
  69.             x2 = ytox(roots[2]);
  70.         }
  71.         else
  72.         {          
  73.             System.out.println("p!=0");
  74.             Complex temp = (q.multiply(q).add(p.multiply(p).multiply(p).multiply((double)4/27))).sqrt();
  75.             Complex z0 = Complex.ZERO.subtract(q).add(temp);
  76.             z0 = z0.divide(2);
  77.             Complex []roots = ComplexEx.rootn(3, z0);
  78.             Complex v0 = roots[0];
  79.             Complex []roots1 = ComplexEx.rootn(3, Complex.ZERO.subtract(q).subtract(z0));
  80.             Complex uaster = roots1[2];
  81.            
  82.             temp = v0.multiply(uaster).divide(p).multiply(-3);
  83.             Complex[] eps = new Complex[3];
  84.             eps[0] = new Complex(1,0);
  85.             eps[1] = new Complex(-1.0/2,Math.sqrt(3)/2);
  86.             eps[2] = new Complex(-1.0/2,-Math.sqrt(3)/2);
  87.             int m = find_m_from_eps(temp,eps);
  88.             int n = find_l_for_m(m);
  89.             Complex u0 = eps[n].multiply(uaster);          
  90.             Complex y0 = v0.add(u0);
  91.             Complex y1 = (eps[1].multiply(v0)).add(eps[1].multiply(eps[1]).multiply(u0));
  92.             Complex y2 = (eps[1].multiply(eps[1]).multiply(v0)).add(eps[1].multiply(u0));
  93.            
  94.             x0 = ytox(y0);
  95.             x1 = ytox(y1);
  96.             x2 = ytox(y2);
  97.         }
  98.     }
  99.    
  100.     public void print()
  101.     {
  102.         System.out.format("(%f,%f)x^3 + (%f,%f)x^2 + (%f,%f)x +(%f,%f)\n",
  103.                 a.getReal(),a.getImaginary(),
  104.                 b.getReal(),b.getImaginary(),
  105.                 c.getReal(),c.getImaginary(),
  106.                 d.getReal(),d.getImaginary());
  107.         System.out.format("p = (%f,%f), q = (%f,%f)\n",
  108.                 p.getReal(),p.getImaginary(),
  109.                 q.getReal(),q.getImaginary());
  110.         System.out.format("x0 = (%f,%f), x1 = (%f,%f), x2 = (%f,%f)\n",
  111.                 x0.getReal(),x0.getImaginary(),
  112.                 x1.getReal(),x1.getImaginary(),
  113.                 x2.getReal(),x2.getImaginary());                   
  114.  
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement