///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Program file name: Poly2.java                                         //
//                                                                       //
// © Tao Pang 2006                                                       //
//                                                                       //
// Last modified: January 18, 2006                                       //
//                                                                       //
// (1) This Java program is part of the book, "An Introduction to        //
//     Computational Physics, 2nd Edition," written by Tao Pang and      //
//     published by Cambridge University Press on January 19, 2006.      //
//                                                                       //
// (2) No warranties, express or implied, are made for this program.     //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

// A test example of evaluating a polynomial.

import java.lang.*;
import java.util.Random;
public class Poly2 {
  static final int n = 5;
  public static void main(String argv[]) {
   Random r = new Random(); 
   double a[] = new double[n];
   for (int i=0; i<n; ++i) a[i] = r.nextDouble();
   double x = 1, y = 1;
// double x = r.nextDouble();
// double y = r.nextDouble();

   double p[] = polynomial2(a, x, y);
   System.out.println("the polynomial is: "+ p[0]
                    + " + i " + p[1]);
  }

// Method to evaluate the polynomial with given c_k and
// z=x+iy.

  public static double[] polynomial2(double c[],
    double x, double y) {
    double p[] = new double[2];
    int n = c.length-1;
    double u = c[n];
    double v = 0;
    for (int i=0; i<n; ++i) {
      double t = x*v+y*u; 
      u = c[n-i-1]+x*u-y*v; 
      v = t;
    }
    p[0] = u;
    p[1] = v;
    return p;
  }
}
