#include <stdlib.h>
#include "stack-c.h"
#include "api_scilab.h"
#include "Scierror.h"
#include "sciprint.h"
#include "string.h"

#include "FiniteSizeScaling.h"
#include "Erreur.h"

/***************************************
 * SCILAB function : RecursionNoe      *
 ***************************************/

int gtw_RecursionNoe ( char * fname )
{
    int m,n,l;
	int N;
    double * p ;
	double Pn ;
	int * g , * h ;
    int un=1;

    CheckRhs(3,3);
    CheckLhs(1,1);
    
    /* Variable p */

    GetRhsVar(1,"d",&m,&n,&l);
	CheckVector(1,m,n);
    p = stk(l);

    N = ( m*n - 1 ) / 2 ;
	
	/* Variable g */
	
	GetRhsVar(2,"i",&m,&n,&l);
	CheckVector(2,m,n);
	CheckLength(2,m*n,2*N+1);
    g = istk(l);
	
	/* Variable h */
	
	GetRhsVar(3,"i",&m,&n,&l);
	CheckVector(3,m,n);
	CheckLength(3,m*n,2*N+1);
    h = istk(l);
	
	/* Appel à l'API */
	
	if ( RecursionNoe ( N , p , g , h , & Pn ) )
        Scierror (999, GetErreur());
		
	/* Retour de la variable Pn */
	
	CreateVar(Rhs + 1,"d",&un,&un,&l);
	*stk(l) = Pn ;
	LhsVar(1) = Rhs + 1 ;
	
	return(0);
}


/******************************************
 * SCILAB function : RecursionBohmMohanty * 
 ******************************************/

int gtw_RecursionBohmMohanty ( char * fname )
{
    int m,n,l;
	int N;
    double * w ;
	double Qn ;
	int * c ;
    int un=1;

    CheckRhs(2,2);
    CheckLhs(1,1);
    
    /* Variable w */

    GetRhsVar(1,"d",&m,&n,&l);
	CheckVector(1,m,n);
    w = stk(l);

    N = ( m*n - 2 ) / 2 ;
	
	/* Variable c */
	
	GetRhsVar(2,"i",&m,&n,&l);
	CheckVector(2,m,n);
	CheckLength(2,m*n,2*N+2);
    c = istk(l);
	
	/* Appel à l'API */
	
	if ( RecursionBohmMohanty ( N , w , c , & Qn ) )
        Scierror (999, GetErreur());
		
	/* Retour de la variable Qn */
	
	CreateVar(Rhs + 1,"d",&un,&un,&l);
	*stk(l) = Qn ;
	LhsVar(1) = Rhs + 1 ;
	
	return(0);
}

/*******************************************
 * SCILAB function : RecursionBohmMohanty2 * 
 *******************************************/

int gtw_RecursionBohmMohanty2 ( char * fname )
{
    int m,n,l;
	int N, i0;
	double L , H , Qn ;
	int * c ;
    int un=1;

    CheckRhs(4,4);
    CheckLhs(1,1);
    
    /* Variable N */

    GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1,m,n);
    N = *istk(l);

    /* Variable i0 */

    GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
    i0 = *istk(l);

    /* Variable L */

    GetRhsVar(3,"d",&m,&n,&l);
	CheckScalar(3,m,n);
    L = *stk(l);

    /* Variable H */

    GetRhsVar(4,"d",&m,&n,&l);
	CheckScalar(4,m,n);
    H = *stk(l);
	
	/* Appel à l'API */
	
	if ( RecursionBohmMohanty2 ( N , i0 , L , H , & Qn ) )
        Scierror (999, GetErreur());
		
	/* Retour de la variable Qn */
	
	CreateVar(Rhs + 1,"d",&un,&un,&l);
	*stk(l) = Qn ;
	LhsVar(1) = Rhs + 1 ;
	
	return(0);
}

/*****************************
 * SCILAB function : ZeroBJ	 * 
 *****************************/

int gtw_ZeroBJ ( char * fname )
{
    int m,n,l,ITMAX ;
	double Fn , p , lambda , x1 , x2 , tol ;
	int un=1;

    CheckRhs(6,6);
    CheckLhs(1,1);
    
    /* Variable Fn */

    GetRhsVar(1,"d",&m,&n,&l);
	CheckScalar(1,m,n);
    Fn = *stk(l);

    /* Variable lambda */

    GetRhsVar(2,"d",&m,&n,&l);
	CheckScalar(2,m,n);
    lambda = *stk(l);

    /* Variable x1 */

    GetRhsVar(3,"d",&m,&n,&l);
	CheckScalar(3,m,n);
    x1 = *stk(l);

    /* Variable x2 */

    GetRhsVar(4,"d",&m,&n,&l);
	CheckScalar(4,m,n);
    x2 = *stk(l);
	
    /* Variable tol */

    GetRhsVar(5,"d",&m,&n,&l);
	CheckScalar(5,m,n);
    tol = *stk(l);
	
    /* Variable ITMAX */

    GetRhsVar(6,"i",&m,&n,&l);
	CheckScalar(6,m,n);
    ITMAX = *istk(l);

	/* Appel à l'API */
	
	if ( ZeroBJ ( Fn , lambda , x1 , x2 , tol , ITMAX , & p ) )
        Scierror (999, GetErreur());
		
	/* Retour de la variable Qn */
	
	CreateVar(Rhs + 1,"d",&un,&un,&l);
	*stk(l) = p ;
	LhsVar(1) = Rhs + 1 ;
	
	return(0);
}

