12 #ifndef YAGE_MATH_MATRIX_H
13 #define YAGE_MATH_MATRIX_H
25 template <
int Rows,
int Cols,
class Type>
46 template <
int Rows,
int Cols,
class Type>
55 : parent_(parent), index_(index)
62 return parent_->data_[index_ * Cols + col];
67 return parent_->data_[index_ * Cols + col];
75 template <
int Rows = 4,
int Cols = 4,
class Type =
double>
104 for (
int i = 0; i < Cols; ++i) {
105 rowMatrix[0][i] =
data_[row][i];
118 for (
int i = 0; i < Rows; ++i) {
119 colMatrix[i][0] =
data_[i][col];
128 typename std::vector<Type>::iterator
begin() {
return data_.begin(); }
134 typename std::vector<Type>::iterator
end() {
return data_.end(); }
144 std::stringstream ss;
146 for (
int i = 0; i < Rows - 1; ++i) {
148 for (
int j = 0; j < Cols - 1; ++j) {
149 ss <<
data_[i * Cols + j] <<
' ';
151 ss <<
data_[(Rows - 1) * Cols + Cols - 1] <<
"],";
154 for (
int j = 0; j < Cols - 1; ++j) {
155 ss <<
data_[(Rows - 1) * Cols + j] <<
' ';
157 ss <<
data_[(Rows - 1) * Cols + Cols - 1] <<
"]]";
174 std::vector<Type> out;
175 out.reserve(
data_.size());
176 std::transform(
data_.begin(),
data_.end(), rhs.data_.begin(),
177 std::back_inserter(out),
178 [](Type a, Type b) {
return a + b; });
179 data_ = std::move(out);
185 std::vector<Type> out;
186 out.reserve(
data_.size());
187 std::transform(
data_.begin(),
data_.end(), rhs.begin(),
188 std::back_inserter(out),
189 [](Type a, Type b) {
return a - b; });
190 data_ = std::move(out);
195 template <
int M,
int N,
class T>
202 template <
int M,
int N,
class T>
209 template <
int M,
int N,
class T>
212 for (
auto &data : lhs) {
218 template <
int M,
int N,
class T>
221 for (
auto &data : rhs) {
227 template <
int M,
int N,
class T>
230 for (
auto &data : lhs) {
236 template <
int M,
int N,
class T>
239 for (
auto &data : rhs) {
245 template <
int M,
int N,
class T>
248 for (
auto &data : lhs) {
254 template <
int M,
int N,
class T>
257 for (
auto &data : rhs) {
263 template <
int M,
int N,
class T>
266 for (
auto &data : lhs) {
272 template <
int M,
int N,
class T>
275 for (
int i = 0; i <
M; ++i) {
276 for (
int j = 0; j <
N; ++j) {
277 if (lhs[i][j] != rhs[i][j]) {
285 template <
int M,
int N,
class T>
286 std::ostream &operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
288 return os << mat.toString();
291 template <
int Rows = 2,
class Type =
double>
312 std::stringstream ss;
314 for (std::size_t i = 0; i < this->
data_.size() - 1; ++i) {
315 ss << this->
data_[i] <<
" ";
317 ss << this->
data_[this->
data_.size() - 1] <<
"]";
326 template <
typename Type =
double>
342 const Type &
x()
const {
return this->
data_[0]; }
345 const Type &
y()
const {
return this->
data_[1]; }
352 template <
typename Type =
double>
375 template <
typename Type =
double>
426 template <
int M,
int N,
class T>
430 for (
int i = 0; i <
M; ++i) {
431 for (
int j = 0; j <
N; ++j) {
432 trans[j][i] = m[i][j];
442 template <
int R,
class T>
446 for (
int i = 0; i <
R; ++i) {
447 sum += m1[i][0] * m2[i][0];
458 template <
int M,
int N,
int P,
int Q,
class T>
463 throw std::runtime_error(
464 "Matrices don't have the right dimensions for multiplication");
471 for (
int i = 0; i <
M; ++i) {
472 for (
int j = 0; j <
Q; ++j) {
Matrix< M, N, T > operator/(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.h:264
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.h:101
Type & z
Definition: matrix.h:379
Matrix< M, N, T > operator*(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.h:246
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.h:91
z(this->data_[2])
Definition: matrix.h:368
2D Vector class.
Definition: matrix.h:327
bool operator==(const Matrix< M, N, T > &lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:273
Type & y
Definition: matrix.h:356
std::vector< Type >::iterator end()
Iterator support for the end.
Definition: matrix.h:134
Type & x
Definition: matrix.h:356
Type & operator[](int col)
Definition: matrix.h:306
Type & y
Definition: matrix.h:379
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.h:83
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.h:459
Type & y()
Definition: matrix.h:344
Type & operator[](int col)
Definition: matrix.h:59
Matrix< Rows, 1, Type > getCol(int col) const
Get a specific column in a column vector.
Definition: matrix.h:115
const Type & operator[](int col) const
Definition: matrix.h:65
w(this->data_[3])
Definition: matrix.h:391
const Type & x() const
Definition: matrix.h:342
Type & x
Definition: matrix.h:379
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.h:427
int colSize() const
Returns the column size of the Matrix.
Definition: matrix.h:94
const Type & operator[](int col) const
Definition: matrix.h:308
Matrix< Rows, Cols, Type > & operator-=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.h:183
3D Vector class.
Definition: matrix.h:353
Type & x()
Definition: matrix.h:341
details::Row< Rows, Cols, Type > operator[](int row) const
Definition: matrix.h:166
details::Row< Rows, Cols, Type > operator[](int row)
Definition: matrix.h:161
Matrix< M, N, T > operator+(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:196
Matrix< M, N, T > operator-(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.h:203
virtual std::string toString() const
Prints out the matrix, but can also be implemented by other classes to print data differently...
Definition: matrix.h:142
Matrix< Rows, Cols, Type > & operator+=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.h:172
Base Matrix class used by other similar classes.
Definition: matrix.h:26
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.h:443
Type & w
Definition: matrix.h:379
const Type & y() const
Definition: matrix.h:345
std::string toString() const override
Prints out the matrix, but can also be implemented by other classes to print data differently...
Definition: matrix.h:310
4D Vector class
Definition: matrix.h:376
std::vector< Type >::iterator begin()
Iterator support for the start.
Definition: matrix.h:128
Type & z
Definition: matrix.h:356