//======================================================
// Numerical Analysis package in java
// Plot class to plot 2D data
// Dr. Turhan Coban
// EGE University, Engineering School, Mechanical Eng. Div.
// turhan.coban@ege.edu.tr
// =====================================================

import java.io.*;
import java.awt.Color;
import static java.lang.Math.*;

//This class organises data and reqired plot classes for actual plot 
//===== classes used by this class=======
// PlotShapesSW // define shapes to draw in plot windows in real coordinate system
// Plot2D       // Plot and coordinates data 
// PLotW        // defines actual graphic screens and pages
//===== Example classes 
// PlotT0,PlotT01,PlotT02,PlotT03,PlotT1,PlotT2,PlotT3,PlotT4

abstract class f_x extends Mathd
{
  // single function single independent variable
  // example f=x*x
  abstract double func(double x);
}

abstract class fi_x extends Mathd
{
  // multifunction single independent variable
  // vector of dependent variables are returned
  // example f[0]=x+sin(x)
  //         f[1]=x*x-x
  // func(x) returns the value of f[0] and f[1]
  // as a two dimensional vector
  abstract double[] func(double x);
}

abstract class f_xj extends Mathd
{
  // single function  multi independent variable
  // a single value is returned indiced to equation_ref
  // example f[0]=x[0]+sin(x[1])
  //         f[1]=x[0]*x[0]-x[1]
  // func(x,1) returns the value of f[1]
  // func(x,0) returns the value of f[0]

  abstract double func(double x[]);
}
 
