Armadillo使用介绍(八):第二个Armadillo程序

源码

#include 
#include 

using namespace std;
using namespace arma;

// Armadillo documentation is available at:
// http://arma.sourceforge.net/docs.html

// NOTE: the C++11 "auto" keyword is not recommended for use with Armadillo objects and functions

int main(int argc, char** argv)
{
	cout << "Armadillo version: " << arma_version::as_string() << endl;

	// construct a matrix according to given size and form of element initialisation
	mat A(2, 3, fill::zeros);

	// .n_rows and .n_cols are read only
	cout << "A.n_rows: " << A.n_rows << endl;
	cout << "A.n_cols: " << A.n_cols << endl;

	A(1, 2) = 456.0;  // 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 specific value
	A.print("A:");

	A = { { 0.165300, 0.454037, 0.995795, 0.124098, 0.047084 },
	{ 0.688782, 0.036549, 0.552848, 0.937664, 0.866401 },
	{ 0.348740, 0.479388, 0.506228, 0.145673, 0.491547 },
	{ 0.148678, 0.682258, 0.571154, 0.874724, 0.444632 },
	{ 0.245726, 0.595218, 0.409327, 0.367827, 0.385736 } };

	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( 0,3, size(3,2) ):" << endl << B(0, 3, size(3, 2)) << 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<mat>(4, 4);

	// random matrix with values uniformly distributed in the [0,1] interval
	mat D = randu<mat>(4, 4);
	D.print("D:");

	// row vectors are treated like a matrix with one row
	rowvec 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 = { 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 = { { 1, 2, 3 },
	{ 4, 5, 6 },
	{ 7, 8, 9 } };

	imat BB = { { 3, 2, 1 },
	{ 6, 5, 4 },
	{ 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<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 is a matrix
		}

	F.print("F:");

	system("pause");
	return 0;
}

运行结果

Armadillo version: 12.8.0 (Cortisol Injector)
A.n_rows: 2
A.n_cols: 3
A:
            0            0            0
            0            0   4.5600e+02
A:
   5.0000
A:
   5.0000   5.0000   5.0000   5.0000   5.0000
   5.0000   5.0000   5.0000   5.0000   5.0000
   5.0000   5.0000   5.0000   5.0000   5.0000
   5.0000   5.0000   5.0000   5.0000   5.0000
A:
   0.1653   0.4540   0.9958   0.1241   0.0471
   0.6888   0.0365   0.5528   0.9377   0.8664
   0.3487   0.4794   0.5062   0.1457   0.4915
   0.1487   0.6823   0.5712   0.8747   0.4446
   0.2457   0.5952   0.4093   0.3678   0.3857
det(A): -0.0246018
inv(A):
    1.2916    2.0000   -7.4695   -6.0752   11.8714
   -0.1011   -0.4619   -1.5556   -0.9830    4.1651
    0.8976   -0.1524    1.9191    1.2554   -3.6600
    0.1869    0.6267   -2.6662    0.1198    1.8289
   -1.7976   -0.9973    7.6647    3.9404   -9.2573

B( span(0,2), span(3,4) ):
   0.1241   0.0471
   0.9377   0.8664
   0.1457   0.4915

B( 0,3, size(3,2) ):
   0.1241   0.0471
   0.9377   0.8664
   0.1457   0.4915

B.row(0):
   0.1653   0.4540   0.9958   0.1241   0.0471

B.col(1):
   0.4540
   0.0365
   0.4794
   0.6823
   0.5952

B.t():
   0.1653   0.6888   0.3487   0.1487   0.2457
   0.4540   0.0365   0.4794   0.6823   0.5952
   0.9958   0.5528   0.5062   0.5712   0.4093
   0.1241   0.9377   0.1457   0.8747   0.3678
   0.0471   0.8664   0.4915   0.4446   0.3857

max(B):
   0.6888   0.6823   0.9958   0.9377   0.8664

max(B,1):
   0.9958
   0.9377
   0.5062
   0.8747
   0.5952

max(max(B)) = 0.995795
sum(B):
   1.5972   2.2474   3.0354   2.4500   2.2354

sum(B,1) =
   1.7863
   3.0822
   1.9716
   2.7214
   2.0038

accu(B): 11.5654
trace(B): 1.96854
D:
   0.7868   0.0193   0.5206   0.1400
   0.2505   0.4049   0.3447   0.5439
   0.7107   0.2513   0.2742   0.5219
   0.9467   0.0227   0.5610   0.8571
r:
   0.5912   0.7732   0.6028   0.3589   0.5168
q:
   0.1433
   0.5948
   0.1448
   0.5856
   0.6081
v:
   0.1653
   0.6888
   0.3487
   0.1487
   0.2457
   0.4540
   0.0365
   0.4794
   0.6823
   0.5952
   0.9958
   0.5528
   0.5062
   0.5712
   0.4093
   0.1241
   0.9377
   0.1457
   0.8747
   0.3678
   0.0471
   0.8664
   0.4915
   0.4446
   0.3857
as_scalar(r*q): 1.15634
q*r:
   0.0847   0.1108   0.0864   0.0514   0.0741
   0.3516   0.4599   0.3585   0.2134   0.3074
   0.0856   0.1120   0.0873   0.0520   0.0748
   0.3462   0.4528   0.3530   0.2101   0.3026
   0.3595   0.4702   0.3665   0.2182   0.3143

accu(A % B) = 7.16744
B:
   0.4959   1.8316   1.6933   0.4215   0.5385
   1.5969   0.1096   1.5116   2.3022   2.0568
   2.3403   1.5851   1.5187   1.2880   1.3102
   0.3969   2.5576   0.8625   2.6242   1.1803
   0.3399   2.3280   1.3924   1.2571   1.1572
ZZ:
        0        1        1
        0        1        1
        0        1        1
Q:
[cube slice: 0]
   0.4959   1.8316   1.6933   0.4215   0.5385
   1.5969   0.1096   1.5116   2.3022   2.0568
   2.3403   1.5851   1.5187   1.2880   1.3102
   0.3969   2.5576   0.8625   2.6242   1.1803
   0.3399   2.3280   1.3924   1.2571   1.1572

[cube slice: 1]
   0.9918   3.6632   3.3865   0.8429   1.0771
   3.1937   0.2193   3.0232   4.6044   4.1137
   4.6807   3.1702   3.0374   2.5760   2.6204
   0.7937   5.1152   1.7250   5.2483   2.3606
   0.6798   4.6560   2.7848   2.5142   2.3144
F:
[field column: 0]
   0.4998   0.7443   0.2393
   0.4194   0.2492   0.3201

   0.9105   0.2455   0.7159
   0.1648   0.1983   0.9678

   0.7694   0.4599   0.7770
   0.0807   0.2573   0.5839

   0.9503   0.3223   0.2564
   0.4381   0.5324   0.0455


[field column: 1]
   0.5050   0.0912   0.0309
   0.6962   0.9071   0.1520

   0.9815   0.2988   0.4810
   0.6204   0.3613   0.2978

   0.2852   0.6289   0.7139
   0.9242   0.7550   0.7228

   0.0698   0.0889   0.4238
   0.4868   0.7596   0.5970


[field column: 2]
   0.0864   0.6238   0.2254
   0.2730   0.2221   0.4341

   0.9873   0.8532   0.8364
   0.2110   0.2841   0.3667

   0.9351   0.4909   0.3621
   0.8599   0.0221   0.7364

   0.5194   0.0290   0.1122
   0.4230   0.9092   0.9802


Press any key to continue . . .

你可能感兴趣的:(Armadillo,armadillo,c++,vs)