//======================================================
// Numerical Analysis package in java
// Clenshaw-Curtis integration formula
// 
// Dr. Turhan Coban
// =====================================================
import java.io.*;


class f1 extends f_x
{
  public double func(double x)
  {return x*Math.sqrt(x);}
}


class integ12
{

public static double[] clenshaw_curtis_coefficients(f_x f,double x1,double x2,int n)
{
    // f integrating function
    // x1,x2 integral limits
    // n : number of sub divisions  
	double a[]=new double[2*n];
	double cc=-1;
	int i,k;
	double x;
	double k1=(x2-x1)/2.0;
    double k2=(x2+x1)/2.0;
	for(k=0;k<n;k++)
	{
	a[k]=0.0;	
	for (i=1;i<n;i++) 
	   {x=k2+k1*Math.cos(Math.PI*(double)i/(double)n); 
	    a[k]+=f.func(x)*Math.cos((double)i*(double)k*Math.PI/(double)n);
	   }
    cc*=-1;
	a[k]+=f.func(x2)/2.0+f.func(x1)/2.0*cc;
	a[k]*=2.0/(double)n;
    }
    //for(i=0;i<50;i++)
    //{for(k=0;k<n;k++){a[i*n+k]=a[k];}}
    return a;
}

public static double clenshaw_curtis_integral(f_x f,double x1,double x2,int n)
{
double a[]=clenshaw_curtis_coefficients(f,x1,x2,n);
double z=a[0];
double k1=(x2-x1)/2.0;
double k2=(x2+x1)/2.0;
for(int k=1;k<n;k+=2)
{ z+=2.0*a[2*k]/(1.0-4.0*(double)(k*k));}
return k1*z;
}

  public static void main(String args[]) throws IOException
  {
  //Gauss-Legendre integrali
  f1 b1=new f1();
  System.out.println("integral  : "+clenshaw_curtis_integral(b1,0.0,1.0,10));
}
}
