-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix2d.hpp
203 lines (186 loc) · 6.39 KB
/
Matrix2d.hpp
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
#ifndef _MATRIX_HPP_
#define _MATRIX_HPP_
#include <iostream>
#include <cstring>
#include <cassert>
/*
*
*/
template <typename T>
class Matrix2d;
template <typename T>
void Matrix2dMultiply(const Matrix2d<T> &op1,const Matrix2d<T> &op2, Matrix2d<T> &Result);
template <typename T>
std::ostream& operator << (std::ostream &OutStream, const Matrix2d<T> &MatrixObj);
template <typename T>
class Matrix2d
{
public:
Matrix2d();
Matrix2d(int iColumn, int iRow);
Matrix2d(int iColumn, int iRow, const T *pData);
//Matrix2d(const Matrix2d<T>& m2d);
virtual ~Matrix2d();
virtual inline T * getMatrix2dData(){ return const_cast<T *>(this->m_pData); };
virtual inline int getMatrix2dRow(){ return this->m_iRow; };
virtual inline int getMatrix2dColumn(){ return this->m_iColumn; };
virtual inline void setMatrix2dData(T * pData){ this->m_pData = pData; };
virtual inline void setMatrix2dRow(int iRow){ this->m_iRow = iRow; };
virtual inline void setMatrix2dColumn(int iColumn){ this->m_iColumn = iColumn;};
virtual Matrix2d<T>& operator +(Matrix2d<T> &op);
virtual Matrix2d<T>& operator -(Matrix2d<T> &op);
virtual Matrix2d<T>& operator =(Matrix2d<T> &op);
virtual Matrix2d<T>* operator *(Matrix2d<T> &op);
virtual Matrix2d<T>& operator *(const T &op);
virtual Matrix2d<T>& operator ~();
friend void Matrix2dMultiply<T>(const Matrix2d<T> &op1,const Matrix2d<T> &op2, Matrix2d<T> &Result);
friend std::ostream& operator << <T>(std::ostream &OutStream, const Matrix2d<T> &MatrixObj);
private:
void initAttributes();
private:
const T *m_pData;
int m_iRow;
int m_iColumn;
};
template <typename T> Matrix2d<T>::Matrix2d()
{
initAttributes();
}
template <typename T> void Matrix2d<T>::initAttributes()
{
this->m_iColumn = 0;
this->m_iRow = 0;
this->m_pData = NULL;
}
template <typename T> Matrix2d<T>::Matrix2d(int iColumn, int iRow)
{
assert(iColumn && iRow);
this->m_iRow = iRow;
this->m_iColumn = iColumn;
this->m_pData = new T[iRow*iColumn];
memset(const_cast<T *>(this->m_pData), 0, sizeof(T)*this->m_iColumn*this->m_iRow);
}
template <typename T> Matrix2d<T>::Matrix2d(int iColumn, int iRow, const T *pData)
{
assert(iColumn && iRow && pData != NULL);
this->m_iRow = iRow;
this->m_iColumn = iColumn;
this->m_pData = new T[this->m_iColumn*this->m_iRow];
memcpy(const_cast<T *>(this->m_pData), pData, sizeof(T)*this->m_iColumn*this->m_iRow);
}
/*
template <typename T> Matrix2d<T>::Matrix2d(const Matrix2d<T>& m2d)
{
this->m_iRow = m2d.m_iRow;
this->m_iColumn = m2d.m_iColumn;
if(this->m_pData == NULL){
this->m_pData = new T[m2d.m_iRow*m2d.m_iColumn];
}
memcpy(const_cast<T *>(this->m_pData), m2d.m_pData, m2d.m_iRow*m2d.m_iColumn*sizeof(T));
}
*/
template <typename T> Matrix2d<T>::~Matrix2d()
{
if(this->m_pData){
delete[] this->m_pData;
this->m_pData = NULL;
}
}
template <typename T> Matrix2d<T>& Matrix2d<T>::operator +(Matrix2d<T> &op)
{
assert(op.m_iRow == this->m_iRow && op.m_iColumn == this->m_iColumn);
for(int i = 0; i < this->m_iRow; i++){
for(int j = 0; j < this->m_iColumn; j++){
*(const_cast<T *>(this->m_pData) + i*this->m_iColumn + j) += (*(op.m_pData + i*op.m_iColumn + j));
}
}
return (*this);
}
template <typename T> Matrix2d<T>& Matrix2d<T>::operator -(Matrix2d<T> &op)
{
assert(op.m_iRow == this->m_iRow && op.m_iColumn == this->m_iColumn);
for(int i = 0; i < this->m_iRow; i++){
for(int j = 0; j < this->m_iColumn; j++){
*(const_cast<T *>(this->m_pData) + i*this->m_iColumn + j) -= *(op.m_pData + i*op.m_iColumn + j);
}
}
return (*this);
}
template <typename T> Matrix2d<T>& Matrix2d<T>::operator =(Matrix2d<T> &op)
{
assert(op.m_iRow == this->m_iRow && op.m_iColumn == this->m_iColumn);
for(int i = 0; i < this->m_iRow; i++){
for(int j = 0; j < this->m_iColumn; j++){
*(const_cast<T *>(this->m_pData) + i*this->m_iColumn + j) = *(op.m_pData + i*op.m_iColumn + j);
}
}
return (*this);
}
template <typename T> Matrix2d<T>* Matrix2d<T>::operator *(Matrix2d<T> &op)
{
assert( this->m_iColumn == op.m_iRow);
Matrix2d<T> * pMatrix2dResult = new Matrix2d<T>(op.m_iColumn, this->m_iRow);
for(int i = 0, m = 0; i < pMatrix2dResult->m_iRow; i++, m++){
for(int j = 0; j < pMatrix2dResult->m_iColumn; j++){
for(int k = 0; k < this->m_iColumn; k++){
*(const_cast<T*>(pMatrix2dResult->m_pData) + i*pMatrix2dResult->m_iColumn + j) += (*(this->m_pData + i*this->m_iColumn + k)) \
* (*(op.m_pData + k*op.m_iColumn + j));
}
}
}
return (pMatrix2dResult);
}
template <typename T> Matrix2d<T>& Matrix2d<T>::operator *(const T &op)
{
for(int i = 0; i < this->m_iRow; i++){
for(int j = 0; j < this->m_iColumn; j++){
*(const_cast<T *>(this->m_pData) + i*this->m_iColumn + j) *= op;
}
}
return (*this);
}
template <typename T> Matrix2d<T>& Matrix2d<T>::operator ~()
{
assert(this->m_iRow && this->m_iColumn && this->m_pData != NULL);
T * pDataTmp = new T[this->m_iRow * this->m_iColumn];
for(int i = 0; i < this->m_iColumn; i++){
for(int j = 0; j < this->m_iRow; j++){
*(pDataTmp + i*this->m_iRow + j) = *(const_cast<T *>(this->m_pData) + j*this->m_iColumn + i);
}
}
memcpy(const_cast<T *>(this->m_pData), pDataTmp, sizeof(T)* this->m_iColumn * this->m_iRow);
int iTmp = this->m_iColumn;
this->setMatrix2dColumn(this->m_iRow);
this->setMatrix2dRow(iTmp);
if(pDataTmp){
delete[] pDataTmp;
pDataTmp = NULL;
}
return (*this);
}
template <typename T> std::ostream& operator << (std::ostream &OutStream, const Matrix2d<T> &MatrixObj)
{
Matrix2d<T> *pMatrixTmp =const_cast<Matrix2d<T> *>( &MatrixObj );
for(int i = 0; i < pMatrixTmp->getMatrix2dRow(); i++){
OutStream << "|"<< " ";
for(int j = 0; j < pMatrixTmp->getMatrix2dColumn(); j++){
OutStream << *(pMatrixTmp->getMatrix2dData() + i*pMatrixTmp->getMatrix2dColumn() + j)<< " ";
}
OutStream << "|" << std::endl;
}
return OutStream;
}
template <typename T> void Matrix2dMultiply(const Matrix2d<T>& op1, const Matrix2d<T>& op2, Matrix2d<T>& Result)
{
assert( op1.m_iColumn == op2.m_iRow);
for(int i = 0, m = 0; i < Result.m_iRow; i++, m++){
for(int j = 0; j < Result.m_iColumn; j++){
for(int k = 0; k < op1.m_iColumn; k++){
*(const_cast<T*>(Result.m_pData) + i*Result.m_iColumn + j) += (*(op1.m_pData + i*op1.m_iColumn + k)) \
* (*(op2.m_pData + k*op2.m_iColumn + j));
}
}
}
return;
}
#endif /*_MATRIX_HPP_*/