c++ - 用 C++ 类替换 Numerical Recipe 的 dmatrix

标签 c++ matrix numerical-recipes

我正在改进一个旧应用程序,该应用程序广泛使用了 Numerical Recipes 的 dmatrix。由于我处理该应用程序的原因之一是因为它的代码即将打开,所以我想用可以自由分发的代码替换所有 Numerical Recipes 代码。

dmatrix 是一个返回 double 矩阵的函数。调用为每个索引提供下限和上限,如下所示:

double **mat = dmatrix(1,3,1,3);

mat 现在有 3 行,从 1 到 3,3 列,从 1 到 3,所以 mat[1][1] 是第一个元素mat[3][3] 是最后一个。

我查看了各种 C++ 矩阵实现,它们都不允许我指定每个维度的下限。有什么我可以使用的东西,还是我必须为此编写另一个矩阵类?

最佳答案

我相信您可以轻松地包装一些其他矩阵实现以添加下限特征。示例(未经测试):

class Matrix {
    OtherMatrix m;
    int lowerX, lowerY;
public:

    Matrix(int lx, int hx, int ly, int hy) :
        m(hx-lx, hy-ly),
        lowerX(lx), lowerY(ly) { }

    MatrixCol operator[] (int x) {
        return {this, x};
    } 
};

class MatrixCol {
    friend class Matrix;
    Matrix* mm;
    int x;
public:
    double& operator[] (int y) {
        return mm->m[x - mm->lowerX, y - mm->lowerY];
    } 
};

这可能需要更强大的实现,具体取决于您的用例。但这是基本思想,从中扩展。

关于c++ - 用 C++ 类替换 Numerical Recipe 的 dmatrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992006/

相关文章:

c++ - pygccxml 中是否有 Python Clang 包装器来包装 GCC-XML?

c++ - 无法使用vs2013使用opencv加载图片

python - 在没有numpy的情况下打印矩阵中的列

c++ - 为什么 Numerical Recipes 头文件中没有 include 守卫?

c++ - QGraphicsScene mouseMoveEvent 在 QGraphicsView wheelEvent 之前不起作用

c++ - 交换字符串值在函数中不起作用 - C++

javascript - 仿射变换矩阵偏移

c++ - 这个矩阵引用了什么?

c - 关于《数值食谱》一书的问题,第 2 版。 : allocation/deallocation of memory for vectors