public class Plot
{
  // This class reads plot input variables
  // and sets relations between absolute scale and relative scale plot windows
  public Color bg=Color.blue;
  public String  skutu1[]={"","","","",""};
  public String  skutu2[]={"","","","",""};
  public double x1,y1;
  public double x2,y2;
  public double dx1;
  public double dy1;
  public double dx2;
  public double dy2;
  public double xl[],yl[],dxl[],dyl[];
  public String label="y=f(x)";                  // Plot Label
  public String xlabel="x";                      // X axis Label
  public String ylabel="y";                      // Y axis Label
  public String label_at_xy[];                   //additional label at x,y point 
  public double label_x[];                       //additional label x points
  public double label_y[];                       //additional label x points
  public String filename;                        // file name
  public double xmin,ymin,xmax,ymax;             // real max,min scale
  public int xabsmin,yabsmin,abswidth,absheight; // absolute max,min scale in Pixel dimension
  public int nmax;                               // max number of data points
  public int ni;                                 // data set number with nmax data                        
  public int nline;                              // max number of data sets
  public double x[][];                           // x data
  public double y[][];                           // y=f(x) data
  public int n[];                                // n : number of data in each line
  public int red[],green[],blue[];               // color code 0-255
  public int xgridon=0;                          // x axis grid line on
  public int ygridon=0;                          // y axis grid line on
  public int xlog=0;                               // x axis logaritmic scale
  public int ylog=0;                               // y axis logaritmic scale
  public int plottype[];
  PlotW p1;
  public String plotst[]={"0 ______",
"1 __ __ ",
"2 . _ . ",
"3 . _ _ .",
"4 - - - ",
"5 bold _____",
"6 bold __ __",
"7 bold . _ . _",
"8 bold . _ _ . .",
"9 bold - - - ",
"10  "+'\u2295',
"11  "+'\u2297',
"12  "+'\u2299',
"13  "+'\u229A',
"14  "+'\u229B',
"15  "+'\u229E',
"16  "+'\u22A0',
"17  "+'\u22A1',
"18  "+'\u22B9',
"19  "+'\u22C7',
"20  square",  
"21  filled square",
"22  circle",
"23  filled circle",
"24  triangle",
"25  equilateral quadrangle",
"26  pentagon",
"27  hexagon",
"28  filled triangle",
"28  filled equilateral quadrangle",
"30  filled  pentagon",
"31  fled hexagon",
"32  3 sided star",
"33  4 sided star",
"34  5 sided star",
"35  6 dided star",
"36  filled 3 sided star",
"37  filled 4 sided star",
"38  filled 5 sided star",
"39  filled 6 sided star",
"40  bar graphic",
"41  filled bar graphic"}; 
  double xminmaxlog,yminmaxlog;   
  public char ch[];    // plot character (used with plottype 10)
  public int xntic=10;    // number of tics in x axis
  public int yntic=10;    // number of tics in y axis
  BufferedReader fini;
  BufferedReader ffile;
  File plotFile;
  File lineDataFile;
  String fn[];
/*  
========== constructors =======================
  public Plot() throws IOException   : Read actual data from file Plot.txt
                                       and plot filenames listed in this file 
  public Plot(String pl,String xl,String yl,int xnt,int ynt
  ,int xgo,int ygo,String fn[],int ipt[],int ir[],int ig[]                
  ,int ib[]) throws IOException      : Read file names from String fn[] and plot them
  public Plot(String fn[],int ipt[],int ir[],int ig[],int ib[]) throws IOException	
  public Plot(String fn[]) throws IOException  : Read file names from String fn[] and plot them 
  public Plot(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[]) : read plot data from xi[][] and yi[][]
  public Plot(double xiyi[][]) : read plot data xi[] and yi[] from xiyi[][] string   
  public Plot(double xi[][],double yi[][]) : read plot data from xi[][] and yi[][]
  public Plot(double xi[],double yi[]) : read plot data from xi[] and yi[]
  public Plot(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib) : create plot data from a simple function f_x f
  public Plot(f_x f,double xm,double xma,int N) : create plot data from a simple function f_x f
  public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function) : create plot data from an array of simple functions fi_x f 

======== Label settings ======================== 
  public void setPlabel(String ip) {label=ip;}
  public void setXlabel(String ix) {xlabel=ix;}
  public void setYlabel(String iy) {ylabel=iy;}
  public void setXYlabel(String ix,String iy) {xlabel=ix;ylabel=iy;}

======== Setting additional data after initial construction =========  
 This groups's functions are similar to constructor classes
public void addData(double xi[],double yi[])
public void addData(double xi[],double yi[],int iplottype)
public void addData(double xi[],double yi[],int iplottype,int ir,int ig,int ib)
public void addData(double xi[][],double yi[][])
public void addData(double xi[][],double yi[][],int ip[])
public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[])
public void addFunction(f_x f,double xmi,double xma,int N,int ipt)
public void addFunction(f_x f,double xmi,double xma,int N)
public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib)	
public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function)
public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[])
public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,

================== Settings of additional plot information =================
----------Plot Type ----------------------
public void setPlotType(int plot_type[])	
public void setPlotType(int dataset,int plot_no)
public void setPlotType(int dataset,char plot_char)
----------x y grid on-off ----------------
public void setXgrid(int igx)
public void setYgrid(int igy)
public void setGrid(int igx,int igy)
public void setXtic(int ixt)
public void setYtic(int iyt)
public void setXYtic(int ixt,int iyt)
public void setColor(int dataset,int ired,int igreen,int iblue)
public void setColor(int ired[],int igreen[],int iblue[])
public void setColor(Color c)
================ Setting of Minimum Maximum fuctions ===================
public void setMinMax()
public void setMinMax(double xi[][],double yi[][])
public void setMinMax(double iminx,double imaxx,double iminy,double imaxy)
=============== Plot Window Formatting ==================================
 public void set_plotwindow(int width,int height,double xip,double yip,double dxp,double dyp) 
 public void setabsMaxMin(int ixmin,int iymin,int idx,int idy) 
================ Actual Plotting of constructed data
public void plot()
*/

public static double[] data(double xmin,double xmax,double dx)
{
 int n=(int)((xmax-xmin)/dx)+1;	
 double x[]=new double[n];
 for(int i=0;i<n-1;i++)
   {x[i]=xmin+dx*i;}
   x[n-1]=xmax;
   return x;
}

public static double[] data(double xmin,double xmax,int n)
{
 double dx=(xmax-xmin)/n;	
 double x[]=new double[n];
 for(int i=0;i<n;i++)
   {x[i]=xmin+dx*i;}
   return x;
}
    
public Plot() throws IOException 
{
	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
	//read input variables from Plot.txt
    //adding max-min prompts and input fields
    //initial values of max and mins
    // Structure of input file
    // filename
    // xlabel
    // ylabel
    // nline 
    // datafilename plottype redcolor greencolor bluecolor
    // ........
    // datafilename plottype redcolor greencolor bluecolor
    //
    // A sample data file input :
    //==========================
    // filename
    // xaxislabel
    // yaxislabel
    // 4 
    // b.dat 22  0 0 0
    // a.dat 21 0 255 0
    // a.dat 0 0 255 0
    // b.dat 0 0 0 255	
    //==========================
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    try{
       fini=new BufferedReader(new FileReader("Plot.txt"));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
    label=Text.readStringLine(fini);
    xlabel=Text.readStringLine(fini);
    ylabel=Text.readStringLine(fini);
    nline=Text.readInt(fini);
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    ch[10]='\u2295';
    ch[11]='\u2297';
    ch[12]='\u2299';
    ch[13]='\u229A';
    ch[14]='\u229B';
    ch[15]='\u229E';
    ch[16]='\u22A0';
    ch[17]='\u22A1';
    ch[18]='\u22B9';
    ch[19]='\u22C7';
    //read all data to determine limit values
    String fn[];
    fn=new String[nline];
    int i,j;
    for(i=0;i<nline;i++)
    {  
	   plottype[i]=i;
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';      
       fn[i]=Text.readString(fini);
       plottype[i]=Text.readInt(fini);
       if((plottype[i] >= 10)&&(plottype[i] <= 19)) ch[i]=Text.readChar(fini);
       red[i]=Text.readInt(fini);
       green[i]=Text.readInt(fini);
       blue[i]=Text.readInt(fini);
       j=0;
       n[i]=0;
       //open ffile             
           try{
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       try{
       while(ffile!=null)
       {
       xtemp=Text.readDouble(ffile);
       ytemp=Text.readDouble(ffile);
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }
       dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;
          } catch(EOFException e_eof)
          {
          //close ffile
          try{
          ffile.close();
             } catch(IOException e)
             {
             System.err.println("Error Closing File\n"+e.toString());
             System.exit(1);
             }
          } //End of EOFException

       if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
    }
//close ffile
    try{
       ffile.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }
    x=new double[nline][nmax];
    y=new double[nline][nmax];
    //re-read the data again to load it
    for(i=0;i<nline;i++)
    {
       //open ffile
           try{      
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       for(j=0;j<n[i];j++)
       {
          x[i][j]=Text.readDouble(ffile);
          y[i][j]=Text.readDouble(ffile);

       }
    //close ffile
    try{
       ffile.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }
    }
    set_plotwindow(500,400,0.1,0.1,0.9,0.9);
//close fini
    try{
       fini.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }
  }
  
 
  public Plot(String pl,String xl,String yl,int xnt,int ynt
  ,int xgo,int ygo,String fn[],int ipt[],int ir[],int ig[]
  ,int ib[]) throws IOException
   {
   // String pl : plot label
   // String x1 : plot x axis label
   // String y1 : Plot y axis label
   	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}    
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    setXtic(10);
    setYtic(10);
    setGrid(0,0);
    double xtemp,ytemp;       
    setPlabel(pl);
    setXlabel(xl);
    setYlabel(yl);
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=fn.length;
    for(i=0;i<nline;i++)
    {
       plottype[i]=ipt[i];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';     
       red[i]=ir[i];
       green[i]=ig[i];
       blue[i]=ib[i];
       j=0;
       n[i]=0;
       //open ffile  
       System.out.println("isim ="+fn[i]);           
           try{
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       try{
       while(ffile!=null)
       {
       xtemp=Text.readDouble(ffile);
       ytemp=Text.readDouble(ffile);
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }
       dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;
          } catch(EOFException e_eof)
          {
          //close ffile
          try{
          ffile.close();
             } catch(IOException e)
             {
             System.err.println("Error Closing File\n"+e.toString());
             System.exit(1);
             }
          } //End of EOFException

        if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
    }
    //System.out.println("nline = "+nline+"nmax="+nmax);
    x=new double[nline][nmax];
    y=new double[nline][nmax];
    //re-read the data again to load it
    for(i=0;i<nline;i++)
    {
       //open ffile
           try{      
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       for(j=0;j<n[i];j++)
       {
          x[i][j]=Text.readDouble(ffile);
          y[i][j]=Text.readDouble(ffile);
       }
    //close ffile
    try{
       ffile.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }

    }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }

   public Plot(String fn[],int ipt[],int ir[],int ig[],int ib[]) throws IOException
   { 
    // reading data from file and initilisation
    label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=fn.length;
    for(i=0;i<nline;i++)
    {
       plottype[i]=ipt[i];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';      
       red[i]=ir[i];
       green[i]=ig[i];
       blue[i]=ib[i];
       j=0;
       n[i]=0;
       //open ffile  
       //System.out.println("isim ="+fn[i]);           
           try{
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       try{
       while(ffile!=null)
       {
       xtemp=Text.readDouble(ffile);
       ytemp=Text.readDouble(ffile);
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }
       dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;
          } catch(EOFException e_eof)
          {
          //close ffile
          try{
          ffile.close();
             } catch(IOException e)
             {
             System.err.println("Error Closing File\n"+e.toString());
             System.exit(1);
             }
          } //End of EOFException

        if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
    }
    x=new double[nline][nmax];
    y=new double[nline][nmax];
    //re-read the data again to load it
    for(i=0;i<nline;i++)
    {
       //open ffile
           try{      
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       for(j=0;j<n[i];j++)
       {
          x[i][j]=Text.readDouble(ffile);
          y[i][j]=Text.readDouble(ffile);
       }
    //close ffile
    try{
       ffile.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }
    }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }
  
 
  public Plot(String fn[]) throws IOException
   {
   //reading data from file(s)	   
   	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
   nline=fn.length;
   int ipt[]=new int[nline];
   int ir[]=new int[nline];
   int ig[]=new int[nline];
   int ib[]=new int[nline];	   
	xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=fn.length;
    for(i=0;i<nline;i++)
    {
       plottype[i]=ipt[i];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7'; 
       red[i]=ir[i];
       green[i]=ig[i];
       blue[i]=ib[i];
       j=0;
       n[i]=0;
       //open ffile  
       //System.out.println("isim ="+fn[i]);           
           try{
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       try{
       while(ffile!=null)
       {
       xtemp=Text.readDouble(ffile);
       ytemp=Text.readDouble(ffile);
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }
       dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;
          } catch(EOFException e_eof)
          {
          //close ffile
          try{
          ffile.close();
             } catch(IOException e)
             {
             System.err.println("Error Closing File\n"+e.toString());
             System.exit(1);
             }
          } //End of EOFException

        if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
    }
    x=new double[nline][nmax];
    y=new double[nline][nmax];
    //re-read the data again to load it
    for(i=0;i<nline;i++)
    {
       //open ffile
           try{      
       ffile=new BufferedReader(new FileReader(fn[i]));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       for(j=0;j<n[i];j++)
       {
          x[i][j]=Text.readDouble(ffile);
          y[i][j]=Text.readDouble(ffile);
       }
    //close ffile
    try{
       ffile.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }
    }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
   }
   
   public Plot(String fn) throws IOException
   {
   	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}	   
   //reading data from file(s)	   
   nline=1;
   int ipt[]=new int[nline];
   int ir[]=new int[nline];
   int ig[]=new int[nline];
   int ib[]=new int[nline];	   
	xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=1;
    for(i=0;i<nline;i++)
    {
       plottype[i]=ipt[i];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7'; 
       red[i]=ir[i];
       green[i]=ig[i];
       blue[i]=ib[i];
       j=0;
       n[i]=0;
       //open ffile  
       //System.out.println("isim ="+fn[i]);           
           try{
       ffile=new BufferedReader(new FileReader(fn));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       try{
       while(ffile!=null)
       {
       xtemp=Text.readDouble(ffile);
       ytemp=Text.readDouble(ffile);
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }
       dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;
          } catch(EOFException e_eof)
          {
          //close ffile
          try{
          ffile.close();
             } catch(IOException e)
             {
             System.err.println("Error Closing File\n"+e.toString());
             System.exit(1);
             }
          } //End of EOFException

        if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
    }
    x=new double[nline][nmax];
    y=new double[nline][nmax];
    //re-read the data again to load it
    for(i=0;i<nline;i++)
    {
       //open ffile
           try{      
       ffile=new BufferedReader(new FileReader(fn));
       } catch(IOException e)
       {
       System.err.println("Error Opening File \n"+e.toString());
       System.exit(1);
       }
       for(j=0;j<n[i];j++)
       {
          x[i][j]=Text.readDouble(ffile);
          y[i][j]=Text.readDouble(ffile);
       }
    //close ffile
    try{
       ffile.close();
       } catch(IOException e)
       {
       System.err.println("Error Closing File\n"+e.toString());
       System.exit(1);
       }
    }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
   }
   
     
 
  public Plot(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[])
  { 
	pp2(xi,yi,ipt,ir,ig,ib);
  }
 
  public void pp2(double xi[][],double yi[][])
  {
  int ir[];
  int ig[];
  int ib[];
  int ipt[];
  int nn=xi.length;
  ir=new int[nn];
  ig=new int[nn];
  ib=new int[nn];
  ipt=new int[nn];
  for(int i=0;i<nn;i++)
  {
    ir[i]=0;
    ig[i]=0;
    ib[i]=0;
    ipt[i]=0;
  }     
  pp2(xi,yi,ipt,ir,ig,ib);	  
  }
 
  public void pp2(double xi[][],double yi[][],int ipt[])
  {
  int ir[];
  int ig[];
  int ib[];
  int nn=xi.length;
  ir=new int[nn];
  ig=new int[nn];
  ib=new int[nn];
  for(int i=0;i<nn;i++)
  {
    ir[i]=0;
    ig[i]=0;
    ib[i]=0;
  }     
  pp2(xi,yi,ipt,ir,ig,ib);	  
  }
       
  public void pp2(double xi[][],double yi[][],int ipt[],int ir[],int ig[],int ib[])
   { 
    // reading data from initial array
		label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];  
    ch=new char[500];
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=xi.length;
    for(i=0;i<nline;i++)
    {
       plottype[i]=ipt[i];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';       
       red[i]=ir[i];
       green[i]=ig[i];
       blue[i]=ib[i];
       nline=xi.length;
       n[i]=0;
       for(j=0;j<xi[i].length;j++)
       {
       xtemp=xi[i][j];
       ytemp=yi[i][j];
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }   
       dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;    
       if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
       } //end of for
       x=new double[nline][nmax];
       y=new double[nline][nmax];
       for(i=0;i<nline;i++)
       {
          for(j=0;j<n[i];j++)
          {
          x[i][j]=xi[i][j];
          y[i][j]=yi[i][j];
          }
       }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }
 
   public void pp2(double ai[][],int ipt[],int ir[],int ig[],int ib[])
   { 
    // reading data from initial array
	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];  
    ch=new char[500];
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=ai.length-1;
    double xi[][]=new double[nline][ai[0].length];
    double yi[][]=new double[nline][ai[0].length];
    for(i=0;i<nline;i++)
    {
       plottype[i]=ipt[i];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';       
       red[i]=ir[i];
       green[i]=ig[i];
       blue[i]=ib[i];
       nline=xi.length;
       n[i]=0;
       for(j=0;j<xi[i].length;j++)
       {
       xtemp=ai[0][j];
       ytemp=ai[i+1][j];
       xi[i][j]=xtemp;
       yi[i][j]=ytemp;
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[i]++;
       }
              dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;       
       if(i==0) {nmax=n[i];ni=i;}
       else
         {
         if(n[i]>nmax) {nmax=n[i];ni=i;}
         }
       } //end of for
       x=new double[nline][nmax];
       y=new double[nline][nmax];
       for(i=0;i<nline;i++)
       {
          for(j=0;j<n[i];j++)
          {
          x[i][j]=xi[i][j];
          y[i][j]=yi[i][j];
          }
       }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }
  
  public void pp2(double ai[][],int ipt[])
  {
    int ir[];
  int ig[];
  int ib[];
  int nn=ai.length-1;
  ir=new int[nn];
  ig=new int[nn];
  ib=new int[nn];
  for(int i=0;i<nn;i++)
  {
    ir[i]=0;
    ig[i]=0;
    ib[i]=0;
  }     
  pp2(ai,ipt,ir,ig,ib);	 
  }
  
  public void pp2(double ai[][])
  {
  int ir[];
  int ig[];
  int ib[];
  int ipt[];
  int nn=ai.length-1;
  ir=new int[nn];
  ig=new int[nn];
  ib=new int[nn];
  ipt=new int[nn];
  for(int i=0;i<nn;i++)
  {
    ir[i]=0;
    ig[i]=0;
    ib[i]=0;
  }     
  pp2(ai,ipt,ir,ig,ib);	 
  }	  
	   
  public Plot(double ai[][])
  {
  pp2(ai);
  }
  
  public Plot(double xi[][],double yi[][])
  { 
  pp2(xi,yi);
  }
  
 
  public void pp1( double xi[],double yi[])
  {
  pp1(xi,yi,0,0,0,0);
  }
    
 
  public void pp1( double xi[],double yi[],int ipt)
  {
  pp1(xi,yi,ipt,0,0,0);
  }
   public void ppA( double xi[],double yi[],int ipt,int ir,int ig,int ib)
  {
    // reading data from initial array
	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500]; 
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=1;
    plottype[0]=ipt;
    red[0]=ir;
    green[0]=ig;
    blue[0]=ib;
    nline=1;
    n[0]=0;
    i=0;
       plottype[i]=ipt;
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';   
       for(j=0;j<xi.length;j++)
       {
       xtemp=xi[j];
       ytemp=yi[j];
       n[0]++;
       }       
       nmax=n[0];ni=0;
       x=new double[nline][nmax];
       y=new double[nline][nmax];
       i=0;
          for(j=0;j<n[0];j++)
          {
          x[i][j]=xi[j];
          y[i][j]=yi[j];
          }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }   

  public void pp1( double xi[],double yi[],int ipt,int ir,int ig,int ib)
  {
    // reading data from initial array
	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500]; 
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=1;
    plottype[0]=ipt;
    red[0]=ir;
    green[0]=ig;
    blue[0]=ib;
    nline=1;
    n[0]=0;
    i=0;
       plottype[i]=ipt;
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';   
       for(j=0;j<xi.length;j++)
       {
       xtemp=xi[j];
       ytemp=yi[j];
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[0]++;
       } 
              dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;      
       nmax=n[0];ni=0;
       x=new double[nline][nmax];
       y=new double[nline][nmax];
       i=0;
          for(j=0;j<n[0];j++)
          {
          x[i][j]=xi[j];
          y[i][j]=yi[j];
          }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }   
  
  public void pp1( double yi[],int ipt,int ir,int ig,int ib)
  {
    // reading data from initial array
	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    n[0]=yi.length;
    double xi[]=new double[n[0]];
    for(int i=0;i<n[0];i++)
    {xi[i]=i+1;}
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500]; 
    ch=new char[500];
    //read all data to determine limit values
    int i,j;
    nline=1;
    plottype[0]=ipt;
    red[0]=ir;
    green[0]=ig;
    blue[0]=ib;
    nline=1;
    n[0]=0;
    i=0;
       plottype[i]=ipt;
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7';   
       for(j=0;j<xi.length;j++)
       {
       xtemp=xi[j];
       ytemp=yi[j];
       if(xtemp<xmin) xmin=xtemp;
       if(xtemp>xmax) xmax=xtemp;
       if(ytemp<ymin) ymin=ytemp;
       if(ytemp>ymax) ymax=ytemp;
       n[0]++;
       } 
              dx1=(xmax-xmin)/30.0;
       dy1=(ymax-ymin)/30.0;
       dx2=(xmax-xmin)/30.0;
       dy2=(ymax-ymin)/30.0;      
       nmax=n[0];ni=0;
       x=new double[nline][nmax];
       y=new double[nline][nmax];
       i=0;
          for(j=0;j<n[0];j++)
          {
          x[i][j]=xi[j];
          y[i][j]=yi[j];
          }
    set_plotwindow(500,500,0.1,0.1,0.9,0.9);
  }
  public void pp1( double yi[],int ipt)
  {pp1(yi,ipt,0,0,0);}   
  
  public void pp1( double yi[])
  {pp1(yi,0,0,0,0);}   
  
  public Plot(double xi[],double yi[])
  { 
    pp1(xi,yi);
  }
   
  public Plot(double yi[])
   { 
    pp1(yi);
  }
   public Plot(double yi[],int ipt)
   { 
    pp1(yi,ipt);
   }
   public Plot(double yi[],int ipt,int ir,int ig,int ib)
   { 
    pp1(yi,ipt,ir,ig,ib);
   }
   
   public void pp10(f_xj f,double xmin,double xmax,double ymin,double ymax,int Nx,int Ny, int Nz,int ipt,int ir,int ig,int ib)
   {
   double z[][]=new double[Nx][Ny];
   double Zc[]=new double[Nz];
   double xc[][];
   double yc[][];
   double zmax=-9e99;
   double zmin=9e99;
   double xx=0;
   double yy=0;
   double dx=(xmax-xmin)/Nx;
   double dy=(ymax-ymin)/Ny;
   double x[]=new double[2];
   for(int i=0;i<Nx;i++)
   {for(int j=0;j<Ny;j++)
     {x[0]=xmin+dx*i;x[1]=ymin+dy*j;z[i][j]=f.func(x);
      if(z[i][j]<zmin) zmin=z[i][j];
      if(z[i][j]>zmax) zmax=z[i][j];
     }
   }
   double dz=(zmax-zmin)/Nz;
    for(int i=0;i<Nz;i++)
    {Zc[i]=zmin+i*dz;}
    
    
   }
   	   
   public void pp3(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib)
   { 
    // reading data from a given function//one data set only
	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;}
    xmin=xm;
    xmax=xma;
    nmax=N;
    ni=0;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    double xi[]=new double[N];
    double yi[]=new double[N];
    double dx=(xmax-xmin)/(N-1);
    double xx=xmin;
    for(int j=0;j<N;j++)
    {  
	  xi[j]=xx;
	  yi[j]=f.func(xx);
	  xx+=dx;
    }
    pp1(xi,yi,ipt,ir,ig,ib);
  }
  
  public Plot(f_x f,double xm,double xma,int N,int ipt,int ir,int ig,int ib)
  {
  pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  public Plot(f_x f,double xm,double xma,int N,int ipt)
  { 
    // reading data from a given function//one data set only
	int ir=0;
	int ig=0;
	int ib=0;
    pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  
  public Plot(f_x f,double xm,double xma,int N)
   { 
    // reading data from a given function//one data set only
	int ipt=0;
	int ir=0;
	int ig=0;
	int ib=0;
    pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  
   public Plot(f_x f,double xm,double xma)
   { 
    // reading data from a given function//one data set only
	int ipt=0;
	int ir=0;
	int ig=0;
	int ib=0;
	int N=500;
    pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  
  public Plot(String s,double xm,double xma,int N,int ipt,int ir,int ig,int ib)
  {
	  functiongenerator ff=new functiongenerator(s);
      try{
	     ff.createfunction();
	     ff.finalize();
         } catch(Throwable e1) {System.err.println("girdi hatası.");}       
      f2D f=new f2D(); 
      pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  public Plot(String s,double xm,double xma,int N,int ipt)
  {
  functiongenerator ff=new functiongenerator(s);
  ff.createfunction();
  f2D f=new f2D(); 
      // reading data from a given function//one data set only
	int ir=0;
	int ig=0;
	int ib=0;
    pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  public Plot(String s,double xm,double xma,int N)
  {
  functiongenerator ff=new functiongenerator(s);
  ff.createfunction();
  f2D f=new f2D(); 
      // reading data from a given function//one data set only
	int ipt=0;
      int ir=0;
	int ig=0;
	int ib=0;
    pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
  public Plot(String s,double xm,double xma)
  {
  functiongenerator ff=new functiongenerator(s);
  ff.createfunction();
  f2D f=new f2D(); 
      // reading data from a given function//one data set only
	int N=400;
    int ipt=0;
    int ir=0;
	int ig=0;
	int ib=0;
    pp3(f,xm,xma,N,ipt,ir,ig,ib);
  }
   public void pp4(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function,int ipt[],int ir[],int ig[],int ib[])
   { 
    // reading data from a given functon set
    // same xmin xmax and Number_of_data for each function
   nline=Number_of_Function;  
   	label_at_xy=new String[5];
	label_x=new double[5];
	label_y=new double[5];
	for(int i=0;i<5;i++) {label_at_xy[i]="";label_x[i]=0.0;label_y[i]=0.0;} 
	xmin=xm;
    xmax=xma;
    ymin=9.99e50;
    ymax=-9.99e50;
    double xtemp,ytemp;       
    n=new int[500];
    red=new int[500];
    green=new int[500];
    blue=new int[500];
    plottype=new int[500];
    ch=new char[500];
    int N=Number_of_Data;
    double dx=(xmax-xmin)/N;
    double xx=xmin;
    int NF=Number_of_Function;
    nline=NF;
    double xi[][]=new double[NF][N];
    double yi[][]=new double[NF][N];
    double yy[]=new double[NF];
    for(int j=0;j<N;j++)
    {  
	  yy=f.func(xx);
	  for(int i=0;i<NF;i++)
	    {  yi[i][j]=yy[i];
	       xi[i][j]=xx;}
	  xx+=dx;
    }
    pp2(xi,yi,ipt,ir,ig,ib);
  }
   
   public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function,
   int ipt[],int ir[],int ig[],int ib[])
   { 
   pp4(f,xm,xma,Number_of_Data,Number_of_Function,ipt,ir,ig,ib);
   }
   
   
   public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function,
   int ipt[])
   { 
   int ir[];
   int ig[];
   int ib[];
   int nn=Number_of_Function;
   ir=new int[nn];
   ig=new int[nn];
   ib=new int[nn];
   for(int i=0;i<nn;i++)
   {
    ir[i]=0;
    ig[i]=0;
    ib[i]=0;
   }     	   
   pp4(f,xm,xma,Number_of_Data,Number_of_Function,ipt,ir,ig,ib);
   }
   
   public Plot(fi_x f,double xm,double xma,int Number_of_Data,int Number_of_Function)
   { 
   int ir[];
   int ig[];
   int ib[];
   int ipt[];
   int nn=Number_of_Function;
   ir=new int[nn];
   ig=new int[nn];
   ib=new int[nn];
   ipt=new int[nn];
   for(int i=0;i<nn;i++)
   {
    ir[i]=0;
    ig[i]=0;
    ib[i]=0;
    ipt[i]=0;
   }     	   
   pp4(f,xm,xma,Number_of_Data,Number_of_Function,ipt,ir,ig,ib);
   }
   
  public void setPlabel(String ip) {label=ip;}
  public void setXlabel(String ix) {xlabel=ix;}
  public void setYlabel(String iy) {ylabel=iy;}
  public void setXYlabel(String ix,String iy) {xlabel=ix;ylabel=iy;}
  
  public void set_plotwindow(int width,int height,double xip,double yip,double dxp,double dyp)
  {
    //sets rectangular plot window    
    //function scale input variables :
    //real scale plot window width
    //real scale plot window height
    //xip: minimum real scale x
    //yip : minimum real scale y
    //dxp: real scale x width  xmax=xip+dxp
    //dyp: real scale y height ymax=yip+dyp

    // xabsmin : absolute (pixel) scale minimum x
    // yabsmin : absolute (pixel) scale minimum y
    //abswidth : absolute (pixel) scale x width (maximum x=minimum x+abswidth)
    //absheight: absolute (pixel) scale y height (maximum y=minimum y+absheight)
    
    xabsmin   =  (int)(width*(xip+0.2));
    yabsmin   =  (int)(height*(yip+0.2));
    abswidth  =  (int)(width*dxp);
    absheight =  (int)(height*dyp);
  }
 
 public void setabsMaxMin(int ixmin,int iymin,int idx,int idy)
 {
 xabsmin=ixmin;
 yabsmin=iymin;
 abswidth=idx;
 absheight=idy;
 }
 
 public void addStringwithPlotType(String s[],double x1i,double y1i)
 { //adding a series of strings with plottype signs at the beginning
  for(int i=0;i<s.length;i++)
  {skutu1[i]=s[i];}
  x1=x1i;
  y1=y1i;
  dx1=(xmax-xmin)/30.0;
  dy1=(ymax-ymin)/30.0;
 }
 
 public void addString(String s[],double x2i,double y2i)
 { // adding a series of strings 
  for(int i=0;i<s.length;i++)
  {skutu2[i]=s[i];}
  x2=x2i;
  y2=y2i;
  dx2=(xmax-xmin)/30.0;
  dy2=(ymax-ymin)/30.0;
 }
 
 public void addString(String s,double x3i,double y3i,int i)
 { // adding a series of strings 
  if(i>4) i=4;
  else if(i<0) i=0;
  label_at_xy[i]=s;
  label_x[i]=x3i;
  label_y[i]=y3i;
 }
 
 public void addData(String fn)
 { //adds data from file  	      
  double x[];
  double y[];
  double a[][]=Text.readDoubleT(fn);
  x=a[0];
  y=a[1]; 
  addData(x,y);    
 }
 
 public void addData(double xi[],double yi[])
 {
 addData(xi,yi,0,0,0,0);
 }

 public void addData(double ai[][])
 {
 pp2(ai);
 }
 
 public void addData(double ai[][],int iplottype[],int ir[],int ig[],int ib[])
 {pp2(ai,iplottype,ir,ig,ib);}
 
 public void addData(double ai[][],int iplottype[])
 {pp2(ai,iplottype);}
 
 public void addData(double xi[],double yi[],int iplottype)
 {
 addData(xi,yi,iplottype,0,0,0);
 }
  	  
 public void addData(double xi[],double yi[],int ipt,int ir,int ig,int ib)
 {
       int oldnmax=nmax;
       int oldnline=nline;
       nline+=1;
	   n[oldnline]=xi.length;
	   plottype[nline-1]=ipt;
       if(plottype[ipt] == 10) ch[ipt]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[ipt] == 11) ch[ipt]='\u2297';
       if(plottype[ipt] == 12) ch[ipt]='\u2299';
       if(plottype[ipt] == 13) ch[ipt]='\u229A';
       if(plottype[ipt] == 14) ch[ipt]='\u229B';
       if(plottype[ipt] == 15) ch[ipt]='\u229E';
       if(plottype[ipt] == 16) ch[ipt]='\u22A0';
       if(plottype[ipt] == 17) ch[ipt]='\u22A1';
       if(plottype[ipt] == 18) ch[ipt]='\u22B9';
       if(plottype[ipt] == 19) ch[ipt]='\u22C7';   
	   red[nline-1]=ir;
	   green[nline-1]=ig;
	   blue[nline-1]=ib;
	   if(xi.length>nmax)
       {
       nmax=xi.length;
       ni=nline;
	   }
	   double xa[][]=new double[nline][nmax];
	   double ya[][]=new double[nline][nmax];
       for(int i=0;i<oldnline;i++)
       {       
	      for(int j=0;j<n[i];j++)
          {
          xa[i][j]=x[i][j];
          ya[i][j]=y[i][j];
          }
      }
      for(int j=0;j<n[nline-1];j++)
          {
          xa[nline-1][j]=xi[j];
          ya[nline-1][j]=yi[j];
          }
      x=xa;
      y=ya;       
      setMinMax();
}

public void addData(double xi[][],double yi[][])
{
int ip[]=new int[0];
int ir[]=new int[0];
int ig[]=new int[0];
int ib[]=new int[0];
addData(xi,yi,ip,ir,ig,ib);
}
 
public void addData(double xi[][],double yi[][],int ip[])
{
int ir[]=new int[0];
int ig[]=new int[0];
int ib[]=new int[0];
addData(xi,yi,ip,ir,ig,ib);
}
 
public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[])
 {
       int oldnmax=nmax;
       int oldnline=nline;
       nline+=xi.length;
	   for(int i=oldnline;i<nline;i++)
	   {
	   plottype[i]=ip[i-oldnline];
       if(plottype[i] == 10) ch[i]='\u2295';//you can change character set by using setPlotType(0,'%');
       if(plottype[i] == 11) ch[i]='\u2297';
       if(plottype[i] == 12) ch[i]='\u2299';
       if(plottype[i] == 13) ch[i]='\u229A';
       if(plottype[i] == 14) ch[i]='\u229B';
       if(plottype[i] == 15) ch[i]='\u229E';
       if(plottype[i] == 16) ch[i]='\u22A0';
       if(plottype[i] == 17) ch[i]='\u22A1';
       if(plottype[i] == 18) ch[i]='\u22B9';
       if(plottype[i] == 19) ch[i]='\u22C7'; 
	   red[i]=ir[i-oldnline];
	   green[i]=ig[i-oldnline];
	   blue[i]=ib[i-oldnline];
	   n[i]=xi[i-oldnline].length;
	   if(xi[i-oldnline].length>nmax)
       {
       nmax=xi[i-oldnline].length;
       ni=i;
	   }
       }
	   double xa[][]=new double[nline][nmax];
	   double ya[][]=new double[nline][nmax];
       for(int i=0;i<oldnline;i++)
       {       
	      for(int j=0;j<n[i];j++)
          {
          xa[i][j]=x[i][j];
          ya[i][j]=y[i][j];
          }
      }
      int k=0;
      for(int i=oldnline;i<nline;i++)
      {     
      for(int j=0;j<n[i];j++)
          {
          xa[i][j]=xi[i-oldnline][j];
          ya[i][j]=yi[i-oldnline][j];          
          }
      }   
      x=xa;
      y=ya;       
      setMinMax();
}


public void addFunction(f_x f,double xmi,double xma,int N,int ipt)
{
addFunction(f,xmi,xma,N,ipt,0,0,0);	
}
 
public void addFunction(f_x f,double xmi,double xma,int N)
{
addFunction(f,xmi,xma,N,0);	
}

public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib)	
{
double xi[]=new double[N];
double yi[]=new double[N];
double dx=(xma-xmi)/(N-1);
double xx=xmi;
for(int j=0;j<N;j++)
{  
	xi[j]=xx;
	yi[j]=f.func(xx);
	xx+=dx;
}
addData(xi,yi,ipt,ir,ig,ib);
}

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function)
{
int ipt[]=new int[Number_of_Function];
int ir[]=new int[Number_of_Function];
int ig[]=new int[Number_of_Function];
int ib[]=new int[Number_of_Function];
addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function,ipt,ir,ig,ib);
}   

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[])
{
int ir[]=new int[Number_of_Function];
int ig[]=new int[Number_of_Function];
int ib[]=new int[Number_of_Function];
addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function,ipt,ir,ig,ib);
} 

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,
   int ipt[],int ir[],int ig[],int ib[])	
{
    int N=Number_of_Data;
    double dx=(xmax-xmin)/N;
    double xx=xmin;
    int NF=Number_of_Function;
    double xi[][]=new double[NF][N];
    double yi[][]=new double[NF][N];
    double yy[]=new double[NF];
    for(int j=0;j<N;j++)
    {  
	  yy=f.func(xx);
	  for(int i=0;i<NF;i++)
	    {  yi[i][j]=yy[i];
	       xi[i][j]=xx;}
	  xx+=dx;
    }
addData(xi,yi,ipt,ir,ig,ib);
}

 
public void setPlotType(int plot_type[])
{
for(int i=0;i<plot_type.length;i++)
  {
  setPlotType(i,plot_type[i]);
  }
setMinMax();
}

