一个矩阵类模板

  1 #ifndef MATRIX_H
  2 #define MATRIX_H
  3 
  4 
  5 #include <cstring>
  6 #include <iostream>
  7 
  8 template<typename T>
  9 class Matrix
 10 {
 11 private:
 12 	T** elem;
 13 	int size_x, size_y;
 14 public:
 15 	Matrix(int, int);
 16 	Matrix(Matrix<T>&);
 17 	Matrix(Matrix<T>*);
 18 	~Matrix()
 19 	{
 20 		for (int i = 0; i < size_x; i++)
 21 			delete elem[i];
 22 		delete elem;
 23 	}
 24 	void free_memory();
 25 	void allot_memory(int _x, int _y);
 26 
 27 	T* operator [](int);
 28 	Matrix<T> operator =(Matrix<T>);
 29 	Matrix<T> operator +(Matrix<T>);
 30 	Matrix<T> operator +=(Matrix<T>);
 31 	Matrix<T> operator -(Matrix<T>);
 32 	Matrix<T> operator -=(Matrix<T>);
 33 	Matrix<T> operator -();
 34 	template<typename _tp>
 35 	Matrix<T> operator *(_tp);
 36 	template<typename _tp>
 37 	Matrix<T> operator *=(_tp);
 38 	Matrix<T> operator *(Matrix<T>);
 39 	Matrix<T> operator *=(Matrix<T>);
 40 
 41 	int getX();
 42 	int getY();
 43 	T at(int, int);
 44 	void set(int, int, T);
 45 	void add(int, int, T);
 46 	void fill(T);
 47 	Matrix<T> power(int);
 48 	inline int max(int a, int b)
 49 	{
 50 		return a > b ? a : b;
 51 	}
 52 
 53 	static Matrix<T> indentity(int);
 54 
 55 	void print();
 56 };
 57 
 58 template<typename T>
 59 Matrix<T>::Matrix(int _x = 1, int _y = 1)
 60 {
 61 	this->allot_memory(_x, _y);
 62 	this->fill(0);
 63 }
 64 
 65 template<typename T>
 66 Matrix<T>::Matrix(Matrix<T> &_mtx)
 67 {
 68 	this->allot_memory(_mtx.getX(), _mtx.getY());
 69 	for (int i = 0; i < size_x; i++)
 70 		for (int j = 0; j < size_y; j++)
 71 			elem[i][j] = _mtx[i][j];
 72 }
 73 
 74 template<typename T>
 75 Matrix<T>::Matrix(Matrix<T> *_ptr)
 76 {
 77 	this->allot_memory(_ptr->getX(), _ptr->getY());
 78 	for (int i = 0; i < size_x; i++)
 79 		for (int j = 0; j < size_y; j++)
 80 			elem[i][j] = _ptr->at(i, j);
 81 }
 82 
 83 template<typename T>
 84 void Matrix<T>::free_memory()
 85 {
 86 	for (int i = 0; i < size_x; i++)
 87 		delete elem[i];
 88 	delete elem;
 89 	elem = NULL;
 90 }
 91 
 92 template<typename T>
 93 void Matrix<T>::allot_memory(int _x, int _y)
 94 {
 95 	size_x = _x, size_y = _y;
 96 	elem = new T*[_x];
 97 	for (int i = 0; i < size_x; i++)
 98 		elem[i] = new T[_y];
 99 }
