ARB
matrix.h
Go to the documentation of this file.
1 // ========================================================= //
2 // //
3 // File : matrix.h //
4 // Purpose : templates for matrices //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in Jul 20 //
7 // http://www.arb-home.de/ //
8 // //
9 // ========================================================= //
10 
11 #ifndef MATRIX_H
12 #define MATRIX_H
13 
14 #ifndef ARB_ASSERT_H
15 #include <arb_assert.h>
16 #endif
17 
18 template<typename T>
19 class symmetric_matrix : virtual Noncopyable {
20  // Symmetrical Matrix (upper triangular matrix)
21 
22  size_t Size;
23  T **m; // m[i][j] i <= j !!!!
24 
25 public:
26 
27  explicit symmetric_matrix(size_t Size_)
28  : Size(Size_)
29  {
30  size_t elements = Size*(Size+1)/2;
31  size_t headsize = Size * sizeof(*m);
32  size_t datasize = elements * sizeof(*(m[0]));
33 
34  m = (T**)ARB_calloc<char>(headsize+datasize);
35  m[0] = (T*)(((char*)m)+headsize);
36 
37  for (size_t i=1; i<Size; i++) {
38  m[i] = m[i-1]+i;
39  }
40  }
41 
43  free(m);
44  }
45 
46  void set(size_t i, size_t j, T val) { if (i>j) m[i][j] = val; else m[j][i] = val; };
47 
48  T fast_get(size_t i, size_t j) const { arb_assert(i>=j); return m[i][j]; };
49  T get(size_t i, size_t j) const { if (i>j) return m[i][j]; else return m[j][i]; };
50 
51  T get_max_value() const { // O(n*2)
52  // Warning: ignores matrix diagonal
53 
54  T max = m[0][0]; // take any
55  for (size_t i=0; i<Size; i++) {
56  for (size_t j=0; j<i; j++) {
57  if (m[i][j] > max) max = m[i][j];
58  }
59  }
60  return max;
61  }
62 
63  size_t size() const { return Size; }
64 };
65 
66 
67 #else
68 #error matrix.h included twice
69 #endif // MATRIX_H
#define arb_assert(cond)
Definition: arb_assert.h:245
void set(size_t i, size_t j, T val)
Definition: matrix.h:46
T get_max_value() const
Definition: matrix.h:51
~symmetric_matrix()
Definition: matrix.h:42
size_t size() const
Definition: matrix.h:63
Definition: trnsprob.h:20
symmetric_matrix(size_t Size_)
Definition: matrix.h:27
T fast_get(size_t i, size_t j) const
Definition: matrix.h:48
#define max(a, b)
Definition: f2c.h:154