///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Program file name: Poly.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 using the polynomial generated
// for a tridiagonal matrix.

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

   double p = polynomial(a, b, x, n);
   System.out.println("the polynomial is: " + p);
  }

// Method to generate the polynomial for the secular
// equation of a symmetric tridiagonal matrix.

  public static double polynomial (double a[], double b[],
    double x, int i) {
    if (i==0) return 1;
     else if (i==1) return a[0]-x;
      else return (a[i-1]-x)*polynomial(a, b, x, i-1)
        -b[i-2]*b[i-2]*polynomial(a, b, x, i-2);
  }
}
