Skip to content

Instantly share code, notes, and snippets.

@ir4y
Created June 26, 2013 15:37
Show Gist options
  • Save ir4y/5868470 to your computer and use it in GitHub Desktop.
Save ir4y/5868470 to your computer and use it in GitHub Desktop.
public class Lagrange {
interface LagrangeFun {
double computate(double x);
}
public static LagrangeFun interpolate(final double[] x, final double[] y,final int n){
return new LagrangeFun() {
@Override
public double computate(double argx) {
double c, s=0;
for (int i=0; i<n; i++)
{
c=1;
for (int j=0; j<n; j++)
{
if (i!=j) c*=(argx-x[j])/(x[i]-x[j]);
}
s+=c*y[i];
}
return s;
}
};
}
}
public class HelloLambda {
public static void main(String[] args) {
double[] xs = new double[10];
double[] ys = new double[10];
int i,j;
for(i=0,j=-5,xs[i]=j; i<10;i++)
ys[i] = j*j;
Lagrange.LagrangeFun y_pow_x = Lagrange.interpolate(xs,ys,10);
for(i=-50; i<=50;i++)
System.out.println(y_pow_x.computate(i));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment