【问题标题】:Using armadillo matrices inside a class在类中使用犰狳矩阵
【发布时间】:2014-02-25 04:32:14
【问题描述】:

我是一名物理学家,在类编程方面没有太多经验。如果有人可以提供帮助,我将不胜感激。我已经在 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;
}

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

#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;
}

当我尝试编译这个时,我得到了一堆错误。

----@----:~/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

我确定我在这里遗漏了一些关键方面;有人请指出。

谢谢!

【问题讨论】:

    标签: c++ matrix armadillo


    【解决方案1】:

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

    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);
    };
    

    一旦您理解了这一点,就会有一些 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);
    };
    

    或者,您可以使用编译时固定大小的矩阵,如下面的 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);
    };
    

    【讨论】:

    • 犰狳矩阵类可以选择使用编译时大小——它们被称为固定大小矩阵。例如 mat::fixed&lt;2,2&gt; A; 请参阅文档中的 advanced constructors 部分。同样,向量也可以选择固定大小:vec::fixed&lt;10&gt; v;
    【解决方案2】:

    您可以使用 Martin J. 的代码,该代码使用动态大小的矩阵,或者下面的代码使用固定大小的矩阵。

    使用固定大小的矩阵有利有弊。一方面,固定大小可以让智能 C++ 编译器(例如 gcc)进行更多优化。另一方面,矩阵大小不能改变,优化的收益只对小矩阵有用。使用大型固定大小的矩阵(即大于 10x10 或 100 个元素)也会占用堆栈内存。

    #include <iostream>
    #include <armadillo>
    
    using namespace std;
    using namespace arma;
    
    class myclass{
    private:
       // a fixed-size matrix: allows more optimization
       // (generally recommended only for sizes <= 10x10)
       mat::fixed<2,2> A;
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多