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

📄 dist.c

📁 nnToolKit 神经网络工具包是基于 MATLAB 神经网络工具箱自行开发的一组神经网络算法函数库
💻 C
字号:
/*
 * MATLAB Compiler: 3.0
 * Date: Sun May 13 16:47:41 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 "dist.h"
#include "libmatlbm.h"

static mxChar _array1_[5] = { 'd', 'e', 'r', 'i', 'v' };
static mxArray * _mxarray0_;
static mxArray * _mxarray2_;

static mxChar _array4_[18] = { 'U', 'n', 'r', 'e', 'c', 'o', 'g', 'n', 'i',
                               'z', 'e', 'd', ' ', 'c', 'o', 'd', 'e', '.' };
static mxArray * _mxarray3_;

static mxChar _array6_[37] = { 'I', 'n', 'n', 'e', 'r', ' ', 'm', 'a', 't', 'r',
                               'i', 'x', ' ', 'd', 'i', 'm', 'e', 'n', 's', 'i',
                               'o', 'n', 's', ' ', 'd', 'o', ' ', 'n', 'o', 't',
                               ' ', 'm', 'a', 't', 'c', 'h', '.' };
static mxArray * _mxarray5_;
static mxArray * _mxarray7_;
static mxArray * _mxarray8_;
static mxArray * _mxarray9_;

void InitializeModule_dist(void) {
    _mxarray0_ = mclInitializeString(5, _array1_);
    _mxarray2_ = mclInitializeCharVector(0, 0, (mxChar *)NULL);
    _mxarray3_ = mclInitializeString(18, _array4_);
    _mxarray5_ = mclInitializeString(37, _array6_);
    _mxarray7_ = mclInitializeDouble(1.0);
    _mxarray8_ = mclInitializeDoubleVector(0, 0, (double *)NULL);
    _mxarray9_ = mclInitializeDouble(2.0);
}

void TerminateModule_dist(void) {
    mxDestroyArray(_mxarray9_);
    mxDestroyArray(_mxarray8_);
    mxDestroyArray(_mxarray7_);
    mxDestroyArray(_mxarray5_);
    mxDestroyArray(_mxarray3_);
    mxDestroyArray(_mxarray2_);
    mxDestroyArray(_mxarray0_);
}

static mxArray * Mdist(int nargout_, mxArray * w, mxArray * p);

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

/*
 * The function "mlfDist" contains the normal interface for the "dist"
 * M-function from file "d:\matlab6p5\toolbox\nnet\nnet\dist.m" (lines 1-109).
 * This function processes any input arguments and passes them to the
 * implementation version of the function, appearing above.
 */
mxArray * mlfDist(mxArray * w, mxArray * p) {
    int nargout = 1;
    mxArray * z = NULL;
    mlfEnterNewContext(0, 2, w, p);
    z = Mdist(nargout, w, p);
    mlfRestorePreviousContext(0, 2, w, p);
    return mlfReturnValue(z);
}

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

/*
 * The function "Mdist" is the implementation version of the "dist" M-function
 * from file "d:\matlab6p5\toolbox\nnet\nnet\dist.m" (lines 1-109). 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 z = dist(w,p)
 */
