aboutsummaryrefslogtreecommitdiffstats
path: root/include/YAGE/Math/matrix.hpp
blob: 47bb6d60d49ab68175bd50c19ccade4a65c684d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef YAGE_MATH_MATRIX_HPP
#define YAGE_MATH_MATRIX_HPP

#include <algorithm>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

namespace yage
{

template<int Rows, int Cols, class Type> class Matrix;
template<int Rows, class Type> class Vector;

namespace detail
{

template<int Rows, int Cols, class Type> class Row
{
private:
	Matrix<Rows, Cols, Type> *parent_;
	int index_;

public:
	Row<Rows, Cols, Type>(Matrix<Rows, Cols, Type> *parent, int index) :
		parent_(parent), index_(index)
	{}

	Type& operator[](int col)
	{
		return parent_->data_[index_*Cols+col];
	}

	const Type& operator[](int col) const
	{
		return parent_->data_[index_*Cols+col];
	}
};

} // detail

template<int Rows=4, int Cols=4, class Type=double> class Matrix
{
	friend class detail::Row<Rows, Cols, Type>;
protected:
	std::vector<Type> data_;

public:
	Matrix<Rows, Cols, Type>() : data_(Rows*Cols) {}
	
	int rowSize() const
	{
		return Rows;
	}

	int colSize() const
	{
		return Cols;
	}

	Matrix<1, Cols, Type> getRow(int row) const
	{
		Matrix<1, Cols, Type> rowMatrix;
		for(int i=0; i<Cols; ++i)
		{
			rowMatrix[0][i]=data_[row][i];
		}
		return rowMatrix;
	}

	Matrix<Rows, 1, Type> getCol(int col) const
	{
		Matrix<Rows, 1, Type> colMatrix;
		for(int i=0; i<Rows; ++i)
		{
			colMatrix[i][0]=data_[i][col];
		}
		return colMatrix;
	}

	typename std::vector<Type>::iterator begin()
	{
		return data_.begin();
	}

	typename std::vector<Type>::iterator end()
	{
		return data_.end();
	}

	virtual std::string toString() const
	{
		std::stringstream ss;
		ss<<'[';
		for(int i=0; i<Rows-1; ++i)
		{
			ss<<'[';
			for(int j=0; j<Cols-1; ++j)
			{
				ss<<data_[i*Cols+j]<<' ';
			}
			ss<<data_[(Rows-1)*Cols+Cols-1]<<"],";
		}
		ss<<'[';
		for(int j=0; j<Cols-1; ++j)
		{
			ss<<data_[(Rows-1)*Cols+j]<<' ';
		}
		ss<<data_[(Rows-1)*Cols+Cols-1]<<"]]";
		return ss.str();
	}

	detail::Row<Rows, Cols, Type> operator[](int row)
	{
		return detail::Row<Rows, Cols, Type>(this, row);
	}

	detail::Row<Rows, Cols, Type> operator[](int row) const
	{
		// TODO got to fix this
		return detail::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type>*)this, row);
	}

	Matrix<Rows, Cols, Type>& operator=(const Matrix<Rows, Cols, Type> &other)
	{
		if(this!=&other)
		{
			data_=other.data_;
		}
		return *this;
	}

	Matrix<Rows, Cols, Type>& operator+=(const Matrix<Rows, Cols, Type> &rhs)
	{
		std::vector<Type> out;
		out.reserve(data_.size());
		std::transform(data_.begin(), data_.end(), rhs.data_.begin(), std::back_inserter(out),
		               [] (Type a, Type b) {
			               return a+b;
		               });
		data_=std::move(out);
		return *this;
	}

	Matrix<Rows, Cols, Type>& operator-=(const Matrix<Rows, Cols, Type> &rhs)
	{
		std::vector<Type> out;
		out.reserve(data_.size());
		std::transform(data_.begin(), data_.end(), rhs.begin(), std::back_inserter(out),
		               [] (Type a, Type b) {
			               return a-b;
		               });
		data_=std::move(out);
		return *this;
	}


};

template<int M, int N, class T>
Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
{
	lhs+=rhs;
	return lhs;
}

template<int M, int N, class T>
Matrix<M, N, T> operator-(Matrix<M, N, T> lhs, const Matrix<M, N, T> &rhs)
{
	lhs-=rhs;
	return lhs;
}

template<int M, int N, class T>
Matrix<M, N, T> operator+(Matrix<M, N, T> lhs, const T &rhs)
{
	for(auto &data : lhs)
	{
		data+=rhs;
	}
	return lhs;
}

template<int M, int N, class T>
Matrix<M, N, T> operator+(const T &lhs, Matrix<M, N, T> rhs)
{
	for(auto &data : rhs)
	{
		data+=lhs;
	}
	return rhs;
}

template<int M, int N, class T>
std::ostream& operator<<(std::ostream &os, const Matrix<M, N, T> &mat)
{
	return os<<mat.toString();
}

template<int Rows=2, class Type=double> class Vector : public Matrix<Rows, 1, Type>
{
public:
	Vector<Rows, Type>() : Matrix<Rows, 1, Type>() {}
	explicit Vector<Rows, Type>(const Matrix<Rows, 1, Type> &other) : Matrix<Rows, 1, Type>(other) {}

	Type& operator[](int col)
	{
		return this->data_[col];
	}

	const Type& operator[](int col) const
	{
		return this->data_[col];
	}

	
};

template<class Type=double> class Vector2 : public Vector<2, Type>
{
public:
	Vector2<Type>() : Vector<2, Type>() {}

	Type& x()
	{
		return this->data_[0];
	}

	const Type& x() const
	{
		return this->data_[0];
	}
	
	Type& y()
	{
		return this->data_[1];
	}

	const Type& y() const
	{
		return this->data_[1];
	}	
};

typedef Vector2<double> Vector2d;

namespace matrix
{

template<int M, int N, class T> Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
{
	Matrix<N, M, T> trans;
	for(int i=0; i<M; ++i)
	{
		for(int j=0; j<N; ++j)
		{
			trans[j][i]=m[i][j];
		}
	}
	return trans;
}

template<int R, class T> T dot(const Vector<R, T> &v1, const Vector<R, T> &v2)
{
	T sum=0;
	for(int i=0; i<R; ++i)
	{
		sum += v1[i]*v2[i];
	}
	return sum;
}

template<int M, int N, int P, int Q, class T>
Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2)
{
	if(N!=P)
	{
		throw std::runtime_error("Matrices don't have the right dimensions for multiplication");
	}
	
	Matrix<M, Q, T> res;

	for(int i=0; i<M; ++i)
	{
		for(int j=0; j<Q; ++j)
		{
			// int sum=0;
		}
	}

	return res;
}

} // matrix

} // yage

#endif