C++中armadillo矩阵库使用说明

article/2025/8/21 2:59:12

在http://blog.csdn.net/piaoxuezhong/article/details/58055709博文中介绍了eigen矩阵库的使用,这里介绍另一种矩阵库:armadillo~

Armadillo:C++下的Matlab替代品

armadillo是目前使用比较广的C++矩阵运算库之一,许多Matlab的矩阵操作函数都可以找到对应,这 对习惯了Matlab的人来说实在是非常方便,另外如果要将Matlab下做研究的代码改写成C++,使用Armadillo也会很方便。下面列了一些Armadillo的特性:
  • 支持整数,浮点数,和复数矩阵。
  • 支持矩阵逐元素操作,包括abs · conj · conv_to · eps · imag/real · misc functions (exp, log, pow, sqrt, round, sign, ...) · trigonometric functions (cos, sin, ...)等等。
  • 支持矩阵分块操作。
  • 支持对整体矩阵的操作diagvec · min/max · prod · sum · statistics (mean, stddev, ...) · accu · as_scalar · det · dot/cdot/norm_dot · log_det · norm · rank · trace等等。
  • Matlab用户,你甚至可以找到你熟悉的hist · histc · unique · cumsum · sort_index · find · repmat · linspace等函数。
  • 除了自带的矩阵基本运算之外,可自动检测是否安装有BLAS,或更快的 OpenBLAS, Intel MKL, AMD ACML,并使用他们替代自带基本运算实现。
  • 提供接口使用LAPACK进行矩阵分解运算,svd · qr · lu · fft等等。
  • 提供了稀疏矩阵类,支持常用操作,但暂时没有矩阵分解的实现。
可以使用OpenBLAS等库进行加速,安装教程参见:http://www.cnblogs.com/youthlion/archive/2012/05/15/2501465.html

vs下安装Armadillo

1、下载Armadillo,解压后把其中的include文件夹完整拷贝出来,放到某处,如D:\Armadillo;

2、修改D:\Armadillo\include\armadillo_bits\config.hpp,将

  #define ARMA_USE_LAPACK 
  #define ARMA_USE_BLAS

这两句取消注释。表示使用这两个库。

3、安装lapack和blas。实际上第一步中下载的压缩包里自带了这两个库,但是在vs2010中用这两个库会出现卡死现象,其他编译环境没有测试。可以去http://icl.cs.utk.edu/lapack-for-windows/clapack/index.html下载blas.lib,libf2c.lib,lapack.lib三个库,并在编译环境的额外依赖库中添加这三个库。(linker-> input-> additional dependencies)

1

4、在编译器的include目录中添加include文件夹的路径以及第三步中三个库文件所在位置。如图:

2

矩阵类Mat简介:

Mat<type>为模板类,其中type可以是:float, double, std::complex, std::complex, short, int, long, and unsigned versions of short, int, long等。为方便起见,Armadillo C++已经预定义了以下类型:

mat  =  Mat<double>  //mat为double类型
fmat =  Mat<float>   //fmat为float类型
cx_mat  =  Mat<cx_double> //cx_表示复数类型
cx_fmat =  Mat<cx_float>
umat =  Mat<uword> //相当于unsign int类型
imat  =  Mat<sword> //相当于int类型

创建方式:

  • mat() 
    mat(n_rowsn_cols) 
    mat(n_rowsn_colsfill_type) 
    mat(size(X)) 
    mat(size(X), fill_type) 
    mat(mat) 
    mat(sp_mat) 
    mat(vec) 
    mat(rowvec) 
    mat(initializer_list) 
    mat(string) 
    mat(std::vector)   
    cx_mat(mat,mat) 

实例:

mat A(5, 5, fill::randu);
double x = A(1,2);mat B = A + A;
mat C = A * B;
mat D = A % B;cx_mat X(A,B);B.zeros();
B.set_size(10,10);
B.ones(5,6);B.print("B:")

Armadillo中常见的功能说明:

Function/Variable Description
:n rows
:n cols
:n elem
number of rows (read only)
number of columns (read only)
total number of elements (read only)
(i)
(r, c)
[i]
.at(r, c)
:memptr()
access the i-th element, assuming a column-by-column layout
access the element at row
r and column c
as per (i), but no bounds check; use only after debugging
as per (r, c), but no bounds check; use only after debugging
obtain the raw memory pointer to element data
:in range(i)
:in range(r, c)
test whether the i-th element can be accessed
test whether the element at row
r and column c can be accessed
:reset()
:copy size(A)
:set size(rows, cols)
.reshape(rows, cols)
.resize(rows, cols)
set the number of elements to zero
set the size to be the same as matrix
A
change size to specified dimensions, without preserving data (fast)
change size to specified dimensions, with elements copied column-wise (slow)
change size to specified dimensions, while preserving elements & their layout (slow)
:ones(rows, cols)
:zeros(rows, cols)
:randu(rows, cols)
:randn(rows, cols)
:fill(k)
:for each( [ ] (double& val) f...g)
set all elements to one, optionally first resizing to specified dimensions
as above, but set all elements to zero
as above, but set elements to uniformly distributed random values in [0,1] interval
as above, but use a Gaussian/normal distribution with
µ = 0 and σ = 1
set all elements to be equal to
k
for each element, pass its reference to a lambda function (C++11 only)
:is empty()
:is finite()
:is square()
:is vec()
:is sorted()
test whether there are no elements
test whether all elements are finite
test whether the matrix is square
test whether the matrix is a vector
test whether the matrix is sorted
:has inf()
:has nan()
test whether any element is ±1
test whether any element is not-a-number
:begin()
:end()
:begin row(i)
:end row(j)
:begin col(i)
:end col(j)
iterator pointing at the first element
iterator pointing at the
past-the-end element
iterator pointing at first element of row
i
iterator pointing at one element past row j
iterator pointing at first element of column i
iterator pointing at one element past column j
:print(header)
:raw print(header)
print elements to the cout stream, with an optional text header
as per .print(), but do not change stream settings
:save(name, format)
:load(name, format)
store matrix in the specified file, optionally specifying storage format
retrieve matrix from the specified file, optionally specifying format
:diag(k)
:row(i)
:col(i)
:rows(a, b)
:cols(c, d)
:submat( span(a,b), span(c,d) )
:submat( p, q, size(A) )
:rows( vector of row indices )
:cols( vector of col indices )
:elem( vector of indices )
read/write access to k-th diagonal
read/write access to row
i
read/write access to column i
read/write access to submatrix, spanning from row a to row b
read/write access to submatrix, spanning from column c to column d
read/write access to submatrix spanning rows a to b and columns c to d
read/write access to submatrix starting at row p and col q with size same as matrix A
read/write access to rows corresponding to the specified indices
read/write access to columns corresponding to the specified indices
read/write access to matrix elements corresponding to the specified indices
:each row()
:each col()
:swap rows(p, q)
:swap cols(p, q)
:insert rows(row, X)
:insert cols(col, X)
:shed rows(first row, last row)
:shed cols(first col, last col)
repeat a vector operation on each row (eg. A.each row() += row vector)
repeat a vector operation on each column (eg.
A.each col() += col vector)
swap the contents of specified rows
swap the contents of specified columns
insert a copy of
X at the specified row
insert a copy of
X at the specified column
remove the specified range of rows
remove the specified range of columns
:min()
:max()
:index min()
:index max()
return minimum value
return maximum value
return index of minimum value
return index of maximum value

实例:

mat A = randu<mat>(10,10);
A(9,9) = 123.0;
double x = A.at(9,9);
double y = A[99];vec p = randu<vec>(10,1);
p(9) = 123.0;
double z = p[9];

Armadillo与Matlab的对比:

Matlab & Octave Armadillo Notes
A(1, 1)
A(k, k)
A(0, 0)
A(k-1, k-1)
indexing in Armadillo starts at 0, following C++ convention
size(A,1)
size(A,2)
size(Q,3)
numel(A)
A.n rows
A.n cols
Q.n slices
A.n elem
member variables are read only
Q is a cube (3D array)
.n elem indicates the total number of elements
A(:, k)
A(k, :)
A(:, p:q)
A(p:q, :)
A(p:q, r:s)
A.col(k)
A.row(k)
A.cols(p, q)
A.rows(p, q)
A( span(p, q), span(r, s) )
read/write access to a specific column
read/write access to a specific row
read/write access to a submatrix spanning the specified cols
read/write access to a submatrix spanning the specified rows
A( span(first row, last row), span(first col, last col) )
Q(:, :, k)
Q(:, :, t:u)
Q.slice(k)
Q.slices(t, u)
Q is a cube (3D array)
A0
A:0
A.t() or trans(A)
A.st() or strans(A)
transpose (for complex matrices the conjugate is taken)
simple transpose (for complex matrices the conjugate is not taken)
A = zeros(size(A))
A = ones(size(A))
A = zeros(k)
A = ones(k)
A.zeros()
A.ones()
A = zeros(k,k)
A = ones(k,k)
set all elements to zero
set all elements to one
create a matrix with elements set to zero
create a matrix with elements set to one
C = complex(A,B) cx mat C = cx mat(A,B) construct a complex matrix out of two real matrices
A B
A
:B
A
:= B
A
n B
A = A
+ 1
A = A
- 1
A B
A
% B
A
= B
solve(A,B)
A
++
A--
% indicates element-wise multiplication
= indicates element-wise division
solve a system of linear equations
A = [ 1 2;
3 4; ]
A = f f 1, 2 g,
f 3, 4 g g
requires C++11 compiler
X = [ A B ]
X = [ A; B ]
X = join rows(A,B)
X = join cols(A,B)
AA.print(“A:”)
or
cout
<< A << endl
print the contents of a matrix to the standard output
save -ascii ‘A.dat’ A
load -ascii ‘A.dat’
A.save(“A.dat”, raw ascii)
A.load(“A.dat”, raw ascii)
Matlab/Octave matrices saved as ascii text are readable
by Armadillo (and vice-versa)
A = rand(2,3);
B = randn(4,5);
F =
f A; B g;
mat A = randu(2,3);
mat B = randn(4,5);
field
<mat> F(2,1);
F(0,0) = A; F(1,2) = B;
randu generates uniformly distributed random numbers
the field class can store multiple varying size matrices

综合测试程序:

#include <iostream>
#include <armadillo>using namespace std;
using namespace arma;int main(int argc, char** argv)
{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 onlycout<<"A.n_cols: "<<A.n_cols<<endl;cout<<A.is_empty()<<endl;cout<<static_cast<bool>(A.memptr())<<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 matrixA.print("A:");A.set_size(4, 5); // change the size (data is not preserved)A.fill(5.0);     // set all elements to a particular valueA.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:");cout<<"det(A): "<<det(A)<<endl;cout<<"inv(A): "<<endl<<inv(A)<<endl;// save matrix as a text fileA.save("A.txt", raw_ascii);// load from filemat B;B.load("A.txt");// imat specifies an integer matriximat 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 umatumat ZZ = (AA>=BB);ZZ.print("ZZ:");// 2D field of matrices; 3D fields are also supportedfield<mat> F(4, 3);for (uword col = 0; col<F.n_cols; ++col)for (uword row = 0; row<F.n_rows; ++row){F(row, col) = randu<mat>(2, 3);  // each element in field<mat> is a matrix}F.print("F:");return 0;
}

参考:

http://blog.csdn.net/jnulzl/article/details/46808515

http://www.cnblogs.com/einyboy/p/3852319.html


http://chatgpt.dhexx.cn/article/JAcMYr40.shtml

相关文章

armadillo库安装教程

目录 armadillo库功能介绍 armadillo库安装 vs中添加步骤 测试 armadillo库功能介绍 在c编程中&#xff0c;我们在进行一些算法运算经常会面对矩阵计算&#xff0c;c的标准库中是没有关于矩阵运算的库的&#xff0c;在面对矩阵计算我们只能自己编写相关代码进行计算&#xf…

armadillo使用,armadillo提高编译效率和速度

Armadillo是一个全面的、基于模板的 C 线性代数库&#xff0c;设计有 LAPACK 和 ATLAS 库的替代接口。 armadillo使用工具旨在提供速度和易用性&#xff0c;以及类似于 Matlab 的熟悉语法(或 API)。 armadillo使用允许您编写可以集成到组件或应用程序中的各种类型的数学函数。它…

C++ Armadillo矩阵库的安装与基本用法

文章目录 Armadillo安装入门案例直接赋值切片常用函数 Armadillo 安装 Armadillo是一个具有Matlab风格的线性代数包。下载之后解压到任意文件夹&#xff0c;然后对VS工程进行设置。 菜单栏生成->配置管理器&#xff0c;将平台改为x64右键项目名称->属性(快捷键ShiftF4…

一个常见的大数据平台架构

这是一个典型的大数据架构&#xff0c;且对架构进行了「分层」&#xff0c;分为「数据源层」、「数据传输层」、「数据存储层」、「编程模型层」和「数据分析层」&#xff0c;如果继续往上走的话&#xff0c;还有「数据可视化层」和「数据应用层」。

大数据平台架构实践

说明 本篇博客整理自参考内容&#xff0c;完整内容请查看原文章&#xff1b; 技术选型 MOLAP 与Druid相类似的实时数据分析工具&#xff0c;还有Linkedln的Pinot和eBay的Kylin&#xff0c;它们都是基于Java开发的。Druid相对比较轻量级&#xff0c;用的人也多&#xff0c;毕…

网易大数据平台架构实践分享!

随着网易云音乐、新闻、考拉、严选等互联网业务的快速发展&#xff0c;网易开始加速大数据平台建设&#xff0c;以提高数据获取速度&#xff0c;提升数据分析效率&#xff0c;更快发挥数据价值。 本次演讲主要分享网易如何围绕和改造开源技术&#xff0c;以产品化思维打造网易自…

详解大数据平台架构

目录: 什么是大数据 Hadoop介绍-HDFS、MR、Hbase 大数据平台应用举例-腾讯 公司的大数据平台架构 “就像望远镜让我们能够感受宇宙,显微镜让我们能够观测微生物一样,大数据正在改变我们的生活以及理解世界的方式……”。 大数据的4V特征 公司的“大数据” 随着公司业…

京东金融大数据平台架构(附82页PPT)

公众号推文规则变了&#xff0c;点击上方 "数据社", 设为星标 后台回复【加群】&#xff0c;申请加入数据学习交流群 大家好&#xff0c;我是一哥&#xff0c;给大家分享一下京东金融大数据分析平台总体架构介绍&#xff0c;废话不说&#xff0c;干货收藏吧&#xff…

大数据平台架构设计探究

本文首发于 vivo互联网技术 微信公众号 链接&#xff1a;https://mp.weixin.qq.com/s/npRRRDqNUHNjbybliFxOxA 作者&#xff1a;刘延江 近年来&#xff0c;随着IT技术与大数据、机器学习、算法方向的不断发展&#xff0c;越来越多的企业都意识到了数据存在的价值&#xff0c;将…

乡村振兴大数据平台架构方案(附下载)

迎加入星球下载所有资料&#xff0c;转发朋友圈截图回复666亦可下载 加入星球&#xff0c;好资料一直有 推荐阅读&#xff1a; 世界的真实格局分析&#xff0c;地球人类社会底层运行原理 不是你需要中台&#xff0c;而是一名合格的架构师&#xff08;附各大厂中台建设PPT&#…

大数据平台架构+ETL

