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