Armadillo C++ Library

Armadillo 简介

Armadillo C++ Library是一种C++的线性代数库(矩阵数学),具有良好的平衡速度与易用性。其底层可以调用不同的BLAS和LAPACK库来提高效率,同时利用模板编程提高了代码的操作性

官网下载链接:点这里下载

Visual Studio安装步骤

1 解压后进入目录 \armadillo-7.950.1\include\armadillo_bits\config.hpp,将

#define ARMA_USE_LAPACK
#define ARMA_USE_BLAS

前面的注释删除,表示使用,修改后config.cpp表头如下:

#if !defined(ARMA_USE_LAPACK)
#define ARMA_USE_LAPACK
//// Comment out the above line if you don't have LAPACK or a high-speed replacement for LAPACK,
//// such as Intel MKL, AMD ACML, or the Accelerate framework.
//// LAPACK is required for matrix decompositions (eg. SVD) and matrix inverse.
#endif

#if !defined(ARMA_USE_BLAS)
#define ARMA_USE_BLAS
//// Comment out the above line if you don't have BLAS or a high-speed replacement for BLAS,
//// such as OpenBLAS, GotoBLAS, Intel MKL, AMD ACML, or the Accelerate framework.
//// BLAS is used for matrix multiplication.
//// Without BLAS, matrix multiplication will still work, but might be slower.
#endif

2 新建一个项目,进入项目属性。如果下载的是64位,选择平台“x64”

3 进入vc++目录,设置如下:

这里写图片描述

4 进入C/C++ 常规,设置如下:

这里写图片描述

5 进入链接器 输入,设置如下:

安装完成!

也可安装mkl ,英特尔数学核心函数库,Intel Math Kernel Library来使用armadillo,设置如下:

这里写图片描述

Armadillo使用说明

1.armadillo的命名空间是arma

2.主要用到的三个类型分别是arma::Mat(矩阵)、arma::Col(列向量)、arma::Row(行向量),是稠密矩阵类

3.armadillo主要有两种矩阵类(对稠密矩阵而言),分别是Mat和fixed(在arma::Mat内),前者是可改变size的,后者是固定size的,其效率和内部存储机制上有所不同

mat:

public:
const uword n_rows; //!< number of rows (read-only)
const uword n_cols; //!< number of columns (read-only)
const uword n_elem; //!< number of elements (read-only)
const uhword vec_state; //!< 0: matrix layout; 1: column vector layout; 2: row vector layout
const uhword mem_state;
// mem_state = 0: normal matrix which manages its own memory
// mem_state = 1: use auxiliary memory until a size change
// mem_state = 2: use auxiliary memory and don't allow the number of elements to be changed
// mem_state = 3: fixed size (eg. via template based size specification)
arma_aligned const eT* const mem; //!< pointer to the memory used for storing elements (memory is read-only)
protected:
arma_align_mem eT mem_local[ arma_config::mat_prealloc ]; // local storage, for small vectors and matrices

直接通过mat.n_rows来获得矩阵的行数。请求的空间小于 arma_config::mat_prealloc 这个数(一般为 16 )时,Mat 直接使用数组 mem_local 的空间来存储矩阵元素,而当矩阵所需空间大于这个值时,其内部通过 new 空间来获得更大的堆空间(一般不能超过 20000*20000 的矩阵)

fixed:

private:
static const uword fixed_n_elem = fixed_n_rows * fixed_n_cols;
static const bool use_extra = (fixed_n_elem > arma_config::mat_prealloc);
arma_align_mem eT mem_local_extra[ (use_extra) ? fixed_n_elem : 1 ];

矩阵规模不超过200*200时,最好使用fixed来存储矩阵元素

Mat
typedef Mat <float> fmat;
typedef Col <float> fvec;
typedef Col <float> fcolvec;
typedef Row <float> frowvec;

typedef Mat <double> dmat;
typedef Col <double> dvec;
typedef Col <double> dcolvec;
typedef Row <double> drowvec;

