SMatrix.h
Go to the documentation of this file.
1 #ifndef _utl_SMatrix_h_
2 #define _utl_SMatrix_h_
3 
4 #include <boost/lambda/lambda.hpp>
5 
6 #include <utl/ListAssignmentProxy.h>
7 
8 
9 namespace utl {
10 
22  template<unsigned int n, unsigned int m, typename T = double>
23  class SMatrix {
24  public:
25  typedef T Type;
26 
27  static const unsigned int NRows = n;
28  static const unsigned int NColumns = m;
29  static const unsigned int Size = n * m;
30 
31  static unsigned int GetNRows() { return n; }
32 
33  static unsigned int GetNColumns() { return m; }
34 
35  static unsigned int GetSize() { return Size; }
36 
37  SMatrix() { }
38 
39  explicit SMatrix(const T& init) { Clear(init); }
40 
41  template<class Matrix>
42  explicit SMatrix(const Matrix& a) { Assign(a); }
43 
44  template<typename Matrix>
45  void
46  Assign(const Matrix& a)
47  {
48  for (unsigned int i = 0; i < n; ++i)
49  for (unsigned int j = 0; j < m; ++j)
50  fMatrix[i][j] = a[i][j];
51  }
52 
54  operator=(const T& e)
55  {
56  fMatrix[0][0] = e;
58  }
59 
60  template<typename Matrix>
61  SMatrix& operator=(const Matrix& a) { Assign(a); return *this; }
62 
63  void
64  Clear(const T& init = T())
65  {
66  T* a = &fMatrix[0][0];
67  *a = init;
68  for (unsigned int i = 1; i < Size; ++i)
69  *(++a) = init;
70  }
71 
72  T* operator[](const unsigned int i) { return fMatrix[i]; }
73 
74  const T* operator[](const unsigned int i) const { return fMatrix[i]; }
75 
76  template<unsigned int i, unsigned int j>
77  T& Get() { return fMatrix[i][j]; }
78 
79  template<unsigned int i, unsigned int j>
80  const T& Get() const { return fMatrix[i][j]; }
81 
82  template<typename U>
83  SMatrix&
84  operator*=(const U& fact)
85  {
86  T* a = &fMatrix[0][0];
87  *a *= fact;
88  for (unsigned int i = 1; i < Size; ++i)
89  *(++a) *= fact;
90  return *this;
91  }
92 
93  template<typename U>
94  SMatrix&
95  operator/=(const U& div)
96  {
97  T* a = &fMatrix[0][0];
98  *a /= div;
99  for (unsigned int i = 1; i < Size; ++i)
100  *(++a) /= div;
101  return *this;
102  }
103 
104  template<typename U>
105  SMatrix&
107  {
108  T* a = &fMatrix[0][0];
109  const T* b = &mat.fMatrix[0][0];
110  *a += *b;
111  for (unsigned int i = 1; i < Size; ++i)
112  *(++a) += *(++b);
113  return *this;
114  }
115 
116  template<typename U>
117  SMatrix&
119  {
120  T* a = &fMatrix[0][0];
121  const T* b = &mat.fMatrix[0][0];
122  *a -= *b;
123  for (unsigned int i = 1; i < Size; ++i)
124  *(++a) -= *(++b);
125  return *this;
126  }
127 
128  template<unsigned int nn, unsigned int mm, typename U>
129  bool
131  const
132  {
133  if (n != nn || m != mm)
134  return false;
135  for (unsigned int i = 0; i < n; ++i)
136  for (unsigned int j = 0; j < m; ++j)
137  if (fMatrix[i][j] != a[i][j])
138  return false;
139  return true;
140  }
141 
142  template<unsigned int nn, unsigned int mm, typename U>
143  bool operator!=(const SMatrix<nn, mm, U>& a) const
144  { return !operator==(a); }
145 
146  private:
147  T fMatrix[n][m];
148  };
149 
150  template<unsigned int n, unsigned int m, typename T>
151  const unsigned int SMatrix<n, m, T>::NRows;
152 
153  template<unsigned int n, unsigned int m, typename T>
154  const unsigned int SMatrix<n, m, T>::NColumns;
155 
156  template<unsigned int n, unsigned int m, typename T>
157  const unsigned int SMatrix<n, m, T>::Size;
158 
159 
160  template<unsigned int n, unsigned int m, typename T, typename U>
161  inline
162  typename boost::lambda::return_type_2<
163  boost::lambda::arithmetic_action<boost::lambda::multiply_action>,
166  >::type
168  {
169  typedef typename boost::lambda::return_type_2<
170  boost::lambda::arithmetic_action<boost::lambda::multiply_action>,
173  >::type ProductMatrixType;
174 
175  ProductMatrixType r;
176 
177  for (unsigned int i = 0; i < n; ++i)
178  for (unsigned int j = 0; j < n; ++j) {
179  r[i][j] = a[i][0] * b[0][j];
180  for (unsigned int k = 1; k < m; ++k)
181  r[i][j] += a[i][k] * b[k][j];
182  }
183 
184  return r;
185  }
186 
187 }
188 
189 
190 namespace boost {
191 
192  namespace lambda {
193 
195  template<unsigned int n, unsigned int m, typename T, typename U>
196  class plain_return_type_2<
197  arithmetic_action<multiply_action>,
198  utl::SMatrix<n, m, T>,
199  utl::SMatrix<m, n, U>
200  > {
201  private:
202  // find type of T * U
203  typedef typename
204  return_type_2<
205  arithmetic_action<multiply_action>,
206  T,
207  U
209 
210  public:
212  };
213 
214  }
215 
216 }
217 
218 
219 #include <utl/SMatrixSVectorOp.h>
220 
221 
222 #endif
constexpr double mm
Definition: AugerUnits.h:113
SMatrix & operator/=(const U &div)
Definition: SMatrix.h:95
static const unsigned int NColumns
Definition: SMatrix.h:28
SMatrix(const T &init)
Definition: SMatrix.h:39
SMatrix & operator*=(const U &fact)
Definition: SMatrix.h:84
const T & Get() const
Definition: SMatrix.h:80
SMatrix & operator-=(const SMatrix< n, m, U > &mat)
Definition: SMatrix.h:118
bool operator==(const SMatrix< nn, mm, U > &a) const
Definition: SMatrix.h:130
#define U
T & Get()
Definition: SMatrix.h:77
SMatrix(const Matrix &a)
Definition: SMatrix.h:42
void Assign(const Matrix &a)
Definition: SMatrix.h:46
static const unsigned int Size
Definition: SMatrix.h:29
Vector operator*(const double d, const Vector &v)
Definition: OperationsVV.h:38
static unsigned int GetSize()
Definition: SMatrix.h:35
SMatrix & operator=(const Matrix &a)
Definition: SMatrix.h:61
T fMatrix[n][m]
Definition: SMatrix.h:147
const T * operator[](const unsigned int i) const
Definition: SMatrix.h:74
SMatrix & operator+=(const SMatrix< n, m, U > &mat)
Definition: SMatrix.h:106
bool operator!=(const SMatrix< nn, mm, U > &a) const
Definition: SMatrix.h:143
static unsigned int GetNRows()
Definition: SMatrix.h:31
constexpr double m
Definition: AugerUnits.h:121
static unsigned int GetNColumns()
Definition: SMatrix.h:33
ListMatrixAssignmentProxy< SMatrix, m > operator=(const T &e)
Definition: SMatrix.h:54
Static (small and dense) matrix class.
Definition: SMatrix.h:23
T * operator[](const unsigned int i)
Definition: SMatrix.h:72
static const unsigned int NRows
Definition: SMatrix.h:27

, generated on Tue Sep 26 2023.