public class Maclaurin
{
public static double[] Maclaurin(f_x f,double x,int n)
{
double a[]=new double[n+1];
double fact=1.0;
for(int i=0;i<=n;i++)
{a[i]=f.dfunc(x,i)/fact;fact*=(i+1);}
return a;
}

public static double[] Maclaurin(if_x f,double x0,int n)
{
double a[]=new double[n+1];
double fact=1.0;
for(int i=0;i<=n;i++)
{a[i]=f.dfunc(x0,i)/fact;fact*=(i+1);}
return a;
}
public static double poly_func(double e[],double x,double x0)
{
// this function calculates the value of
// least square curve fitting function
// y=a0+a1*(x-x0)+a2*(x-x0)^2+a3*(x-x0)^3+..
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-x0)+e[i]; }
  }
else
  ff=0;
return ff;
}
public static void main(String arg[])
{ if_x f=x->Math.sin(x);
  double a[]=Maclaurin(f,1.0,20);
  double y=poly_func(a,Math.PI/2.0,1.0);
  System.out.println("y="+y+"y library="+Math.sin(Math.PI/2.0));
 }
}
