在班级中使用犰狳矩阵

Jay*_*rya 5 c++ matrix armadillo

我是一名物理学家,在课程编程方面经验不足.如果有人可以帮助我,我将不胜感激.我已经在python类中成功使用了numpy数组,但我在这里迷失了.

动机很简单.我需要使用一个带有几个矩阵的类作为私有成员,并对它们执行一些操作.请看以下内容.

#include<iostream>
#include<armadillo>

using namespace std;

class myclass{
    // a matrix
    double A[2][2];
public:
    int set_element(double);
};

int main(){
    myclass x;
    x.set_element(2.0);
}

int myclass::set_element(double num){
    // a function to assign a value to the first array element.
    A[0][0] = num;
    cout << A[0][0] << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这编译并正确运行.但是,如果我尝试使用犰狳矩阵,事情就行不通了.

#include<iostream>
#include<armadillo>

using namespace std;
using namespace arma;

class myclass{
private:
    // a matrix
    mat A(2,2);
public:
    int set_element(double);
};

int main(){
    myclass x;
    x.set_element(2.0);
}

int myclass::set_element(double num){
    //function to set the first element.
    A(0,0) = num;
    cout << A(0,0) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我得到一堆或错误.

jayasurya@yvjs:~/comp/cpp$ g++ dummy.cpp -larmadillo
dummy.cpp:10:15: error: expected identifier before numeric constant
dummy.cpp:10:15: error: expected ‘,’ or ‘...’ before numeric constant
dummy.cpp: In member function ‘int myclass::set_element(double)’:
dummy.cpp:22:14: error: no matching function for call to ‘myclass::A(int, int)’
dummy.cpp:22:14: note: candidate is:
dummy.cpp:10:13: note: arma::mat myclass::A(int)
dummy.cpp:10:13: note:   candidate expects 1 argument, 2 provided
dummy.cpp:23:22: error: no matching function for call to ‘myclass::A(int, int)’
dummy.cpp:23:22: note: candidate is:
dummy.cpp:10:13: note: arma::mat myclass::A(int)
dummy.cpp:10:13: note:   candidate expects 1 argument, 2 provided
Run Code Online (Sandbox Code Playgroud)

我相信我在这里错过了一些关键方面; 有人请指出来.

谢谢.!

Mar*_* J. 5

您需要在类主体中声明犰狳矩阵,然后稍后对其进行初始化,例如在您的类构造函数中。由于犰狳矩阵没有编译时大小,因此矩阵的大小属于对象构造,而不是其定义。

class myclass{
private:
    // a matrix - DECLARATION of a member variable
    mat A;
public:
    myclass() // Constructor
    : A(2, 2) { // Default matrix member variable initialization
    }

    // another constructor where you can supply other dimensions:
    myclass(int rows, int cols)
    : A(rows, cols) { // matrix member variable initialization
    } 

    int set_element(double);
};
Run Code Online (Sandbox Code Playgroud)

一旦您了解了这一点,就会对C ++ 11进行一些语法更改,使您可以更优雅地编写默认构造用例,并使用与所需语法接近的语法:

class myclass {
private:
    // a matrix - this syntax allows you to specify the default initialization parameters for this variable
    mat A {2, 2}; 
public:
    int set_element(double);
};
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用编译时固定大小的矩阵,如下面的mtall所建议的,这使您不必在初始化时设置大小:

class myclass {
private:
    // a matrix - this syntax allows you to specify a compile-time size
    mat::fixed<2, 2> A; 
public:
    int set_element(double);
};
Run Code Online (Sandbox Code Playgroud)

  • 犰狳矩阵类可以选择使用编译时大小-被称为固定大小矩阵。例如,`mat :: fixed &lt;2,2&gt; A;`参见文档中的[高级构造函数](http://arma.sourceforge.net/docs.html#adv_constructors_mat)部分。同样,向量也可以选择具有固定大小:`vec :: fixed &lt;10&gt; v;`。 (2认同)