static mxArray * Mdist(int nargout_, mxArray * w, mxArray * p) {
    mexLocalFunctionTable save_local_function_table_
      = mclSetCurrentLocalFunctionTable(&_local_function_table_dist);
    int nargin_ = mclNargin(2, w, p, NULL);
    mxArray * z = NULL;
    mxArray * i = NULL;
    mxArray * q = NULL;
    mxArray * copies = NULL;
    mxArray * Q = NULL;
    mxArray * R2 = NULL;
    mxArray * R = NULL;
    mxArray * S = NULL;
    mxArray * ans = NULL;
    mclCopyArray(&w);
    mclCopyArray(&p);
    /*
     * %DIST Euclidean distance weight function.
     * %
     * %  Syntax
     * %
     * %    Z = dist(W,P)
     * %    df = dist('deriv')
     * %    D = dist(pos)
     * %
     * %  Description
     * %
     * %    DIST is the Euclidean distance weight function. Weight
     * %    functions apply weights to an input to get weighted inputs.
     * %
     * %    DIST(W,P) takes these inputs,
     * %      W - SxR weight matrix.
     * %      P - RxQ matrix of Q input (column) vectors.
     * %    and returns the SxQ matrix of vector distances.
     * %
     * %    DIST('deriv') returns '' because DIST does not have
     * %    a derivative function.
     * %
     * %    DIST is also a layer distance function which can be used
     * %    to find the distances between neurons in a layer.
     * %
     * %    DIST(POS) takes one argument,
     * %      POS - NxS matrix of neuron positions.
     * %     and returns the SxS matrix of distances.
     * %
     * %  Examples
     * %
     * %    Here we define a random weight matrix W and input vector P
     * %    and calculate the corresponding weighted input Z.
     * %
     * %      W = rand(4,3);
     * %      P = rand(3,1);
     * %      Z = dist(W,P)
     * %
     * %    Here we define a random matrix of positions for 10 neurons
     * %    arranged in three dimensional space and find their distances.
     * %
     * %      pos = rand(3,10);
     * %      D = dist(pos)
     * %
     * %  Network Use
     * %
     * %    You can create a standard network that uses DIST
     * %    by calling NEWPNN or NEWGRNN.
     * %
     * %    To change a network so an input weight uses DIST set
     * %    NET.inputWeight{i,j}.weightFcn to 'dist.  For a layer weight
     * %    set NET.inputWeight{i,j}.weightFcn to 'dist'.
     * %
     * %    To change a network so that a layer's topology uses DIST set
     * %    NET.layers{i}.distanceFcn to 'dist'.
     * %
     * %    In either case, call SIM to simulate the network with DIST.
     * %    See NEWPNN or NEWGRNN for simulation examples.
     * %
     * %  Algorithm
     * %
     * %    The Euclidean distance D between two vectors X and Y is:
     * %  
     * %      D = sqrt(sum((x-y).^2))
     * %
     * %  See also SIM, DOTPROD, NEGDIST, NORMPROD, MANDIST, LINKDIST.
     * 
     * % Mark Beale, 12-15-93
     * % Revised 11-31-97, MB
     * % Copyright 1992-2002 The MathWorks, Inc.
     * % $Revision: 1.12 $  $Date: 2002/03/29 05:56:49 $
     * 
     * % FUNCTION INFO
     * if isstr(w)
     */
    if (mlfTobool(mlfIsstr(mclVa(w, "w")))) {
        /*
         * switch (w)
         */
        mxArray * v_ = mclInitialize(mclVa(w, "w"));
        if (mclSwitchCompare(v_, _mxarray0_)) {
            /*
             * case 'deriv',
             * z = '';
             */
            mlfAssign(&z, _mxarray2_);
        /*
         * otherwise
         */
        } else {
            /*
             * error('Unrecognized code.')
             */
            mlfError(_mxarray3_, NULL);
        /*
         * end
         */
        }
        mxDestroyArray(v_);
        /*
         * return
         */
        goto return_;
    /*
     * end
     */
    }
    /*
     * 
     * % CALCULATION
     * if nargin == 1
     */
    if (nargin_ == 1) {
        /*
         * p = w;
         */
        mlfAssign(&p, mclVa(w, "w"));
        /*
         * w = w';
         */
        mlfAssign(&w, mlfCtranspose(mclVa(w, "w")));
    /*
     * end
     */
    }
    /*
     * 
     * [S,R] = size(w);
     */
    mlfSize(mlfVarargout(&S, &R, NULL), mclVa(w, "w"), NULL);
    /*
     * [R2,Q] = size(p);
     */
    mlfSize(mlfVarargout(&R2, &Q, NULL), mclVa(p, "p"), NULL);
    /*
     * if (R ~= R2), error('Inner matrix dimensions do not match.'),end
     */
    if (mclNeBool(mclVv(R, "R"), mclVv(R2, "R2"))) {
        mlfError(_mxarray5_, NULL);
    }
    /*
     * 
     * z = zeros(S,Q);
     */
    mlfAssign(&z, mlfZeros(mclVv(S, "S"), mclVv(Q, "Q"), NULL));
    /*
     * if (Q<S)
     */
    if (mclLtBool(mclVv(Q, "Q"), mclVv(S, "S"))) {
        /*
         * p = p';
         */
        mlfAssign(&p, mlfCtranspose(mclVa(p, "p")));
        /*
         * copies = zeros(1,S);
         */
        mlfAssign(&copies, mlfZeros(_mxarray7_, mclVv(S, "S"), NULL));
        /*
         * for q=1:Q
         */
        {
            int v_ = mclForIntStart(1);
            int e_ = mclForIntEnd(mclVv(Q, "Q"));
            if (v_ > e_) {
                mlfAssign(&q, _mxarray8_);
            } else {
                /*
                 * z(:,q) = sum((w-p(q+copies,:)).^2,2);
                 * end
                 */
                for (; ; ) {
                    mclArrayAssign2(
                      &z,
                      mlfSum(
                        mlfPower(
                          mclMinus(
                            mclVa(w, "w"),
                            mclArrayRef2(
                              mclVa(p, "p"),
                              mclPlus(mlfScalar(v_), mclVv(copies, "copies")),
                              mlfCreateColonIndex())),
                          _mxarray9_),
                        _mxarray9_),
                      mlfCreateColonIndex(),
                      mlfScalar(v_));
                    if (v_ == e_) {
                        break;
                    }
                    ++v_;
                }
                mlfAssign(&q, mlfScalar(v_));
            }
        }
    /*
     * else
     */
    } else {
        /*
         * w = w';
         */
        mlfAssign(&w, mlfCtranspose(mclVa(w, "w")));
        /*
         * copies = zeros(1,Q);
         */
        mlfAssign(&copies, mlfZeros(_mxarray7_, mclVv(Q, "Q"), NULL));
        /*
         * for i=1:S
         */
        {
            int v_ = mclForIntStart(1);
            int e_ = mclForIntEnd(mclVv(S, "S"));
            if (v_ > e_) {
                mlfAssign(&i, _mxarray8_);
            } else {
                /*
                 * z(i,:) = sum((w(:,i+copies)-p).^2,1);
                 * end
                 */
                for (; ; ) {
                    mclArrayAssign2(
                      &z,
                      mlfSum(
                        mlfPower(
                          mclMinus(
                            mclArrayRef2(
                              mclVa(w, "w"),
                              mlfCreateColonIndex(),
                              mclPlus(mlfScalar(v_), mclVv(copies, "copies"))),
                            mclVa(p, "p")),
                          _mxarray9_),
                        _mxarray7_),
                      mlfScalar(v_),
                      mlfCreateColonIndex());
                    if (v_ == e_) {
                        break;
                    }
                    ++v_;
                }
                mlfAssign(&i, mlfScalar(v_));
            }
        }
    /*
     * end
     */
    }
    /*
     * z = sqrt(z);
     */
    mlfAssign(&z, mlfSqrt(mclVv(z, "z")));
    return_:
    mclValidateOutput(z, 1, nargout_, "z", "dist");
    mxDestroyArray(ans);
    mxDestroyArray(S);
    mxDestroyArray(R);
    mxDestroyArray(R2);
    mxDestroyArray(Q);
    mxDestroyArray(copies);
    mxDestroyArray(q);
    mxDestroyArray(i);
    mxDestroyArray(p);
    mxDestroyArray(w);
    mclSetCurrentLocalFunctionTable(save_local_function_table_);
    return z;
}

⌨️ 快捷键说明

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