/*
 * MATLAB Compiler: 3.0
 * Date: Mon Sep 20 09:40:12 2004
 * Arguments: "-B" "macro_default" "-O" "all" "-O" "fold_scalar_mxarrays:on"
 * "-O" "fold_non_scalar_mxarrays:on" "-O" "optimize_integer_for_loops:on" "-O"
 * "array_indexing:on" "-O" "optimize_conditionals:on" "-B" "sgl" "-m" "-W"
 * "main" "-L" "C" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg"
 * "libmwsglm.mlib" "recarga_multiple" 
 */
#include "iga.h"
#include "libmatlbm.h"
static mxArray * _mxarray0_;

void InitializeModule_iga(void) {
    _mxarray0_ = mclInitializeDouble(1.0);
}

void TerminateModule_iga(void) {
    mxDestroyArray(_mxarray0_);
}

static mxArray * Miga(int nargout_,
                      mxArray * t,
                      mxArray * F,
                      mxArray * Ks,
                      mxArray * A);

_mexLocalFunctionTable _local_function_table_iga
  = { 0, (mexFunctionTableEntry *)NULL };

/*
 * The function "mlfIga" contains the normal interface for the "iga" M-function
 * from file "u:\final_recarga6\recarga\iga.m" (lines 1-2). This function
 * processes any input arguments and passes them to the implementation version
 * of the function, appearing above.
 */
mxArray * mlfIga(mxArray * t, mxArray * F, mxArray * Ks, mxArray * A) {
    int nargout = 1;
    mxArray * dFdt = NULL;
    mlfEnterNewContext(0, 4, t, F, Ks, A);
    dFdt = Miga(nargout, t, F, Ks, A);
    mlfRestorePreviousContext(0, 4, t, F, Ks, A);
    return mlfReturnValue(dFdt);
}

/*
 * The function "mlxIga" contains the feval interface for the "iga" M-function
 * from file "u:\final_recarga6\recarga\iga.m" (lines 1-2). The feval function
 * calls the implementation version of iga through this function. This function
 * processes any input arguments and passes them to the implementation version
 * of the function, appearing above.
 */
void mlxIga(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    mxArray * mprhs[4];
    mxArray * mplhs[1];
    int i;
    if (nlhs > 1) {
        mlfError(
          mxCreateString(
            "Run-time Error: File: iga Line: 1 Column: 1 The function \"iga\""
            " was called with more than the declared number of outputs (1)."),
          NULL);
    }
    if (nrhs > 4) {
        mlfError(
          mxCreateString(
            "Run-time Error: File: iga Line: 1 Column: 1 The function \"iga\""
            " was called with more than the declared number of inputs (4)."),
          NULL);
    }
    for (i = 0; i < 1; ++i) {
        mplhs[i] = NULL;
    }
    for (i = 0; i < 4 && i < nrhs; ++i) {
        mprhs[i] = prhs[i];
    }
    for (; i < 4; ++i) {
        mprhs[i] = NULL;
    }
    mlfEnterNewContext(0, 4, mprhs[0], mprhs[1], mprhs[2], mprhs[3]);
    mplhs[0] = Miga(nlhs, mprhs[0], mprhs[1], mprhs[2], mprhs[3]);
    mlfRestorePreviousContext(0, 4, mprhs[0], mprhs[1], mprhs[2], mprhs[3]);
    plhs[0] = mplhs[0];
}

/*
 * The function "Miga" is the implementation version of the "iga" M-function
 * from file "u:\final_recarga6\recarga\iga.m" (lines 1-2). It contains the
 * actual compiled code for that M-function. It is a static function and must
 * only be called from one of the interface functions, appearing below.
 */
/*
 * function dFdt=iga(t,F,Ks,A)
 */
static mxArray * Miga(int nargout_,
                      mxArray * t,
                      mxArray * F,
                      mxArray * Ks,
                      mxArray * A) {
    mexLocalFunctionTable save_local_function_table_
      = mclSetCurrentLocalFunctionTable(&_local_function_table_iga);
    mxArray * dFdt = NULL;
    mclCopyArray(&t);
    mclCopyArray(&F);
    mclCopyArray(&Ks);
    mclCopyArray(&A);
    /*
     * dFdt=Ks*(1+A./F);   % what if F=0???
     */
    mlfAssign(
      &dFdt,
      mclMtimes(
        mclVa(Ks, "Ks"),
        mclPlus(_mxarray0_, mclRdivide(mclVa(A, "A"), mclVa(F, "F")))));
    mclValidateOutput(dFdt, 1, nargout_, "dFdt", "iga");
    mxDestroyArray(A);
    mxDestroyArray(Ks);
    mxDestroyArray(F);
    mxDestroyArray(t);
    mclSetCurrentLocalFunctionTable(save_local_function_table_);
    return dFdt;
}
