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