MatrixExpression-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: MatrixExpression-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Base class for matrix expression.
6 > Created Time: 2017/09/27
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX_EXPRESSION_IMPL_H
10 #define CUBBYFLOW_MATRIX_EXPRESSION_IMPL_H
11 
12 namespace CubbyFlow
13 {
14  // MARK: MatrixExpression
15  template <typename T, typename E>
17  {
18  return static_cast<const E&>(*this).size();
19  }
20 
21  template <typename T, typename E>
23  {
24  return static_cast<const E&>(*this).Rows();
25  }
26 
27  template <typename T, typename E>
29  {
30  return static_cast<const E&>(*this).Cols();
31  }
32 
33  template <typename T, typename E>
35  {
36  return static_cast<const E&>(*this);
37  }
38 
39  template <typename T>
40  MatrixConstant<T>::MatrixConstant(size_t m, size_t n, const T& c) : m_m(m), m_n(n), m_c(c)
41  {
42  // Do nothing
43  }
44 
45  template <typename T>
47  {
48  return Size2(Rows(), Cols());
49  }
50 
51  template <typename T>
52  size_t MatrixConstant<T>::Rows() const
53  {
54  return m_m;
55  }
56 
57  template <typename T>
58  size_t MatrixConstant<T>::Cols() const
59  {
60  return m_n;
61  }
62 
63  template <typename T>
64  T MatrixConstant<T>::operator()(size_t, size_t) const
65  {
66  return m_c;
67  }
68 
69  template <typename T>
71  {
72  // Do nothing
73  }
74 
75  template <typename T>
77  {
78  return Size2(m_m, m_m);
79  }
80 
81  template <typename T>
82  size_t MatrixIdentity<T>::Rows() const
83  {
84  return m_m;
85  }
86 
87  template <typename T>
88  size_t MatrixIdentity<T>::Cols() const
89  {
90  return m_m;
91  }
92 
93  template <typename T>
94  T MatrixIdentity<T>::operator()(size_t i, size_t j) const
95  {
96  return (i == j) ? 1 : 0;
97  }
98 
99  // MARK: MatrixUnaryOp
100  template <typename T, typename E, typename Op>
102  {
103  // Do nothing
104  }
105 
106  template <typename T, typename E, typename Op>
108  {
109  return m_u.size();
110  }
111 
112  template <typename T, typename E, typename Op>
114  {
115  return m_u.Rows();
116  }
117 
118  template <typename T, typename E, typename Op>
120  {
121  return m_u.Cols();
122  }
123 
124  template <typename T, typename E, typename Op>
125  T MatrixUnaryOp<T, E, Op>::operator()(size_t i, size_t j) const
126  {
127  return m_op(m_u(i, j));
128  }
129 
130  template <typename T, typename E>
131  MatrixDiagonal<T, E>::MatrixDiagonal(const E& u, bool isDiag) : m_u(u), m_isDiag(isDiag)
132  {
133  // Do nothing
134  }
135 
136  template <typename T, typename E>
138  {
139  return m_u.size();
140  }
141 
142  template <typename T, typename E>
144  {
145  return m_u.Rows();
146  }
147 
148  template <typename T, typename E>
150  {
151  return m_u.Cols();
152  }
153 
154  template <typename T, typename E>
155  T MatrixDiagonal<T, E>::operator()(size_t i, size_t j) const
156  {
157  if (m_isDiag)
158  {
159  return (i == j) ? m_u(i, j) : 0;
160  }
161 
162  return (i != j) ? m_u(i, j) : 0;
163  }
164 
165  template <typename T, typename E>
166  MatrixTriangular<T, E>::MatrixTriangular(const E& u, bool isUpper, bool isStrict) :
167  m_u(u), m_isUpper(isUpper), m_isStrict(isStrict)
168  {
169  // Do nothing
170  }
171 
172  template <typename T, typename E>
174  {
175  return m_u.size();
176  }
177 
178  template <typename T, typename E>
180  {
181  return m_u.Rows();
182  }
183 
184  template <typename T, typename E>
186  {
187  return m_u.Cols();
188  }
189 
190  template <typename T, typename E>
191  T MatrixTriangular<T, E>::operator()(size_t i, size_t j) const
192  {
193  if (i < j)
194  {
195  return (m_isUpper) ? m_u(i, j) : 0;
196  }
197 
198  if (i > j)
199  {
200  return (!m_isUpper) ? m_u(i, j) : 0;
201  }
202 
203  return (!m_isStrict) ? m_u(i, j) : 0;
204  }
205 
206  // MARK: MatrixBinaryOp
207  template <typename T, typename E1, typename E2, typename Op>
208  MatrixBinaryOp<T, E1, E2, Op>::MatrixBinaryOp(const E1& u, const E2& v) : m_u(u), m_v(v)
209  {
210  assert(u.size() == v.size());
211  }
212 
213  template <typename T, typename E1, typename E2, typename Op>
215  {
216  return m_v.size();
217  }
218 
219  template <typename T, typename E1, typename E2, typename Op>
221  {
222  return m_v.Rows();
223  }
224 
225  template <typename T, typename E1, typename E2, typename Op>
227  {
228  return m_v.Cols();
229  }
230 
231  template <typename T, typename E1, typename E2, typename Op>
232  T MatrixBinaryOp<T, E1, E2, Op>::operator()(size_t i, size_t j) const
233  {
234  return m_op(m_u(i, j), m_v(i, j));
235  }
236 
237  // MARK: MatrixScalarBinaryOp
238  template <typename T, typename E, typename Op>
239  MatrixScalarBinaryOp<T, E, Op>::MatrixScalarBinaryOp(const E& u, const T& v) : m_u(u), m_v(v)
240  {
241  // Do nothing
242  }
243 
244  template <typename T, typename E, typename Op>
246  {
247  return m_u.size();
248  }
249 
250  template <typename T, typename E, typename Op>
252  {
253  return m_u.Rows();
254  }
255 
256  template <typename T, typename E, typename Op>
258  {
259  return m_u.Cols();
260  }
261 
262  template <typename T, typename E, typename Op>
263  T MatrixScalarBinaryOp<T, E, Op>::operator()(size_t i, size_t j) const
264  {
265  return m_op(m_u(i, j), m_v);
266  }
267 
268  template <typename T, typename ME, typename VE>
269  MatrixVectorMul<T, ME, VE>::MatrixVectorMul(const ME& m, const VE& v) : m_m(m), m_v(v)
270  {
271  assert(m_m.Cols() == m_v.size());
272  }
273 
274  template <typename T, typename ME, typename VE>
276  {
277  return m_v.size();
278  }
279 
280  template <typename T, typename ME, typename VE>
282  {
283  T sum = 0;
284  const size_t n = m_m.Cols();
285 
286  for (size_t j = 0; j < n; ++j)
287  {
288  sum += m_m(i, j) * m_v[j];
289  }
290 
291  return sum;
292  }
293 
294  // MARK: MatrixMul
295  template <typename T, typename E1, typename E2>
296  MatrixMul<T, E1, E2>::MatrixMul(const E1& u, const E2& v) : m_u(u), m_v(v)
297  {
298  assert(m_u.Cols() == m_v.Rows());
299  }
300 
301  template <typename T, typename E1, typename E2>
303  {
304  return Size2(m_u.Rows(), m_v.Cols());
305  }
306 
307  template <typename T, typename E1, typename E2>
309  {
310  return m_u.Rows();
311  }
312 
313  template <typename T, typename E1, typename E2>
315  {
316  return m_v.Cols();
317  }
318 
319  template <typename T, typename E1, typename E2>
320  T MatrixMul<T, E1, E2>::operator()(size_t i, size_t j) const
321  {
322  // Unoptimized mat-mat-mul
323  T sum = 0;
324  const size_t n = m_u.Cols();
325 
326  for (size_t k = 0; k < n; ++k)
327  {
328  sum += m_u(i, k) * m_v(k, j);
329  }
330 
331  return sum;
332  }
333 
334  // MARK: Operator overloadings
335  template <typename T, typename E>
337  {
338  return MatrixScalarMul<T, E>(a(), T(-1));
339  }
340 
341  template <typename T, typename E1, typename E2>
343  {
344  return MatrixAdd<T, E1, E2>(a(), b());
345  }
346 
347  template <typename T, typename E>
349  {
350  return MatrixScalarAdd<T, E>(a(), b);
351  }
352 
353  template <typename T, typename E>
355  {
356  return MatrixScalarAdd<T, E>(b(), a);
357  }
358 
359  template <typename T, typename E1, typename E2>
361  {
362  return MatrixSub<T, E1, E2>(a(), b());
363  }
364 
365  template <typename T, typename E>
367  {
368  return MatrixScalarSub<T, E>(a(), b);
369  }
370 
371  template <typename T, typename E>
373  {
374  return MatrixScalarRSub<T, E>(b(), a);
375  }
376 
377  template <typename T, typename E>
379  {
380  return MatrixScalarMul<T, E>(a(), b);
381  }
382 
383  template <typename T, typename E>
385  {
386  return MatrixScalarMul<T, E>(b(), a);
387  }
388 
389  template <typename T, typename ME, typename VE>
391  {
392  return MatrixVectorMul<T, ME, VE>(a(), b());
393  }
394 
395  template <typename T, typename E1, typename E2>
397  {
398  return MatrixMul<T, E1, E2>(a(), b());
399  }
400 
401  template <typename T, typename E>
403  {
404  return MatrixScalarDiv<T, E>(a(), b);
405  }
406 
407  template <typename T, typename E>
409  {
410  return MatrixScalarRDiv<T, E>(a(), b);
411  }
412 }
413 
414 #endif
MatrixBinaryOp(const E1 &u, const E2 &v)
Constructs binary operation expression for given input matrix expressions.
Definition: MatrixExpression-Impl.h:208
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:113
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:149
size_t size() const
Size of the vector.
Definition: MatrixExpression-Impl.h:275
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:185
Matrix< T, 2, 2 > operator+(const Matrix< T, 2, 2 > &a, const Matrix< T, 2, 2 > &b)
Returns a + b (element-size).
Definition: Matrix2x2-Impl.h:660
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:179
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:257
Matrix expression for matrix-scalar binary operation.
Definition: MatrixExpression.h:261
Base class for vector expression.
Definition: VectorExpression.h:28
MatrixIdentity(size_t m)
Constructs m x m identity matrix expression.
Definition: MatrixExpression-Impl.h:70
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:52
MatrixUnaryOp(const E &u)
Constructs unary operation expression for given input expression.
Definition: MatrixExpression-Impl.h:101
Vector expression for matrix-vector multiplication.
Definition: MatrixExpression.h:296
2-D point class.
Definition: Point2.h:25
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:214
Point2< size_t > Size2
Definition: Size2.h:16
T operator[](size_t i) const
Returns vector element at i.
Definition: MatrixExpression-Impl.h:281
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:220
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:308
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:119
Matrix< T, 2, 2 > operator/(const Matrix< T, 2, 2 > &a, T b)
Definition: Matrix2x2-Impl.h:720
Definition: pybind11Utils.h:24
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:46
MatrixTriangular(const E &u, bool isUpper, bool isStrict)
Definition: MatrixExpression-Impl.h:166
MatrixVectorMul(const ME &m, const VE &v)
Definition: MatrixExpression-Impl.h:269
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:88
MatrixConstant(size_t m, size_t n, const T &c)
Constructs m x n constant matrix expression.
Definition: MatrixExpression-Impl.h:40
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:16
MatrixScalarBinaryOp(const E &u, const T &v)
Constructs a binary expression for given matrix and scalar.
Definition: MatrixExpression-Impl.h:239
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:251
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:22
Matrix< T, 2, 2 > operator-(const Matrix< T, 2, 2 > &a)
Returns a matrix with opposite sign.
Definition: Matrix2x2-Impl.h:654
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:302
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:76
const E & operator()() const
Returns actual implementation (the subclass).
Definition: MatrixExpression-Impl.h:34
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:245
Base class for matrix expression.
Definition: MatrixExpression.h:27
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:137
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:82
MatrixMul(const E1 &u, const E2 &v)
Constructs matrix-matrix multiplication expression for given two input matrices.
Definition: MatrixExpression-Impl.h:296
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:58
MatrixDiagonal(const E &u, bool isDiag)
Definition: MatrixExpression-Impl.h:131
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.h:481
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:173
Matrix expression for binary operation.
Definition: MatrixExpression.h:226
Size2 size() const
Size of the matrix.
Definition: MatrixExpression-Impl.h:107
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:28
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:226
size_t Rows() const
Number of rows.
Definition: MatrixExpression-Impl.h:143
size_t Cols() const
Number of columns.
Definition: MatrixExpression-Impl.h:314
Matrix expression for matrix-matrix multiplication.
Definition: MatrixExpression.h:323