SPIDAR API Library  0x16033101
Space Interface Device for Artificial Reality
Matrix.hpp
Go to the documentation of this file.
1 
5 #ifndef SPIDAR_MATRIX_HPP
6 #define SPIDAR_MATRIX_HPP
7 
8 #include <cmath>
9 #include "Vector.hpp"
10 
11 namespace Spidar
12 {
13 namespace Math
14 {
24 template <class T, size_t M, size_t N>
25 struct Matrix
26 {
27  typedef T ValueType;
28 
29  static const size_t row = M;
30  static const size_t column = N;
31 
32  ValueType element[M][N];
33 
34  // コンストラクタ
35  Matrix(void)
36  {
37  for (size_t i=0; i<M; ++i)
38  {
39  for (size_t j=0; j<N; ++j)
40  {
41  element[i][j] = 0;
42  }
43  }
44  }
45 
46  Matrix(const Matrix &other)
47  {
48  for (size_t i=0; i<M; ++i)
49  {
50  for (size_t j=0; j<N; ++j)
51  {
52  element[i][j] = other.element[i][j];
53  }
54  }
55  }
56 
57  Matrix& operator=(const Matrix &other)
58  {
59  for (size_t i=0; i<M; ++i)
60  {
61  for (size_t j=0; j<N; ++j)
62  {
63  element[i][j] = other.element[i][j];
64  }
65  }
66  return *this;
67  }
68 
69  //
70  ValueType* operator[](size_t i)
71  {
72  return element[i];
73  }
74 
75  //
76  const ValueType* operator[](size_t i) const
77  {
78  return element[i];
79  }
80 
81  //
82  Matrix& operator+=(const Matrix &other)
83  {
84  for (size_t i=0; i<M; ++i)
85  {
86  for (size_t j=0; j<N; ++j)
87  {
88  element[i][j] += other.element[i][j];
89  }
90  }
91  return *this;
92  }
93 
94  //
95  Matrix& operator-=(const Matrix &other)
96  {
97  for (size_t i=0; i<M; ++i)
98  {
99  for (size_t j=0; j<N; ++j)
100  {
101  element[i][j] -= other.element[i][j];
102  }
103  }
104  return *this;
105  }
106 
107  //
108  Matrix& operator*=(double value)
109  {
110  for (size_t i=0; i<M; ++i)
111  {
112  for (size_t j=0; j<N; ++j)
113  {
114  element[i][j] = static_cast<ValueType>(value * element[i][j]);
115  }
116  }
117  return *this;
118  }
119 
120  //
121  Matrix& operator/=(double value)
122  {
123  for (size_t i=0; i<M; ++i)
124  {
125  for (size_t j=0; j<N; ++j)
126  {
127  element[i][j] = static_cast<ValueType>(element[i][j]/value);
128  }
129  }
130  return *this;
131  }
132 
133  //
134  void clear(void)
135  {
136  for (size_t i=0; i<M; ++i)
137  {
138  for (size_t j=0; j<N; ++j)
139  {
140  element[i][j] = 0;
141  }
142  }
143  }
144 
145  //
146  Matrix<T,N,M> trans(void) const
147  {
148  Matrix<T,N,M> mat;
149 
150  for (size_t i=0; i<M; ++i)
151  {
152  for (size_t j=0; j<N; ++j)
153  {
154  mat.element[j][i] = element[i][j];
155  }
156  }
157  return mat;
158  }
159 
160 }; // end of struct Matrix.
161 
162 //
163 template <class T, size_t M, size_t N>
164 std::ostream& operator<<(std::ostream &os, const Matrix<T,M,N> &m)
165 {
166  os << "[";
167 
168  for (size_t i=0; i<M; ++i)
169  {
170  if (i>0) os << " ";
171  for (size_t j=0; j<N; ++j)
172  {
173  os << m.element[i][j];
174  if (j<N-1) os << " ";
175  }
176  if (i<M-1) os << std::endl;
177  }
178 
179  os << "]";
180 
181  return os;
182 }
183 
184 //
185 template <class T, size_t N>
186 void Identity(Matrix<T,N,N>& mat)
187 {
188  mat.clear();
189  for (size_t i=0; i<N; ++i) mat[i][i] = 1;
190 }
191 
192 //
193 template <class T, size_t M, size_t N>
194 const Matrix<T,M,N> operator+(Matrix<T,M,N> lhs, const Matrix<T,M,N> &rhs)
195 {
196  return lhs += rhs;
197 }
198 
199 //
200 template <class T, size_t M, size_t N>
201 const Matrix<T,M,N> operator-(Matrix<T,M,N> lhs, const Matrix<T,M,N> &rhs)
202 {
203  return lhs -= rhs;
204 }
205 
206 //
207 template <class T, size_t M, size_t N>
208 const Matrix<T,M,N> operator*(Matrix<T,M,N> lhs, double rhs)
209 {
210  return lhs *= rhs;
211 }
212 
213 //
214 template <class T, size_t M, size_t N>
215 const Matrix<T,M,N> operator*(double lhs, Matrix<T,M,N> rhs)
216 {
217  return rhs *= lhs;
218 }
219 
220 //
221 template <class T, size_t L, size_t M, size_t N>
222 const Matrix<T,L,N> operator*(const Matrix<T,L,M> &lhs, const Matrix<T,M,N> &rhs)
223 {
224  Matrix<T,L,N> temp;
225 
226  for (size_t i=0; i<L; ++i)
227  {
228  for (size_t j=0; j<N; ++j)
229  {
230  temp[i][j] = 0;
231 
232  for(size_t k=0; k<M; ++k)
233  {
234  temp[i][j] += lhs[i][k] * rhs[k][j];
235  }
236  }
237  }
238  return temp;
239 }
240 
241 //
242 template <class T, size_t M, size_t N>
243 const Vector<T,M> operator*(const Matrix<T,M,N> &mat, const Vector<T,N> &vec)
244 {
245  Vector<T,M> temp;
246 
247  for (size_t i=0; i<M; ++i)
248  {
249  temp[i] = 0;
250 
251  for (size_t j=0; j<N; ++j)
252  {
253  temp[i] += mat[i][j] * vec[j];
254  }
255  }
256  return temp;
257 }
258 
259 //
260 template <class T, size_t M, size_t N>
261 const Matrix<T,M,N> operator/(Matrix<T,M,N> lhs, double rhs)
262 {
263  return lhs /= rhs;
264 }
265 
266 } // end of namespace Math.
267 } // end of namespace Spidar.
268 
269 #endif // SPIDAR_MATRIX_HPP
270 
271 // end of file.
static const size_t row
行の数
Definition: Matrix.hpp:29
static const size_t column
列の数
Definition: Matrix.hpp:30
SPIDARライブラリのルート名前空間です.
ベクトルクラスです.
Definition: Vector.hpp:24
ValueType element[M][N]
行列の要素
Definition: Matrix.hpp:32
行列クラスです.
Definition: Matrix.hpp:25
T ValueType
要素の型
Definition: Matrix.hpp:27