summaryrefslogtreecommitdiff
path: root/tags/START/glagen/algo_distribue/tools/matrix.hh
blob: 489436027e524465707b2d4112994069eab52fb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// matrix.hh

#include <iostream>

#ifndef		MATRIX_HH_
# define	MATRIX_HH_

// la class TYPE ne peut etre que de type nombre (ex: double, int, long,...)

template <class TYPE> class Matrix
{
public:
  // Constructor
  Matrix(const unsigned int i, const unsigned int j)
  {
    this->_i = i;
    this->_j = j;
    this->_lines = new _line[i];	// Create lines...
    for (unsigned int k = 0; k < i; ++k)
      this->_lines[k] = new TYPE[j];	// ...and create column
    for (unsigned int k = 0; k < i; ++k)
      for (unsigned int l = 0; l < j; ++l)
	this->_lines[k][l] = 0;
  }

  // Constructor by copy
  Matrix(const Matrix& mat)
  {
    this->_i = mat._i;
    this->_j = mat._j;
    this->_lines = new _line[this->_i];
    for (unsigned int k = 0; k < this->_i; ++k)
      {
	this->_lines[k] = new TYPE[this->_j];
	for (unsigned int l = 0; l < this->_j; ++l)
	  this->_lines[k][l] = mat._lines[k][l];
      }
  }

  // Destructor
  ~Matrix()
  {
    if (this->_i != 0 || this->_j != 0)
      {
	for (unsigned int i = 0; i < this->_i; i++)
	  delete [] this->_lines[i];
	delete [] this->_lines;
      }
  }

  // Affected operator
  Matrix& operator=(const Matrix& mat)
  {
    if (&mat != this)
      {
	if (mat._i != this->_i || mat._j != this->_j)	// Test dim
	  {
	    this->~Matrix();	// Destroy...
	    this->_i = mat._i;
	    this->_j = mat._j;
	    this->_lines = new _line[this->_i];	// ...and realloc
	    for (unsigned int i = 0; i < this->_i; ++i)
	      this->_lines[i] = new TYPE[this->_j];
	  }
	for (unsigned int i = 0; i < this->_i; ++i)	// Copy
	  for (unsigned int j = 0; j < this->_j; ++j)
	    this->_lines[i][j] = mat._lines[i][j];
      }
    return (*this);
  }
  
  // Add elements to matrix
  Matrix&	add_elems(const int& nb)
  {
    Matrix mat(this->_i + nb, this->_j + nb);
    for (unsigned int i = 0; i < this->_i; i++)
      for (unsigned int j = 0; j < this->_j; j++)
	mat(i, j) = this->_lines[i][j];
    *this = mat;
    return (*this);
  }
  
  // Add matrix to matrix
  Matrix&	add_mat(const Matrix& mat)
  {
    unsigned int i = this->_i;
    unsigned int j = this->_j;
    this->add_elems(mat._i);
    for (unsigned int k = i; k < this->_i; k++)
      for (unsigned int l = j; l < this->_j; l++)
	this->_lines[k][l] = mat(k - i, l - j);
    return (*this);
  }

  // Add of matrix
  Matrix		operator+(const Matrix& mat)
  {
    Matrix mat_tmp(this->_i, mat._j);
    if (this->_i != mat._i || this->_j != mat._j)
      {
	std::cerr << "Error of addiction of 2 matrix (dimensions)"\
		  << std::endl;
	return (mat_tmp);
      }
    for (unsigned int i = 0; i < this->_i; ++i)
      for (unsigned int j = 0; j < this->_j; ++j)
	mat_tmp._lines[i][j] = this->_lines[i][j] + mat._lines[i][j];
    return (mat_tmp);
  }

  Matrix		operator+(const Matrix& mat) const
  {
    Matrix mat_tmp(this->_i, mat._j);
    if (this->_i != mat._i || this->_j != mat._j)
      {
	std::cerr << "Error of addiction of 2 matrix (dimensions)"\
		  << std::endl;
	return (mat_tmp);
      }
    for (unsigned int i = 0; i < this->_i; ++i)
      for (unsigned int j = 0; j < this->_j; ++j)
	mat_tmp._lines[i][j] = this->_lines[i][j] + mat._lines[i][j];
    return (mat_tmp);
  }

  // Sub matrix
  Matrix		operator-(const Matrix& mat)
  {
    Matrix mat_tmp(this->_i, mat._j);
    if (this->_i != mat._i || this->_j != mat._j)
      {
	std::cerr << "Error of substraction of 2 matrix (dimensions)"\
		  << std::endl;
	return (mat_tmp);
      }
    for (unsigned int i = 0; i < this->_i; ++i)
      for (unsigned int j = 0; j < this->_j; ++j)
	mat_tmp._lines[i][j] = this->_lines[i][j] - mat._lines[i][j];
    return (mat_tmp);
  } 

  Matrix		operator-(const Matrix& mat) const
  {
    Matrix mat_tmp(this->_i, mat._j);
    if (this->_i != this->_j || this->_i != mat._i || mat._i != mat._j)
      {
	std::cerr << "Error of substraction of 2 matrix (dimensions)"\
		  << std::endl;
	return (mat_tmp);
      }
    for (unsigned int i = 0; i < this->_i; ++i)
      for (unsigned int j = 0; j < this->_j; ++j)
	mat_tmp._lines[i][j] = this->_lines[i][j] - mat._lines[i][j];
    return (mat_tmp);
  }
  
  // Multiplication of matrix
  Matrix		operator*(const Matrix& mat)
  {
    Matrix mat_tmp(this->_i, mat._j);
    if (this->_j != mat._i)	// Check dimension
      {
	std::cerr << "Error of produce of 2 matrix (dimensions)"\
		  << std::endl;
	return (mat_tmp);
      }
    for (unsigned int i = 0; i < this->_i; ++i)
      {
	for (unsigned int j = 0; j < mat._j; ++j)
	  {
	    TYPE res = 0;
	    // Produce lines and column of matrix
	    for (unsigned int k = 0; k < this->_j; ++k)
	      res += this->_lines[i][k] * mat._lines[k][j];
	    mat_tmp._lines[i][j] = res;
	  }
      }
    return (mat_tmp);
  }

  // Multiplication with real
  Matrix&		operator*(const TYPE& nbr)
  {
    for (unsigned int i = 0; i < this->_i; i++)
      for (unsigned int j = 0; j < this->_j; j++)
	{
	  this->_lines[i][j] *= nbr;
	}
    return (*this);
  }

  // Divide with real
  Matrix&		operator/(const TYPE& nbr)
  {
    for (unsigned int i = 0; i < this->_i; i++)
      for (unsigned int j = 0; j < this->_j; j++)
	{
	  this->_lines[i][j] /= nbr;
	}
    return (*this);
  }  

  //Get dimension of matrix
  const int	get_dim() const
  {
    if (this->_i != this->_j)
      {
	std::cerr << "Matrix isn't nxn, cannot give dimension" << std::endl;
	return (0);
      }
    return (this->_i);
  }
  
  // Operator to change mat(i, j)
  // to can write mat(i, j) = ...
  TYPE&	operator()(unsigned int i, unsigned int j)
  {
    return (this->_lines[i][j]);
  }
  
  // Operator to access mat(i, j)
  // to can write int x = mat(i, j)
  TYPE	operator()(unsigned int i, unsigned int j) const
  {
    return (this->_lines[i][j]);
  }
  
  // Test matrix nul
  const bool	test_nul() const
  {
    for (unsigned int i = 0; i < this->_i; i++)
      for (unsigned int j = 0; j < this->_j; j++)
	if (this->_lines[i][j] != 0)
	  return (0);
    return (1);
  }
  
  // Test diagonal matrix nul
  const int	test_diag() const
  {
    for (unsigned int i = 0; i < this->_i; i++)
      if (this->_lines[i][i] != 0)
	return (i);
    return (-1);
  }
  
  // Test diagonal matrix nul with element
  const int	test_diag(const unsigned int& j) const
  {
    for (unsigned int i = 0; i < this->_i; i++)
      if (this->_lines[i][i] != 0 && i == j)
	return (i);
    return (-1);
  }

  // Calculate trace
  const TYPE	trace() const
  {
    TYPE res = 0;
    if (this->_i != this->_j)
      {
	std::cerr << "Matrix isn't nxn, cannot give trace" << std::endl;
	return (0);
      }
    for (unsigned int i = 0; i < this->_i; i++)
      res += this->_lines[i][i];
    return (res);
  }

  // Transpose
  Matrix&	transpose()
  {
    if (this->_i != this->_j)
      {
	std::cerr << "Matrix isn't nxn, cannot transpose" << std::endl;
	return (*this);
      }
    TYPE tmp = 0;
    for (unsigned int i = 0; i < this->_i; i++)
      for (unsigned int j = 0; j < i; j++)
	{
	  tmp = this->_lines[i][j];
	  this->_lines[i][j] = this->_lines[j][i];
	  this->_lines[j][i] = tmp;
	}
    return (*this);
  }

  Matrix	transpose() const
  {
    Matrix mat(this->_j, this->_i);
    for (unsigned int i = 0; i < this->_i; i++)
      for (unsigned int j = 0; j < this->_j; j++)
	mat._lines[i][j] = this->_lines[j][i];
    return (mat);
  }

  // Display matrix
  void print(std::ostream &os) const
  {
  for (unsigned int i = 0; i < this->_i; ++i)
    {
      for (unsigned int j = 0; j < this->_j; ++j)
	os << this->_lines[i][j] << " ";
      os << std::endl;
    }
  }

protected:
  typedef TYPE* _line;
  _line* _lines;
  unsigned int _i;	// Number of lines
  unsigned int _j;	// Number of column
};

template <class TYPE>
inline std::ostream& operator<<(std::ostream& ostr, const Matrix<TYPE>& stream)
{
  stream.print(ostr);
  return (ostr);
}


#endif		// MATRIX_HH