2014-02-25 139 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; 
}