21 template <
int Rows,
int Cols,
class Type>
42 template <
int Rows,
int Cols,
class Type>
51 : parent_(parent), index_(index)
58 return parent_->data_[index_ * Cols + col];
63 return parent_->data_[index_ * Cols + col];
71 template <
int Rows = 4,
int Cols = 4,
class Type =
double>
100 for (
int i = 0; i < Cols; ++i) {
101 rowMatrix[0][i] =
data_[row][i];
114 for (
int i = 0; i < Rows; ++i) {
115 colMatrix[i][0] =
data_[i][col];
124 typename std::vector<Type>::iterator
begin() {
return data_.begin(); }
130 typename std::vector<Type>::iterator
end() {
return data_.end(); }
140 std::stringstream ss;
142 for (
int i = 0; i < Rows - 1; ++i) {
144 for (
int j = 0; j < Cols - 1; ++j) {
145 ss <<
data_[i * Cols + j] <<
' ';
147 ss <<
data_[(Rows - 1) * Cols + Cols - 1] <<
"],";
150 for (
int j = 0; j < Cols - 1; ++j) {
151 ss <<
data_[(Rows - 1) * Cols + j] <<
' ';
153 ss <<
data_[(Rows - 1) * Cols + Cols - 1] <<
"]]";
170 std::vector<Type> out;
171 out.reserve(
data_.size());
172 std::transform(
data_.begin(),
data_.end(), rhs.data_.begin(),
173 std::back_inserter(out),
174 [](Type a, Type b) {
return a + b; });
175 data_ = std::move(out);
181 std::vector<Type> out;
182 out.reserve(
data_.size());
183 std::transform(
data_.begin(),
data_.end(), rhs.begin(),
184 std::back_inserter(out),
185 [](Type a, Type b) {
return a - b; });
186 data_ = std::move(out);
191 template <
int M,
int N,
class T>
198 template <
int M,
int N,
class T>
205 template <
int M,
int N,
class T>
208 for (
auto &data : lhs) {
214 template <
int M,
int N,
class T>
217 for (
auto &data : rhs) {
223 template <
int M,
int N,
class T>
226 for (
auto &data : lhs) {
232 template <
int M,
int N,
class T>
235 for (
auto &data : rhs) {
241 template <
int M,
int N,
class T>
244 for (
auto &data : lhs) {
250 template <
int M,
int N,
class T>
253 for (
auto &data : rhs) {
259 template <
int M,
int N,
class T>
262 for (
auto &data : lhs) {
268 template <
int M,
int N,
class T>
271 for (
int i = 0; i <
M; ++i) {
272 for (
int j = 0; j <
N; ++j) {
273 if (lhs[i][j] != rhs[i][j]) {
281 template <
int M,
int N,
class T>
282 std::ostream &operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
284 return os << mat.toString();
287 template <
int Rows = 2,
class Type =
double>
308 std::stringstream ss;
310 for (std::size_t i = 0; i < this->
data_.size() - 1; ++i) {
311 ss << this->
data_[i] <<
" ";
313 ss << this->
data_[this->
data_.size() - 1] <<
"]";
322 template <
typename Type =
double>
338 const Type &
x()
const {
return this->
data_[0]; }
341 const Type &
y()
const {
return this->
data_[1]; }
348 template <
typename Type =
double>
371 template <
typename Type =
double>
422 template <
int M,
int N,
class T>
426 for (
int i = 0; i <
M; ++i) {
427 for (
int j = 0; j <
N; ++j) {
428 trans[j][i] = m[i][j];
438 template <
int R,
class T>
442 for (
int i = 0; i <
R; ++i) {
443 sum += m1[i][0] * m2[i][0];
454 template <
int M,
int N,
int P,
int Q,
class T>
459 throw std::runtime_error(
460 "Matrices don't have the right dimensions for multiplication");
467 for (
int i = 0; i <
M; ++i) {
468 for (
int j = 0; j <
Q; ++j) {
Matrix< M, N, T > operator/(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.h:260
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.h:97
Type & z
Definition: matrix.h:375
Matrix< M, N, T > operator*(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.h:242
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.h:87
z(this->data_[2])
Definition: matrix.h:364
2D Vector class.
Definition: matrix.h:323
bool operator==(const Matrix< M, N, T > &lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:269
Type & y
Definition: matrix.h:352
std::vector< Type >::iterator end()
Iterator support for the end.
Definition: matrix.h:130
Type & x
Definition: matrix.h:352
Type & operator[](int col)
Definition: matrix.h:302
Type & y
Definition: matrix.h:375
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.h:79
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.h:455
Type & y()
Definition: matrix.h:340
Type & operator[](int col)
Definition: matrix.h:55
Matrix< Rows, 1, Type > getCol(int col) const
Get a specific column in a column vector.
Definition: matrix.h:111
const Type & operator[](int col) const
Definition: matrix.h:61
w(this->data_[3])
Definition: matrix.h:387
const Type & x() const
Definition: matrix.h:338
Type & x
Definition: matrix.h:375
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.h:423
int colSize() const
Returns the column size of the Matrix.
Definition: matrix.h:90
const Type & operator[](int col) const
Definition: matrix.h:304
Matrix< Rows, Cols, Type > & operator-=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.h:179
3D Vector class.
Definition: matrix.h:349
Type & x()
Definition: matrix.h:337
details::Row< Rows, Cols, Type > operator[](int row) const
Definition: matrix.h:162
details::Row< Rows, Cols, Type > operator[](int row)
Definition: matrix.h:157
Matrix< M, N, T > operator+(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:192
Matrix< M, N, T > operator-(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:199
virtual std::string toString() const
Prints out the matrix, but can also be implemented by other classes to print data differently...
Definition: matrix.h:138
Matrix< Rows, Cols, Type > & operator+=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.h:168
Base Matrix class used by other similar classes.
Definition: matrix.h:22
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.h:439
Type & w
Definition: matrix.h:375
const Type & y() const
Definition: matrix.h:341
std::string toString() const override
Prints out the matrix, but can also be implemented by other classes to print data differently...
Definition: matrix.h:306
4D Vector class
Definition: matrix.h:372
std::vector< Type >::iterator begin()
Iterator support for the start.
Definition: matrix.h:124
Type & z
Definition: matrix.h:352