在cvm库中设置schmatrix类的成员函数是抛出异常,但为什么呢?

时间:2016-09-09 21:32:49

标签: c++ visual-studio exception visual-c++ visual-studio-2013

我已通过在var interestingData = context.Cars .GroupBy(c => false) // always get one result .Select(g => new { AnyVisibleCars = visibleCars.Any(), AnyInvisibleCars = invisibleCars.Any(), CarsWithXStatus = visibleCars.Any() && invisibleCars.Any() ? g.Where(c => c.Status == "x") : g.Where(c => false) }) .Single();

中添加以下代码创建了mex功能
mx_minimum_power.cpp

然后我通过在matlab命令行中输入以下代码创建了#include <math.h> #include <complex> #include "mex.h" #include "matrix.h" #include "cvm.h" #include "blas.h" #include "cfun.h" using std::complex; using namespace cvm; /* The gateway function */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { const int arraysize = 62172; const int matrixDimention = 3; float *inMatrixA11 = (float *)mxGetPr(prhs[0]); complex<float> *inMatrixA12 = (complex<float> *)mxGetPr(prhs[1]); complex<float> *inMatrixA13 = (complex<float> *)mxGetPr(prhs[2]); float *inMatrixA22 = (float *)mxGetPr(prhs[3]); complex<float> *inMatrixA23 = (complex<float> *)mxGetPr(prhs[4]); float *inMatrixA33 = (float *)mxGetPr(prhs[5]); basic_schmatrix< float, complex<float> > A(matrixDimention); int i = 0; for (i = 0; i < arraysize; i++) { A.set(1, 1, inMatrixA11[i]); A.set(1, 2, inMatrixA12[i]); A.set(1, 3, inMatrixA13[i]); A.set(2, 2, inMatrixA22[i]); A.set(2, 3, inMatrixA23[i]); A.set(3, 3, inMatrixA33[i]); } }

mx_minimum_power.mexw64

但似乎set member function of basic_schmatrix class在第27行投掷了cvmexception mex -g mx_minimum_power.cpp cvm_em64t_debug.lib

我真的不明白这个例外的原因是因为我使用了CVM0 based indexing范围A.set(1, 1, inMatrixA11[i]);范围内的索引1,1 并且考虑到在运行第27行[1,4)之前,我们有A.set(1, 1, inMatrixA11[i]);的以下值:

enter image description here

正如预期的那样。

enter image description here

以下内存分配给A11[0],A12[0],A13[0],A22[0],A23[0],A33[0]

enter image description here

1 个答案:

答案 0 :(得分:0)

出现问题是因为我们在cvm.h文件的第48行到第53行中有以下代码:

// 5.7 0-based indexing
#if defined (CVM_ZERO_BASED)
#   define CVM0 TINT_ZERO //!< Index base, 1  by default or 0 when \c CVM_ZERO_BASED is defined
#else
#   define CVM0 TINT_ONE  //!< Index base, 1  by default or 0 when \c CVM_ZERO_BASED is defined
#endif  

默认情况下会CVM0 = 1。因此,如果我们按两次指定行的Step Into(F11)按钮,我们将进入文件cvm.h的第34883行:

basic_schmatrix& set(tint nRow, tint nCol, TC c) throw(cvmexception)
{
    this->_set_at(nRow - CVM0, nCol - CVM0, c);
    return *this;
}  

在第Step Into(F11)行按this->_set_at(nRow - CVM0, nCol - CVM0, c);,您将转到函数_set_at的定义:

// sets both elements to keep matrix hermitian, checks ranges
// zero based
void _set_at(tint nRow, tint nCol, TC val) throw(cvmexception)
{
    _check_lt_ge(CVM_OUTOFRANGE_LTGE1, nRow, CVM0, this->msize() + CVM0);
    _check_lt_ge(CVM_OUTOFRANGE_LTGE2, nCol, CVM0, this->nsize() + CVM0);
    if (nRow == nCol && _abs(val.imag()) > basic_cvmMachMin<TR>()) { // only reals on main diagonal
        throw cvmexception(CVM_BREAKS_HERMITIANITY, "real number");
    }
    this->get()[this->ld() * nCol + nRow] = val;
    if (nRow != nCol) {
        this->get()[this->ld() * nRow + nCol] = _conjugate(val);
    }
}

Step Over(F10)按钮,您将获得结果:

enter image description here

所以为了得到nRow=1nCol=1而不是nRow=0nCol=0,这超出范围[1,4),你应该写出那一行代码为:

A.set(2, 2, inMatrixA11[i]);