24 template <
int Rows,
int Cols,
class Type>
45 template <
int Rows,
int Cols,
class Type>
54 : parent_(parent), index_(index)
61 return parent_->data_[index_ * Cols + col];
66 return parent_->data_[index_ * Cols + col];
74 template <
int Rows = 4,
int Cols = 4,
class Type =
double>
103 for (
int i = 0; i < Cols; ++i) {
104 rowMatrix[0][i] =
data_[row][i];
117 for (
int i = 0; i < Rows; ++i) {
118 colMatrix[i][0] =
data_[i][col];
127 typename std::vector<Type>::iterator
begin() {
return data_.begin(); }
133 typename std::vector<Type>::iterator
end() {
return data_.end(); }
143 std::stringstream ss;
145 for (
int i = 0; i < Rows - 1; ++i) {
147 for (
int j = 0; j < Cols - 1; ++j) {
148 ss <<
data_[i * Cols + j] <<
' ';
150 ss <<
data_[(Rows - 1) * Cols + Cols - 1] <<
"],";
153 for (
int j = 0; j < Cols - 1; ++j) {
154 ss <<
data_[(Rows - 1) * Cols + j] <<
' ';
156 ss <<
data_[(Rows - 1) * Cols + Cols - 1] <<
"]]";
173 std::vector<Type> out;
174 out.reserve(
data_.size());
175 std::transform(
data_.begin(),
data_.end(), rhs.data_.begin(),
176 std::back_inserter(out),
177 [](Type a, Type b) {
return a + b; });
178 data_ = std::move(out);
184 std::vector<Type> out;
185 out.reserve(
data_.size());
186 std::transform(
data_.begin(),
data_.end(), rhs.begin(),
187 std::back_inserter(out),
188 [](Type a, Type b) {
return a - b; });
189 data_ = std::move(out);
194 template <
int M,
int N,
class T>
201 template <
int M,
int N,
class T>
208 template <
int M,
int N,
class T>
211 for (
auto &data : lhs) {
217 template <
int M,
int N,
class T>
220 for (
auto &data : rhs) {
226 template <
int M,
int N,
class T>
229 for (
auto &data : lhs) {
235 template <
int M,
int N,
class T>
238 for (
auto &data : rhs) {
244 template <
int M,
int N,
class T>
247 for (
auto &data : lhs) {
253 template <
int M,
int N,
class T>
256 for (
auto &data : rhs) {
262 template <
int M,
int N,
class T>
265 for (
auto &data : lhs) {
271 template <
int M,
int N,
class T>
274 for (
int i = 0; i <
M; ++i) {
275 for (
int j = 0; j <
N; ++j) {
276 if (lhs[i][j] != rhs[i][j]) {
284 template <
int M,
int N,
class T>
285 std::ostream &operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
287 return os << mat.toString();
290 template <
int Rows = 2,
class Type =
double>
311 std::stringstream ss;
313 for (std::size_t i = 0; i < this->
data_.size() - 1; ++i) {
314 ss << this->
data_[i] <<
" ";
316 ss << this->
data_[this->
data_.size() - 1] <<
"]";
325 template <
typename Type =
double>
341 const Type &
x()
const {
return this->
data_[0]; }
344 const Type &
y()
const {
return this->
data_[1]; }
351 template <
typename Type =
double>
374 template <
typename Type =
double>
425 template <
int M,
int N,
class T>
429 for (
int i = 0; i <
M; ++i) {
430 for (
int j = 0; j <
N; ++j) {
431 trans[j][i] = m[i][j];
441 template <
int R,
class T>
445 for (
int i = 0; i <
R; ++i) {
446 sum += m1[i][0] * m2[i][0];
457 template <
int M,
int N,
int P,
int Q,
class T>
462 throw std::runtime_error(
463 "Matrices don't have the right dimensions for multiplication");
470 for (
int i = 0; i <
M; ++i) {
471 for (
int j = 0; j <
Q; ++j) {
Matrix< M, N, T > operator/(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.h:263
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.h:100
Type & z
Definition: matrix.h:378
Matrix< M, N, T > operator*(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.h:245
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.h:90
z(this->data_[2])
Definition: matrix.h:367
2D Vector class.
Definition: matrix.h:326
bool operator==(const Matrix< M, N, T > &lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:272
Type & y
Definition: matrix.h:355
std::vector< Type >::iterator end()
Iterator support for the end.
Definition: matrix.h:133
Type & x
Definition: matrix.h:355
Type & operator[](int col)
Definition: matrix.h:305
Type & y
Definition: matrix.h:378
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.h:82
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.h:458
Type & y()
Definition: matrix.h:343
Type & operator[](int col)
Definition: matrix.h:58
Matrix< Rows, 1, Type > getCol(int col) const
Get a specific column in a column vector.
Definition: matrix.h:114
const Type & operator[](int col) const
Definition: matrix.h:64
w(this->data_[3])
Definition: matrix.h:390
const Type & x() const
Definition: matrix.h:341
Type & x
Definition: matrix.h:378
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.h:426
int colSize() const
Returns the column size of the Matrix.
Definition: matrix.h:93
const Type & operator[](int col) const
Definition: matrix.h:307
Matrix< Rows, Cols, Type > & operator-=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.h:182
3D Vector class.
Definition: matrix.h:352
Type & x()
Definition: matrix.h:340
details::Row< Rows, Cols, Type > operator[](int row) const
Definition: matrix.h:165
details::Row< Rows, Cols, Type > operator[](int row)
Definition: matrix.h:160
Matrix< M, N, T > operator+(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:195
Matrix< M, N, T > operator-(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:202
virtual std::string toString() const
Prints out the matrix, but can also be implemented by other classes to print data differently...
Definition: matrix.h:141
Matrix< Rows, Cols, Type > & operator+=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.h:171
Base Matrix class used by other similar classes.
Definition: matrix.h:25
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.h:442
Type & w
Definition: matrix.h:378
const Type & y() const
Definition: matrix.h:344
std::string toString() const override
Prints out the matrix, but can also be implemented by other classes to print data differently...
Definition: matrix.h:309
4D Vector class
Definition: matrix.h:375
std::vector< Type >::iterator begin()
Iterator support for the start.
Definition: matrix.h:127
Type & z
Definition: matrix.h:355