⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rands.c

📁 nnToolKit 神经网络工具包是基于 MATLAB 神经网络工具箱自行开发的一组神经网络算法函数库
💻 C
字号:
/*
 * MATLAB Compiler: 3.0
 * Date: Sun May 13 16:47:40 2007
 * 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" "-M" "-silentsetup" "-d"
 * "d:/MATLAB6p5/work/nnToolKit/src" "-B" "csglcom:nnToolKit,nnToolKit,2.0"
 * "-B" "sgl" "-m" "-W" "main" "-L" "C" "-t" "-T" "link:exe" "-h"
 * "libmmfile.mlib" "-W" "mainhg" "libmwsglm.mlib" "-t" "-W"
 * "comhg:nnToolKit,nnToolKit,2.0" "-T" "link:lib" "-h" "libmmfile.mlib" "-i"
 * "-i" "D:/MATLAB6p5/work/nnToolKit/lmnet/LmSimu.m"
 * "D:/MATLAB6p5/work/nnToolKit/lmnet/LmTrain.m"
 * "D:/MATLAB6p5/work/nnToolKit/sofm/SofmSimu.m"
 * "D:/MATLAB6p5/work/nnToolKit/sofm/SofmTrain.m" 
 */
#include "rands.h"
#include "libmatlbm.h"

static mxChar _array1_[27] = { 'N', 'o', 't', ' ', 'e', 'n', 'o', 'u', 'g',
                               'h', ' ', 'i', 'n', 'p', 'u', 't', ' ', 'a',
                               'r', 'g', 'u', 'm', 'e', 'n', 't', 's', '.' };
static mxArray * _mxarray0_;
static mxArray * _mxarray2_;
static mxArray * _mxarray3_;

void InitializeModule_rands(void) {
    _mxarray0_ = mclInitializeString(27, _array1_);
    _mxarray2_ = mclInitializeDouble(1.0);
    _mxarray3_ = mclInitializeDouble(2.0);
}

void TerminateModule_rands(void) {
    mxDestroyArray(_mxarray3_);
    mxDestroyArray(_mxarray2_);
    mxDestroyArray(_mxarray0_);
}

static mxArray * Mrands(mxArray * * b, int nargout_, mxArray * s, mxArray * pr);

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

/*
 * The function "mlfNRands" contains the nargout interface for the "rands"
 * M-function from file "d:\matlab6p5\toolbox\nnet\nnet\rands.m" (lines 1-66).
 * This interface is only produced if the M-function uses the special variable
 * "nargout". The nargout interface allows the number of requested outputs to
 * be specified via the nargout argument, as opposed to the normal interface
 * which dynamically calculates the number of outputs based on the number of
 * non-NULL inputs it receives. This function processes any input arguments and
 * passes them to the implementation version of the function, appearing above.
 */
mxArray * mlfNRands(int nargout, mxArray * * b, mxArray * s, mxArray * pr) {
    mxArray * w = NULL;
    mxArray * b__ = NULL;
    mlfEnterNewContext(1, 2, b, s, pr);
    w = Mrands(&b__, nargout, s, pr);
    mlfRestorePreviousContext(1, 2, b, s, pr);
    if (b != NULL) {
        mclCopyOutputArg(b, b__);
    } else {
        mxDestroyArray(b__);
    }
    return mlfReturnValue(w);
}

/*
 * The function "mlfRands" contains the normal interface for the "rands"
 * M-function from file "d:\matlab6p5\toolbox\nnet\nnet\rands.m" (lines 1-66).
 * This function processes any input arguments and passes them to the
 * implementation version of the function, appearing above.
 */
mxArray * mlfRands(mxArray * * b, mxArray * s, mxArray * pr) {
    int nargout = 1;
    mxArray * w = NULL;
    mxArray * b__ = NULL;
    mlfEnterNewContext(1, 2, b, s, pr);
    if (b != NULL) {
        ++nargout;
    }
    w = Mrands(&b__, nargout, s, pr);
    mlfRestorePreviousContext(1, 2, b, s, pr);
    if (b != NULL) {
        mclCopyOutputArg(b, b__);
    } else {
        mxDestroyArray(b__);
    }
    return mlfReturnValue(w);
}

/*
 * The function "mlfVRands" contains the void interface for the "rands"
 * M-function from file "d:\matlab6p5\toolbox\nnet\nnet\rands.m" (lines 1-66).
 * The void interface is only produced if the M-function uses the special
 * variable "nargout", and has at least one output. The void interface function
 * specifies zero output arguments to the implementation version of the
 * function, and in the event that the implementation version still returns an
 * output (which, in MATLAB, would be assigned to the "ans" variable), it
 * deallocates the output. This function processes any input arguments and
 * passes them to the implementation version of the function, appearing above.
 */