public void setXlogScaleOn()
{xlog=1;xminmaxlog=Math.log10(xmax-xmin+10.0)-1.0;}

public void setYlogScaleOn()
{ylog=1;yminmaxlog=Math.log10(ymax-ymin+10.0)-1.0;}

public void setXlogScaleOff()
{xlog=0;}

public void setYlogScaleOff()
{ylog=0;}
 	
public void setPlotType(int dataset,int plot_no)
{
  // plottype = 0 continuous line
  // plottype = 1 dashed line
  // plottype = 2
  // plottype = 3 
  // plottype = 10 variable character plot type 
        // plottype  = 10 ch[i]='*';//you can change character set by using setPlotType(0,'%');
        // plottype  = 11 ch[i]='#'
        // plottype  = 12 ch[i]='$'
        // plottype  = 13 ch[i]='%'
        // plottype  = 14 ch[i]='&'
        // plottype  = 15 ch[i]='£'
        // plottype  = 16 ch[i]='~'
        // plottype  = 17 ch[i]='+'
        //plottype   = 18 ch[i]='^'
        //plottype   = 19 ch[i]='='
  // plottype = 20 rectangle
  // plottype = 21 filled rectangle
  // plottype = 22 circle
  // plottype = 23 filled circle
  // plottype = 24 triangle
  // plottype = 25 diamond
  // plottype = 26 pentagon
  // plottype = 27 hexagon
  // plottype = 28 filled triangle
  // plottype = 29 filled diamond
  // plottype = 30 filled pentagon
  // plottype = 31 filled hexagon
  // plottype = 32 triangle star
  // plottype = 33 diamond star
  // plottype = 34 pentagon star
  // plottype = 35 hexagon star
  // plottype = 36 filled triangle star
  // plottype = 37 filled diamond star
  // plottype = 38 filled pentagon star
  // plottype = 39 filled hexagon star
  // plottype = 40 bar plot
  // plottype = 41 filled bar plot 
  plottype[dataset]=plot_no;
  if(plot_no==10) ch[dataset]='*';   //user defined char is defined as * 
}
 
