//======================================================
// Numerical Analysis package in java
// example to show utilisation of integration (integral)
// and differentiation (derivative) functions
// Gauss-Legendre integration with two variables
// constant number of coefficients (60 coefficients) 
// and roots
// Dr. Turhan Coban
// =====================================================
import java.io.*;

abstract class fdi
{
public double a,b;
public fdi(double ai,double bi)
{a=ai;b=bi;}
public fdi()
{a=0;b=1;}
public void setab(double ai,double bi)
{a=ai;b=bi;}	
abstract public  double c(double x);
abstract public  double d(double x);
abstract public  double f(double x[]); 
}

class f1 extends fdi 
{
  public f1(double ai,double bi)
  {super(ai,bi);}
  //lower integration limit function
  public double c(double x)
  {return 1.0;}
  //upper integration limit function
  public double d(double x)
  {return 1.5;}
  
  public double f(double x[]) 
  {
  //function required to integrate
  double ff;
  ff=Math.log(x[0]+2.0*x[1]);
  return ff; 
  }
}  


class NA75
{
	
public static double I(fdi f) 
{
//integral f(x)dx
//integral of a function by using gauss-legendre quadrature
//coefficients are pre-calculated for 60 terms for [-1,1]
//band then utilises variable transform
 int i,j;
 int n=8;
 int n1=n/2;
 double r[],c[];
 r=new double[n];
 c=new double[n];
 r[ 0] = -0.861136311594052;
 r[ 1] = -0.339981043584856;
 r[ 2] = 0.339981043584856;
 r[ 3] = 0.861136311594052;
 c[ 0] = 0.347854845137447;
 c[ 1] = 0.652145154862546;
 c[ 2] = 0.652145154862546;
 c[ 3] = 0.347854845137447;
for(i=0;i<n1;i++) {r[i+n1]=-r[i];c[i+n1]=c[i];}
int m=n;
double x[],h1,h2,J,a1,b1,c1,d1,k1,k2,JX,Q;
x=new double[2];
//1=========================
a1=f.a;
b1=f.b;
h1=(b1-a1)/2.0;
h2=(b1+a1)/2.0;
J=0;
//2=========================
for(i=0;i<m;i++)
{
//3=========================
JX=0;
x[0]=h1*r[i]+h2;
d1=f.d(x[0]);
c1=f.c(x[0]);
k1=(d1-c1)/2.0;
k2=(d1+c1)/2.0;
//4=========================
    for(j=0;j<n;j++)
    {
	x[1]=k1*r[j]+k2;
	Q=f.f(x);
	JX+=c[j]*Q;
	}
//System.out.println("JX="+JX);	
//=========================	
J=J+c[i]*k1*JX;	
}
J=h1*J;
return J;
}

  public static void main(String args[]) throws IOException
  {
  f1 ff=new f1(1.4,2.0);	  
  System.out.println("integral of class         f1 : "+I(ff));
}
}
