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

class f1 extends f_xj
{
public double func(double x[]) 
{
//çözümü istenen fonksiyon	
double ff;
ff=2.0*x[0]*x[1]+2.0*x[0]-x[0]*x[0]-2.0*x[1]*x[1];
return -ff; //minimum değil maxsimum istendiğinden - ile çarptık
}
}

class NA74
{
public static double monte_carlo(f_xj ff,double a[],double b[], long n)
{
	    int m=a.length;
	    double x[]=new double[m];
	    double h=1.0;
	    double sum=0;
	    for(int j=0;j<m;j++) {h*=(b[j]-a[j]);}
	    for(int i=0;i<n;i++)
        { for(int j=0;j<m;j++) {x[j]=a[j]+(b[j]-a[j])*Math.random();}
          sum+=ff.func(x); 
        }
        return h*sum/(double)n; 
}

  public static void main(String args[]) throws IOException
  {
  f1 b1=new f1();
  double a[]=new double[2];
  double b[]=new double[2];
  a[0]=-1;
  a[1]=-1;
  b[0]=1;
  b[1]=1;
  System.out.println("integral of class         f1 : "+monte_carlo(b1,a,b,1000000));
}}
