LoveBrains  1.0.0
matrix.h
1 /*
2 ** matrix.h for GANNEngine in /home/robin_f/Programming/Git/CPP/GANNEngine/include/ANN
3 **
4 ** Made by Guillaume ROBIN
5 ** Login <robin_f@epitech.eu>
6 **
7 ** Started on Wed Jul 8 14:03:07 2015 Guillaume ROBIN
8 ** Last update Tue Jul 14 13:20:31 2015 Guillaume ROBIN
9 */
10 
11 #ifndef MATRIX_H_
12 # define MATRIX_H_
13 
14 # include <vector>
15 # include <iostream>
16 
17 # define ERR_MATRIX_RANGE (char *)"Error: Matrix: index is out of range !"
18 # define ERR_MATRIX_INVALID (char *)"Error: Matrix: size is invalid !"
19 # define ERR_MATRIX_INVOP (char *)"Error: Matrix: invalid operation !"
20 
21 # define RANGE(i, j, m_i, m_j)((i) >= 0 && (j) >= 0 && (i) < (m_i) && (j) < (m_j) ? true : false)
22 
23 namespace GANN
24 {
25  template <typename T>
26  class Matrix
27  {
28  public:
29  Matrix(void)
30  {
31  _rows = 1;
32  _cols = 1;
33  }
34  Matrix(unsigned int rows, unsigned int cols, T const& elem)
35  {
36  if (rows == 0 || cols == 0)
37  {
38  std::cerr << ERR_MATRIX_INVALID << std::endl;
39  _rows = 1;
40  _cols = 1;
41  }
42  else
43  {
44  _rows = rows;
45  _cols = cols;
46  }
47  _init = elem;
48  for (unsigned int i = 0; i < rows * cols; i++)
49  _m.push_back(elem);
50  }
51  Matrix(Matrix const& copy)
52  {
53  _rows = copy.rows();
54  _cols = copy.cols();
55  _init = copy.init();
56  _m.clear();
57  for (unsigned int i = 0; i < _rows * _cols; i++)
58  _m.push_back(copy[i]);
59  }
60 
61  /*
62  ** Getters.
63  */
64  unsigned int rows(void) const { return (_rows); }
65  unsigned int cols(void) const { return (_cols); }
66  T const& init(void) const { return (_init); }
67  Matrix<T> identity(unsigned int size, T const& null, T const& id)
68  {
69  Matrix<T> m(size, size, null);
70 
71  for (unsigned int i = 0; i < size; i++)
72  m(i, i) = id;
73  return (m);
74  }
75 
76  /*
77  ** Overload.
78  */
79  Matrix<T>& operator=(Matrix<T> const& m)
80  {
81  _rows = m.rows();
82  _cols = m.cols();
83  _m.clear();
84  for (unsigned int i = 0; i < _rows * _cols; i++)
85  _m.push_back(m[i]);
86  return (*this);
87  }
88  const T& operator[](unsigned int i) const
89  {
90  if (i < _rows * _cols)
91  return (_m[i]);
92  else
93  std::cerr << ERR_MATRIX_RANGE << std::endl;
94  return (_m[_rows * _cols - 1]);
95  }
96  T& operator[](unsigned int i)
97  {
98  if (i < _rows * _cols)
99  return (_m[i]);
100  else
101  std::cerr << ERR_MATRIX_RANGE << std::endl;
102  return (_m[_rows * _cols - 1]);
103  }
104  const T& operator()(unsigned int i, unsigned int j) const
105  {
106  if (RANGE(i, j, _rows, _cols))
107  return (_m[i * _cols + j]);
108  else
109  std::cerr << ERR_MATRIX_RANGE << std::endl;
110  return (_m[_cols * _cols - 1]);
111  }
112  T& operator()(unsigned int i, unsigned int j)
113  {
114  if (RANGE(i, j, _rows, _cols))
115  return (_m[i * _cols + j]);
116  else
117  std::cerr << ERR_MATRIX_RANGE << std::endl;
118  return (_m[_cols * _cols - 1]);
119  }
120 
121  private:
122  unsigned int _rows;
123  unsigned int _cols;
124  std::vector<T> _m;
125  T _init;
126  };
127 
128  /*
129  ** Overload.
130  */
131  template <class T>
132  Matrix<T> operator+(Matrix<T> const& m1, Matrix<T> const& m2)
133  {
134  Matrix<T> result;
135 
136  if (m1.rows() == m2.rows() && m1.cols() == m2.cols())
137  {
138  result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
139  for (unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
140  result[i] = m1[i] + m2[i];
141  }
142  else
143  std::cerr << ERR_MATRIX_INVOP << std::endl;
144  return (result);
145  }
146 
147  template <class T>
148  Matrix<T> operator-(Matrix<T> const& m1, Matrix<T> const& m2)
149  {
150  Matrix<T> result;
151 
152  if (m1.rows() == m2.rows() && m1.cols() == m2.cols())
153  {
154  result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
155  for (unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
156  result[i] = m1[i] - m2[i];
157  }
158  else
159  std::cerr << ERR_MATRIX_INVOP << std::endl;
160  return (result);
161  }
162 
163  template <class T>
164  Matrix<T> operator+(Matrix<T> const& m1, T const& s)
165  {
166  Matrix<T> result;
167 
168  result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
169  for (unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
170  result[i] = m1[i] + s;
171  return (result);
172  }
173 
174  template <class T>
175  Matrix<T> operator*(Matrix<T> const& m1, T const& s)
176  {
177  Matrix<T> result;
178 
179  result = Matrix<T>(m1.rows(), m1.cols(), m1.init());
180  for (unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
181  result[i] = m1[i] * s;
182  return (result);
183  }
184 
185  template <class T>
186  Matrix<T> operator*(Matrix<T> const& m1, Matrix<T> const& m2)
187  {
188  Matrix<T> result;
189 
190  if (m1.cols() == m2.rows())
191  {
192  result = Matrix<T>(m1.rows(), m2.cols(), m1.init());
193  for (unsigned int i = 0; i < result.rows(); i++)
194  {
195  for (unsigned int j = 0; j < result.cols(); j++)
196  {
197  for (unsigned int k = 0; k < m1.cols(); k++)
198  {
199  if (k == 0)
200  result(i, j) = m1(i, k) * m2(k, j);
201  else
202  result(i, j) += m1(i, k) * m2(k, j);
203  }
204  }
205  }
206  }
207  else
208  std::cerr << ERR_MATRIX_INVOP << std::endl;
209  return (result);
210  }
211 
212  template <class T>
213  bool operator==(Matrix<T> const& m1, Matrix<T> const& m2)
214  {
215  if (m1.cols() == m2.rows())
216  {
217  for (unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
218  {
219  if (m1[i] != m2[i])
220  return (false);
221  }
222  return (true);
223  }
224  else
225  std::cerr << ERR_MATRIX_INVOP << std::endl;
226  return (false);
227  }
228 
229  template <class T>
230  bool operator==(Matrix<T>& m1, Matrix<T>& m2)
231  {
232  if (m1.cols() == m2.rows())
233  {
234  for (unsigned int i = 0; i < m1.rows() * m1.cols(); i++)
235  {
236  if (m1[i] != m2[i])
237  return (false);
238  }
239  return (true);
240  }
241  else
242  std::cerr << ERR_MATRIX_INVOP << std::endl;
243  return (false);
244  }
245 
246  template <class T>
247  std::ostream& operator<<(std::ostream& flux, Matrix<T> const& m)
248  {
249  for (unsigned int i = 0; i < m.rows(); i++)
250  {
251  for (unsigned j = 0; j < m.cols(); j++)
252  flux << m(i, j) << ' ';
253  flux << std::endl;
254  }
255  return (flux);
256  }
257 }
258 
259 #endif /* !MATRIX_H_ */
Definition: matrix.h:26
Contains all the objects that provide the management for the genetic algorithm with neural networks...
Definition: ann_exception.h:16