100 
101 template<typename T>
102 T* Matrix<T>::operator [](int _index)
103 {
104 	return elem[_index];
105 }
106 
107 template<typename T>
108 Matrix<T> Matrix<T>::operator =(Matrix<T> _mtx)
109 {
110 	this->free_memory();
111 	this->allot_memory(_mtx.getX(), _mtx.getY());
112 	for (int i = 0; i < size_x; i++)
113 		for (int j = 0; j < size_y; j++)
114 			elem[i][j] = _mtx[i][j];
115 	return *this;
116 }
117 
118 template<typename T>
119 Matrix<T> Matrix<T>::operator +(Matrix<T> _mtx)
120 {
121 	int _x = max(size_x, _mtx.getX()), _y = max(size_y, _mtx.getY());
122 	Matrix<T> res(_x, _y);
123 	for (int i = 0; i < size_x; i++)
124 		for (int j = 0; j < size_y; j++)
125 			res.set(i, j, elem[i][j]);
126 	for (int i = 0; i < _mtx.getX(); i++)
127 		for (int j = 0; j < _mtx.getY(); j++)
128 			res.add(i, j, _mtx[i][j]);
129 	return res;
130 }
131 
132 template<typename T>
133 Matrix<T> Matrix<T>::operator +=(Matrix<T> _mtx)
134 {
135 	Matrix<T> res(max(size_x, _mtx.getX()), max(size_y, _mtx.getY()));
136 	for (int i = 0; i < size_x; i++)
137 		for (int j = 0; j < size_y; j++)
138 			res.set(i, j, elem[i][j]);
139 	for (int i = _mtx.getX() - 1; i >= 0; i--)
140 		for (int j = _mtx.getY() - 1; j >= 0; j--)
141 			res.add(i, j, _mtx[i][j]);
142 	*this = res;
143 	return res;
144 }
145 
146 template<typename T>
147 Matrix<T> Matrix<T>::operator -(Matrix<T> _mtx)
148 {
149 	int _x = max(size_x, _mtx.getX()), _y = max(size_y, _mtx.getY());
150 	Matrix<T> res(_x, _y);
151 	for (int i = 0; i < size_x; i++)
152 		for (int j = 0; j < size_y; j++)
153 			res.set(i, j, elem[i][j]);
154 	for (int i = 0; i < _mtx.getX(); i++)
155 		for (int j = 0; j < _mtx.getY(); j++)
156 			res.add(i, j, -_mtx[i][j]);
157 	return res;
158 }
159 
160 template<typename T>
161 Matrix<T> Matrix<T>::operator-=(Matrix<T> _mtx)
162 {
163 	return *this - _mtx;
164 }
165 
166 template<typename T>
167 Matrix<T> Matrix<T>::operator -()
168 {
169 	Matrix<T> res(this);
170 	for (int i = res.getX() - 1; i >= 0; i--)
171 		for (int j = res.getY() - 1; j >= 0; j--)
172 			res.set(i, j, -res[i][j]);
173 }
174 
175 template<typename T>
176 template<typename _tp>
177 Matrix<T> Matrix<T>::operator *(_tp _t)
178 {
179 	Matrix<T> res(this);
180 	for (int i = res.getX() - 1; i >= 0; i--)
181 		for (int j = res.getY() - 1; j >= 0; j--)
182 			res.set(i, j, res.at(i, j) * _t);
183 	return res;
184 }
185 
186 template<typename T>
187 template<typename _tp>
188 Matrix<T> Matrix<T>::operator *=(_tp _t)
189 {
190 	for (int i = 0; i < size_x; i++)
191 		for (int j = 0; j < size_y; j++)
192 			elem[i][j] *= _t;
193 	return *this;
194 }
195 
196 template<typename T>
197 Matrix<T> Matrix<T>::operator *(Matrix<T> _mtx)
198 {
199 	Matrix<T> res(size_x, _mtx.getY());
200 	if (size_y != _mtx.size_x)
201 		return res;
202 	for (int i = 0; i < size_x; i++)
203 		for (int j = 0; j < _mtx.size_y; j++)
204 			for (int k = 0; k < size_y; k++)
205 				res.add(i, j, elem[i][k] * _mtx[k][j]);
206 	return res;
207 }
208 
209 template<typename T>
210 Matrix<T> Matrix<T>::operator *=(Matrix<T> _mtx)
211 {
212 	Matrix<T> res(size_x, _mtx.getY());
213 	if (size_y != _mtx.size_x)
214 		return res;
215 	for (int i = 0; i < size_x; i++)
216 		for (int j = 0; j < _mtx.size_y; j++)
217 			for (int k = 0; k < size_y; k++)
218 				res.add(i, j, elem[i][k] * _mtx[k][j]);
219 	*this = res;
220 	return res;
221 }
222 
223 template<typename T>
224 int Matrix<T>::getX()
225 {
226 	return size_x;
227 }
228 
229 template<typename T>
230 int Matrix<T>::getY()
231 {
232 	return size_y;
233 }
234 
235 template<typename T>
236 T Matrix<T>::at(int _x, int _y)
237 {
238 	return elem[_x][_y];
239 }
240 
241 template<typename T>
242 void Matrix<T>::set(int _x, int _y, T _val)
243 {
244 	elem[_x][_y] = _val;
245 }
246 
247 template<typename T>
248 void Matrix<T>::add(int _x, int _y, T _val)
249 {
250 	elem[_x][_y] += _val;
251 }
252 
253 template<typename T>
254 void Matrix<T>::fill(T _val)
255 {
256 	for (int i = 0; i < size_x; i++)
257 		for (int j = 0; j < size_y; j++)
258 			elem[i][j] = _val;
259 }
260 
261 template<typename T>
262 Matrix<T> Matrix<T>::power(int _t)
263 {
264 	Matrix<T> res(this->indentity(size_x));
265 	Matrix<T> base(this);
266 	while (_t)
267 	{
268 		if (_t & 1) res *= base;
269 		base *= base;
270 		_t >>= 1;
271 	}
272 	return res;
273 }
274 
275 template<typename T>
276 Matrix<T> Matrix<T>::indentity(int _size)
277 {
278 	Matrix<T> res(_size, _size);
279 	for (int i = 0; i < _size; i++)
280 		res.set(i, i, 1);
281 	return res;
282 }
283 
284 template<typename T>
285 void Matrix<T>::print()
286 {
287 	for (int i = 0; i < size_x; i++)
288 	{
289 		cout << elem[i][0];
290 		for (int j = 1; j < size_y; j++)
291 			cout << ' ' << elem[i][j];
292 		cout << endl;
293 	}
294 	return;
295 }
296 
297 
298 
299 #endif // !MATRIX_H

猜你喜欢

转载自www.cnblogs.com/Rhein-E/p/9486461.html