typedef Mat <double> mat;
typedef Col <double> vec;
typedef Col <double> colvec;
typedef Row <double> rowvec;

typedef Mat <cx_float> cx_fmat;
typedef Col <cx_float> cx_fvec;
typedef Col <cx_float> cx_fcolvec;
typedef Row <cx_float> cx_frowvec;

typedef Mat <cx_double> cx_dmat;
typedef Col <cx_double> cx_dvec;
typedef Col <cx_double> cx_dcolvec;
typedef Row <cx_double> cx_drowvec;

typedef Mat <cx_double> cx_mat;
typedef Col <cx_double> cx_vec;
typedef Col <cx_double> cx_colvec;
typedef Row <cx_double> cx_rowvec;

fixed:
typedef cx_mat::fixed<2,2> cx_mat22;
typedef cx_mat::fixed<3,3> cx_mat33;
typedef cx_mat::fixed<4,4> cx_mat44;
typedef cx_mat::fixed<5,5> cx_mat55;
typedef cx_mat::fixed<6,6> cx_mat66;
typedef cx_mat::fixed<7,7> cx_mat77;
typedef cx_mat::fixed<8,8> cx_mat88;
typedef cx_mat::fixed<9,9> cx_mat99;



typedef vec::fixed<2> vec2;
typedef vec::fixed<3> vec3;
typedef vec::fixed<4> vec4;
typedef vec::fixed<5> vec5;
typedef vec::fixed<6> vec6;
typedef vec::fixed<7> vec7;
typedef vec::fixed<8> vec8;
typedef vec::fixed<9> vec9;

typedef cx_vec::fixed<2> cx_vec2;
typedef cx_vec::fixed<3> cx_vec3;
typedef cx_vec::fixed<4> cx_vec4;
typedef cx_vec::fixed<5> cx_vec5;
typedef cx_vec::fixed<6> cx_vec6;
typedef cx_vec::fixed<7> cx_vec7;
typedef cx_vec::fixed<8> cx_vec8;
typedef cx_vec::fixed<9> cx_vec9;

常用接口

打印:

.print() 
.print( header ) 

.print( stream ) 
.print( stream, header )

构造函数:

//构造函数
inline Mat();   

//指定Mat行列,元素默认未初始化
inline explicit Mat(const uword in_rows, const uword in_cols);

//以arma::size()方式获得的行列信息作为参数
inline explicit Mat(const SizeMat& s);

//初始化设定矩阵大小同时用f方式填充
template<typename fill_type> inline Mat(const uword in_rows, const uword in_cols, const fill::fill_class<fill_type>& f);

//以arma::size()方式获得的行列信息作为参数,同时用f方式填充
template<typename fill_type> inline Mat(const SizeMat& s, const fill::fill_class<fill_type>& f);

//以文本作为输入,eg.”1 2;3 4;”就是用来新建一个矩阵的,空格表示不同元素,分号代表换行
inline Mat(const char* text);
inline Mat& operator=(const char* text);
inline Mat(const std::string& text);
inline Mat& operator=(const std::string& text);

//以STL的vector作为输入
inline Mat(const std::vector<eT>& x);
inline Mat& operator=(const std::vector<eT>& x);

//复制构造函数
inline Mat(const Mat& m);

//此处subview是用过切片(获取子矩阵)得到的,这个在后面会讲到
inline Mat(cons::
 subview<eT>& X);

矩阵运算:

mat P = A + B;
mat Q = A - B;
mat R = -B;
mat S = A / 123.0;
mat T = A % B; //dot
mat U = A * C;

转置与共轭:

Eg.
mat A = randu<mat>(4,5);
mat B = A.t();  //转置
Eg.
cx_mat X = randu<cx_mat>(5,5);
cx_mat Y = conj(X); //共轭

切片:

X.col( col_number ) //取行
X.row( row_number ) //取列

X.cols( first_col, last_col )
X.rows( first_row, last_row )
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值