public void setPlotType(int dataset,char plot_char)
{ 
  // plottype = 10 user defined character plot
  plottype[dataset]=10;
       if(plottype[10] == 10) ch[1]='\u2295';//you can change character set by using setPlotType(0,'%');
 
}

 
public void setXgrid(int igx)
{
  xgridon=igx;
}
 
public void setYgrid(int igy)
{
  ygridon=igy;
}
 
public void setGrid(int igx,int igy)
{
 xgridon=igx;
 ygridon=igy;
}
 
public void setXtic(int ixt)
{
  xntic=ixt;
}
 
public void setYtic(int iyt)
{
  yntic=iyt;
}
 
public void setXYtic(int ixt,int iyt)
{
    xntic=ixt;
	yntic=iyt;
}
 
public void setColor(int dataset,int ired,int igreen,int iblue)
{
 red[dataset]=ired;
 green[dataset]=igreen;
 blue[dataset]=iblue;
}

public void setColor(int dataset,Color x)
{
 red[dataset]=x.getRed();
 green[dataset]=x.getGreen();
 blue[dataset]=x.getBlue();
}
 
public void setColor(int ired[],int igreen[],int iblue[])
{
  for(int i=0;i<nline;i++)
  {	
   red[i]=ired[i];
   green[i]=igreen[i];
   blue[i]=iblue[i];
  }
}


