Debian Codegraph @

yo/classtemplates/examples/matrix.h

raw
1 #include <istream>
2 #include <algorithm>
3 #include <numeric>
4
5 template <size_t Rows, size_t Columns, typename DataType = double>
6 class Matrix
7 {
8 //HEAD
9 using MatrixRow = Matrix<1, Columns, DataType>;
10 using MatrixColumn = Matrix<Rows, 1, DataType>;
11
12 MatrixRow d_matrix[Rows];
13 //=
14 public:
15 Matrix();
16 Matrix(std::istream &str);
17 MatrixRow &operator[](size_t idx);
18 MatrixRow const &operator[](size_t idx) const;
19 MatrixRow columnMarginals() const;
20 MatrixColumn rowMarginals() const;
21 DataType sum() const;
22 };
23
24 //MATRIXROW
25 template <size_t Columns, typename DataType> // no default allowed
26 class Matrix<1, Columns, DataType>
27 //=
28 {
29 //ROWDATA
30 DataType d_column[Columns];
31 //=
32 public:
33 Matrix();
34
35 template <size_t Rows>
36 Matrix(Matrix<Rows, Columns, DataType> const &matrix);
37
38 DataType &operator[](size_t idx);
39 DataType const &operator[](size_t idx) const;
40 };
41
42
43 //MATRIXCOLUMN
44 template <size_t Rows, typename DataType>
45 class Matrix<Rows, 1, DataType>
46 //=
47 {
48 DataType d_row[Rows];
49 public:
50 Matrix();
51
52 template <size_t Columns>
53 Matrix(Matrix<Rows, Columns, DataType> const &matrix);
54
55 DataType &operator[](size_t idx);
56 DataType const &operator[](size_t idx) const;
57 DataType sum();
58 };
59
60 //MATRIX1X1
61 template <typename DataType>
62 class Matrix<1, 1, DataType>
63 //=
64 {
65 DataType d_cell;
66 public:
67 Matrix();
68
69 template <size_t Rows, size_t Columns>
70 Matrix(Matrix<Rows, Columns, DataType> const &matrix);
71
72 template <size_t Rows>
73 Matrix(Matrix<Rows, 1, DataType> const &matrix);
74
75 operator DataType &();
76 DataType &get();
77 operator DataType const &() const;
78 DataType const &get() const;
79 };
80
81 //MARGINALS
82 template <size_t Rows, size_t Columns, typename DataType>
83 Matrix<1, Columns, DataType>
84 Matrix<Rows, Columns, DataType>::columnMarginals() const
85 {
86 return MatrixRow(*this);
87 }
88
89 template <size_t Rows, size_t Columns, typename DataType>
90 Matrix<Rows, 1, DataType>
91 Matrix<Rows, Columns, DataType>::rowMarginals() const
92 {
93 return MatrixColumn(*this);
94 }
95
96 template <size_t Rows, size_t Columns, typename DataType>
97 DataType Matrix<Rows, Columns, DataType>::sum() const
98 {
99 return rowMarginals().sum();
100 }
101 //=
102
103
104 //CONSTRUCTORS
105 template <size_t Rows, size_t Columns, typename DataType>
106 Matrix<Rows, Columns, DataType>::Matrix()
107 {
108 std::fill(d_matrix, d_matrix + Rows, MatrixRow());
109 }
110 template <size_t Rows, size_t Columns, typename DataType>
111 Matrix<Rows, Columns, DataType>::Matrix(std::istream &str)
112 {
113 for (size_t row = 0; row < Rows; row++)
114 for (size_t col = 0; col < Columns; col++)
115 str >> d_matrix[row][col];
116 }
117 //=
118
119 //OPERATORINDEX
120 template <size_t Rows, size_t Columns, typename DataType>
121 Matrix<1, Columns, DataType>
122 &Matrix<Rows, Columns, DataType>::operator[](size_t idx)
123 {
124 return d_matrix[idx];
125 }
126 //=
127
128 template <size_t Rows, size_t Columns, typename DataType>
129 Matrix<1, Columns, DataType>
130 const &Matrix<Rows, Columns, DataType>::operator[](size_t idx) const
131 {
132 return d_matrix[idx];
133 }
134
135 //ROWCONS1
136 template <size_t Columns, typename DataType>
137 Matrix<1, Columns, DataType>::Matrix()
138 {
139 std::fill(d_column, d_column + Columns, DataType());
140 }
141 //=
142 //ROWCONS2
143 template <size_t Columns, typename DataType>
144 template <size_t Rows>
145 Matrix<1, Columns, DataType>::Matrix(
146 Matrix<Rows, Columns, DataType> const &matrix)
147 {
148 std::fill(d_column, d_column + Columns, DataType());
149
150 for (size_t col = 0; col < Columns; col++)
151 for (size_t row = 0; row < Rows; row++)
152 d_column[col] += matrix[row][col];
153 }
154 //=
155
156 //ROWOPERATORINDEX
157 template <size_t Columns, typename DataType>
158 DataType &Matrix<1, Columns, DataType>::operator[](size_t idx)
159 {
160 return d_column[idx];
161 }
162 //=
163
164 template <size_t Columns, typename DataType>
165 DataType const &Matrix<1, Columns, DataType>::operator[](size_t idx) const
166 {
167 return d_column[idx];
168 }
169
170
171 template <size_t Rows, typename DataType>
172 Matrix<Rows, 1, DataType>::Matrix()
173 {
174 std::fill(d_row, d_row + Rows, DataType());
175 }
176
177 template <size_t Rows, typename DataType>
178 template <size_t Columns>
179 Matrix<Rows, 1, DataType>::Matrix(
180 Matrix<Rows, Columns, DataType> const &matrix)
181 {
182 for (size_t row = 0; row < Rows; row++)
183 d_row[row] =
184 std::accumulate
185 (
186 &matrix[row][0], &matrix[row][Columns], DataType()
187 );
188 }
189
190 template <size_t Rows, typename DataType>
191 DataType &Matrix<Rows, 1, DataType>::operator[](size_t idx)
192 {
193 return d_row[idx];
194 }
195
196 template <size_t Rows, typename DataType>
197 DataType const &Matrix<Rows, 1, DataType>::operator[](size_t idx) const
198 {
199 return d_row[idx];
200 }
201
202 //SUMCOLUMN
203 template <size_t Rows, typename DataType>
204 DataType Matrix<Rows, 1, DataType>::sum()
205 {
206 return std::accumulate(d_row, d_row + Rows, DataType());
207 }
208 //=
209
210 template <typename DataType>
211 Matrix<1, 1, DataType>::Matrix()
212 :
213 d_cell(DataType())
214 {}
215
216 //1X1CONS
217 template <typename DataType>
218 template <size_t Rows, size_t Columns>
219 Matrix<1, 1, DataType>::Matrix(
220 Matrix<Rows, Columns, DataType> const &matrix)
221 :
222 d_cell(matrix.rowMarginals().sum())
223 {}
224
225 template <typename DataType>
226 template <size_t Rows>
227 Matrix<1, 1, DataType>::Matrix(Matrix<Rows, 1, DataType> const &matrix)
228 :
229 d_cell(matrix.sum())
230 {}
231 //=
232 //ACCESSORS
233 template <typename DataType>
234 Matrix<1, 1, DataType>::operator DataType &()
235 {
236 return d_cell;
237 }
238
239 template <typename DataType>
240 DataType &Matrix<1, 1, DataType>::get()
241 {
242 return d_cell;
243 }
244 //=
245 template <typename DataType>
246 Matrix<1, 1, DataType>::operator DataType const &() const
247 {
248 return d_cell;
249 }
250
251 template <typename DataType>
252 DataType const &Matrix<1, 1, DataType>::get() const
253 {
254 return d_cell;
255 }