1 ETL&#xff0c;Extraction-Trasformation-Loading&#xff0c;即数据读取&#xff0c;转换&#xff0c;装载的过程&#xff0c;是构建数据仓库的重要环节。 ETL是将业务系统的数据经过抽取&#xff0c;清洗转换之后加载到数据仓库的过程&#xff0c;目的是将企业中的额分散的…

百度、阿里、腾讯平台架构都熟悉,小米大数据平台架构OLAP架构演进是否了解

阿里、百度、腾讯的大数据平台架构都有很多地方有介绍了&#xff0c;接下来给大家深入了解小米大数据平台架构OLAP架构演进 全网最详细的Hadoop文章系列&#xff0c;强烈建议收藏加关注&#xff01; 2021大数据领域优质创作博客&#xff0c;带你从入门到精通&#xff0c;该博客…

大数据平台架构的层次划分

1. 数据源层&#xff1a;包括传统的数据库&#xff0c;数据仓库&#xff0c;分布式数据库&#xff0c;NOSQL数据库&#xff0c;半结构化数据&#xff0c;无结构化数据&#xff0c;爬虫&#xff0c;日志系统等&#xff0c;是大数据平台的数据产生机构。 2. 数据整理层&#xff…

常见的大数据平台架构设计思路

近年来&#xff0c;随着IT技术与大数据、机器学习、算法方向的不断发展&#xff0c;越来越多的企业都意识到了数据存在的价值&#xff0c;将数据作为自身宝贵的资产进行管理&#xff0c;利用大数据和机器学习能力去挖掘、识别、利用数据资产。如果缺乏有效的数据整体架构设计或…

大数据平台架构浅析——以讯飞大数据平台Odeon为例

文章目录 大数据平台架构解析——以讯飞大数据平台Odeon为例定义功能数据采集数据开发数据分析数据编程补充 大数据平台架构解析——以讯飞大数据平台Odeon为例 定义 Odeon大数据平台以全图形化Web操作的形式为用户提供一站式的大数据能力&#xff1a;包括数据采集、任务编排、…

浅谈大数据平台架构设计

全文共3735个字&#xff0c;建议8分钟阅读 近年来&#xff0c;随着IT技术与大数据、机器学习、算法方向的不断发展&#xff0c;越来越多的企业都意识到了数据存在的价值&#xff0c;将数据作为自身宝贵的资产进行管理&#xff0c;利用大数据和机器学习能力去挖掘、识别、利用数…

大数据平台架构包含哪些功能

为了满足企业对于数据的各种需求&#xff0c;需要基于大数据技术构建大数据平台。结合大数据在企业的实际应用场景&#xff0c;如下图所示的大数据平台架构所示&#xff1a; 最上层为应用提供数据服务与可视化&#xff0c;解决企业实际问题。第2层是大数据处理核心&#xff0c;…

大数据平台层级架构图

主流数据平台架构 一般包含三个层级&#xff0c;ODS层、数据仓库层、数据应用层。 业务系统的操作和日志数据抽取到ODS层&#xff0c;ODS的数据经过ETL过程&#xff08;抽取Extraction&#xff0c;转化Transformation&#xff0c;加载Loading&#xff09;进入数据仓库&#xff…

大数据平台架构

1 大数据组件架构 大数据架构分为:数据采集,传输,存储,调度和处理这五个部分.其中任务定期执行和任务分配,分别使用Azkaban和Zookeeper&#xff0c;大数据平台整体架构如图1所示&#xff0c;由图1可知&#xff0c;大数据平台的基础是服务器&#xff08;硬件&#xff09;&#…

大数据平台核心架构图鉴,建议收藏!

我们先来看看这张图&#xff0c;这是某公司使用的大数据平台架构图&#xff0c;大部分公司应该都差不多&#xff1a; 从这张大数据的整体架构图上看来&#xff0c;大数据的核心层应该是&#xff1a;数据采集层、数据存储与分析层、数据共享层、数据应用层&#xff0c;可能叫法有…