cheshirekow  v0.1.0
matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of fiber.
5  *
6  * fiber is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * fiber is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with fiber. If not, see <http://www.gnu.org/licenses/>.
18  */
25 #ifndef FIBER_MATRIX_H_
26 #define FIBER_MATRIX_H_
27 
28 
29 namespace fiber {
30 
31 template <typename Scalar, int Rows, int Cols>
32 class Matrix : public _LValue<Scalar, Matrix<Scalar, Rows, Cols> >,
33  public _RValue<Scalar, Matrix<Scalar, Rows, Cols> > {
34 
35  protected:
36  Scalar data_[Rows * Cols];
37 
38  public:
39  enum {
41  COLS_ = Cols,
42  SIZE_ = Rows * Cols
43  };
44 
45  Size size() const { return Rows * Cols; }
46  Size rows() const { return Rows; }
47  Size cols() const { return Cols; }
48 
49  Scalar& operator[](Index i) {
50  assert(i < SIZE_);
51  return data_[i];
52  }
53 
54  Scalar const& operator[](Index i) const {
55  assert(i < SIZE_);
56  return data_[i];
57  }
58 
59  Scalar& operator()(int i, int j) {
60  assert(i < Rows && j < Cols);
61  return data_[i * Cols + j];
62  }
63 
64  Scalar operator()(int i, int j) const {
65  assert(i < Rows && j < Cols);
66  return data_[i * Cols + j];
67  }
68 
70  Matrix() {}
71 
73  template <typename Exp>
75  static_assert(Exp::ROWS_ == ROWS_ && Exp::COLS_ == COLS_,
76  "Cannot construct a matrix from a matrix of a differnt size");
77 
78  for (int i = 0; i < ROWS_; i++) {
79  for (int j = 0; j < COLS_; j++) {
80  (*this)(i, j) = exp(i, j);
81  }
82  }
83  }
84 
85 #ifdef FIBER_USE_VARIADIC_TEMPLATES
86  template <int Index>
87  void BulkAssign() {}
88 
89  template <int Index, typename... Tail>2
90  void BulkAssign(Scalar head, Tail... tail) {
91  static_assert(Index < Rows * Cols, "Too many inputs to BulkAssign!");
92  (*this)[Index] = head;
93  BulkAssign<Index + 1>(tail...);
94  }
95 
98  template <typename... Scalars>
99  Matrix(Scalar a, Scalar b, Scalars... scalars) {
100  BulkAssign<0>(a, b, scalars...);
101  }
102 #else
103 
105  Matrix(Scalar x0, Scalar x1, Scalar x2, Scalar x3) {
106  static_assert(SIZE_ == 4,
107  "This constructor is only for size 4 matrices");
108  data_[0] = x0;
109  data_[1] = x1;
110  data_[2] = x2;
111  data_[3] = x3;
112  }
113 
115  Matrix(Scalar x0, Scalar x1, Scalar x2) {
116  static_assert(SIZE_ == 3,
117  "This constructor is only for size 3 matrices");
118  data_[0] = x0;
119  data_[1] = x1;
120  data_[2] = x2;
121  }
122 
124  Matrix(Scalar x0, Scalar x1) {
125  static_assert(SIZE_ == 2,
126  "This constructor is only for size 2 matrices");
127  data_[0] = x0;
128  data_[1] = x1;
129  }
130 #endif // USE_VARIADIC_TEMPLATES
131 
132  Matrix(Scalar a) {
133  static_assert(SIZE_ == 1,
134  "This constructor is only for size 1 matrices");
135  data_[0] = a;
136  }
137 
138 };
139 
143 
147 
151 
155 
156 } // namespace fiber
157 
158 
159 #endif // FIBER_MATRIX_H_
Matrix(const _RValue< Scalar, Exp > &exp)
Construct from any MatrixExpression:
Definition: matrix.h:74
Matrix< float, 4, 1 > Vector4f
Definition: matrix.h:154
Matrix< float, 2, 1 > Vector2f
Definition: matrix.h:152
Matrix< double, 4, 1 > Vector4d
Definition: matrix.h:146
Scalar data_[Rows *Cols]
Definition: matrix.h:36
Matrix(Scalar x0, Scalar x1, Scalar x2, Scalar x3)
Fixed size construction.
Definition: matrix.h:105
expression template for rvalues
Definition: rvalue.h:33
Matrix< float, 4, 4 > Matrix4f
Definition: matrix.h:150
Matrix(Scalar x0, Scalar x1)
Fixed size construction.
Definition: matrix.h:124
Size cols() const
Definition: matrix.h:47
unsigned int Size
Definition: fiber.h:32
Matrix(Scalar a)
Definition: matrix.h:132
Matrix< double, 3, 1 > Vector3d
Definition: matrix.h:145
Matrix< float, 2, 2 > Matrix2f
Definition: matrix.h:148
Matrix< float, 3, 3 > Matrix3f
Definition: matrix.h:149
Matrix(Scalar x0, Scalar x1, Scalar x2)
Fixed size construction.
Definition: matrix.h:115
Scalar & operator[](Index i)
Definition: matrix.h:49
int Index
Definition: fiber.h:31
Size rows() const
Definition: matrix.h:46
Matrix< float, 3, 1 > Vector3f
Definition: matrix.h:153
Matrix< double, 2, 1 > Vector2d
Definition: matrix.h:144
LView< Scalar, Exp, rows, Exp::COLS_ > Rows(_LValue< Scalar, Exp > &A, int i)
Definition: view.h:172
expression template for rvalues
Definition: lvalue.h:33
Scalar const & operator[](Index i) const
Definition: matrix.h:54
Scalar operator()(int i, int j) const
Definition: matrix.h:64
Matrix< double, 2, 2 > Matrix2d
Definition: matrix.h:140
Matrix()
Default constructor.
Definition: matrix.h:70
Matrix< double, 4, 4 > Matrix4d
Definition: matrix.h:142
Matrix< double, 3, 3 > Matrix3d
Definition: matrix.h:141
Size size() const
Definition: matrix.h:45
Scalar & operator()(int i, int j)
Definition: matrix.h:59