21 #ifndef YAGE_MATH_MATRIX_HPP 22 #define YAGE_MATH_MATRIX_HPP 34 template <
int Rows,
int Cols,
class Type>
class Matrix;
54 template <
int Rows,
int Cols,
class Type>
class Row 69 return parent_->
data_[index_ * Cols + col];
74 return parent_->
data_[index_ * Cols + col];
88 template <
int Rows = 4,
int Cols = 4,
class Type =
double>
class Matrix 117 for (
int i = 0; i < Cols; ++i) {
118 rowMatrix[0][i] = data_[row][i];
127 for (
int i = 0; i < Rows; ++i) {
128 colMatrix[i][0] = data_[i][col];
134 typename std::vector<Type>::iterator
begin() {
return data_.begin(); }
137 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] <<
"]]";
175 std::vector<Type> out;
176 out.reserve(data_.size());
177 std::transform(data_.begin(), data_.end(), rhs.
data_.begin(),
178 std::back_inserter(out),
179 [](Type a, Type b) {
return a + b; });
180 data_ = std::move(out);
186 std::vector<Type> out;
187 out.reserve(data_.size());
188 std::transform(data_.begin(), data_.end(), rhs.
begin(),
189 std::back_inserter(out),
190 [](Type a, Type b) {
return a - b; });
191 data_ = std::move(out);
196 template <
int M,
int N,
class T>
203 template <
int M,
int N,
class T>
210 template <
int M,
int N,
class T>
213 for (
auto &data : lhs) {
219 template <
int M,
int N,
class T>
222 for (
auto &data : rhs) {
228 template <
int M,
int N,
class T>
231 for (
auto &data : lhs) {
237 template <
int M,
int N,
class T>
240 for (
auto &data : rhs) {
246 template <
int M,
int N,
class T>
249 for (
auto &data : lhs) {
255 template <
int M,
int N,
class T>
258 for (
auto &data : rhs) {
264 template <
int M,
int N,
class T>
267 for (
auto &data : lhs) {
273 template <
int M,
int N,
class T>
276 for (
int i = 0; i < M; ++i) {
277 for (
int j = 0; j < N; ++j) {
278 if (lhs[i][j] != rhs[i][j]) {
286 template <
int M,
int N,
class T>
287 std::ostream &operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
289 return os << mat.toString();
292 template <
int Rows = 2,
class Type =
double>
308 const Type &
operator[](
int col)
const {
return this->data_[col]; }
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] <<
"]";
340 Type &
x() {
return this->data_[0]; }
342 const Type &
x()
const {
return this->data_[0]; }
344 Type &
y() {
return this->data_[1]; }
346 const Type &
y()
const {
return this->data_[1]; }
360 template <
int M,
int N,
class T>
364 for (
int i = 0; i < M; ++i) {
365 for (
int j = 0; j < N; ++j) {
366 trans[j][i] = m[i][j];
376 template <
int R,
class T>
380 for (
int i = 0; i < R; ++i) {
381 sum += m1[i][0] * m2[i][0];
392 template <
int M,
int N,
int P,
int Q,
class T>
396 throw std::runtime_error(
397 "Matrices don't have the right dimensions for multiplication");
402 for (
int i = 0; i < M; ++i) {
403 for (
int j = 0; j < Q; ++j) {
Matrix< M, N, T > operator/(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.hpp:265
const Type & y() const
Definition: matrix.hpp:346
Matrix< M, N, T > operator*(Matrix< M, N, T > lhs, const T &rhs)
Definition: matrix.hpp:247
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:103
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:106
2D Vector class.
Definition: matrix.hpp:326
bool operator==(const Matrix< M, N, T > &lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:274
std::vector< Type >::iterator end()
iterator support for end
Definition: matrix.hpp:137
Matrix< Rows, 1, Type > getCol(int col) const
Definition: matrix.hpp:124
Type & operator[](int col)
Definition: matrix.hpp:306
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:95
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:393
const Type & operator[](int col) const
Definition: matrix.hpp:72
Type & y()
Definition: matrix.hpp:344
detail::Row< Rows, Cols, Type > operator[](int row) const
Definition: matrix.hpp:166
Definition: matrix.hpp:54
Matrix< Rows, Cols, Type > * parent_
Definition: matrix.hpp:57
int index_
Definition: matrix.hpp:58
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:361
Matrix< Rows, Cols, Type > & operator-=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.hpp:184
Type & x()
Definition: matrix.hpp:340
Matrix< M, N, T > operator+(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:197
Definition: matrix.hpp:293
Matrix< M, N, T > operator-(Matrix< M, N, T > lhs, const Matrix< M, N, T > &rhs)
Definition: matrix.hpp:204
Matrix< Rows, Cols, Type > & operator+=(const Matrix< Rows, Cols, Type > &rhs)
Definition: matrix.hpp:173
Type & operator[](int col)
Definition: matrix.hpp:66
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:142
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:377
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:114
std::string toString() const override
prints out the matrix, but can also be implemented by other classes to print data differently ...
Definition: matrix.hpp:310
const Type & x() const
Definition: matrix.hpp:342
Templated matrix class.
Definition: camera2d.hpp:17
std::vector< Type >::iterator begin()
iterator support for begin
Definition: matrix.hpp:134
detail::Row< Rows, Cols, Type > operator[](int row)
Definition: matrix.hpp:161
const Type & operator[](int col) const
Definition: matrix.hpp:308