//
// MATLAB Compiler: 2.1
// Date: Thu Dec 19 07:01:13 2002
// 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" "Find_Var.m"
// "isnumber.m" "Change_Number_of_Populations.m" "Switch_Populations.m"
// "Store_Parameters.m" 
//
#include "blkdiag.hpp"
#include "libmatlbm.hpp"

static mxChar _array1_[132] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'b', 'l', 'k', 'd', 'i',
                                'a', 'g', ' ', 'L', 'i', 'n', 'e', ':', ' ',
                                '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n', ':',
                                ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f', 'u',
                                'n', 'c', 't', 'i', 'o', 'n', ' ', '"', 'b',
                                'l', 'k', 'd', 'i', 'a', 'g', '"', ' ', '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 mwArray _mxarray2_ = mclInitializeDoubleVector(0, 0, (double *)NULL);

void InitializeModule_blkdiag() {
}

void TerminateModule_blkdiag() {
}

static mwArray Mblkdiag(int nargout_, mwArray varargin);

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

//
// The function "blkdiag" contains the normal interface for the "blkdiag"
// M-function from file "C:\matlabR12\toolbox\matlab\elmat\blkdiag.m" (lines
// 1-20). This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
mwArray blkdiag(mwVarargin varargin) {
    int nargout(1);
    mwArray y(mclGetUninitializedArray());
    y = Mblkdiag(nargout, varargin.ToArray());
    return y;
}

//
// The function "mlxBlkdiag" contains the feval interface for the "blkdiag"
// M-function from file "C:\matlabR12\toolbox\matlab\elmat\blkdiag.m" (lines
// 1-20). The feval function calls the implementation version of blkdiag
// through this function. This function processes any input arguments and
// passes them to the implementation version of the function, appearing above.
//
void mlxBlkdiag(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    MW_BEGIN_MLX();
    {
        mwArray mprhs[1];
        mwArray mplhs[1];
        mclCppUninitializeArrays(1, mplhs);
        if (nlhs > 1) {
            error(_mxarray0_);
        }
        mprhs[0] = mclCreateVararginCell(nrhs, prhs);
        mplhs[0] = Mblkdiag(nlhs, mprhs[0]);
        plhs[0] = mplhs[0].FreezeData();
    }
    MW_END_MLX();
}

//
// The function "Mblkdiag" is the implementation version of the "blkdiag"
// M-function from file "C:\matlabR12\toolbox\matlab\elmat\blkdiag.m" (lines
// 1-20). 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 y = blkdiag(varargin)
//
static mwArray Mblkdiag(int nargout_, mwArray varargin) {
    mwLocalFunctionTable save_local_function_table_(&_local_function_table_blkdiag);
    int nargin_(nargin(-1, mwVarargin(varargin)));
    mwArray y(mclGetUninitializedArray());
    mwArray m2(mclGetUninitializedArray());
    mwArray p2(mclGetUninitializedArray());
    mwArray m1(mclGetUninitializedArray());
    mwArray p1(mclGetUninitializedArray());
    mwArray x(mclGetUninitializedArray());
    mwArray k(mclGetUninitializedArray());
    //
    // %BLKDIAG  Block diagonal concatenation of input arguments.
    // %
    // %                                   |A 0 .. 0|
    // %   Y = BLKDIAG(A,B,...)  produces  |0 B .. 0|
    // %                                   |0 0 ..  |
    // %
    // %   See also DIAG, HORZCAT, VERTCAT
    // 
    // % Wes Wang 9/9/94, 9/30/95.  Greg Wolodkin 1/30/98
    // % Copyright 1984-2000 The MathWorks, Inc. 
    // % $Revision: 1.4 $
    // 
    // y = [];
    //
    y = _mxarray2_;
    //
    // for k=1:nargin
    //
    {
        int v_(mclForIntStart(1));
        int e_(mclForIntEnd(mwArray(nargin_)));
        if (v_ > e_) {
            k = _mxarray2_;
        } else {
            //
            // x = varargin{k};
            // [p1,m1] = size(y); [p2,m2] = size(x);
            // y = [y zeros(p1,m2); zeros(p2,m1) x];
            // end
            //
            for (; ; ) {
                x = mwVsa(varargin, "varargin").cell(v_);
                size(mwVarargout(p1, m1), mwVv(y, "y"));
                size(mwVarargout(p2, m2), mwVv(x, "x"));
                y
                  = vertcat(
                      mwVarargin(
                        horzcat(
                          mwVarargin(
                            mwVv(y, "y"),
                            mwVe(
                              zeros(
                                mwVarargin(mwVv(p1, "p1"), mwVv(m2, "m2")))))),
                        horzcat(
                          mwVarargin(
                            mwVe(
                              zeros(
                                mwVarargin(mwVv(p2, "p2"), mwVv(m1, "m1")))),
                            mwVv(x, "x")))));
                if (v_ == e_) {
                    break;
                }
                ++v_;
            }
            k = v_;
        }
    }
    mwValidateOutput(y, 1, nargout_, "y", "blkdiag");
    return y;
}
