R語言科學(xué)計(jì)算RcppArmadillo簡(jiǎn)明手冊(cè)
RcppArmadillo幾乎和C++中的Armadillo一樣,因此本文主要參考Armadillo主頁上的手冊(cè)。
http://arma.sourceforge.net/docs.html
1. 常用數(shù)據(jù)類型
Mat<type>為模板類,其中type可以是:float, double, std::complex, std::complex, short, int, long, and unsigned versions of short, int, long等。為方便起見,Armadillo C++已經(jīng)預(yù)定義了以下類型。
在Armadillo中,矩陣是按照一列一列(column by column)存在內(nèi)存中的(column-major ordering)。
| Data Type | Mathematics | Details |
|---|---|---|
| mat, cx_mat | Matrix 矩陣 | Dense real/complex matrix class |
| vec, cx_vec | Column Vector 列向量 | Dense real/complex column vector class |
| rowvec, cx_rowvec | Row Vector 行向量 | Dense real/complex row vector class |
| cube, cx_cube | Cube 3維矩陣 | Dense real/complex cube class (“3D matrix”) |
| field | 域 | Class for storing arbitrary objects in matrix-like or cube-like layouts |
| sp_mat, sp_cx_mat | Matrix 矩陣 | Sparse real/complex matrix class |
| umat/imat | Matrix 矩陣 | Matrix with unsigned/integer elements |
| uvec/ivec | Vector 矩陣 | Vector with unsigned/integer elements |
2. 數(shù)學(xué)運(yùn)算
| 算子Operator | 描述 |
|---|---|
| + | Addition of two objects |
| - | Subtraction of one object from another or negation of an object |
| / | Element-wise division of an object by another object or a scalar |
| * | Matrix multiplication of two objects; not applicable to the Cube class unless multiplying a cube by a scalar |
| % | Schur product: element-wise multiplication of two objects |
| == | Element-wise equality evaluation of two objects; generates a matrix of type umat with entries that indicate whether at a given position the two elements from the two objects are equal (1) or not equal (0) |
| != | Element-wise non-equality evaluation of two objects |
| <= | As for ==, but the check is for “greater than or equal to” |
| >= | As for ==, but the check is for “l(fā)ess than or equal to” |
| < | As for ==, but the check is for “greater than” |
| > | As for ==, but the check is for “l(fā)ess than” |
3. 向量、矩陣和域的創(chuàng)建
基本創(chuàng)建
//Matrix: X
mat X(n_rows, n_cols);
mat X(n_rows, n_cols, fill_type);
mat X(size(Y));
mat X(size(Y), fill_type);
mat X("1 0;2 0"); \\create a matrix from a string. (first row is "1 0", second row is "2 0")
cx_mat X(mat,mat); \\for constructing a complex matrix out of two real matrices
//Vector: X
vec X(n_elem);
vec X(n_elem, fill_type);
vec X(size(Y));
vec X(size(Y), fill_type);
cx_vec X(vec,vec); \\for constructing a complex vector out of two real vectors
//Cube or 3-D matrix: X
cube X(n_rows, n_cols, n_slices);
cube X(n_rows, n_cols, n_slices, fill_type);
cube X(size(Y));
cube X(size(Y), fill_type);
cx_cube X(cube, cube); \\for constructing a complex cube out of two real cubes
//Field: F
field<object_type> F(n_elem)
field<object_type> F(n_rows, n_cols)
field<object_type> F(n_rows, n_cols, n_slices)
field<object_type> F(size(X))
//似乎object_type只能是mat或者cube,且不能存儲(chǔ)復(fù)數(shù)類型,比如cx_mat.
//An example of field
mat A(3,4,fill::randu);
vec B(5,fill::randn);
field <mat> F(2,1);
F(0)=A;
F(1)=B;
其中fill_type是可選的,可以是以下選擇
| fill_type | 描述 |
|---|---|
| fill::zeros | set all elements to 0 |
| fill::ones | set all elements to 1 |
| fill::eye | set the elements along the main diagonal to 1 and off-diagonal elements to 0 |
| fill::randu | set each element to a random value from a uniform distribution in the [0,1] interval |
| fill::randn | set each element to a random value from a normal/Gaussian distribution with zero mean and unit variance |
用函數(shù)創(chuàng)建
| 函數(shù) | 語法 |
|---|---|
| eye() | matrix_type X = eye<matrix_type>(n_rows,n_cols) |
| matrix_type Y = eye<matrix_type>(size(X)) | |
| linspace() | vector_type v = linspace<vector_type>(start,end,N) |
| logspace() | vector_type v = logspace<vector_type>(A, B, N) |
| regspace() | vector_type v = regspace<vector_type>(start, delta, end) |
| ones() | vector_type v = ones<vector_type>(n_elem) |
| matrix_type X = ones<matrix_type>(n_rows,n_cols) | |
| cube_type Q = ones<cube_type>(n_rows,n_cols,n_slices) | |
| some_type R = ones<some_type>(size(Q)) | |
| randi() | vector_type v = rand_type<vector_type>( n_elem, distr_param(a,b)) |
| or randu() | matrix_type X = rand_type<matrix_type>( n_rows, n_cols, distr_param(a,b)) |
| or randn() | matrix_type Y = rand_type<matrix_type>(size(X),distr_param(a,b)) |
| or randg() | cube_type Q = rand_type<cube_type>( n_rows, n_cols, n_slices, distr_param(a,b)) |
注釋:
rand_type可以是randi()、randu()、randn()和randg(),分別代表 [a,b] 區(qū)間中的整數(shù)隨機(jī)值, U[0,1] 分布中的隨機(jī)浮點(diǎn)值,從標(biāo)準(zhǔn)正態(tài)分布中抽取的隨機(jī)值,從參數(shù)為a,b的Gamma分布中抽取的隨機(jī)值。distr_param(a,b) 只適用于randi()和randg()。
e.g.
vec v=randu<vec>(5);
regspace()中delta默認(rèn)是1或-1。
4. 初始化,元素訪問,屬性和成員函數(shù)
4.1. 元素初始化 Element initialization
// C++11
vec v = { 1, 2, 3 };
mat A = { {1, 3, 5},
{2, 4, 6} };
4.2. 元素訪問 Element access
無論是向量vec,矩陣mat,立方體cube,還是域field,每個(gè)維數(shù)均是從0開始的。
| 元素訪問 | 描述 |
|---|---|
| (n) | 對(duì)于vec和rowvec,訪問第n個(gè)元素。對(duì)于mat和field,首先把矩陣的下一列接到上一列之下,從而構(gòu)成一個(gè)長(zhǎng)列向量,并訪問第n個(gè)元素。 |
| (i,j) | 對(duì)于mat和二維field,訪問第(i,j)個(gè)元素。 |
| (i,j,k) | 對(duì)于cube和3D field,訪問第(i,j,k)個(gè)元素 |
4.3. 子矩陣訪問 Submatrix view
矩陣X的連續(xù)子集訪問
| 函數(shù) | 描述 |
|---|---|
| X.diag(k) | 訪問矩陣X的第k個(gè)對(duì)角線(k是可選的,主對(duì)角線為k=0,上對(duì)角線為k>0,下對(duì)角線為k<0) |
| X.row(i) | 訪問矩陣X的第i行 |
| X.col(i) | 訪問矩陣X的第i列 |
| X.rows(a,b) | 訪問矩陣X從第a行到第b行的子矩陣 |
| X.cols(c,d) | 訪問舉證X從第c列到第d列的子矩陣 |
| X.submat(a,c,b,d) | 訪問矩陣從第a行到第b行和第c列到第d列的子矩陣 |
| X.submat(span(a,b),span(c,d)) | 訪問矩陣從第a行到第b行和第c列到第d列的子矩陣 |
| X(a,c, size(n_rows, n_cols)) | 訪問矩陣從第a行和第c列開始大小為n_rows和n_cols大小的子矩陣 |
| X(a,c, size(Y)) | 訪問矩陣從a行和第c列開始大小和Y相當(dāng)?shù)淖泳仃?/td> |
| X(span(a, b), sel_col) | 訪問第sel_col列,從第a行到第b行之間的數(shù)據(jù)。返回值為向量。 |
| X(sel_row, span(c,d)) | 訪問第sel_row行,從第c列到第d列之間的數(shù)據(jù)。返回值為向量。 |
| X.head_cols( number_of_cols) | 返回頭幾列 |
| X.head_rows( number_of_rows) | 返回頭幾行 |
| X.tail_cols( number_of_cols) | 返回尾幾列 |
| X.tail_rows( number_of_rows) | 返回尾幾行 |
注釋:
(1) span(start,end)可以被span::all代替,意味著這一維上所有的元素。
(2) X.diag(k)可以改變第k個(gè)對(duì)角線的值。
mat X=randn<mat>(4,4);
vec v={1,2,3,4};
X.diag()=v;
向量V的連續(xù)子集訪問
| 函數(shù) | 描述 |
|---|---|
| V(span(a,b)) | 訪問向量V從第a個(gè)元素開始到第b個(gè)元素結(jié)束的子向量 |
| V.subvec(a,b) | 訪問向量V從第a個(gè)元素開始到第b個(gè)元素結(jié)束的子向量 |
| V.subvec(a,size(W)) | 訪問向量V從第a個(gè)元素開始,長(zhǎng)度和W相當(dāng)?shù)淖酉蛄?/td> |
| V.head(n_ele) | 訪問向量V頭幾個(gè)元素 |
| V.tail(n_ele) | 訪問向量V尾幾個(gè)元素 |
向量或矩陣X的間斷子集訪問
| 函數(shù) | 描述 |
|---|---|
| X.elem(vector_of_indices) | 向量或者矩陣(按照列向量化以后)中坐標(biāo)為vector_of_indices的元素;返回向量 |
| X(vector_of_indices) | 向量或者矩陣(按照列向量化以后)中坐標(biāo)為vector_of_indices的元素;返回向量 |
| X.cols(vector_of_column_indices) | 矩陣X列坐標(biāo)為vector_of_column_indices的子矩陣;返回矩陣 |
| X.rows(vector_of_row_indices) | 矩陣X行坐標(biāo)為vector_of_row_indices的子矩陣;返回矩陣 |
| X.submat(vector_of_row_indices, vector_of_column_indices) | 矩陣X行坐標(biāo)為vector_of_row_indices和列坐標(biāo)為vector_of_column_indices的子矩陣;返回矩陣 |
| X(vector_of_row_indices, vector_of_column_indices) | 矩陣X行坐標(biāo)為vector_of_row_indices和列坐標(biāo)為vector_of_column_indices的子矩陣;返回矩陣 |
立方體(三維矩陣)Q 的切片 slice
| 函數(shù) |
|---|
| Q.slice(slice_number) |
| Q.slices(first_slice, last_slice) |
| Q.subcube(first_row,first_col,first_slice,last_row,last_col,last_slice) |
| Q(span(first_row,last_row),span(first_col,last_col),span(first_slice,last_slice)) |
| Q(first_row,first_col,first_slice,size(n_rows,n_cols,n_slices)) |
| Q(first_row,first_col,first_slice,size(R)) (R is a cube) |
| Q.elem(vector_of_indices) (間斷的切片) |
| Q(vector_of_indices) (間斷的切片) |
域F的子集訪問
| 二維域 2-D Field |
|---|
| F.row( row_number ) |
| F.col( col_number ) |
| F.rows( first_row, last_row ) |
| F.cols( first_col, last_col ) |
| F.subfield(first_row, first_col, last_row, last_col) |
| F(span(first_row, last_row), span(first_col, last_col)) |
| 三維域 3-D Field |
|---|
| F.slice( slice_number ) |
| F.slices( first_slice, last_slice ) |
| F.subfield(first_row, first_col, first_slice, last_row, last_col, last_slice) |
| F(span(first_row,last_row),span(first_col,last_col),span(first_slice,last_slice)) |
4.4. 屬性 Attribute
| 屬性 | 描述 |
|---|---|
| .n_rows | 行數(shù); 適用于Mat, Col, Row, Cube, field and SpMat |
| .n_cols | 列數(shù);適用于Mat, Col, Row, Cube, field and SpMat |
| .n_elem | 所有元素個(gè)數(shù);適用于Mat, Col, Row, Cube, field and SpMat |
| .n_slices | 立方體Cube第三維的維數(shù) |
| .n_nonzero | 非零元素個(gè)數(shù);適用于SpMat |
注釋:
- 返回值是無符號(hào)整數(shù)(
uword) - 返回值是read-only的;如果要改變大?。ňS數(shù)),用成員函數(shù)
.set_size(),.copy_size(),.zeros(),.ones(), 或者.reset()。
.set_size()
.set_size( n_elem ) .set_size( n_rows, n_cols ) .set_size( n_rows, n_cols, n_slices ) .set_size( size(X) )
.copy_size(A)
把維數(shù)設(shè)置成和A一樣。
.zeros()
.zeros( n_elem ) .zeros( n_rows, n_cols ) .zeros( n_rows, n_cols, n_slices ) .zeros( size(X) )
.ones()
參見.zeros()。
.reset()
把維數(shù)設(shè)置成0,意味著無元素。
4.5. 其他成員函數(shù) Other member function
| 函數(shù) | 描述 |
|---|---|
| .eye(n,n) / .eye(size(X)) | 創(chuàng)建nxn 單位矩陣;適用于Mat和SpMat |
| .randu(n_elem) | 把向量的值設(shè)置成從均勻分布中抽取的隨機(jī)值 |
| .randu(n_rows,n_cols) | 把矩陣的值設(shè)置成從均勻分布中抽取的隨機(jī)值 |
| .randu(n_rows,n_cols,n_slices) | 把立方體的值設(shè)置成從均勻分布中抽取的隨機(jī)值 |
| .randn() | 與.randu()相同,只不過從正態(tài)分布中抽取隨機(jī)數(shù) |
| .fill(value) | 將Mat, Col, Row, Cube元素設(shè)置為value |
| .replace(old_value, new_value) | 可用于替換缺失值:A.replace(datum::nan, 0); 適用于Mat, Col, Row, Cube |
| .transform(lambda_function) (C++11 Only) | 利用lambda函數(shù)改變每一個(gè)元素的值;適用于Mat, Col, Row和Cube;對(duì)于矩陣,按照column-by-column來進(jìn)行變換;對(duì)于立方體,按照slice-by-slice進(jìn)行變換,每一個(gè)slice是一個(gè)矩陣。e.g.見此表后注釋。 |
| .reshape(n_rows, n_cols) | 適用于矩陣;按照給定的維數(shù)建立新的矩陣,轉(zhuǎn)換時(shí),先將舊矩陣按照列轉(zhuǎn)換為長(zhǎng)列向量,然后按照給定維數(shù),一列一列地建立新的矩陣。原始結(jié)構(gòu)會(huì)被改變。 |
| .reshape(n_rows,n_cols,n_slices) | 適用于立方體;與上類似 |
| .reshape(size(X)) | 適用于矩陣和立方體;與上類似 |
| .resize(n_elem) | 適用于向量;保留原向量結(jié)構(gòu),增加部分填為0 |
| .resize(n_rows,n_cols) | 適用于矩陣;保留原矩陣結(jié)構(gòu),增加部分填為0 |
| .resize(n_rows,n_cols,n_slices) | 適用于立方體;保留原立方體結(jié)構(gòu),增加部分填為0 |
| .resize(size(X)) | 適用于向量、矩陣和立方體 |
| Y.set_imag(X) | 將復(fù)數(shù)矩陣Y的虛部設(shè)置成實(shí)數(shù)矩陣X |
| Y.set_real(X) | 將復(fù)數(shù)矩陣Y的實(shí)部設(shè)置成實(shí)數(shù)矩陣X |
| .insert_rows() | 插入行 |
| .insert_cols() | 插入列 |
| .insert_slices() | 插入切片 |
| .shed_row()/.shed_rows() | 移除行 |
| .shed_col()/.shed_cols() | 移除列 |
| .shed_slice()/.shed_slices() | 移除切片 |
| .swap_rows( row1, row2 ) | 交換行 |
| .swap_cols( col1, col2 ) | 交換列 |
| .memptr() | 獲取對(duì)象的指針;適用于Mat,Col,Row和Cube |
| .colptr(col_number) | 獲取某一列的指針 |
| iterators | STL-style iterators and associated member functions |
| .t() | 轉(zhuǎn)置或者共軛轉(zhuǎn)置,適用于mat和cx_mat |
| .st() | 普通轉(zhuǎn)置(不取共軛),僅僅適用于cx_mat |
| .i() | 逆矩陣 |
| .min()/.max() | 返回矩陣或立方體的極值;如果是復(fù)數(shù),則返回模的極值 |
| .index_min()/.index_max() | 返回矩陣或立方體極值的坐標(biāo);返回值為一個(gè)無符號(hào)整數(shù) |
| .in_range() | 檢查給定的坐標(biāo)或者范圍是合法的 |
| .is_empty() | 檢查是否為空 |
| .is_square() | 檢查是否是方陣 |
| .is_vec() | 檢查一個(gè)矩陣是否是向量 |
| .is_sorted() | 檢查對(duì)象是否是被排列過的 |
| .is_finite() | 檢查對(duì)象是否有限 |
| .has_inf() | 檢查是否含有inf值 |
| .has_nan() | 檢查是否含有NaN |
| .print() | 打印此對(duì)象 |
| .save()/.load() | 向或從文件或流寫入或讀取對(duì)象 |
注釋:
.transform(lambda_function)
// C++11 only example
mat A = ones<mat>(4,5);
// add 123 to every element
A.transform( [](double val) { return (val + 123.0); } );
.reshape()和.resize()的區(qū)別在于前者不會(huì)保存原對(duì)象的布局,而后者會(huì)保留原對(duì)象的布局,且后者更快。例如,如果新對(duì)象的維數(shù)大于原對(duì)象的維數(shù),則新對(duì)象中原維數(shù)外的元素會(huì)被設(shè)置成0。
e.g.
mat A = randu<mat>(2,3);
A.reshape(4,4);
[,1] [,2] [,3] [,4]
[1,] 0.02567623 0.8880936 0 0
[2,] 0.12546129 0.6520889 0 0
[3,] 0.52724939 0.0000000 0 0
[4,] 0.30407942 0.0000000 0 0
mat A = randu<mat>(2,3);
A.resize(4,4);
[,1] [,2] [,3] [,4]
[1,] 0.5451790 0.2632051 0.6375933 0
[2,] 0.3753245 0.8050394 0.1627499 0
[3,] 0.0000000 0.0000000 0.0000000 0
[4,] 0.0000000 0.0000000 0.0000000 0
.memptr()
可被用于和一些庫交互,比如FFTW。
mat A = randu<mat>(5,5);
const mat B = randu<mat>(5,5);
double* A_mem = A.memptr();
const double* B_mem = B.memptr();
5. 常用函數(shù)
5.1. 向量、矩陣和立方體的一般函數(shù)
| 函數(shù) | 描述 |
|---|---|
| abs(X) | 求對(duì)象元素的絕對(duì)值或長(zhǎng)度(復(fù)數(shù)) |
| accu(X) | 求對(duì)象所有元素的和 |
| all(X,dim) | 檢查向量或者矩陣是否全部元素為非零 |
| any(X,dim) | 檢查向量或者矩陣是否至少有一個(gè)元素為非零 |
| approx_equal(A,B,method,abs_tol,rel_tol) | 檢查A和B中的元素是否近似,近似返回True(Bool值); method可以是absdiff、reldiff和both |
| cond(A) | 返回矩陣A的conditional number |
| conj(X) | 求矩陣或立方體的元素的共軛 |
| conv_to<type>::from(X) | 不同Armadillo矩陣類型之間的轉(zhuǎn)換(e.g. mat和imat);不同立方體之間的轉(zhuǎn)換(e.g.cube和icube);std::vector與Armadillo向量或矩陣之間的轉(zhuǎn)換;將mat轉(zhuǎn)換為colvec, rowvec or std::vector |
| cross(A,B) | 向量叉乘cross product |
| cumsum(X,dim) | 累積加法;如果X是向量,則返回所有元素的和;如果X是矩陣,若dim=0,則返回所有列的和,若dim=1,則返回所有行的和 |
| cumprod(X,dim) | 累積乘法;如果X是向量,則返回所有元素的乘積;如果X是矩陣,若dim=0,則返回所有列的乘積,若dim=1,則返回所有行的乘積 |
| det(A) | 計(jì)算方陣的行列式;對(duì)于大矩陣,log_det()更加精確 |
| log_det(val,sign,A) | Log determinant of square matrix A; the determinant is equal to exp(val)*sign |
| diagmat(X,k) | 生成新矩陣,用向量X或者矩陣X的對(duì)角線元素作為新矩陣的第k個(gè)對(duì)角線,其他元素設(shè)置為零 |
| diagvec(A,k) | 取矩陣A的第k個(gè)對(duì)角線 |
| dot(A,B)(dot/cdot/norm_dot) | 向量的點(diǎn)乘dot product |
| find(condition) | 返回向量或矩陣滿足某條件的元素的坐標(biāo)向量;e.g. find(A>B)or find(A>0) |
| find_finite(X) | 返回非Inf和NaN的元素的坐標(biāo)向量 |
| find_nonfinite(X) | 返回是Inf和NaN的元素的坐標(biāo)向量 |
| find_unique(X,ascending_indices) | 返回X中獨(dú)一無二的元素;ascending_indices是可選參數(shù),取true(默認(rèn))意味著按照遞增排列,取false意味著隨機(jī)排列 |
| imag() / real() | 取復(fù)數(shù)矩陣虛數(shù)或者實(shí)數(shù)部分 |
| inplace_trans(X,method) / inplace_strans(X,method) | in-place transpose, 相當(dāng)于 X=X⊺ ,X的值改變了 |
| is_finite() | 檢查是否所有元素都是有限的 |
| join_rows(A,B) / join_horiz(A,B) | 按照水平方向連接兩個(gè)矩陣 |
| join_cols(A,B) / join_vert(A,B) | 按照垂直方向連接兩個(gè)矩陣 |
| join_slices(cube_C,cube_D) | 按照第三維連接兩個(gè)立方體,兩個(gè)立方體的第一維和第二維的維數(shù)必須相等 |
| join_slices(mat_M,mat_N) | 連接兩個(gè)矩陣構(gòu)成一個(gè)立方體,兩個(gè)矩陣必須維數(shù)相同 |
| join_slices(mat_M,cube_C)/join_slices(cube_C,mat_M) | 將一個(gè)矩陣加入一個(gè)立方體中 |
| kron(A,B) | Kronecker tensor product |
| min(X,dim) / max(X,dim) | 尋找X在某一維上的極值;X可以是向量(則無dim參數(shù))、矩陣或者立方體;dim是可選參數(shù),0表示返回每一列的極值,1表示返回每一行的極值,2表示返回每一個(gè)切片的極值 |
| min(A,B) / max(A,B) | 返回值為一個(gè)矩陣或者立方體,其每一個(gè)元素代表A和B當(dāng)中同樣坐標(biāo)的兩個(gè)元素的最小值和最大值 |
| nonzeros(X) | 返回一個(gè)列向量,存儲(chǔ)著非零的元素的坐標(biāo) |
| norm(X,p) | 計(jì)算向量或矩陣的p-norm;向量:p可以是大于等于1的整數(shù),”-inf”,”inf”,”fro”;矩陣:p可以是1,2,”inf”,”fro”,并且此為matrix norm (not entrywise norm);”-inf”是minimum norm, “inf”是maximum norm, “fro”是Frobenius norm |
| normalise(V,p)/normalise(X,p,dim) | 標(biāo)準(zhǔn)化向量V或者矩陣X使其有unit p-norm |
| rank(X,tolerance) | 計(jì)算矩陣的秩;tolerance為可選參數(shù) |
| rcond(A) | 矩陣A的conditional number的倒數(shù)的估計(jì)值;如果接近1代表A是well-conditioned;如果接近0代表A是badly-conditioned |
| repmat(A,num_copies_per_row,num_copies_per_col) | 把矩陣A按照分塊矩陣的形式進(jìn)行復(fù)制并生成新的矩陣 |
| shuffle(V)/shuffle(X,dim) | 重新排列向量元素或者矩陣的列或行 |
| sort(V,sort_direction)/sort(X,sort_direction,dim) | 對(duì)向量進(jìn)行排序,或者對(duì)矩陣的列(dim=0)或者行(dim=1)中的元素進(jìn)行排序(默認(rèn)是列);sort_direction可以是ascend(默認(rèn))或者descend |
| sort_index(X,sort_direction) | 返回X按照某順序排序后的元素的坐標(biāo) |
| B=sqrtmat(A)/sqrtmat(B,A) | 矩陣的Complex square root;B是cx_mat |
| sqrtmat_sympd(A)/sqrtmat_sympd(B,A) | 對(duì)稱矩陣的Complex square root |
| sum(X,dim) | 向量:返回所有元素的和;矩陣:返回每一列(dim=0)或每一行(dim=1)的和;立方體:返回某一維上(第三維是dim=2)的和 |
| trace(X) | 計(jì)算矩陣的跡即計(jì)算主對(duì)角線上元素的和 |
| trans(A)/strans(A) | 矩陣轉(zhuǎn)置;如果是復(fù)數(shù)矩陣,前者進(jìn)行的是共軛轉(zhuǎn)置,而后者是直接轉(zhuǎn)置 |
| unique(A) | 返回A的獨(dú)一無二的元素,并且按照升序排列;如果A是矩陣,則返回一個(gè)列向量 |
| vectorise(X,dim) | 將矩陣向量化;如果dim=0,按照column-wise;如果dim=1,按照row-wise |
5.2. 其他一些數(shù)學(xué)函數(shù)
miscellaneous element-wise functions:

