///======================================================
// 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
{
  public double func(double x)
  {return x*Math.sqrt(x);}
}


class integ12A
{

public static double[] clenshaw_curtis_coefficients(f_x f1,double a,double b,int n)
{
int k,j;
double fac,bpa,bma;
double f[]=new double[n];
double c[]=new double[2*n];
bma=0.5*(b-a);
bpa=0.5*(b+a);
double y;
for (k=0;k<n;k++) {y=Math.cos(Math.PI*(k+0.5)/n);f[k]=f1.func(y*bma+bpa);}
fac=2.0/n;
for (j=0;j<n;j++) {
double sum=0.0; 
for (k=0;k<n;k++)
sum += f[k]*Math.cos(Math.PI*j*(k+0.5)/n);
c[j]=fac*sum;
}
return c;
}

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*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,100));
}
}
