很多时候,用矩阵求解的方法是很简便的,但是C++要完成矩阵运算,自己写实现显然是工程浩大的。因此我们常常借助第三方函数库来解决问题。看到有人推荐说Armadillo,看他给的安装教程也挺简单的,就尝试了一下。结果,弄了一整天才能够正常运行。罪魁祸首是编译器的编译环境设置问题。
下面就具体说一下x64环境下,Armadillo + Vs2013的安装和配置方法:
http://download.csdn.net/detail/u014357799/9246851
1.安装配置 mingw-w64 用于开发64位的程序开发
1)在mingw-w64官网下载mingw-w64在装包: http://www.mingw.org/->Download Installer(右上角)
2)点击mingw-w64进行安装,选择:
Version:选最新版本 我这个是4.9.2
Architecture:x86_64 (64位系统环境开发64位程序)
Threads:posix
Exception:seh
Build revision:1
3)配置mingw-w64环境变量:
在桌面找到我的电脑图标->右键->属性->高级系统设置->选择“高级”选项->选择下面“环境变量”->Administrator 的用 户变量
如果有Path 变量的话直接双击打开变量值栏输入G:\MinGW\mingw64\bin;
并且输入的这段放在最开头,记得有分号。【我这个是将mingw64安装在G盘,所以“G:\MinGW\mingw64\bin;”如果你是 安装在C盘那么就是C:\MinGW\mingw64\bin;】
如果没有Path 变量这一项,就新建一项然后再输入G:\MinGW\mingw64\bin
2、下载Armadillo(官网:http://arma.sourceforge.net/),得到下面的包;
3、将其解压,得到如下目录:
4、打开VS2010,新建一个win32的工程;
5、上网狂搜各种教程和问题,都没找到解决办法。后来有个人的帖子提醒了我,说32位的lib和64位的lib是有区别的,混用可能会出这样的链接问题,因此,将编译器的编译环境改为x64的。
6、选择生成/配置管理器:
7、在出来的框框里,在平台那里新建一个x64的,图中为已经建了x64的,所以有x64的选项。
8、右击新建的项目,选择属性,出来下面的东西:
9、选择VC++目录一项,将包含目录设置为你解压后,include文件夹的路径(记住,一定是知道include这一级,我就多进入了一级目录,错了。);
将库目录设置为解压后example里的lib文件夹;设置后如下:
10、接着选C/C++里的常规项,设置附加包含目录,同样为上述的include文件夹路径:
11、选择链接器/常规,将附加库目录设置为上述的lib文件夹路径:
12、开启工程属性对话框为工程链接所需库文件,包括lapack_win64_MT.lib和 blas_win64_MT.lib,如下图所示:
13、开启工程属性对话框为工程添加开启Lapack和blas的预处理定义,如下图所示:
14、拷贝lib_win32文件夹下的两个dll文件到目录\armadilloTest\armadilloTest下保证开发环境能够调用到这两个dll,当程序发布后则应该放到可执行文件所在目录或者系统system32目录下。
15、OK,点击确定,在cpp里写测试代码试一下:
#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)
int main()
{
cout << "Armadillo version: " << arma_version::as_string() << endl;
mat A(2, 3); // 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(1, 2) = 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(4, 5); // 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(0,2), span(3,4) ):" << endl << B(span(0, 2), span(3, 4)) << 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(B,1): " << endl << max(B, 1) << 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(B,1) =" << endl << sum(B, 1) << 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
// random matrix with values uniformly distributed in the [0,1] interval
mat D = randu
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.58558 << 0.60809;
q.print("q:");
// convert matrix to vector; data in matrices is stored column-by-column
vec v = vectorise(A);
v.print("v:");
// dot or inner product
cout << "as_scalar(r*q): " << as_scalar(r*q) << endl;
// outer product
cout << "q*r: " << endl << q*r << endl;
// multiply-and-accumulate operation (no temporary matrices are created)
cout << "accu(A % B) = " << accu(A % B) << endl;
// example of a compound operation
B += 2.0 * A.t();
B.print("B:");
// imat specifies an integer matrix
imat AA;
imat BB;
AA << 1 << 2 << 3 << endr << 4 << 5 << 6 << endr << 7 << 8 << 9;
BB << 3 << 2 << 1 << endr << 6 << 5 << 4 << endr << 9 << 8 << 7;
// comparison of matrices (element-wise); output of a relational operator is a umat
umat ZZ = (AA >= BB);
ZZ.print("ZZ:");
// cubes ("3D matrices")
cube Q(B.n_rows, B.n_cols, 2);
Q.slice(0) = B;
Q.slice(1) = 2.0 * B;
Q.print("Q:");
// 2D field of matrices; 3D fields are also supported
field
for (uword col = 0; col < F.n_cols; ++col)
for (uword row = 0; row < F.n_rows; ++row)
{
F(row, col) = randu
}
F.print("F:");
system("pause");
return 0;
}