资源简介
armadillo C++矩阵运算函数库,具体教程压缩包内有,不知道为什么国内去官网下下来的少很多东西
代码片段和文件信息
#include
#include
using namespace std;
using namespace arma;
// Armadillo documentation is available at:
// http://arma.sourceforge.net/docs.html
int
main(int argc char** argv)
{
cout << “Armadillo version: “ << arma_version::as_string() << endl;
mat A(23); // directly specify the matrix size (elements are uninitialised)
cout << “A.n_rows: “ << A.n_rows << endl; // .n_rows and .n_cols are read only
cout << “A.n_cols: “ << A.n_cols << endl;
A(12) = 456.0; // directly access an element (indexing starts at 0)
A.print(“A:“);
A = 5.0; // scalars are treated as a 1x1 matrix
A.print(“A:“);
A.set_size(45); // change the size (data is not preserved)
A.fill(5.0); // set all elements to a particular value
A.print(“A:“);
// endr indicates “end of row“
A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
<< 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
<< 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
<< 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
<< 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
A.print(“A:“);
// determinant
cout << “det(A): “ << det(A) << endl;
// inverse
cout << “inv(A): “ << endl << inv(A) << endl;
// save matrix as a text file
A.save(“A.txt“ raw_ascii);
// load from file
mat B;
B.load(“A.txt“);
// submatrices
cout << “B( span(02) span(34) ):“ << endl << B( span(02) span(34) ) << endl;
cout << “B( 03 size(32) ):“ << endl << B( 03 size(32) ) << endl;
cout << “B.row(0): “ << endl << B.row(0) << endl;
cout << “B.col(1): “ << endl << B.col(1) << endl;
// transpose
cout << “B.t(): “ << endl << B.t() << endl;
// maximum from each column (traverse along rows)
cout << “max(B): “ << endl << max(B) << endl;
// maximum from each row (traverse along columns)
cout << “max(B1): “ << endl << max(B1) << endl;
// maximum value in B
cout << “max(max(B)) = “ << max(max(B)) << endl;
// sum of each column (traverse along rows)
cout << “sum(B): “ << endl << sum(B) << endl;
// sum of each row (traverse along columns)
cout << “sum(B1) =“ << endl << sum(B1) << endl;
// sum of all elements
cout << “accu(B): “ << accu(B) << endl;
// trace = sum along diagonal
cout << “trace(B): “ << trace(B) << endl;
// generate the identity matrix
mat C = eye(44);
// random matrix with values uniformly distributed in the [01] interval
mat D = randu(44);
D.print(“D:“);
// row vectors are treated like a matrix with one row
rowvec r;
r << 0.59119 << 0.77321 << 0.60275 << 0.35887 << 0.51683;
r.print(“r:“);
// column vectors are treated like a matrix with one column
vec q;
q << 0.14333 << 0.59478 << 0.14481 << 0.
- 上一篇:推箱子 源代码 MFC
- 下一篇:MFC界面设计
评论
共有 条评论