public void setColor(Color x[])
{
  for(int i=0;i<nline;i++)
  {	
   red[i]=x[i].getRed();
   green[i]=x[i].getGreen();
   blue[i]=x[i].getBlue();
  }
}
 
public void setMinMax()
{   
	xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    for(int i=0;i<nline;i++)
       {       
	      for(int j=0;j<n[i];j++)
          {
          if(x[i][j]>xmax) xmax=x[i][j];
          if(x[i][j]<xmin) xmin=x[i][j];
          if(y[i][j]>ymax) ymax=y[i][j];
          if(y[i][j]<ymin) ymin=y[i][j];
          }
      }
}
 
public void setMinMax(double xi[][],double yi[][])
{
    xmin=9.99e50;
    xmax=-9.99e50;
    ymin=9.99e50;
    ymax=-9.99e50;
    for(int i=0;i<nline;i++)
          { 
	      if(xi[i].length>nmax) {nmax=xi[i].length;ni=i;}	      
	      for(int j=0;j<xi[i].length;j++)
          {
          if(xi[i][j]>xmax) xmax=xi[i][j];
          if(xi[i][j]<xmin) xmin=xi[i][j];
          if(yi[i][j]>ymax) ymax=yi[i][j];
          if(yi[i][j]<ymin) ymin=yi[i][j];
          }          
      }
}
 
