1 //package com.polytechnik.utils;
2 /*
3 * (C) Vladislav Malyshkin 2010
4 * This file is under GPL version 3.
5 *
6 */
7
8 /** Polynomial root.
9 * @version $Id: PolynomialRoot.java,v 1.105 2012/08/18 00:00:05 mal Exp $
10 * @author Vladislav Malyshkin mal@gromco.com
11 */
12
13 /**
14 * @test
15 * @bug 8005956
16 * @summary C2: assert(!def_outside->member(r)) failed: Use of external LRG overlaps the same LRG defined in this block
17 *
18 * @run main/timeout=300 PolynomialRoot
19 */
20
21 public class PolynomialRoot {
22
23
24 public static int findPolynomialRoots(final int n,
25 final double [] p,
26 final double [] re_root,
27 final double [] im_root)
28 {
29 if(n==4)
30 {
31 return root4(p,re_root,im_root);
32 }
33 else if(n==3)
34 {
35 return root3(p,re_root,im_root);
36 }
37 else if(n==2)
38 {
39 return root2(p,re_root,im_root);
40 }
41 else if(n==1)
42 {
43 return root1(p,re_root,im_root);
44 }
45 else
46 {
47 throw new RuntimeException("n="+n+" is not supported yet");
48 }
49 }
50
51
52
53 static final double SQRT3=Math.sqrt(3.0),SQRT2=Math.sqrt(2.0);
54
55
56 private static final boolean PRINT_DEBUG=false;
57
58 public static int root4(final double [] p,final double [] re_root,final double [] im_root)
59 {
60 if(PRINT_DEBUG) System.err.println("=====================root4:p="+java.util.Arrays.toString(p));
61 final double vs=p[4];
62 if(PRINT_DEBUG) System.err.println("p[4]="+p[4]);
63 if(!(Math.abs(vs)>EPS))
64 {
65 re_root[0]=re_root[1]=re_root[2]=re_root[3]=
66 im_root[0]=im_root[1]=im_root[2]=im_root[3]=Double.NaN;
67 return -1;
68 }
69
70 /* zsolve_quartic.c - finds the complex roots of
71 * x^4 + a x^3 + b x^2 + c x + d = 0
72 */
73 final double a=p[3]/vs,b=p[2]/vs,c=p[1]/vs,d=p[0]/vs;
74 if(PRINT_DEBUG) System.err.println("input a="+a+" b="+b+" c="+c+" d="+d);
75
76
77 final double r4 = 1.0 / 4.0;
78 final double q2 = 1.0 / 2.0, q4 = 1.0 / 4.0, q8 = 1.0 / 8.0;
79 final double q1 = 3.0 / 8.0, q3 = 3.0 / 16.0;
80 final int mt;
350 w3_re=w3_im=0;
351 }
352
353 final double h = r4 * a;
354 if(PRINT_DEBUG) System.err.println("w1_re="+w1_re+" w1_im="+w1_im+" w2_re="+w2_re+" w2_im="+w2_im+" w3_re="+w3_re+" w3_im="+w3_im+" h="+h);
355
356 re_root[0]=w1_re+w2_re+w3_re-h;
357 im_root[0]=w1_im+w2_im+w3_im;
358 re_root[1]=-(w1_re+w2_re)+w3_re-h;
359 im_root[1]=-(w1_im+w2_im)+w3_im;
360 re_root[2]=w2_re-w1_re-w3_re-h;
361 im_root[2]=w2_im-w1_im-w3_im;
362 re_root[3]=w1_re-w2_re-w3_re-h;
363 im_root[3]=w1_im-w2_im-w3_im;
364
365 return 4;
366 }
367
368
369
370 static void setRandomP(final double [] p,final int n,java.util.Random r)
371 {
372 if(r.nextDouble()<0.1)
373 {
374 // integer coefficiens
375 for(int j=0;j<p.length;j++)
376 {
377 if(j<=n)
378 {
379 p[j]=(r.nextInt(2)<=0?-1:1)*r.nextInt(10);
380 }
381 else
382 {
383 p[j]=0;
384 }
385 }
386 }
387 else
388 {
389 // real coefficiens
390 for(int j=0;j<p.length;j++)
448 buf.append("order="+(p.length-1)+"\t");
449 for(int k=0;k<p.length;k++)
450 {
451 buf.append("p["+k+"]="+p[k]+";");
452 }
453 return buf.toString();
454 }
455
456 static String getRootsTXT(int nr,final double [] re,final double [] im)
457 {
458 final StringBuilder buf=new StringBuilder();
459 for(int k=0;k<nr;k++)
460 {
461 buf.append("x."+k+"("+re[k]+","+im[k]+")\n");
462 }
463 return buf.toString();
464 }
465
466 static void testRoots(final int n,
467 final int n_tests,
468 final java.util.Random rn,
469 final double eps)
470 {
471 final double [] p=new double [n+1];
472 final double [] rex=new double [n],imx=new double [n];
473 for(int i=0;i<n_tests;i++)
474 {
475 for(int dg=n;dg-->-1;)
476 {
477 for(int dr=3;dr-->0;)
478 {
479 setRandomP(p,n,rn);
480 for(int j=0;j<=dg;j++)
481 {
482 p[j]=0;
483 }
484 if(dr==0)
485 {
486 p[0]=-1+2.0*rn.nextDouble();
487 }
488 else if(dr==1)
746 im_root[i]);
747 }
748 else
749 {
750 System.err.println(String.valueOf(i)+"\t"+p[i]+"\t");
751 }
752 }
753 }
754 }
755
756
757
758 public static void main(final String [] args)
759 {
760 if (System.getProperty("os.arch").equals("x86") ||
761 System.getProperty("os.arch").equals("amd64") ||
762 System.getProperty("os.arch").equals("x86_64")){
763 final long t0=System.currentTimeMillis();
764 final double eps=1e-6;
765 //checkRoots();
766 final java.util.Random r=new java.util.Random(-1381923);
767 printSpecialValues();
768
769 final int n_tests=100000;
770 //testRoots(2,n_tests,r,eps);
771 //testRoots(3,n_tests,r,eps);
772 testRoots(4,n_tests,r,eps);
773 final long t1=System.currentTimeMillis();
774 System.err.println("PolynomialRoot.main: "+n_tests+" tests OK done in "+(t1-t0)+" milliseconds. ver=$Id: PolynomialRoot.java,v 1.105 2012/08/18 00:00:05 mal Exp $");
775 System.out.println("PASSED");
776 } else {
777 System.out.println("PASS test for non-x86");
778 }
779 }
780
781
782
783 }
|
1 /*
2 * (C) Vladislav Malyshkin 2010
3 * This file is under GPL version 3.
4 *
5 */
6
7 /** Polynomial root.
8 * @version $Id: PolynomialRoot.java,v 1.105 2012/08/18 00:00:05 mal Exp $
9 * @author Vladislav Malyshkin mal@gromco.com
10 */
11
12 /**
13 * @test
14 * @bug 8005956
15 * @summary C2: assert(!def_outside->member(r)) failed: Use of external LRG overlaps the same LRG defined in this block
16 * @library /testlibrary
17 * @run main/timeout=300 PolynomialRoot
18 */
19
20 import com.oracle.java.testlibrary.Utils;
21 import java.util.Arrays;
22 import java.util.Random;
23
24 public class PolynomialRoot {
25
26
27 public static int findPolynomialRoots(final int n,
28 final double [] p,
29 final double [] re_root,
30 final double [] im_root)
31 {
32 if(n==4)
33 {
34 return root4(p,re_root,im_root);
35 }
36 else if(n==3)
37 {
38 return root3(p,re_root,im_root);
39 }
40 else if(n==2)
41 {
42 return root2(p,re_root,im_root);
43 }
44 else if(n==1)
45 {
46 return root1(p,re_root,im_root);
47 }
48 else
49 {
50 throw new RuntimeException("n="+n+" is not supported yet");
51 }
52 }
53
54
55
56 static final double SQRT3=Math.sqrt(3.0),SQRT2=Math.sqrt(2.0);
57
58
59 private static final boolean PRINT_DEBUG=false;
60
61 public static int root4(final double [] p,final double [] re_root,final double [] im_root)
62 {
63 if (PRINT_DEBUG) { System.err.println("=====================root4:p=" + Arrays.toString(p)); }
64 final double vs=p[4];
65 if(PRINT_DEBUG) System.err.println("p[4]="+p[4]);
66 if(!(Math.abs(vs)>EPS))
67 {
68 re_root[0]=re_root[1]=re_root[2]=re_root[3]=
69 im_root[0]=im_root[1]=im_root[2]=im_root[3]=Double.NaN;
70 return -1;
71 }
72
73 /* zsolve_quartic.c - finds the complex roots of
74 * x^4 + a x^3 + b x^2 + c x + d = 0
75 */
76 final double a=p[3]/vs,b=p[2]/vs,c=p[1]/vs,d=p[0]/vs;
77 if(PRINT_DEBUG) System.err.println("input a="+a+" b="+b+" c="+c+" d="+d);
78
79
80 final double r4 = 1.0 / 4.0;
81 final double q2 = 1.0 / 2.0, q4 = 1.0 / 4.0, q8 = 1.0 / 8.0;
82 final double q1 = 3.0 / 8.0, q3 = 3.0 / 16.0;
83 final int mt;
353 w3_re=w3_im=0;
354 }
355
356 final double h = r4 * a;
357 if(PRINT_DEBUG) System.err.println("w1_re="+w1_re+" w1_im="+w1_im+" w2_re="+w2_re+" w2_im="+w2_im+" w3_re="+w3_re+" w3_im="+w3_im+" h="+h);
358
359 re_root[0]=w1_re+w2_re+w3_re-h;
360 im_root[0]=w1_im+w2_im+w3_im;
361 re_root[1]=-(w1_re+w2_re)+w3_re-h;
362 im_root[1]=-(w1_im+w2_im)+w3_im;
363 re_root[2]=w2_re-w1_re-w3_re-h;
364 im_root[2]=w2_im-w1_im-w3_im;
365 re_root[3]=w1_re-w2_re-w3_re-h;
366 im_root[3]=w1_im-w2_im-w3_im;
367
368 return 4;
369 }
370
371
372
373 static void setRandomP(final double [] p, final int n, Random r)
374 {
375 if(r.nextDouble()<0.1)
376 {
377 // integer coefficiens
378 for(int j=0;j<p.length;j++)
379 {
380 if(j<=n)
381 {
382 p[j]=(r.nextInt(2)<=0?-1:1)*r.nextInt(10);
383 }
384 else
385 {
386 p[j]=0;
387 }
388 }
389 }
390 else
391 {
392 // real coefficiens
393 for(int j=0;j<p.length;j++)
451 buf.append("order="+(p.length-1)+"\t");
452 for(int k=0;k<p.length;k++)
453 {
454 buf.append("p["+k+"]="+p[k]+";");
455 }
456 return buf.toString();
457 }
458
459 static String getRootsTXT(int nr,final double [] re,final double [] im)
460 {
461 final StringBuilder buf=new StringBuilder();
462 for(int k=0;k<nr;k++)
463 {
464 buf.append("x."+k+"("+re[k]+","+im[k]+")\n");
465 }
466 return buf.toString();
467 }
468
469 static void testRoots(final int n,
470 final int n_tests,
471 final Random rn,
472 final double eps)
473 {
474 final double [] p=new double [n+1];
475 final double [] rex=new double [n],imx=new double [n];
476 for(int i=0;i<n_tests;i++)
477 {
478 for(int dg=n;dg-->-1;)
479 {
480 for(int dr=3;dr-->0;)
481 {
482 setRandomP(p,n,rn);
483 for(int j=0;j<=dg;j++)
484 {
485 p[j]=0;
486 }
487 if(dr==0)
488 {
489 p[0]=-1+2.0*rn.nextDouble();
490 }
491 else if(dr==1)
749 im_root[i]);
750 }
751 else
752 {
753 System.err.println(String.valueOf(i)+"\t"+p[i]+"\t");
754 }
755 }
756 }
757 }
758
759
760
761 public static void main(final String [] args)
762 {
763 if (System.getProperty("os.arch").equals("x86") ||
764 System.getProperty("os.arch").equals("amd64") ||
765 System.getProperty("os.arch").equals("x86_64")){
766 final long t0=System.currentTimeMillis();
767 final double eps=1e-6;
768 //checkRoots();
769 final Random r = Utils.getRandomInstance();
770 printSpecialValues();
771
772 final int n_tests=100000;
773 //testRoots(2,n_tests,r,eps);
774 //testRoots(3,n_tests,r,eps);
775 testRoots(4,n_tests,r,eps);
776 final long t1=System.currentTimeMillis();
777 System.err.println("PolynomialRoot.main: "+n_tests+" tests OK done in "+(t1-t0)+" milliseconds. ver=$Id: PolynomialRoot.java,v 1.105 2012/08/18 00:00:05 mal Exp $");
778 System.out.println("PASSED");
779 } else {
780 System.out.println("PASS test for non-x86");
781 }
782 }
783
784
785
786 }
|