19 #ifndef YAGE_MATH_MATRIX_HPP 20 #define YAGE_MATH_MATRIX_HPP 31 template <
int Rows,
int Cols,
class Type>
51 template <
int Rows,
int Cols,
class Type>
59 : parent_(parent), index_(index) {}
61 Type& operator[](
int col) {
63 return parent_->
data_[index_ * Cols + col];
66 const Type& operator[](
int col)
const {
67 return parent_->
data_[index_ * Cols + col];
81 template <
int Rows = 4,
int Cols = 4,
class Type =
double>
84 friend class detail::Row<Rows, Cols, Type>;
109 for (
int i = 0; i < Cols; ++i) {
110 rowMatrix[0][i] = data_[row][i];
118 for (
int i = 0; i < Rows; ++i) {
119 colMatrix[i][0] = data_[i][col];
125 typename std::vector<Type>::iterator begin() {
return data_.begin(); }
128 typename std::vector<Type>::iterator end() {
return data_.end(); }
132 virtual std::string toString()
const {
133 std::stringstream ss;
135 for (
int i = 0; i < Rows - 1; ++i) {
137 for (
int j = 0; j < Cols - 1; ++j) {
138 ss << data_[i * Cols + j] <<
' ';
140 ss << data_[(Rows - 1) * Cols + Cols - 1] <<
"],";
143 for (
int j = 0; j < Cols - 1; ++j) {
144 ss << data_[(Rows - 1) * Cols + j] <<
' ';
146 ss << data_[(Rows - 1) * Cols + Cols - 1] <<
"]]";
150 detail::Row<Rows, Cols, Type> operator[](
int row) {
151 return detail::Row<Rows, Cols, Type>(
this, row);
154 detail::Row<Rows, Cols, Type> operator[](
int row)
const {
161 std::vector<Type> out;
162 out.reserve(data_.size());
163 std::transform(data_.begin(), data_.end(), rhs.
data_.begin(),
164 std::back_inserter(out),
165 [](Type a, Type b) {
return a + b; });
166 data_ = std::move(out);
171 std::vector<Type> out;
172 out.reserve(data_.size());
173 std::transform(data_.begin(), data_.end(), rhs.begin(),
174 std::back_inserter(out),
175 [](Type a, Type b) {
return a - b; });
176 data_ = std::move(out);
181 template <
int M,
int N,
class T>
187 template <
int M,
int N,
class T>
193 template <
int M,
int N,
class T>
195 for (
auto& data : lhs) {
201 template <
int M,
int N,
class T>
203 for (
auto& data : rhs) {
209 template <
int M,
int N,
class T>
211 for (
auto& data : lhs) {
217 template <
int M,
int N,
class T>
219 for (
auto& data : rhs) {
225 template <
int M,
int N,
class T>
227 for (
auto& data : lhs) {
233 template <
int M,
int N,
class T>
235 for (
auto& data : rhs) {
241 template <
int M,
int N,
class T>
243 for (
auto& data : lhs) {
249 template <
int M,
int N,
class T>
251 for (
int i = 0; i < M; ++i)
252 for (
int j = 0; j < N; ++j)
253 if (lhs[i][j] != rhs[i][j])
return false;
257 template <
int M,
int N,
class T>
258 std::ostream& operator<<(std::ostream& os, const Matrix<M, N, T>& mat) {
259 return os << mat.toString();
262 template <
int Rows = 2,
class Type =
double>
263 class Vector :
public Matrix<Rows, 1, Type> {
268 Vector<Rows, Type>(
const std::vector<Type>& data)
271 Type& operator[](
int col) {
return this->data_[col]; }
273 const Type& operator[](
int col)
const {
return this->data_[col]; }
275 virtual std::string toString()
const {
276 std::stringstream ss;
278 for (std::size_t i = 0; i < this->data_.size() - 1; ++i) {
279 ss << this->data_[i] <<
" ";
281 ss << this->data_[this->data_.size() - 1] <<
"]";
290 template <
class Type =
double>
294 Vector2<Type>(
const std::vector<Type>& data) : Vector<2, Type>(data) {}
303 Type& x() {
return this->data_[0]; }
305 const Type& x()
const {
return this->data_[0]; }
307 Type& y() {
return this->data_[1]; }
309 const Type& y()
const {
return this->data_[1]; }
322 template <
int M,
int N,
class T>
325 for (
int i = 0; i < M; ++i) {
326 for (
int j = 0; j < N; ++j) {
327 trans[j][i] = m[i][j];
337 template <
int R,
class T>
340 for (
int i = 0; i < R; ++i) {
341 sum += m1[i][0] * m2[i][0];
352 template <
int M,
int N,
int P,
int Q,
class T>
355 throw std::runtime_error(
356 "Matrices don't have the right dimensions for multiplication");
361 for (
int i = 0; i < M; ++i) {
362 for (
int j = 0; j < Q; ++j) {
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:96
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:99
2D Vector class.
Definition: matrix.hpp:291
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:88
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:353
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:323
Base Matrix class used by other similar classes.
Definition: matrix.hpp:32
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:338
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:107
Definition: camera2d.hpp:17