Skip to content

Instantly share code, notes, and snippets.

Created February 23, 2014 14:10
Show Gist options
  • Save anonymous/9171970 to your computer and use it in GitHub Desktop.
Save anonymous/9171970 to your computer and use it in GitHub Desktop.
#include <iostream>
2 #include <cmath>
3 #include <vector>
4
5 template
6 <typename Elements, int size>
7 class Matrix {
8 private:
9 std::vector<std::vector<Elements> > matrix;
10 public:
11 class Iterator {
12 public:
13 int index_row;
14 int index_column;
15 std::vector<std::vector<Elements> > new_matrix;
16
17 Iterator() {
18 index_row = 0;
19 index_column = 0;
20 }
21
22 Iterator(const int& number1, const int& number2, std::vector<std::vector<Elements> > other) {
23 index_row = number1;
24 index_column = number2;
25
26 }
27
28 Iterator& operator ++ () {
29 if (index_column == size - 1) {
30 index_row++;
31 index_column = 0;
32 } else {
33 index_column++;
34 }
35 return *this;
36 }
37
38 Iterator operator ++ (int) {
39 Iterator tmp(*this);
40 return tmp;
41 }
42
43 Elements& operator* () {
44 return new_matrix[index_row][index_column];
45 }
46
47 friend inline bool operator != (const Iterator& it1, const Iterator& it2) {
48 if (it1.index_row == it2.index_row) {
49 if (it2.index_column == it2.index_column) {
50 return false;
51 } else {
52 return true;
53 }
54 } else {
55 return true;
56 }
57 }
58
59 };
60
61 class ConstIterator {
62 public:
63 int const_index_row;
64 int const_index_column;
65 std::vector<std::vector<Elements> > const_pointer;
66
67 explicit ConstIterator() {
68 const_index_row = 0;
69 const_index_column = 0;
70 }
71
72 ConstIterator(const int& number1, const int& number2, const Matrix& other) {
73 const_index_row = number1;
74 const_index_column = number2;
75 const_pointer = other.matrix;
76 }
77
78 ConstIterator& operator ++ () {
79 if (const_index_column == size - 1) {
80 const_index_column = 0;
81 const_index_row++;
82 } else {
83 const_index_column++;
84 }
85 return *this;
86 }
87
88 ConstIterator operator ++ (int) {
89 ConstIterator tmp(*this);
90 return tmp;
91 }
92
93 Elements& operator* () {
94 return const_pointer[const_index_row][const_index_column];
95 }
96
97 friend inline bool operator != (const ConstIterator& it1, const ConstIterator& it2) {
98 if (it1.const_index_row == it2.const_index_row) {
99 if (it1.const_index_column == it2.const_index_column) {
100 return false;
101 } else {
102 return true;
103 }
104 } else {
105 return true;
106 }
107 }
108
109 ~ConstIterator() {
110 free(const_pointer);
111 }
112 };
113
114 typedef Iterator iterator;
115 typedef ConstIterator const_iterator;
116
117 Matrix(const Elements& number) {
118 std::vector<Elements> new_matrix;
119 for (size_t i = 0; i < size; ++i) {
120 for (size_t j = 0; j < size; ++i) {
121 new_matrix.clear();
122 if (i == j) {
123 new_matrix.push_back(number);
124 } else {
125 new_matrix.push_back(0);
126 }
127 }
128 matrix.push_back(new_matrix);
129 }
130 new_matrix.clear();
131 }
132
133 Matrix() {
134 std::vector<Elements> new_matrix;
135 for (size_t i = 0; i < size; ++i) {
136 new_matrix.push_back(0);
137 }
138 for (size_t i = 0; i < size; ++i) {
139 matrix.push_back(new_matrix);
140 }
141 new_matrix.clear();
142 }
143
144 Matrix(const std::vector<std::vector<Elements> >& other) {
145 matrix = other;
146 }
147
148 Matrix& operator += (const Matrix& other) {
149 std::vector<Elements> a;
150 std::vector<Elements> b;
151 for (size_t i = 0; i < matrix.size(); ++i) {
152 a = matrix.at(i);
153 b = matrix.at(i);
154 for (size_t j = 0; j < matrix.size(); ++j) {
155 a[j] += b[j];
156 }
157 this->matrix[i] = a;
158 a.clear();
159 b.clear();
160 }
161 a.clear();
162 b.clear();
163 return *this;
164 }
165
166 Matrix operator + (const Matrix& other) const {
167 Matrix tmp(*this);
168 tmp += other;
169 return tmp;
170 }
171
172 Matrix& operator -= (const Matrix& other) {
173 std::vector<Elements> a;
174 std::vector<Elements> b;
175 for (size_t i = 0; i < matrix.size(); ++i) {
176 a = matrix.at(i);
177 b = matrix.at(i);
178 for (size_t j = 0; j < matrix.size(); ++j) {
179 a[j] -= b[j];
180 }
181 this->matrix[i] = a;
182 a.clear();
183 b.clear();
184 }
185 a.clear();
186 b.clear();
187 return *this;
188 }
189
190 Matrix operator - (const Matrix& other) const {
191 Matrix tmp(*this);
192 tmp -= other;
193 return tmp;
194 }
195
196 Matrix& operator *= (const Matrix& other) {
197 std::vector<Elements> a;
198 std::vector<Elements> b;
199 Elements c(0);
200 for (size_t l = 0; l < matrix.size(); ++l) {
201 a.clear();
202 b.clear();
203 c = 0;
204 for (size_t m = 0; m < matrix.size(); ++m) {
205 a.push_back(matrix[m][l]);
206 }
207 for (size_t i = 0; i < matrix.size(); ++i) {
208 b.clear();
209 c = 0;
210 b = other.matrix.at(i);
211 for (size_t k = 0; k < matrix.size(); ++k) {
212 c = c + a[k] * b[k];
213 }
214 this->matrix[i][l] = c;
215 }
216 }
217 return *this;
218 }
219
220 Matrix operator * (const Matrix& other) const {
221 Matrix tmp(*this);
222 tmp *= other;
223 return tmp;
224 }
225
226 Matrix& operator /= (Matrix& other) {
227 std::vector<std::vector<Elements> > lambda;
228 std::vector<Elements> el_lambda;
229 std::vector<Elements> c;
230 for(size_t i = 0; i < matrix.size(); ++i) {
231 el_lambda = other.matrix.at(i);
232 for (size_t j = 0; j < matrix.size(); ++j) {
233 if (j != i) {
234 el_lambda[j] = (-1) * el_lambda[j] / other.matrix[i][i];
235 } else {
236 el_lambda[j] = 1 / other.matrix[i][i];
237 }
238 }
239 for (size_t j = 0; j < matrix.size(); ++j) {
240 if (j == i) {
241 lambda.push_back(el_lambda);
242 } else {
243 for (size_t k = 0; k < matrix.size(); ++k) {
244 if (k == j) {
245 c.push_back(1);
246 } else {
247 c.push_back(0);
248 }
249 }
250 lambda.push_back(c);
251 c.clear();
252 }
253 }
254 other *= Matrix(lambda);
255 lambda.clear();
256 el_lambda.clear();
257 }
258 this->matrix = other.matrix;
259 return *this;
260 }
261
262 Matrix operator / (Matrix& other) const {
263 Matrix tmp(*this);
264 tmp /= other;
265 return tmp;
266 }
267
268 friend std::istream& operator >> (std::istream& is, Matrix& other) {
269 for (size_t i = 0; i < size; ++i) {
270 for (size_t j = 0; j < size; ++j) {
271 is >> other.matrix[i][j];
272 }
273 }
274 return is;
275 }
276
277 friend std::ostream& operator << (std::ostream& os, const Matrix& other) {
278 for (size_t i = 0; i < size; ++i) {
279 for (size_t j = 0; j < size; ++j) {
280 os << other.matrix[i][j];
281 if (j + 1 < size) {
282 os << " ";
283 }
284 }
285 if (i + 1 < size) {
286 std::cout << "\n";
287 }
288 }
289 return os;
290 }
291
292 ConstIterator begin() const {
293 return ConstIterator(0, 0, matrix);
294 }
295
296 ConstIterator end() const {
297 return ConstIterator(size, size, matrix);
298 }
299
300 Iterator begin() {
301 return Iterator(0, 0, matrix);
302 }
303
304 Iterator end() {
305 return Iterator(size, size, matrix);
306 }
307 };
308
309 int main() {
310 Matrix<int, 3> a;
311 for (Matrix<int, 3>::iterator it = a.begin(); it != a.end(); ++it) {
312 std::cin >> *it;
313 }
314 for (Matrix<int, 3>::iterator it = a.begin(); it != a.end(); ++it) {
315 std::cout << *it << " ";
316 }
317 return 0;
318 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment