21 #ifndef YAGE_MATH_MATRIX_HPP 22 #define YAGE_MATH_MATRIX_HPP 33 template <
int Rows,
int Cols,
class Type>
53 template <
int Rows,
int Cols,
class Type>
65 return parent_->
data_[index_ * Cols + col];
69 return parent_->
data_[index_ * Cols + col];
83 template <
int Rows = 4,
int Cols = 4,
class Type =
double>
111 for (
int i = 0; i < Cols; ++i) {
112 rowMatrix[0][i] = data_[row][i];
120 for (
int i = 0; i < Rows; ++i) {
121 colMatrix[i][0] = data_[i][col];
127 typename std::vector<Type>::iterator
begin() {
return data_.begin(); }
130 typename std::vector<Type>::iterator
end() {
return data_.end(); }
136 std::stringstream ss;
138 for (
int i = 0; i < Rows - 1; ++i) {
140 for (
int j = 0; j < Cols - 1; ++j) {
141 ss << data_[i * Cols + j] <<
' ';
143 ss << data_[(Rows - 1) * Cols + Cols - 1] <<
"],";
146 for (
int j = 0; j < Cols - 1; ++j) {
147 ss << data_[(Rows - 1) * Cols + j] <<
' ';
149 ss << data_[(Rows - 1) * Cols + Cols - 1] <<
"]]";
164 std::vector<Type> out;
165 out.reserve(data_.size());
166 std::transform(data_.begin(), data_.end(), rhs.
data_.begin(),
167 std::back_inserter(out),
168 [](Type a, Type b) {
return a + b; });
169 data_ = std::move(out);
174 std::vector<Type> out;
175 out.reserve(data_.size());
176 std::transform(data_.begin(), data_.end(), rhs.
begin(),
177 std::back_inserter(out),
178 [](Type a, Type b) {
return a - b; });
179 data_ = std::move(out);
184 template <
int M,
int N,
class T>
190 template <
int M,
int N,
class T>
196 template <
int M,
int N,
class T>
198 for (
auto& data : lhs) {
204 template <
int M,
int N,
class T>
206 for (
auto& data : rhs) {
212 template <
int M,
int N,
class T>
214 for (
auto& data : lhs) {
220 template <
int M,
int N,
class T>
222 for (
auto& data : rhs) {
228 template <
int M,
int N,
class T>
230 for (
auto& data : lhs) {
236 template <
int M,
int N,
class T>
238 for (
auto& data : rhs) {
244 template <
int M,
int N,
class T>
246 for (
auto& data : lhs) {
252 template <
int M,
int N,
class T>
254 for (
int i = 0; i < M; ++i)
255 for (
int j = 0; j < N; ++j)
256 if (lhs[i][j] != rhs[i][j])
return false;
260 template <
int M,
int N,
class T>
261 std::ostream& operator<<(std::ostream& os, const Matrix<M, N, T>& mat) {
262 return os << mat.toString();
265 template <
int Rows = 2,
class Type =
double>
276 const Type&
operator[](
int col)
const {
return this->data_[col]; }
278 std::string
toString()
const override override override {
279 std::stringstream ss;
281 for (std::size_t i = 0; i < this->data_.size() - 1; ++i) {
282 ss << this->data_[i] <<
" ";
284 ss << this->data_[this->data_.size() - 1] <<
"]";
293 template <
class Type =
double>
306 Type&
x() {
return this->data_[0]; }
308 const Type&
x()
const {
return this->data_[0]; }
310 Type&
y() {
return this->data_[1]; }
312 const Type&
y()
const {
return this->data_[1]; }
325 template <
int M,
int N,
class T>
328 for (
int i = 0; i < M; ++i) {
329 for (
int j = 0; j < N; ++j) {
330 trans[j][i] = m[i][j];
340 template <
int R,
class T>
343 for (
int i = 0; i < R; ++i) {
344 sum += m1[i][0] * m2[i][0];
355 template <
int M,
int N,
int P,
int Q,
class T>
358 throw std::runtime_error(
359 "Matrices don't have the right dimensions for multiplication");
364 for (
int i = 0; i < M; ++i) {
365 for (
int j = 0; j < Q; ++j) {
Matrix< M, N, T > operator/(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.hpp:245
const Type & y() const
Definition: matrix.hpp:312
Matrix< M, N, T > operator*(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.hpp:229
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:98
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:101
2D Vector class.
Definition: matrix.hpp:294
bool operator==(const Matrix< M, N, T > &lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:253
std::vector< Type >::iterator end()
iterator support for end
Definition: matrix.hpp:130
Matrix< Rows, 1, Type > getCol(int col) const
Definition: matrix.hpp:118
Type & operator[](int col)
Definition: matrix.hpp:274
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:90
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:356
const Type & operator[](int col) const
Definition: matrix.hpp:68
Type & y()
Definition: matrix.hpp:310
detail::Row< Rows, Cols, Type > operator[](int row) const
Definition: matrix.hpp:157
Definition: matrix.hpp:54
Matrix< Rows, Cols, Type > * parent_
Definition: matrix.hpp:56
int index_
Definition: matrix.hpp:57
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:326
std::string toString() const override override override
prints out the matrix, but can also be implemented by other classes to print data differently ...
Definition: matrix.hpp:278
Matrix< Rows, Cols, Type > & operator-=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.hpp:173
Type & x()
Definition: matrix.hpp:306
Matrix< M, N, T > operator+(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:185
Definition: matrix.hpp:266
Matrix< M, N, T > operator-(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:191
Matrix< Rows, Cols, Type > & operator+=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.hpp:163
Type & operator[](int col)
Definition: matrix.hpp:63
Base Matrix class used by other similar classes.
Definition: matrix.hpp:34
virtual std::string toString() const
prints out the matrix, but can also be implemented by other classes to print data differently ...
Definition: matrix.hpp:135
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:341
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:109
const Type & x() const
Definition: matrix.hpp:308
Templated matrix class.
Definition: camera2d.hpp:17
std::vector< Type >::iterator begin()
iterator support for begin
Definition: matrix.hpp:127
detail::Row< Rows, Cols, Type > operator[](int row)
Definition: matrix.hpp:153
const Type & operator[](int col) const
Definition: matrix.hpp:276