2014-02-25 86 views
4

我是一位物理学家,在课程编程方面没有多少经验。如果有人能为此提供帮助,我将不胜感激。我已经成功地在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; 
} 

当我尝试编译,我得到了一堆或错误。

[email protected]:~/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 

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

谢谢。

回答

3

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

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

Armadillo矩阵类可以选择使用编译时的大小 - 它们被称为固定大小的矩阵。例如'mat :: fixed <2,2> A;'请参阅文档中的[高级构造函数](http://arma.sourceforge.net/docs.html#adv_constructors_mat)部分。同样,向量也可以有一个固定大小的选项:'vec :: fixed <10> v;' – mtall

+0

好点,会编辑 –

1

你可以用马丁·代码WH它使用动态大小的矩阵,或者下面的代码使用固定大小的矩阵。

使用固定大小的矩阵有优点和缺点。一方面,固定大小可以让智能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; 
}