21 #ifndef YAGE_MATH_MATRIX_HPP 22 #define YAGE_MATH_MATRIX_HPP 35 template<
int Rows,
int Cols,
class Type>
class Matrix;
55 template<
int Rows,
int Cols,
class Type>
class Row
63 parent_(parent), index_(index)
66 Type& operator[](
int col)
69 return parent_->
data_[index_*Cols+col];
72 const Type& operator[](
int col)
const 74 return parent_->
data_[index_*Cols+col];
88 template<
int Rows=4,
int Cols=4,
class Type=
double>
class Matrix 91 friend class detail::Row<Rows, Cols, Type>;
121 for(
int i=0; i<Cols; ++i)
123 rowMatrix[0][i]=data_[row][i];
132 for(
int i=0; i<Rows; ++i)
134 colMatrix[i][0]=data_[i][col];
140 typename std::vector<Type>::iterator begin()
142 return data_.begin();
146 typename std::vector<Type>::iterator end()
153 virtual std::string toString()
const 155 std::stringstream ss;
157 for(
int i=0; i<Rows-1; ++i)
160 for(
int j=0; j<Cols-1; ++j)
162 ss<<data_[i*Cols+j]<<
' ';
164 ss<<data_[(Rows-1)*Cols+Cols-1]<<
"],";
167 for(
int j=0; j<Cols-1; ++j)
169 ss<<data_[(Rows-1)*Cols+j]<<
' ';
171 ss<<data_[(Rows-1)*Cols+Cols-1]<<
"]]";
175 detail::Row<Rows, Cols, Type> operator[](
int row)
177 return detail::Row<Rows, Cols, Type>(
this, row);
180 detail::Row<Rows, Cols, Type> operator[](
int row)
const 197 std::vector<Type> out;
198 out.reserve(data_.size());
199 std::transform(data_.begin(), data_.end(), rhs.
data_.begin(), std::back_inserter(out),
200 [] (Type a, Type b) {
203 data_=std::move(out);
209 std::vector<Type> out;
210 out.reserve(data_.size());
211 std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out),
212 [] (Type a, Type b) {
215 data_=std::move(out);
220 template<
int M,
int N,
class T>
227 template<
int M,
int N,
class T>
234 template<
int M,
int N,
class T>
237 for(
auto &data : lhs)
244 template<
int M,
int N,
class T>
247 for(
auto &data : rhs)
254 template<
int M,
int N,
class T>
257 for(
auto &data : lhs)
264 template<
int M,
int N,
class T>
267 for(
auto &data : rhs)
274 template<
int M,
int N,
class T>
277 for(
auto &data : lhs)
284 template<
int M,
int N,
class T>
287 for(
auto &data : rhs)
294 template<
int M,
int N,
class T>
297 for(
auto &data : lhs)
304 template<
int M,
int N,
class T>
307 for(
int i=0; i<M; ++i)
308 for(
int j=0; j<N; ++j)
309 if(lhs[i][j]!=rhs[i][j])
314 template<
int M,
int N,
class T>
315 std::ostream& operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
317 return os<<mat.toString();
320 template<
int Rows=2,
class Type=
double>
class Vector :
public Matrix<Rows, 1, Type>
326 Type& operator[](
int col)
328 return this->data_[col];
331 const Type& operator[](
int col)
const 333 return this->data_[col];
336 virtual std::string toString()
const 338 std::stringstream ss;
340 for(std::size_t i=0; i<this->data_.size()-1; ++i)
342 ss<<this->data_[i]<<
" ";
344 ss<<this->data_[this->data_.size()-1]<<
"]";
353 template<
class Type=
double>
class Vector2 :
public Vector<2, Type>
368 return this->data_[0];
371 const Type& x()
const 373 return this->data_[0];
378 return this->data_[1];
381 const Type& y()
const 383 return this->data_[1];
401 for(
int i=0; i<M; ++i)
403 for(
int j=0; j<N; ++j)
418 for(
int i=0; i<R; ++i)
420 sum += m1[i][0]*m2[i][0];
431 template<
int M,
int N,
int P,
int Q,
class T>
436 throw std::runtime_error(
"Matrices don't have the right dimensions for multiplication");
441 for(
int i=0; i<M; ++i)
443 for(
int j=0; j<Q; ++j)
int rowSize() const
Returns the row size of the Matrix.
Definition: matrix.hpp:101
int colSize() const
Returns the column size of the Matrixxs.
Definition: matrix.hpp:107
2D Vector class.
Definition: matrix.hpp:353
std::vector< Type > data_
Vector containing the data of the matrix.
Definition: matrix.hpp:94
Matrix< M, Q, T > multiply(const Matrix< M, N, T > &m1, const Matrix< P, Q, T > &m2)
Multiplies two matrices together.
Definition: matrix.hpp:432
Matrix< N, M, T > transpose(const Matrix< M, N, T > &m)
Transposes a matrix and returns the result.
Definition: matrix.hpp:398
Base Matrix class used by other similar classes.
Definition: matrix.hpp:35
T dot(const Matrix< R, 1, T > &m1, const Matrix< R, 1, T > &m2)
Returns the dot product between two vectors.
Definition: matrix.hpp:415
Matrix< 1, Cols, Type > getRow(int row) const
Return the row specified row as a Matrix with only one row.
Definition: matrix.hpp:118
Definition: camera2d.hpp:17