public void setMinMax(double iminx,double imaxx,double iminy,double imaxy)
{
xmin=iminx;
ymin=iminy;
xmax=imaxx;
ymax=imaxy;
}
 
public void plot()
{
p1=new PlotW();
p1.plot(this);
}

public void dispose()
{
p1.pencere.setVisible( false);
p1.pencere.dispose();
}
public void exit()
{System.exit(0);}

//==== B-Spline intrepolation=============
  public static double[] hi(double ti[])
  {
  int n=ti.length-1;//ti 0..n
  int nn=n+2;
  double h[]=new double[nn];
  for(int i=1;i<=n;i++)
     {h[i]=ti[i]-ti[i-1];
     }
  h[0]=h[1];
  h[n+1]=h[n];
  return h;
  }
  
  public static double [] B_Spline(double ti[],double yi[])
  {
  int n=ti.length-1;
  int nn=n+2;
  int i;
  double delta,gamma,p,q,r;
  double a[]=new double[nn];
  double h[]=new double[nn];
  h=hi(ti);
  delta=-1;
  gamma=2.0*yi[0];
  p=delta*gamma;
  q=2.0;
  for(i=1;i<=n;i++)
  {r=h[i+1]/h[i];
  delta=-r*delta;
  gamma=-r*gamma+(r+1)*yi[i];
  p=p+gamma*delta;
  q=q+delta*delta;
  }
  a[0]=-p/q;
  for(i=1;i<=(n+1);i++)
  {a[i]=((h[i-1]+h[i])*yi[i-1]-h[i]*a[i-1])/h[i-1];}
  return a;
 }
 
  public static double funcB_Spline(double ti[],double a[],double x)
  {
  double h[]=hi(ti);
  int i;
  int n=ti.length-1;
  double nn=n+2;
  double d,e;
  for(i=(n-1);i>=1;i--)
    {if((x-ti[i])>=0) break;}
  i++;
  d=(a[i+1]*(x-ti[i-1])+a[i]*(ti[i]-x+h[i+1]))/(h[i]+h[i+1]);
  e=(a[i]*(x-ti[i-1]+h[i-1])+a[i-1]*(ti[i-1]-x+h[i]))/(h[i-1]+h[i]);
  double bs=(d*(x-ti[i-1])+e*(ti[i]-x))/h[i];
  return bs;
  }
   
 public static double[][]  funcB_Spline(double ti[],double yi[],int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=ti.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double A[]=B_Spline(ti,yi);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(ti[i+1]-ti[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcB_Spline(ti,A,z[0][k]);k++;}
       }
       z[0][k]=ti[i];z[1][k]=funcB_Spline(ti,A,z[0][k]);
    return z;
 }
public void addB_Spline(double xi[],double yi[],int nSpline)
 {
   addSpline(xi,yi,nSpline,0,0,0,0);
 }
 
 public void addB_Spline(double ai[][],int nSpline)
 {
   addSpline(ai[0],ai[1],nSpline,0,0,0,0);
 }
 
 public void addB_Spline(double xi[],double yi[],int nSpline,int iplottype)
 {
   addB_Spline(xi,yi,nSpline,iplottype,0,0,0);
 }
  
 public void addB_Spline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
    double S[][]=funcB_Spline(xi,yi,aradegersayisi);
    addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function)
    }
 
 public void addB_Spline(int linenumber,int nSpline)
{addB_Spline(linenumber,nSpline,0,0,0,0);}
 
 public void addB_Spline(int linenumber,int nSpline,int iplottype)
 {addB_Spline(linenumber,nSpline,iplottype,0,0,0);}
 
 public void addB_Spline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
  double x2[]=new double[n[linenumber]];
  double y2[]=new double[n[linenumber]];  
  if(linenumber<0) linenumber=0;
  else if(linenumber>nline) linenumber=nline;	 
  	      for(int j=0;j<n[linenumber];j++)
          { x2[j]=x[linenumber][j];
            y2[j]=y[linenumber][j];
          }   
  double S[][]=funcB_Spline(x2,y2,aradegersayisi);	
  addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function)
  }
//==== spline interpolation===============
  public static double [] thomas(double a[][],double r[])
  {
  //
  int n=a.length;
  double f[]=new double[n];
  double e[]=new double[n];
  double g[]=new double[n];
  double x[]=new double[n];
  for(int i=0;i<n;i++)     {f[i]=a[i][i];}
  for(int i=0;i<(n-1);i++) {g[i]=a[i][i+1];}
  for(int i=0;i<(n-1);i++) {e[i+1]=a[i+1][i];}
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
  
  public static double [] thomas(double f[],double e[],double g[],double r[])
  {
  int n=f.length;
  double x[]=new double[n];
  for(int k=1;k<n;k++)
     {e[k]=e[k]/f[k-1];
      f[k]=f[k]-e[k]*g[k-1];
     }
  for(int k=1;k<n;k++)
     {r[k]=r[k]-e[k]*r[k-1];
     }
  x[n-1]=r[n-1]/f[n-1];   
  for(int k=(n-2);k>=0;k--)
     {x[k]=(r[k]-g[k]*x[k+1])/f[k];}
  return x;      
  }
  
  
  public static double [][] cubic_spline(double xi[],double yi[],double c0,double cn)
  {
  int n=xi.length;
  double h[]=new double[n];
  double w[]=new double[n];
  double f[]=new double[n];
  double e[]=new double[n];
  double g[]=new double[n];
  double d[]=new double[n];
  double x[]=new double[n];
  double S[][]=new double[4][n];
  int k;	  
  for(k=0;k<(n-1);k++)
     {h[k]=xi[k+1]-xi[k];
      w[k]=(yi[k+1]-yi[k])/h[k];      
	 }
  d[0]=c0;
  d[n-1]=cn;
  for(k=1;k<(n-1);k++)
     {d[k]=6.0*(w[k]-w[k-1]);}
  f[0]=1.0;
  f[n-1]=1.0;
  g[0]=0.0;
  g[n-1]=0.0;
  e[0]=0.0;
  e[n-1]=0.0;
  for(k=1;k<(n-1);k++)
  {f[k]=2.0*(h[k]+h[k-1]);e[k]=h[k-1];g[k]=h[k];}
  S[2]=thomas(f,e,g,d);
  S[3]=xi;
  for(k=0;k<(n-1);k++)
    {S[0][k]=(6.*yi[k+1]-h[k]*h[k]*S[2][k+1])/(6.0*h[k]);
     S[1][k]=(6.*yi[k]-h[k]*h[k]*S[2][k])/(6.0*h[k]);
    } 
  return S;    	 	     	 
  }
  
  public static double funcSpline(double S[][],double x)
  {
  int n=S[0].length;
  double xx1=0;
  double xx2=0;
  double y=0;
  double hk=0;
  for(int k=0;k<(n-1);k++)
     {if(S[3][k]<=x && x<=S[3][k+1]) 
      {hk=(S[3][k+1]-S[3][k]);
       xx1=(x-S[3][k]);
       xx2=(S[3][k+1]-x);
       y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk);
       break;
       }
     }
       if(y==0 && S[3][n-2]<=x )
       {    
	   int k=n-2;
	   hk=(S[3][k+1]-S[3][k]);
       xx1=(x-S[3][k]);
       xx2=(S[3][k+1]-x);
       y=S[0][k]*xx1+S[1][k]*xx2+(xx1*xx1*xx1*S[2][k+1]+xx2*xx2*xx2*S[2][k])/(6.0*hk);
       }
  return y;
  }     
   
 public static double[][]  funcSpline(double xi[],double yi[],int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double S[][]=cubic_spline(xi,yi,0,0);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcSpline(S,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcSpline(S,z[0][k]);
    return z;
 }   


public void addSpline(double xi[],double yi[],int nSpline)
 {
   addSpline(xi,yi,nSpline,0,0,0,0);
 }
 
 public void addSpline(double ai[][],int nSpline)
 {
   addSpline(ai[0],ai[1],nSpline,0,0,0,0);
 }
 
 public void addSpline(double xi[],double yi[],int nSpline,int iplottype)
 {
   addSpline(xi,yi,nSpline,iplottype,0,0,0);
 }
  
 public void addSpline(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
    double S[][]=funcSpline(xi,yi,aradegersayisi);
    addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function)
    }
 
 public void addSpline(int linenumber,int nSpline)
{addSpline(linenumber,nSpline,0,0,0,0);}
 
 public void addSpline(int linenumber,int nSpline,int iplottype)
 {addSpline(linenumber,nSpline,iplottype,0,0,0);}
 
 public void addSpline(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
  double x2[]=new double[n[linenumber]];
  double y2[]=new double[n[linenumber]];  
  if(linenumber<0) linenumber=0;
  else if(linenumber>nline) linenumber=nline;	 
  	      for(int j=0;j<n[linenumber];j++)
          { x2[j]=x[linenumber][j];
            y2[j]=y[linenumber][j];
          }   
  double S[][]=funcSpline(x2,y2,aradegersayisi);	
  addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of spline function)
  }
//==========orthogonal polynomial interpolation (curve fitting)
public static double[][] OPEKK(double xi[],double fi[],int m)
//ortogonal polinom en küçük kareler metodu
//Referans : A First Course in Numerical Analysis
// Anthony Ralston,Philip Rabinowitz, Mc Graw Hill ISBN 0-07-051158-6 
//m polinom derecesi xi fi girdi verisi
{
int i,j,k;
int n=xi.length;
int mp2=n+2;
int mp1=n+1;
double p[][]=new double[mp2][n];
double gamma[]=new double[mp1];
double beta[]=new double[mp1];
double omega[]=new double[mp1];
double alpha[]=new double[mp1];
double b[]=new double[mp1];
double wi[]=new double[n];
double a[][]=new double[3][mp1];
for(i=0;i<n;i++)
  {
   p[1][i]=1.0;
   p[0][i]=0.0;
   wi[i]=1.0;
  }
gamma[0]=0;
for(i=0;i<n;i++)
  {
  gamma[0]+=wi[i];
  }
beta[0]=0.0;
for(j=0;j<m+1;j++)
  {
  omega[j]=0;
  for(i=0;i<n;i++)
    {omega[j]+=wi[i]*fi[i]*p[j+1][i];}
  b[j]=omega[j]/gamma[j];
if( j != m)
    {
    alpha[j+1]=0;
    for(i=0;i<n;i++)
      { alpha[j+1]+=wi[i]*xi[i]*p[j+1][i]*p[j+1][i]/gamma[j];}
    for(i=0;i<n;i++)
      {p[j+2][i]=(xi[i]-alpha[j+1])*p[j+1][i]-beta[j]*p[j][i];}
      gamma[j+1]=0;
    for(i=0;i<n;i++)
      {gamma[j+1]+=wi[i]*p[j+2][i]*p[j+2][i];}
    beta[j+1]=gamma[j+1]/gamma[j];
    }
}//end of j
for(j=0;j<m+1;j++)
  {
  a[0][j]=b[j];
  a[1][j]=alpha[j];
  a[2][j]=beta[j];
  }
return a;
}

public static double funcOPEKK(double a[][],double x)
{
// polinom değerleri hesaplama fonksiyonu
double yy=0;
int k;
int m=a[0].length-1;
int mp2=m+2;
double q[];
q=new double[mp2];
//vector<double> q(m+2,0.0);
for(k=m-1;k>=0;k--)
  {
  q[k]=a[0][k]+(x-a[1][k+1])*q[k+1]-a[2][k+1]*q[k+2];
  yy=q[k];
  }
return yy;
}

public static double[][]  funcOPEKK(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	 int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double E[][]=OPEKK(xi,yi,polinomkatsayisi);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {z[0][k]=xi[i];z[1][k]=funcOPEKK(E,z[0][k]);k++;
       for(int j=0;j<aradegersayisi;j++) 
       {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
       z[0][k]=z[0][k-1]+dx;z[1][k]=funcOPEKK(E,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcOPEKK(E,z[0][k]);
    return z;
 }

public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi)
 {
   addOPEKK(xi,yi,npolinom,aradegersayisi,0,0,0,0);
 }
 
 public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype)
 {
   addOPEKK(xi,yi,npolinom,npolinom,iplottype,0,0,0);
 }
  
 public void addOPEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
    double S[][]=funcOPEKK(xi,yi,npolinom,aradegersayisi);
    addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of OPEKK function)
    }
  
 public void addOPEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype)
 {addOPEKK(linenumber,npolinom,aradegersayisi,iplottype,0,0,0);}
 
 public void addOPEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
  double x2[]=new double[n[linenumber]];
  double y2[]=new double[n[linenumber]];  
  if(linenumber<0) linenumber=0;
  else if(linenumber>nline) linenumber=nline;	 
  	      for(int j=0;j<n[linenumber];j++)
          { x2[j]=x[linenumber][j];
            y2[j]=y[linenumber][j];
          }   
  double S[][]=funcOPEKK(x2,y2,npolinom,aradegersayisi);
  addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of OPEKK function)
  }
// en kucuk kareler
  public static double[] pivotlugauss(double a[][],double b[])
  { //kısmi pivotlu gauss eleme yöntemi
  int n=b.length;	  
  double x[]=new double[n];
  double carpan=0;
  double toplam=0;
  double buyuk;
  double dummy=0;
  //gauss eleme
  int i,j,k,p,ii,jj;
  for(k=0;k<(n-1);k++)
  { //pivotlama
	p=k;
	buyuk=Math.abs(a[k][k]);
	for(ii=k+1;ii<n;ii++)
	{  dummy=Math.abs(a[ii][k]);
	   if(dummy > buyuk) {buyuk=dummy;p=ii;}
	}
	if(p!=k)
	{ for(jj=k;jj<n;jj++)
	  { dummy=a[p][jj];
	    a[p][jj]=a[k][jj];
	    a[k][jj]=dummy;
	  }
	  dummy=b[p];
	  b[p]=b[k];
	  b[k]=dummy;
	}
	//gauss elemeyi çözme  
    for(i=k+1;i<n;i++)
    {  carpan=a[i][k]/a[k][k];
       a[i][k]=0;
       for(j=k+1;j<n;j++)
       {   a[i][j]-=carpan*a[k][j]; }
           b[i]   =b[i]   -carpan*b[k];
	}
  }
  //geriye doğru yerine koyma
  x[n-1]=b[n-1]/a[n-1][n-1];
  for(i=n-2;i>=0;i--)
  {
    toplam=0;
    for(j=i+1;j<n;j++)
    {  toplam+=a[i][j]*x[j];}
    x[i]=(b[i]-toplam)/a[i][i];
  }
  return x;
}
  
public static double[] EKK(double xi[],double yi[],int n)
{
int l=xi.length;
int i,j,k;
int np1=n+1;
double A[][];
A=new double[np1][np1];
double B[];
B=new double[np1];
double X[];
X=new double[np1];
for(i=0;i<n+1;i++)
  {  for(j=0;j<n+1;j++)
     {if(i==0 && j==0) A[i][j]=l;
     else  for(k=0;k<l;k++) A[i][j] += Math.pow(xi[k],(i+j));
     }
     for(k=0;k<l;k++) { if(i==0)  B[i]+= yi[k];
                       else      B[i] += Math.pow(xi[k],i)*yi[k];}
  }
System.out.println(Matrix.toString(A));
System.out.println(Matrix.toStringT(B));  
X=pivotlugauss(A,B);
//X=B/A;
double max=0;
for(i=0;i<n+1;i++)
 if(Math.abs(X[i]) > max) max = Math.abs(X[i]);
for(i=0;i<n+1;i++)
 if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0;
return X;
}

public static double funcEKK(double e[],double x)
{
// this function calculates the value of
// least square curve fitting function
int n=e.length;
double ff;
if(n!=0.0)
  { ff=e[n-1];
  for(int i=n-2;i>=0;i--)
   { ff=ff*x+e[i]; }
  }
else
  ff=0;
return ff;
}

public static double hata(double x[],double y[],double e[])
{
//calculates absolute square root error of a least square approach
double n=x.length;
 int k;
 double total=0;
 for(k=0;k<n;k++)
 {
 total+=(y[k]-funcEKK(e,x[k]))*(y[k]-funcEKK(e,x[k]));
 }
total=Math.sqrt(total);
return total;
}