void mlfVRands(mxArray * s, mxArray * pr) {
    mxArray * w = NULL;
    mxArray * b = NULL;
    mlfEnterNewContext(0, 2, s, pr);
    w = Mrands(&b, 0, s, pr);
    mlfRestorePreviousContext(0, 2, s, pr);
    mxDestroyArray(w);
    mxDestroyArray(b);
}

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

/*
 * The function "Mrands" is the implementation version of the "rands"
 * M-function from file "d:\matlab6p5\toolbox\nnet\nnet\rands.m" (lines 1-66).
 * 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 [w,b] = rands(s,pr)
 */
static mxArray * Mrands(mxArray * * b,
                        int nargout_,
                        mxArray * s,
                        mxArray * pr) {
    mexLocalFunctionTable save_local_function_table_
      = mclSetCurrentLocalFunctionTable(&_local_function_table_rands);
    int nargin_ = mclNargin(2, s, pr, NULL);
    mxArray * w = NULL;
    mxArray * r = NULL;
    mxArray * ans = NULL;
    mclCopyArray(&s);
    mclCopyArray(&pr);
    /*
     * %RANDS Symmetric random weight/bias initialization function.
     * %
     * %  Syntax
     * %
     * %    W = rands(S,PR)
     * %    M = rands(S,R)
     * %    v = rands(S);
     * %
     * %  Description
     * %
     * %    RANDS is a weight/bias initialization function.
     * %
     * %    RANDS(S,PR) takes,
     * %      S  - number of neurons.
     * %      PR - Rx2 matrix of R input ranges.
     * %    and returns an S-by-R weight matrix of random values between -1 and 1.
     * %
     * %    RANDS(S,R) returns an S-by-R matrix of random values.
     * %    RANDS(S) returns an S-by-1 vector of random values.
     * %
     * %  Examples
     * %
     * %    Here three sets of random values are generated with RANDS.
     * %
     * %      rands(4,[0 1; -2 2])
     * %      rands(4)
     * %      rands(2,3)
     * %
     * %  Network Use
     * %
     * %    To prepare the weights and the bias of layer i of a custom network
     * %    to be initialized with RANDS:
     * %    1) Set NET.initFcn to 'initlay'.
     * %       (NET.initParam will automatically become INITLAY's default parameters.)
     * %    2) Set NET.layers{i}.initFcn to 'initwb'.
     * %    3) Set each NET.inputWeights{i,j}.initFcn to 'rands'.
     * %       Set each NET.layerWeights{i,j}.initFcn to 'rands';
     * %       Set each NET.biases{i}.initFcn to 'rands'.
     * %
     * %    To initialize the network call INIT.
     * %
     * %  See also RANDNR, RANDNC, INITWB, INITLAY, INIT
     * 
     * % Mark Beale, 1-31-92
     * % Revised 12-15-93, MB
     * % Revised 11-31-97, MB
     * % Copyright 1992-2002 The MathWorks, Inc.
     * % $Revision: 1.10 $  $Date: 2002/03/25 16:52:39 $
     * 
     * if nargin < 1, error('Not enough input arguments.'); end
     */
    if (nargin_ < 1) {
        mlfError(_mxarray0_, NULL);
    }
    /*
     * 
     * if nargin == 1
     */
    if (nargin_ == 1) {
        /*
         * r = 1;
         */
        mlfAssign(&r, _mxarray2_);
    /*
     * elseif size(pr,2) == 1
     */
    } else if (mclEqBool(
                 mlfSize(mclValueVarargout(), mclVa(pr, "pr"), _mxarray3_),
                 _mxarray2_)) {
        /*
         * r = pr;
         */
        mlfAssign(&r, mclVa(pr, "pr"));
    /*
     * else
     */
    } else {
        /*
         * r = size(pr,1);
         */
        mlfAssign(
          &r, mlfSize(mclValueVarargout(), mclVa(pr, "pr"), _mxarray2_));
    /*
     * end
     */
    }
    /*
     * w = 2*rand(s,r)-1;
     */
    mlfAssign(
      &w,
      mclMinus(
        mclMtimes(_mxarray3_, mlfNRand(1, mclVa(s, "s"), mclVv(r, "r"), NULL)),
        _mxarray2_));
    /*
     * 
     * % **[ NNT2 Support ]**
     * if nargout == 2
     */
    if (nargout_ == 2) {
        /*
         * b = 2*rand(s,1)-1;
         */
        mlfAssign(
          b,
          mclMinus(
            mclMtimes(_mxarray3_, mlfNRand(1, mclVa(s, "s"), _mxarray2_, NULL)),
            _mxarray2_));
    /*
     * end
     */
    }
    mclValidateOutput(w, 1, nargout_, "w", "rands");
    mclValidateOutput(*b, 2, nargout_, "b", "rands");
    mxDestroyArray(ans);
    mxDestroyArray(r);
    mxDestroyArray(pr);
    mxDestroyArray(s);
    mclSetCurrentLocalFunctionTable(save_local_function_table_);
    return w;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -