class f1gibbs extends f_xj
{	gibbsSpecies[] gs;
	double[][] A;
    double[] b0;
	double[] b;
	double[] mu;
	double P;
	double T;
	double Te[];
	String gn[];
	double n0[];
	double G0;
	int l,NG;
	public f1gibbs(String igs[],double[] in0,double iTe[],double iT,double iP){
			     T=iT;
			     P=iP;
		         Te=new double[iTe.length];
		         gn=new String[igs.length];
		         gs=new gibbsSpecies[igs.length];
		         n0=new double[in0.length];
			     for(int i=0;i<igs.length;i++)			     
			     {Te[i]=iTe[i];gn[i]=igs[i];gs[i]=new gibbsSpecies(gn[i],"gas");n0[i]=in0[i];}
			  	 gs=new gibbsSpecies[igs.length];		
			     for(int i=0;i<gs.length;i++){gs[i]=new gibbsSpecies(igs[i],"gas");}
	    	     int nn=gs.length;
			     int nat=110;
			     double B[][]=new double[nat][nn];
			     int nk=0;		
			     for(int i=0;i<nn;i++){
				    for(nk=0;nk<gs[i].atomList.length;nk++){
					   B[gs[i].atomList[nk].number-1][i]=gs[i].atomList[nk].N;
					}
				 }
			     int natcount=0;
			     boolean zeroflag[]=new boolean[110];
			     for(int j=0;j<nat;j++)
			     {zeroflag[j]=false;for(int i=0;i<nn;i++){if(B[j][i]!=0){zeroflag[j]=true;}};if(zeroflag[j]) natcount++;}
			     A=new double[natcount][nn];		
			     int k=0;
			     for(int j=0;j<nat;j++)
			      {if(zeroflag[j]){A[k]=B[j];k++;}}	
			     l=A.length;
			     NG=A[0].length;
			     b0=new double[l];
			     for(int ii=0;ii<b0.length;ii++){
				   for(int ij=0;ij<NG;ij++){
					  double carpim=A[ii][ij]*n0[ij];	
					  b0[ii]+=carpim;		
				      }						
			       }
	    double nt=0;
       for(int i=0;i<NG;i++) nt+=n0[i];
       double G0=0;
       for(int i=0;i<NG;i++) {G0+=gs[i].mu(Te[0],P,n0[i],nt);  
            }					
		}		
	public double func(double x[]) 
    {  double nt=0;
       for(int i=0;i<NG;i++) nt+=x[i];
       double G=0;
       for(int i=0;i<NG;i++) {G+=gs[i].mu(T,P,x[i],nt);}  
       b=new double[l];
			     for(int ii=0;ii<b0.length;ii++){
				   for(int ij=0;ij<NG;ij++){
					  double carpim=A[ii][ij]*x[ij];	
					  b[ii]+=carpim;		
				      }
				      b[ii]=b[ii]-b0[ii];						
			       }  
       for(int i=NG;i<l;i++) G+=x[i]*b[i-NG];
	    return G;
	   }
	   
	    public double[] setX()
	    {double xx[]=new double[NG+l];
	        for(int i=0;i<NG;i++){xx[i]=n0[i];}
	        for(int i=NG;i<l;i++) xx[i]=1.0;
	      return xx;  
	    }
}