/*****************************************
 * SCILAB function : PointWiseConfidence * 
 *****************************************/
 
int gtw_PointWiseConfidence ( char * fname )
{
    int m,n,l,N,i0,ITMAX_BM,ITMAX_BJ ;
	double Fn,alpha,UBlambda,L,H,cl ;
	int un=1;

    CheckRhs(7,7);
    CheckLhs(3,3);
    
    /* Variable N */

    GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1,m,n);
    N = *istk(l);

    /* Variable i0 */

    GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
    i0 = *istk(l);

    /* Variable Fn */

    GetRhsVar(3,"d",&m,&n,&l);
	CheckScalar(3,m,n);
    Fn = *stk(l);

    /* Variable alpha */

    GetRhsVar(4,"d",&m,&n,&l);
	CheckScalar(4,m,n);
    alpha = *stk(l);
	
    /* Variable UBlambda */

    GetRhsVar(5,"d",&m,&n,&l);
	CheckScalar(5,m,n);
    UBlambda = *stk(l);
		
    /* Variable ITMAX_BM */

    GetRhsVar(6,"i",&m,&n,&l);
	CheckScalar(6,m,n);
    ITMAX_BM = *istk(l);

    /* Variable ITMAX_BJ */

    GetRhsVar(7,"i",&m,&n,&l);
	CheckScalar(7,m,n);
    ITMAX_BJ = *istk(l);

	/* Appel à l'API */
	
	ClearErreur();
	
	if ( PointWiseConfidence ( N , i0 , Fn , alpha , UBlambda , ITMAX_BM , ITMAX_BJ , & L , & H , & cl ) )
        Scierror (999, GetErreur());
		
	/* Retour des variable L,H,cl */
	
	CreateVar(Rhs + 1,"d",&un,&un,&l);
	*stk(l) = L ;
	LhsVar(1) = Rhs + 1 ;

	CreateVar(Rhs + 2,"d",&un,&un,&l);
	*stk(l) = H ;
	LhsVar(2) = Rhs + 2 ;
	
	CreateVar(Rhs + 3,"d",&un,&un,&l);
	*stk(l) = cl ;
	LhsVar(3) = Rhs + 3 ;
	
	return(0);
}

/************************************
 * SCILAB function : CoverageLevel  * 
 ************************************/
 
int gtw_CoverageLevel ( char * fname )
{
    int m,n,l,N,i0,ITMAX ;
	double Fn,lambda,tol,L,H,cl ;
	int un=1;

    CheckRhs(6,6);
    CheckLhs(3,3);
    
    /* Variable N */

    GetRhsVar(1,"i",&m,&n,&l);
	CheckScalar(1,m,n);
    N = *istk(l);

    /* Variable i0 */

    GetRhsVar(2,"i",&m,&n,&l);
	CheckScalar(2,m,n);
    i0 = *istk(l);

    /* Variable Fn */

    GetRhsVar(3,"d",&m,&n,&l);
	CheckScalar(3,m,n);
    Fn = *stk(l);
	
    /* Variable lambda */

    GetRhsVar(4,"d",&m,&n,&l);
	CheckScalar(4,m,n);
    lambda = *stk(l);
		
    /* Variable ITMAX */

    GetRhsVar(5,"i",&m,&n,&l);
	CheckScalar(5,m,n);
    ITMAX = *istk(l);
	
	/* Variable tol */

    GetRhsVar(6,"d",&m,&n,&l);
	CheckScalar(6,m,n);
    tol = *stk(l);

	/* Appel à l'API */
	
	ClearErreur();
	
	if ( CoverageLevel ( N , i0 , Fn , lambda , ITMAX , tol , & L , & H , & cl ) )
        Scierror (999, GetErreur());
		
	/* Retour des variable L,H,cl */
	
	CreateVar(Rhs + 1,"d",&un,&un,&l);
	*stk(l) = L ;
	LhsVar(1) = Rhs + 1 ;

	CreateVar(Rhs + 2,"d",&un,&un,&l);
	*stk(l) = H ;
	LhsVar(2) = Rhs + 2 ;
	
	CreateVar(Rhs + 3,"d",&un,&un,&l);
	*stk(l) = cl ;
	LhsVar(3) = Rhs + 3 ;
	
	return(0);
}