三角函數(shù) Trigonometric element-wise functions (cos, sin, tan, …)
cos, acos, cosh, acosh
sin, asin, sinh, asinh
tan, atan, tanh, atanh
atan2, hypot
5.3. 矩陣的分解、因子化、逆矩陣和線性方程的解
Dense Matrix
| 函數(shù) | 描述 |
|---|---|
| chol | Cholesky decomposition |
| eig_sym | eigen decomposition of dense symmetric/hermitian matrix |
| eig_gen | eigen decomposition of dense general square matrix |
| eig_pair | eigen decomposition for pair of general dense square matrices |
| inv | inverse of general square matrix |
| inv_sympd | inverse of symmetric positive definite matrix |
| lu | lower-upper decomposition |
| null | orthonormal basis of null space |
| orth | orthonormal basis of range space |
| pinv | pseudo-inverse |
| qr | QR decomposition |
| qr_econ | economical QR decomposition |
| qz | generalised Schur decomposition |
| schur | Schur decomposition |
| solve | solve systems of linear equations |
| svd | singular value decomposition |
| svd_econ | economical singular value decomposition |
| syl | Sylvester equation solver |
Sparse Matrix
| 函數(shù) | 描述 |
|---|---|
| eigs_sym | limited number of eigenvalues & eigenvectors of sparse symmetric real matrix |
| eigs_gen | limited number of eigenvalues & eigenvectors of sparse general square matrix |
| spsolve | solve sparse systems of linear equations |
| svds | limited number of singular values & singular vectors of sparse matrix |
5.4. 信號(hào)和圖像處理
| 函數(shù) | 描述 |
|---|---|
| conv | 1D convolution |
| conv2 | 2D convolution |
| fft / ifft | 1D fast Fourier transform and its inverse |
| fft2 / ifft2 | 2D fast Fourier transform and its inverse |
| interp1 | 1D interpolation |
| polyfit | find polynomial coefficients for data fitting |
| polyval | evaluate polynomial |
5.5. 統(tǒng)計(jì)和聚類
| 函數(shù) | 描述 |
|---|---|
| mean(X,dim) | 均值;適用于Vec,Mat,Cube |
| median(X,dim) | 中間值;適用于Vec,Mat |
| stddev(X,norm_type,dim) | 標(biāo)準(zhǔn)差;norm_type可選參數(shù)0或1,0代表除以n-1(無偏估計(jì)),1表示除以n;適用于Vec,Mat |
| var(X,norm_type,dim) | 方差;適用于Vec,Mat |
| range(X,dim) | 極差;適用于Vec,Mat |
| cov(X,Y,norm_type)/cov(X,norm_type) | 協(xié)方差;當(dāng)兩個(gè)矩陣X、Y時(shí),矩陣的行表示樣本,列表示變量,則cov(X,Y)的第(i,j)-th個(gè)元素等于X的第i個(gè)變量和Y的第j個(gè)變量的協(xié)方差; norm_type=0表示除以n-1,norm_type=1表示除以n |
| cor(X,Y,norm_type)/cor(X,norm_type) | 相關(guān)系數(shù);與協(xié)方差類似 |
| hist/histc | 直方圖 |
| princomp | 主成分分析 |
| kmeans(means,data,k,seed_mode,n_iter,print_mode) | K-means聚類,把數(shù)據(jù)分成k個(gè)不相交的集合 |
| gmm_diag | 聚類;Gaussian Mixture Model (GMM) |
以上就是R語言科學(xué)計(jì)算RcppArmadillo簡(jiǎn)明手冊(cè)的詳細(xì)內(nèi)容,更多關(guān)于RcppArmadillo簡(jiǎn)明手冊(cè)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言學(xué)習(xí)VennDiagram包繪制韋恩圖示例
這篇文章主要為大家介紹了R語言學(xué)習(xí)VennDiagram包繪制韋恩圖示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
R語言繪圖數(shù)據(jù)可視化Ridgeline plot山脊圖畫法
這篇文章主要為大家介紹了R語言繪圖數(shù)據(jù)可視化Ridgeline plot山脊圖畫法的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
利用R語言合并數(shù)據(jù)框的行與列實(shí)例代碼
實(shí)際操作中我們經(jīng)常需要引入其他表中的列,即將其他表中列加入到表中,需要把兩個(gè)或者更多的表合并成一個(gè),下面這篇文章主要給大家介紹了關(guān)于利用R語言合并數(shù)據(jù)框的行與列的相關(guān)資料,需要的朋友可以參考下2022-07-07
R包制作后出現(xiàn)not available for錯(cuò)誤問題解決解決
這篇文章主要為大家介紹了R包制作后出現(xiàn)not available for...錯(cuò)誤的問題解決方式,有需要的朋友,可以借鑒參考下,希望能夠有所幫助2021-11-11
R語言-進(jìn)行數(shù)據(jù)的重新編碼(recode)操作
這篇文章主要介紹了R語言-進(jìn)行數(shù)據(jù)的重新編碼(recode)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
R語言繪制corrplot相關(guān)熱圖分析美化示例及詳細(xì)圖解
這篇文章主要為大家介紹了R語言corrplot相關(guān)熱圖分析美化示例及詳細(xì)圖解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