public static double[][]  funcEKK(double xi[],double yi[],int polinomkatsayisi,int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	 int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double E[]=EKK(xi,yi,polinomkatsayisi);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {z[0][k]=xi[i];z[1][k]=funcEKK(E,z[0][k]);k++;
       for(int j=0;j<aradegersayisi;j++) 
       {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
       z[0][k]=z[0][k-1]+dx;z[1][k]=funcEKK(E,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcEKK(E,z[0][k]);
    return z;
 } 
public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi)
 {
   addEKK(xi,yi,npolinom,aradegersayisi,0,0,0,0);
 }
 
 public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype)
 {
   addEKK(xi,yi,npolinom,npolinom,iplottype,0,0,0);
 }
  
 public void addEKK(double xi[],double yi[],int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
    double S[][]=funcEKK(xi,yi,npolinom,aradegersayisi);
    addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of EKK function)
    }
  
 public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype)
 {addEKK(linenumber,npolinom,aradegersayisi,iplottype,0,0,0);}
 
 public void addEKK(int linenumber,int npolinom,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
  double x2[]=new double[n[linenumber]];
  double y2[]=new double[n[linenumber]];  
  if(linenumber<0) linenumber=0;
  else if(linenumber>nline) linenumber=nline;	 
  	      for(int j=0;j<n[linenumber];j++)
          { x2[j]=x[linenumber][j];
            y2[j]=y[linenumber][j];
          }   
  double S[][]=funcEKK(x2,y2,npolinom,aradegersayisi);
  addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of EKK function)
  } 
//=====Lagrange interpolasyon
  public static double[][]  Lagrange(double xi[],double yi[])
  {
	 int n=xi.length;
     double L[][]=new double[2][n];
     for(int i=0;i<n;i++)
     {L[0][i]=yi[i];
     for(int j=0;j<n;j++)
     {if(i!=j) L[0][i]/=(xi[i]-xi[j]);}
     }
     for(int i=0;i<n;i++)
     {L[1][i]=xi[i];}
     return L;
  }
  	
  public static double  funcLagrange(double L[][],double x)
  {
	 int n=L[0].length;
     double carpim=1.0;
     double toplam=0;
     for(int i=0;i<n;i++)
     {carpim=L[0][i];
     for(int j=0;j<n;j++)
     {if(i!=j) carpim*=(x-L[1][j]);}
     toplam+=carpim;
     }
     return toplam;
  }
   
 public static double[][]  funcLagrange(double xi[],double yi[],int aradegersayisi)
 {
	//aradegersayisi:  x--o--o--x--o--o--x zincirinde x deneysel noktalar ise
	// ara değer sayısı 2 dir
	int n=xi.length;
	int nn=(n-1)*(aradegersayisi+1)+1;
	double z[][]=new double[2][nn];
    double Q[][]=Lagrange(xi,yi);	
    double dx=0;
    int k=0;
    int i;
    for(i=0;i<(n-1);i++)
       {  z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]);k++;
          for(int j=0;j<aradegersayisi;j++) 
             {dx=(xi[i+1]-xi[i])/((double)aradegersayisi+1.0);
          z[0][k]=z[0][k-1]+dx;z[1][k]=funcLagrange(Q,z[0][k]);k++;}
       }
       z[0][k]=xi[i];z[1][k]=funcLagrange(Q,z[0][k]);
    return z;
 }

public void addLagrange(double xi[],double yi[],int aradegersayisi)
 {
   addLagrange(xi,yi,aradegersayisi,0,0,0,0);
 }
 
 public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype)
 {
   addLagrange(xi,yi,aradegersayisi,iplottype,0,0,0);
 }
  
 public void addLagrange(double xi[],double yi[],int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
    double S[][]=funcLagrange(xi,yi,aradegersayisi);
    addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of Lagrange function)
    }
  
 public void addLagrange(int linenumber,int aradegersayisi,int iplottype)
 {addLagrange(linenumber,aradegersayisi,iplottype,0,0,0);}
 
 public void addLagrange(int linenumber,int aradegersayisi,int iplottype,int ir,int ig,int ib)
 {
  double x2[]=new double[n[linenumber]];
  double y2[]=new double[n[linenumber]];  
  if(linenumber<0) linenumber=0;
  else if(linenumber>nline) linenumber=nline;	 
  	      for(int j=0;j<n[linenumber];j++)
          { x2[j]=x[linenumber][j];
            y2[j]=y[linenumber][j];
          }   
  double S[][]=funcLagrange(x2,y2,aradegersayisi);
  addData(S[0],S[1],iplottype,ir,ig,ib); //one set of additional data (of Lagrange function)
  }
  
  public void setBackground(Color  b1)
  {bg=b1;}
  
  public void setBackground(int ir,int ig,int ib)
  {bg=new Color(ir,ig,ib);}
     //Static plot functions 
     //===========
     
     public static Plot func(f_x ff1,double xmin,double xmax,int n,int pnumber)
     {    
     Plot pp=new Plot(ff1,xmin,xmax,n,pnumber);
     pp.plot();
     return pp;
     }
     public static Plot func(f_x ff1,double xmin,double xmax,int n)
     {    
     Plot pp=new Plot(ff1,xmin,xmax,n,0);
     pp.plot();
     return pp;
     }
     
     public static Plot func(f_x ff1,double xmin,double xmax)
     {
     Plot pp=new Plot(ff1,xmin,xmax,300,0);
     pp.plot();
     return pp;
     }

     public static Plot func(fi_x ff1,double xmin,double xmax,int n,int pnumber)
     {    
     Plot pp=new Plot(ff1,xmin,xmax,n,pnumber);
     pp.plot();
     return pp;
     }
     public static Plot func(String  s,double xmin,double xmax,int n)
     {    
     Plot pp=new Plot(s,xmin,xmax,n,0);
     pp.plot();
     return pp;
     }
     public static Plot func(String  s,double xmin,double xmax)
     {    
     Plot pp=new Plot(s,xmin,xmax,400,0);
     pp.plot();
     return pp;
     }
     public static Plot func(fi_x ff1,double xmin,double xmax,int n)
     {    
     Plot pp=new Plot(ff1,xmin,xmax,n,0);
     pp.plot();
     return pp;
     }
     
     public static Plot func(fi_x ff1,double xmin,double xmax)
     {
     Plot pp=new Plot(ff1,xmin,xmax,300,0);
     pp.plot();
     return pp;
     }
     
     public static Plot func(f_x ff1,f_x ff2,double xmin,double xmax,int n)
     {    
     Plot pp=new Plot(ff1,xmin,xmax,n);
     pp.addFunction(ff2,xmin,xmax,n);
     pp.plot();
     return pp;
     }
     
     public static Plot func(f_x ff1,f_x ff2,double xmin,double xmax)
     {    
     Plot pp=new Plot(ff1,xmin,xmax,300);
     pp.addFunction(ff2,xmin,xmax,300);
     pp.plot();
     return pp;
     }
     
     public static Plot file(String ff1)throws IOException
     {
     Plot pp=new Plot(ff1);
     pp.plot();
     return pp;
     }
     
     public static Plot file()throws IOException
     {
     Plot pp=new Plot();
     pp.plot();
     return pp;
     }
     
     public static Plot file(String ff1,String ff2)throws IOException
     {
	 String ff3[]={ff1,ff2};
     Plot pp=new Plot(ff3);
     pp.plot();
     return pp;
     }
     
     public static Plot file(String ff1,String ff2,String ff3)throws IOException
     {
	 String ff4[]={ff1,ff2,ff3};
     Plot pp=new Plot(ff4);
     pp.plot();
     return pp;
     }
     
     public static Plot file(String ff1,String ff2,String ff4,String ff3)throws IOException
     {
	 String ff5[]={ff1,ff2,ff3,ff4};
     Plot pp=new Plot(ff5);
     pp.plot();
     return pp;
     }
     
     public static Plot file(String ff1[])throws IOException
     {
     Plot pp=new Plot(ff1);
     pp.plot();
     return pp;
     }
     
     public static Plot data(double x[],double y[])
     {
     Plot pp=new Plot(x,y);
     pp.plot();
     return pp;
     }
     
     public static Plot data(double x[],double y1[],double y2[])
     {
     Plot pp=new Plot(x,y1);
     pp.addData(x,y2);
     pp.plot();
     return pp;
     }
     
     public static Plot data(double x[],double y1[],double y2[],double y3[])
     {
     Plot pp=new Plot(x,y1);
     pp.addData(x,y2);
     pp.addData(x,y3);
     pp.plot();
     return pp;
     }
     
     public static Plot data(double a[][])
     {
     Plot pp=new Plot(a);
     pp.plot();
     return pp;
     }  
 public static void main(String args[]) throws IOException  
{Plot pp=new Plot("a.txt");  
pp.plot();}  
             
} 
