//======================================================
// Numerical Analysis package in java
// example to show utilisation of integration (integral)
// and differentiation (derivative) functions
// Dr. Turhan Coban
// =====================================================
import java.io.*;

class f1 extends f_x
{
  double func(double x)
  {return Math.sin(x);}
}


class integ17
{
public static double adaptive_boole_integral(f_x ff,double a,double b)
{   int i;
	Plot pp=new Plot(ff,a,b,100);
    double h=(b-a);
    double h2=h/4.0;
    double h3=h/8.0;
    double m=(a+b)/2.0;
	double x[]={a,m,b};
    double y[]=new double[3];
    double x1[]=new double[5];
    double y1[]=new double[5];
    double c[]={0.9501,0.2311,0.6068,0.4860,0.8913};
    double sum1=0;double sum2=0;
    for(i=0;i<3;i++) {y[i]=ff.func(x[i]);sum1+=y[i];}
    for(i=0;i<5;i++) {x1[i]=a+c[i]*h;y1[i]=ff.func(x1[i]);sum2+=y1[i];}
    double fa = y[0]; 
    double fm = y[1]; 
    double fb = y[2];
    double is = h3*(sum1+sum2);
    if(is==0) is = b-a;
    double Q = adaptboolep(ff,a,b,is);
    return Q;	
}
	
public static double adaptboolep(f_x ff,double a,double b,double is)
{
  double m = (a + b)/2; 
  double h = (b - a)/4;
  double h1=(b - a)/2.0;  
  double h3=(b - a)/8.0;    
  double x[]=new double[9];
  double y[]=new double[9];
  double Q=0;
  for(int i=0;i<9;i++) {x[i]=a+i*h3;y[i]=ff.func(x[i]);}
  double i1=h*(7.0*y[0]+32.0*y[2]+12.0*y[4]+32.0*y[6]+7.0*y[8])/90.0;
  double sum1=h1*(7.0*y[0]+32.0*y[1]+12.0*y[2]+32.0*y[3]+7.0*y[4])/90.0; 
  double sum2=h1*(7.0*y[4]+32.0*y[5]+12.0*y[6]+32.0*y[7]+7.0*y[8])/90.0; 
  double i2=sum1+sum2; 
  System.out.println("i1="+i1+"i2="+i2+"is="+is);
  i1 = (16.0*i2 - i1)/15.0; //romberg integrasyonu ile iyileştirme  
  if (((is + (i1-i2)) == is) || (m <= a) || (b<=m))
  {
    if ((m <= a) || (b<=m))
       {System.out.println("Interval contains no more machine number.\n"+
       "Required tolerance may not be met.");}
    Q = i1;
  }  
  else
  {
    Q = adaptboolep(ff,a,m,is)+
        adaptboolep(ff,m,b,is);
  }
  return Q;
}

  public static void main(String args[]) throws IOException
  {
  f1 b1=new f1();
  System.out.println("integral of class         f1 : "+adaptive_boole_integral(b1,0.0,Math.PI));
  }
}
