import java.io.*;
//import Numeric;
//import Text;


class least
{
  public static void main(String args[]) throws IOException
  {
 double e[];  //coefficients of polynomial least square
 e=new double[20];
 double xi[];
 xi=new double[800];
 double yi[];
 yi=new double[800];
 DataInputStream cin=new DataInputStream(System.in);
 System.out.println("name of input file : ");
 String in_name=Text.readString(cin);
 System.out.println(" ");
 DataInputStream fin=new DataInputStream(new FileInputStream(in_name));
 PrintStream fout=new PrintStream(new FileOutputStream("out.dat"));
 int i=-1;
 try {
 while(fin != null)
 {
   i++;
   xi[i]=Text.readDouble(fin);
   yi[i]=Text.readDouble(fin);
   System.out.println(" "+xi[i]+" "+yi[i]);
 }
     } catch(EOFException e_eof) {System.out.println("end of file"); }
 System.out.println("degree of curve fitting : ");
 int n=Text.readInt(cin);
 double x[]=new double[i];
 double y[]=new double[i];
 for(i=0;i<x.length;i++)
 {
 x[i]=xi[i];
 y[i]=yi[i];
 }
 e=Numeric.poly_least_square(x,y,n);
 System.out.println(Matrix.toStringT(e));
 fout.println(Matrix.toStringT(e));
 System.out.println("least square error : "+Numeric.error(x,y,e));
 System.out.println("would you like to send curve fitted data to a data file (y/n) ");
 char answer=Text.readChar(cin);
 if(answer=='y')
 {
 System.out.println("name of output file : ");
 String out_name=Text.readString(cin);
 PrintStream fout1=new PrintStream(new FileOutputStream(out_name));
 double xmin,xmax;
 System.out.println("minimum x : ");
 xmin=Text.readDouble(cin);
 System.out.println("maximum x : ");
 xmax=Text.readDouble(cin);
 System.out.println("number of data points : ");
 int n_data=Text.readInt(cin);
 double xx;
 for(i=0;i<n_data;i++)
   {
   xx=xmin+(xmax-xmin)/(n_data-1.0)*i;
   System.out.println(" "+xx+" "+Numeric.f_least_square(e,xx));
   fout1.println(" "+xx+" "+Numeric.f_least_square(e,xx));
   } //end of for
 }//end of if
  }
}
