//
// MATLAB Compiler: 2.2
// Date: Wed Mar 10 16:44:48 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" "-p" "-W" "main" "-L"
// "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-B" "sglcpp" "-p" "-W"
// "main" "-L" "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg"
// "libmwsglm.mlib" "hier_community.m" "Default.m" "Stop_Simulation.m"
// "Call_SaveLoad_Fig.m" "SaveLoad_Fig.m" "SaveLoad.m" "Load_Population_Data.m"
// "Save_Population_Data.m" "Start_hier_community.m" "MyInline.m" "My_Eval.m"
// "isnumber.m" "Find_Var.m" "Change_Number_of_Populations.m"
// "Switch_Populations.m" "Store_Parameters.m" 
//
#include "My_Eval.hpp"
#include "libmatlbm.hpp"
#include "libmmfile.hpp"

static mxChar _array1_[132] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'M', 'y', '_', 'E', 'v',
                                'a', 'l', ' ', 'L', 'i', 'n', 'e', ':', ' ',
                                '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n', ':',
                                ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f', 'u',
                                'n', 'c', 't', 'i', 'o', 'n', ' ', '"', 'M',
                                'y', '_', 'E', 'v', 'a', 'l', '"', ' ', 'w',
                                'a', 's', ' ', 'c', 'a', 'l', 'l', 'e', 'd',
                                ' ', 'w', 'i', 't', 'h', ' ', 'm', 'o', 'r',
                                'e', ' ', 't', 'h', 'a', 'n', ' ', 't', 'h',
                                'e', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e',
                                'd', ' ', 'n', 'u', 'm', 'b', 'e', 'r', ' ',
                                'o', 'f', ' ', 'o', 'u', 't', 'p', 'u', 't',
                                's', ' ', '(', '1', ')', '.' };
static mwArray _mxarray0_ = mclInitializeString(132, _array1_);

static mxChar _array3_[131] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'M', 'y', '_', 'E', 'v',
                                'a', 'l', ' ', 'L', 'i', 'n', 'e', ':', ' ',
                                '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n', ':',
                                ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f', 'u',
                                'n', 'c', 't', 'i', 'o', 'n', ' ', '"', 'M',
                                'y', '_', 'E', 'v', 'a', 'l', '"', ' ', 'w',
                                'a', 's', ' ', 'c', 'a', 'l', 'l', 'e', 'd',
                                ' ', 'w', 'i', 't', 'h', ' ', 'm', 'o', 'r',
                                'e', ' ', 't', 'h', 'a', 'n', ' ', 't', 'h',
                                'e', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e',
                                'd', ' ', 'n', 'u', 'm', 'b', 'e', 'r', ' ',
                                'o', 'f', ' ', 'i', 'n', 'p', 'u', 't', 's',
                                ' ', '(', '5', ')', '.' };
static mwArray _mxarray2_ = mclInitializeString(131, _array3_);
static mwArray _mxarray4_ = mclInitializeDouble(0.0);
static mwArray _mxarray5_ = mclInitializeDouble(1.0);
static mwArray _mxarray6_ = mclInitializeDouble(-1.0);
static mwArray _mxarray7_ = mclInitializeDouble(2.0);
static mwArray _mxarray8_ = mclInitializeDouble(4.0);

void InitializeModule_My_Eval() {
}

void TerminateModule_My_Eval() {
}

static mwArray MMy_Eval(int nargout_,
                        mwArray expr,
                        mwArray s2,
                        mwArray var,
                        mwArray var_order,
                        mwArray pos);

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

//
// The function "My_Eval" contains the normal interface for the "My_Eval"
// M-function from file "C:\shu\hier_community_exe\My_Eval.m" (lines 1-27).
// This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
mwArray My_Eval(mwArray expr,
                mwArray s2,
                mwArray var,
                mwArray var_order,
                mwArray pos) {
    int nargout(1);
    mwArray err(mclGetUninitializedArray());
    err = MMy_Eval(nargout, expr, s2, var, var_order, pos);
    return err;
}

