annotate armadillo-2.4.4/examples/example1.cpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
rev   line source
max@0 1 #include <iostream>
max@0 2
max@0 3 #include "armadillo"
max@0 4
max@0 5 using namespace arma;
max@0 6 using namespace std;
max@0 7
max@0 8
max@0 9 int main(int argc, char** argv)
max@0 10 {
max@0 11 cout << "Armadillo version: " << arma_version::as_string() << endl;
max@0 12
max@0 13 // directly specify the matrix size (elements are uninitialised)
max@0 14 mat A(2,3);
max@0 15
max@0 16 // .n_rows = number of rows (read only)
max@0 17 // .n_cols = number of columns (read only)
max@0 18 cout << "A.n_rows = " << A.n_rows << endl;
max@0 19 cout << "A.n_cols = " << A.n_cols << endl;
max@0 20
max@0 21 // directly access an element (indexing starts at 0)
max@0 22 A(1,2) = 456.0;
max@0 23
max@0 24 A.print("A:");
max@0 25
max@0 26 // scalars are treated as a 1x1 matrix,
max@0 27 // hence the code below will set A to have a size of 1x1
max@0 28 A = 5.0;
max@0 29 A.print("A:");
max@0 30
max@0 31 // if you want a matrix with all elements set to a particular value
max@0 32 // the .fill() member function can be used
max@0 33 A.set_size(3,3);
max@0 34 A.fill(5.0);
max@0 35 A.print("A:");
max@0 36
max@0 37
max@0 38 mat B;
max@0 39
max@0 40 // endr indicates "end of row"
max@0 41 B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr
max@0 42 << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr
max@0 43 << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr
max@0 44 << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr;
max@0 45
max@0 46 // print to the cout stream
max@0 47 // with an optional string before the contents of the matrix
max@0 48 B.print("B:");
max@0 49
max@0 50 // the << operator can also be used to print the matrix
max@0 51 // to an arbitrary stream (cout in this case)
max@0 52 cout << "B:" << endl << B << endl;
max@0 53
max@0 54 // save to disk
max@0 55 B.save("B.txt", raw_ascii);
max@0 56
max@0 57 // load from disk
max@0 58 mat C;
max@0 59 C.load("B.txt");
max@0 60
max@0 61 C += 2.0 * B;
max@0 62 C.print("C:");
max@0 63
max@0 64
max@0 65 // submatrix types:
max@0 66 //
max@0 67 // .submat(first_row, first_column, last_row, last_column)
max@0 68 // .row(row_number)
max@0 69 // .col(column_number)
max@0 70 // .cols(first_column, last_column)
max@0 71 // .rows(first_row, last_row)
max@0 72
max@0 73 cout << "C.submat(0,0,3,1) =" << endl;
max@0 74 cout << C.submat(0,0,3,1) << endl;
max@0 75
max@0 76 // generate the identity matrix
max@0 77 mat D = eye<mat>(4,4);
max@0 78
max@0 79 D.submat(0,0,3,1) = C.cols(1,2);
max@0 80 D.print("D:");
max@0 81
max@0 82 // transpose
max@0 83 cout << "trans(B) =" << endl;
max@0 84 cout << trans(B) << endl;
max@0 85
max@0 86 // maximum from each column (traverse along rows)
max@0 87 cout << "max(B) =" << endl;
max@0 88 cout << max(B) << endl;
max@0 89
max@0 90 // maximum from each row (traverse along columns)
max@0 91 cout << "max(B,1) =" << endl;
max@0 92 cout << max(B,1) << endl;
max@0 93
max@0 94 // maximum value in B
max@0 95 cout << "max(max(B)) = " << max(max(B)) << endl;
max@0 96
max@0 97 // sum of each column (traverse along rows)
max@0 98 cout << "sum(B) =" << endl;
max@0 99 cout << sum(B) << endl;
max@0 100
max@0 101 // sum of each row (traverse along columns)
max@0 102 cout << "sum(B,1) =" << endl;
max@0 103 cout << sum(B,1) << endl;
max@0 104
max@0 105 // sum of all elements
max@0 106 cout << "sum(sum(B)) = " << sum(sum(B)) << endl;
max@0 107 cout << "accu(B) = " << accu(B) << endl;
max@0 108
max@0 109 // trace = sum along diagonal
max@0 110 cout << "trace(B) = " << trace(B) << endl;
max@0 111
max@0 112 // random matrix -- values are uniformly distributed in the [0,1] interval
max@0 113 mat E = randu<mat>(4,4);
max@0 114 E.print("E:");
max@0 115
max@0 116 cout << endl;
max@0 117
max@0 118 // row vectors are treated like a matrix with one row
max@0 119 rowvec r;
max@0 120 r << 0.59499 << 0.88807 << 0.88532 << 0.19968;
max@0 121 r.print("r:");
max@0 122
max@0 123 // column vectors are treated like a matrix with one column
max@0 124 colvec q;
max@0 125 q << 0.81114 << 0.06256 << 0.95989 << 0.73628;
max@0 126 q.print("q:");
max@0 127
max@0 128 // dot or inner product
max@0 129 cout << "as_scalar(r*q) = " << as_scalar(r*q) << endl;
max@0 130
max@0 131
max@0 132 // outer product
max@0 133 cout << "q*r =" << endl;
max@0 134 cout << q*r << endl;
max@0 135
max@0 136 // multiply-and-accumulate operation
max@0 137 // (no temporary matrices are created)
max@0 138 cout << "accu(B % C) = " << accu(B % C) << endl;
max@0 139
max@0 140 // sum of three matrices (no temporary matrices are created)
max@0 141 mat F = B + C + D;
max@0 142 F.print("F:");
max@0 143
max@0 144 // imat specifies an integer matrix
max@0 145 imat AA;
max@0 146 imat BB;
max@0 147
max@0 148 AA << 1 << 2 << 3 << endr << 4 << 5 << 6 << endr << 7 << 8 << 9;
max@0 149 BB << 3 << 2 << 1 << endr << 6 << 5 << 4 << endr << 9 << 8 << 7;
max@0 150
max@0 151 // comparison of matrices (element-wise)
max@0 152 // output of a relational operator is a umat
max@0 153 umat ZZ = (AA >= BB);
max@0 154 ZZ.print("ZZ =");
max@0 155
max@0 156
max@0 157 // 2D field of arbitrary length row vectors
max@0 158 // (fields can also store abitrary objects, e.g. instances of std::string)
max@0 159 field<rowvec> xyz(3,2);
max@0 160
max@0 161 xyz(0,0) = randu(1,2);
max@0 162 xyz(1,0) = randu(1,3);
max@0 163 xyz(2,0) = randu(1,4);
max@0 164 xyz(0,1) = randu(1,5);
max@0 165 xyz(1,1) = randu(1,6);
max@0 166 xyz(2,1) = randu(1,7);
max@0 167
max@0 168 cout << "xyz:" << endl;
max@0 169 cout << xyz << endl;
max@0 170
max@0 171
max@0 172 // cubes ("3D matrices")
max@0 173 cube Q( B.n_rows, B.n_cols, 2 );
max@0 174
max@0 175 Q.slice(0) = B;
max@0 176 Q.slice(1) = 2.0 * B;
max@0 177
max@0 178 Q.print("Q:");
max@0 179
max@0 180
max@0 181 return 0;
max@0 182 }
max@0 183