example1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <iostream>
  2. #include <armadillo>
  3. using namespace std;
  4. using namespace arma;
  5. // Armadillo documentation is available at:
  6. // http://arma.sourceforge.net/docs.html
  7. // NOTE: the C++11 "auto" keyword is not recommended for use with Armadillo objects and functions
  8. int
  9. main2(int argc, char** argv)
  10. {
  11. cout << "Armadillo version: " << arma_version::as_string() << endl;
  12. // construct a matrix according to given size and form of element initialisation
  13. mat A(2,3,fill::zeros);
  14. // .n_rows and .n_cols are read only
  15. cout << "A.n_rows: " << A.n_rows << endl;
  16. cout << "A.n_cols: " << A.n_cols << endl;
  17. A(1,2) = 456.0; // access an element (indexing starts at 0)
  18. A.print("A:");
  19. A = 5.0; // scalars are treated as a 1x1 matrix
  20. A.print("A:");
  21. A.set_size(4,5); // change the size (data is not preserved)
  22. A.fill(5.0); // set all elements to a specific value
  23. A.print("A:");
  24. // endr indicates "end of row"
  25. A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr
  26. << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr
  27. << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr
  28. << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr
  29. << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr;
  30. A.print("A:");
  31. // determinant
  32. cout << "det(A): " << det(A) << endl;
  33. // inverse
  34. cout << "inv(A): " << endl << inv(A) << endl;
  35. // save matrix as a text file
  36. A.save("A.txt", raw_ascii);
  37. // load from file
  38. mat B;
  39. B.load("A.txt");
  40. // submatrices
  41. cout << "B( span(0,2), span(3,4) ):" << endl << B( span(0,2), span(3,4) ) << endl;
  42. cout << "B( 0,3, size(3,2) ):" << endl << B( 0,3, size(3,2) ) << endl;
  43. cout << "B.row(0): " << endl << B.row(0) << endl;
  44. cout << "B.col(1): " << endl << B.col(1) << endl;
  45. // transpose
  46. cout << "B.t(): " << endl << B.t() << endl;
  47. // maximum from each column (traverse along rows)
  48. cout << "max(B): " << endl << max(B) << endl;
  49. // maximum from each row (traverse along columns)
  50. cout << "max(B,1): " << endl << max(B,1) << endl;
  51. // maximum value in B
  52. cout << "max(max(B)) = " << max(max(B)) << endl;
  53. // sum of each column (traverse along rows)
  54. cout << "sum(B): " << endl << sum(B) << endl;
  55. // sum of each row (traverse along columns)
  56. cout << "sum(B,1) =" << endl << sum(B,1) << endl;
  57. // sum of all elements
  58. cout << "accu(B): " << accu(B) << endl;
  59. // trace = sum along diagonal
  60. cout << "trace(B): " << trace(B) << endl;
  61. // generate the identity matrix
  62. mat C = eye<mat>(4,4);
  63. // random matrix with values uniformly distributed in the [0,1] interval
  64. mat D = randu<mat>(4,4);
  65. D.print("D:");
  66. // row vectors are treated like a matrix with one row
  67. rowvec r;
  68. r << 0.59119 << 0.77321 << 0.60275 << 0.35887 << 0.51683;
  69. r.print("r:");
  70. // column vectors are treated like a matrix with one column
  71. vec q;
  72. q << 0.14333 << 0.59478 << 0.14481 << 0.58558 << 0.60809;
  73. q.print("q:");
  74. // convert matrix to vector; data in matrices is stored column-by-column
  75. vec v = vectorise(A);
  76. v.print("v:");
  77. // dot or inner product
  78. cout << "as_scalar(r*q): " << as_scalar(r*q) << endl;
  79. // outer product
  80. cout << "q*r: " << endl << q*r << endl;
  81. // multiply-and-accumulate operation (no temporary matrices are created)
  82. cout << "accu(A % B) = " << accu(A % B) << endl;
  83. // example of a compound operation
  84. B += 2.0 * A.t();
  85. B.print("B:");
  86. // imat specifies an integer matrix
  87. imat AA;
  88. imat BB;
  89. AA << 1 << 2 << 3 << endr << 4 << 5 << 6 << endr << 7 << 8 << 9;
  90. BB << 3 << 2 << 1 << endr << 6 << 5 << 4 << endr << 9 << 8 << 7;
  91. // comparison of matrices (element-wise); output of a relational operator is a umat
  92. umat ZZ = (AA >= BB);
  93. ZZ.print("ZZ:");
  94. // cubes ("3D matrices")
  95. cube Q( B.n_rows, B.n_cols, 2 );
  96. Q.slice(0) = B;
  97. Q.slice(1) = 2.0 * B;
  98. Q.print("Q:");
  99. // 2D field of matrices; 3D fields are also supported
  100. field<mat> F(4,3);
  101. for(uword col=0; col < F.n_cols; ++col)
  102. for(uword row=0; row < F.n_rows; ++row)
  103. {
  104. F(row,col) = randu<mat>(2,3); // each element in field<mat> is a matrix
  105. }
  106. F.print("F:");
  107. return 0;
  108. }