-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.h
executable file
·290 lines (231 loc) · 5.04 KB
/
Matrix.h
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
#include <iostream>
#include <fstream>
#include <cassert>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
// This is a copy program
template <class T>
class Matrix
{
private :
int row;
int col;
T *ptr;
public:
int getrow()
{
return row;
}
Matrix(int nrow ,int ncol ,T ini);//Constructor
~Matrix();//Destructor
Matrix(const Matrix &obj);// Copy constructor
Matrix(int nrow ,int ncol);// Assignment Constructor
Matrix& operator = (const Matrix& obj);//Assignment Operator
void display() const ;// Dislplay Function
Matrix& operator+=(const Matrix& obj);
Matrix& operator-=(const Matrix& obj);
const T& operator()(int r, int c) const;
T& operator()(int r, int c ) ;
Matrix operator+(const Matrix& obj) const;
Matrix operator-(const Matrix& obj) const;
Matrix& operator*=(const Matrix& obj);
Matrix operator*(const Matrix& obj);
bool operator==(const Matrix& obj);
bool operator!=(const Matrix& obj);
//template <class U>
//ostream& operator<<(ostream& os,const Matrix<U>& obj ) ;
};
template <class T>
const T& Matrix<T>::operator()(int r, int c) const
{
return(*(ptr + r * col + c ));
}
template <class T>
T& Matrix<T>:: operator()(int r, int c )
{
return (*(ptr +r * col + c));
}
template <class T>
Matrix<T>::Matrix(int nrow, int ncol ,T ini)
{
//cout<<" constructor created "<<endl;
row= nrow ;
col= ncol ;
ptr = new T [row*col];
for (int i =0;i < row*col ;++i)
*(ptr+ i)= ini ;
}
template <class T>
Matrix<T>::Matrix(int nrow, int ncol)
{
int i;
row= nrow;
col= ncol ;
ptr = new T [row * col ];
for (i =0;i < row*col ;++i)
*(ptr+ i)= 0 ;
}
template <class T>
Matrix<T>::~Matrix()
{
//cout<<" Destructor called"<<endl;
delete [] ptr;
}
template <class T>
Matrix<T>& Matrix<T>:: operator = (const Matrix& obj)
{
//cout<<"The Assignment copy constructor is created"<<endl;
if (this != &obj)
{
row= obj.row;
col = obj.col;
for (int i =0;i < row * col; ++i)
*(ptr+i)= *(obj.ptr + i);
}
return *this ;
}
template <class T>
Matrix<T>::Matrix(const Matrix &obj)
{
//cout<<" The copy constructor is created "<<endl;
row= obj.row ;
col = obj.col ;
ptr = new T [row*col];
for (int j =0;j<row*col ;++j)
*(ptr+j)= *(obj.ptr +j);
}
template <class T>
Matrix<T>& Matrix<T>::operator+=(const Matrix& obj)
{
//cout<<" Assignment addition operator is called"<<endl;
row=obj.row;
col=obj.col;
for (int i= 0;i <row*col;++i)
{
*(ptr +i)+= *(obj.ptr +i) ;
}
return(* this);
}
template <class T>
Matrix<T>& Matrix<T>::operator-=(const Matrix& obj)
{
//cout<<" Assignment subtraction operator is called "<<endl;
row=obj.row;
col=obj.col;
for(int i=0;i<row*col;++i)
{
*(ptr +i)-= *(obj.ptr +i);
}
return (*this);
}
template <class T>
Matrix<T> Matrix<T>::operator+(const Matrix& obj) const
{
//cout<<" the Assignment binary addition operator is created"<<endl;
Matrix temp(*this);
temp+= obj;
return temp;
}
template <class T>
Matrix<T> Matrix<T>::operator-(const Matrix& obj) const
{
//cout<<" the Assignment binary substraction operator is created"<<endl;
Matrix temp(* this);
temp-=obj;
return temp;
}
template <class T>
Matrix<T>& Matrix<T>::operator*=(const Matrix& obj)
{
assert(this->col == obj.row);
Matrix temp (* this );
//cout<<"Assignment Matrix Mult is created"<<endl;
int j;
double sum;
int colu =obj.col;
cout<<" col"<<colu<<endl;
Matrix m3(this->row,obj.col,0);
for (int i=0;i<this->row;++i)
{
for ( j=0;j<colu;++j)
{
sum =0;
for (int k=0;k< obj.row;++k)
{
//cout<<"i"<<" "<<"j"<<i<<" "<<j<<" "<<k<<endl;
//sum+= ((*(ptr+(k+ i*(obj.row)))) * (*(obj.ptr +(k+j*(obj.row)))));
//cout<< *(ptr+(k+ i*(obj.row)))<<endl;
//cout<< *(obj.ptr + (k+ j*(obj.row)))<<endl;
//cout<<"the sum is "<<sum<<endl;
//cout<<" The iteration number is"<<k<<endl;
sum+= temp(i,k)* obj(k,j);
}
//cout<<"the total sum is"<<sum<<endl;
*(m3.ptr + i*(obj.col) +j) = sum;
}
}
(*this) = m3;
return (*this);
}
template <class T>
Matrix<T> Matrix<T>::operator*(const Matrix& obj)
{
//cout<<" The binary Assignment multiplication operator is initialised "<<endl;
Matrix temp(*this);
temp *= obj;
return temp;
}
template <class T>
bool Matrix<T>::operator==(const Matrix& obj)
{
bool var =true ;
row= obj.row;
col= obj.col;
if(this->row!=obj.row || this->col!=obj.col)
{
var =false;
}
else
{
for(int i =0; i <row* col ;++i)
{
if(*(ptr +i) != *(obj.ptr +i))
{
var=false;
break;
}
}
}
return var;
}
template <class T>
bool Matrix<T>:: operator !=(const Matrix& obj)
{
if(*this == obj)
return false;
else
return true;
}
template <class T>
void Matrix<T>::display() const
{
Matrix m3 = (* this);
//cout<<" The contents of the matrix are being displayed "<<endl;
int i ;
int j;
for (i=0;i<row;++i)
{
cout<<endl;
for (j=0; j<col;++j)
//cout<<i<<" "<<j<<endl;
cout<<(m3(i,j))<<"\t\t";
}
}
template <class T>
ostream& operator<<(ostream& os,const Matrix<T>& obj )
{
obj.display();
return os;
}