//
// The function "mlxMy_Eval" contains the feval interface for the "My_Eval"
// M-function from file "C:\shu\hier_community_exe\My_Eval.m" (lines 1-27). The
// feval function calls the implementation version of My_Eval through this
// function. This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
void mlxMy_Eval(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    MW_BEGIN_MLX();
    {
        mwArray mprhs[5];
        mwArray mplhs[1];
        int i;
        mclCppUninitializeArrays(1, mplhs);
        if (nlhs > 1) {
            error(_mxarray0_);
        }
        if (nrhs > 5) {
            error(_mxarray2_);
        }
        for (i = 0; i < 5 && i < nrhs; ++i) {
            mprhs[i] = mwArray(prhs[i], 0);
        }
        for (; i < 5; ++i) {
            mprhs[i].MakeDIN();
        }
        mplhs[0]
          = MMy_Eval(nlhs, mprhs[0], mprhs[1], mprhs[2], mprhs[3], mprhs[4]);
        plhs[0] = mplhs[0].FreezeData();
    }
    MW_END_MLX();
}

//
// The function "MMy_Eval" is the implementation version of the "My_Eval"
// M-function from file "C:\shu\hier_community_exe\My_Eval.m" (lines 1-27). 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 err=My_Eval(expr,s2,var,var_order,pos)
//
static mwArray MMy_Eval(int nargout_,
                        mwArray expr,
                        mwArray s2,
                        mwArray var,
                        mwArray var_order,
                        mwArray pos) {
    mwLocalFunctionTable save_local_function_table_
      (&_local_function_table_My_Eval);
    mwArray err(mclGetUninitializedArray());
    mwArray ans(mclGetUninitializedArray());
    mwArray p(mclGetUninitializedArray());
    mwArray i(mclGetUninitializedArray());
    mwArray L_expr(mclGetUninitializedArray());
    mwArray L_pos(mclGetUninitializedArray());
    mwArray value(mclGetUninitializedArray());
    //
    // 
    // value=num2str(0);
    //
    value = num2str(_mxarray4_);
    //
    // try
    //
    try {
        //
        // if(~isempty(var_order))
        //
        if (mclNotBool(mwVe(isempty(mwVa(var_order, "var_order"))))) {
            //
            // L_pos = length(pos(1,:));
            //
            L_pos
              = mclLengthInt(
                  mwVe(mclArrayRef(mwVsa(pos, "pos"), _mxarray5_, colon())));
            //
            // L_expr = length(expr);
            //
            L_expr = mclLengthInt(mwVa(expr, "expr"));
            //
            // for i = L_pos:-1:1
            //
            {
                mwForLoopIterator viter__;
                for (viter__.Start(
                       mwVv(L_pos, "L_pos"), _mxarray6_, _mxarray5_);
                     viter__.Next(&i);
                     ) {
                    //
                    // p = find(strcmp(var_order{i},var));
                    //
                    p
                      = find(
                          mwVe(
                            feval(
                              mwValueVarargout(),
                              mlxStrcmp,
                              mwVarargin(
                                mwVe(
                                  mwVsa(var_order, "var_order").cell(
                                    mwVsv(i, "i"))),
                                mwVa(var, "var")))));
                    //
                    // pos(:,i) = pos(:,i);
                    //
                    mclArrayAssign(
                      &pos,
                      mclArrayRef(mwVsa(pos, "pos"), colon(), mwVsv(i, "i")),
                      colon(),
                      mwVsv(i, "i"));
                    //
                    // if(pos(1,i)==1 & pos(2,i)<L_expr)
                    //
                    {
                        mwArray a_
                          (mwVe(
                             mclArrayRef(
                               mwVsa(pos, "pos"), _mxarray5_, mwVsv(i, "i")))
                           == _mxarray5_);
                        if (tobool(a_)
                            && tobool(
                                 a_
                                 & mwVe(
                                     mclArrayRef(
                                       mwVsa(pos, "pos"),
                                       _mxarray7_,
                                       mwVsv(i, "i")))
                                   < mwVv(L_expr, "L_expr"))) {
                            //
                            // expr = [value,expr(pos(2,i)+1:end)];
                            //
                            expr
                              = horzcat(
                                  mwVarargin(
                                    mwVv(value, "value"),
                                    mwVe(
                                      mclArrayRef(
                                        mwVsa(expr, "expr"),
                                        colon(
                                          mwVe(
                                            mclArrayRef(
                                              mwVsa(pos, "pos"),
                                              _mxarray7_,
                                              mwVsv(i, "i")))
                                          + _mxarray5_,
                                          end(
                                            mwVa(expr, "expr"),
                                            _mxarray5_,
                                            _mxarray5_))))));
                        //
                        // elseif(pos(1,i)>1 & pos(2,i)<L_expr)
                        //
                        } else {
                            mwArray a_0
                              (mwVe(
                                 mclArrayRef(
                                   mwVsa(pos, "pos"),
                                   _mxarray5_,
                                   mwVsv(i, "i")))
                               > _mxarray5_);
                            if (tobool(a_0)
                                && tobool(
                                     a_0
                                     & mwVe(
                                         mclArrayRef(
                                           mwVsa(pos, "pos"),
                                           _mxarray7_,
                                           mwVsv(i, "i")))
                                       < mwVv(L_expr, "L_expr"))) {
                                //
                                // expr = [expr(1:pos(1,i)-1),value,expr(pos(2,i)+1:end)];
                                //
                                expr
                                  = horzcat(
                                      mwVarargin(
                                        mwVe(
                                          mclArrayRef(
                                            mwVsa(expr, "expr"),
                                            colon(
                                              _mxarray5_,
                                              mwVe(
                                                mclArrayRef(
                                                  mwVsa(pos, "pos"),
                                                  _mxarray5_,
                                                  mwVsv(i, "i")))
                                              - _mxarray5_))),
                                        mwVv(value, "value"),
                                        mwVe(
                                          mclArrayRef(
                                            mwVsa(expr, "expr"),
                                            colon(
                                              mwVe(
                                                mclArrayRef(
                                                  mwVsa(pos, "pos"),
                                                  _mxarray7_,
                                                  mwVsv(i, "i")))
                                              + _mxarray5_,
                                              end(
                                                mwVa(expr, "expr"),
                                                _mxarray5_,
                                                _mxarray5_))))));
                            //
                            // elseif(pos(1,i)>1 & pos(2,i)==L_expr)
                            //
                            } else {
                                mwArray a_1
                                  (mwVe(
                                     mclArrayRef(
                                       mwVsa(pos, "pos"),
                                       _mxarray5_,
                                       mwVsv(i, "i")))
                                   > _mxarray5_);
                                if (tobool(a_1)
                                    && tobool(
                                         a_1
                                         & mwVe(
                                             mclArrayRef(
                                               mwVsa(pos, "pos"),
                                               _mxarray7_,
                                               mwVsv(i, "i")))
                                           == mwVv(L_expr, "L_expr"))) {
                                    //
                                    // expr = [expr(1:pos(1,i)-1),value];
                                    //
                                    expr
                                      = horzcat(
                                          mwVarargin(
                                            mwVe(
                                              mclArrayRef(
                                                mwVsa(expr, "expr"),
                                                colon(
                                                  _mxarray5_,
                                                  mwVe(
                                                    mclArrayRef(
                                                      mwVsa(pos, "pos"),
                                                      _mxarray5_,
                                                      mwVsv(i, "i")))
                                                  - _mxarray5_))),
                                            mwVv(value, "value")));
                                //
                                // else
                                //
                                } else {
                                    //
                                    // expr = value;
                                    //
                                    expr = mwVsv(value, "value");
                                }
                            }
                        }
                    //
                    // end
                    //
                    }
                //
                // end
                //
                }
            }
        //
        // end
        //
        }
        //
        // eval(expr);
        //
        ans.EqAns(eval(mwAnsVarargout(), mwVa(expr, "expr")));
        //
        // err=0;
        //
        err = _mxarray4_;
    //
    // catch
    //
    } catch(mwException e_) {
        //
        // eval(s2);
        //
        ans.EqAns(eval(mwAnsVarargout(), mwVa(s2, "s2")));
        //
        // err=4;
        //
        err = _mxarray8_;
    //
    // end
    //
    }
    mwValidateOutput(err, 1, nargout_, "err", "My_Eval");
    return err;
}
