max@0: #include max@0: max@0: #include "armadillo" max@0: max@0: using namespace arma; max@0: using namespace std; max@0: max@0: max@0: int main(int argc, char** argv) max@0: { max@0: cout << "Armadillo version: " << arma_version::as_string() << endl; max@0: max@0: // directly specify the matrix size (elements are uninitialised) max@0: mat A(2,3); max@0: max@0: // .n_rows = number of rows (read only) max@0: // .n_cols = number of columns (read only) max@0: cout << "A.n_rows = " << A.n_rows << endl; max@0: cout << "A.n_cols = " << A.n_cols << endl; max@0: max@0: // directly access an element (indexing starts at 0) max@0: A(1,2) = 456.0; max@0: max@0: A.print("A:"); max@0: max@0: // scalars are treated as a 1x1 matrix, max@0: // hence the code below will set A to have a size of 1x1 max@0: A = 5.0; max@0: A.print("A:"); max@0: max@0: // if you want a matrix with all elements set to a particular value max@0: // the .fill() member function can be used max@0: A.set_size(3,3); max@0: A.fill(5.0); max@0: A.print("A:"); max@0: max@0: max@0: mat B; max@0: max@0: // endr indicates "end of row" max@0: B << 0.555950 << 0.274690 << 0.540605 << 0.798938 << endr max@0: << 0.108929 << 0.830123 << 0.891726 << 0.895283 << endr max@0: << 0.948014 << 0.973234 << 0.216504 << 0.883152 << endr max@0: << 0.023787 << 0.675382 << 0.231751 << 0.450332 << endr; max@0: max@0: // print to the cout stream max@0: // with an optional string before the contents of the matrix max@0: B.print("B:"); max@0: max@0: // the << operator can also be used to print the matrix max@0: // to an arbitrary stream (cout in this case) max@0: cout << "B:" << endl << B << endl; max@0: max@0: // save to disk max@0: B.save("B.txt", raw_ascii); max@0: max@0: // load from disk max@0: mat C; max@0: C.load("B.txt"); max@0: max@0: C += 2.0 * B; max@0: C.print("C:"); max@0: max@0: max@0: // submatrix types: max@0: // max@0: // .submat(first_row, first_column, last_row, last_column) max@0: // .row(row_number) max@0: // .col(column_number) max@0: // .cols(first_column, last_column) max@0: // .rows(first_row, last_row) max@0: max@0: cout << "C.submat(0,0,3,1) =" << endl; max@0: cout << C.submat(0,0,3,1) << endl; max@0: max@0: // generate the identity matrix max@0: mat D = eye(4,4); max@0: max@0: D.submat(0,0,3,1) = C.cols(1,2); max@0: D.print("D:"); max@0: max@0: // transpose max@0: cout << "trans(B) =" << endl; max@0: cout << trans(B) << endl; max@0: max@0: // maximum from each column (traverse along rows) max@0: cout << "max(B) =" << endl; max@0: cout << max(B) << endl; max@0: max@0: // maximum from each row (traverse along columns) max@0: cout << "max(B,1) =" << endl; max@0: cout << max(B,1) << endl; max@0: max@0: // maximum value in B max@0: cout << "max(max(B)) = " << max(max(B)) << endl; max@0: max@0: // sum of each column (traverse along rows) max@0: cout << "sum(B) =" << endl; max@0: cout << sum(B) << endl; max@0: max@0: // sum of each row (traverse along columns) max@0: cout << "sum(B,1) =" << endl; max@0: cout << sum(B,1) << endl; max@0: max@0: // sum of all elements max@0: cout << "sum(sum(B)) = " << sum(sum(B)) << endl; max@0: cout << "accu(B) = " << accu(B) << endl; max@0: max@0: // trace = sum along diagonal max@0: cout << "trace(B) = " << trace(B) << endl; max@0: max@0: // random matrix -- values are uniformly distributed in the [0,1] interval max@0: mat E = randu(4,4); max@0: E.print("E:"); max@0: max@0: cout << endl; max@0: max@0: // row vectors are treated like a matrix with one row max@0: rowvec r; max@0: r << 0.59499 << 0.88807 << 0.88532 << 0.19968; max@0: r.print("r:"); max@0: max@0: // column vectors are treated like a matrix with one column max@0: colvec q; max@0: q << 0.81114 << 0.06256 << 0.95989 << 0.73628; max@0: q.print("q:"); max@0: max@0: // dot or inner product max@0: cout << "as_scalar(r*q) = " << as_scalar(r*q) << endl; max@0: max@0: max@0: // outer product max@0: cout << "q*r =" << endl; max@0: cout << q*r << endl; max@0: max@0: // multiply-and-accumulate operation max@0: // (no temporary matrices are created) max@0: cout << "accu(B % C) = " << accu(B % C) << endl; max@0: max@0: // sum of three matrices (no temporary matrices are created) max@0: mat F = B + C + D; max@0: F.print("F:"); max@0: max@0: // imat specifies an integer matrix max@0: imat AA; max@0: imat BB; max@0: max@0: AA << 1 << 2 << 3 << endr << 4 << 5 << 6 << endr << 7 << 8 << 9; max@0: BB << 3 << 2 << 1 << endr << 6 << 5 << 4 << endr << 9 << 8 << 7; max@0: max@0: // comparison of matrices (element-wise) max@0: // output of a relational operator is a umat max@0: umat ZZ = (AA >= BB); max@0: ZZ.print("ZZ ="); max@0: max@0: max@0: // 2D field of arbitrary length row vectors max@0: // (fields can also store abitrary objects, e.g. instances of std::string) max@0: field xyz(3,2); max@0: max@0: xyz(0,0) = randu(1,2); max@0: xyz(1,0) = randu(1,3); max@0: xyz(2,0) = randu(1,4); max@0: xyz(0,1) = randu(1,5); max@0: xyz(1,1) = randu(1,6); max@0: xyz(2,1) = randu(1,7); max@0: max@0: cout << "xyz:" << endl; max@0: cout << xyz << endl; max@0: max@0: max@0: // cubes ("3D matrices") max@0: cube Q( B.n_rows, B.n_cols, 2 ); max@0: max@0: Q.slice(0) = B; max@0: Q.slice(1) = 2.0 * B; max@0: max@0: Q.print("Q:"); max@0: max@0: max@0: return 0; max@0: } max@0: