矩阵类的实现

矩阵类 正常存储:

Design a template class Matrix that has the following private member variables:

  1. int rows
  2. int columns
  3. vector values

where T is the type parameter.
Besides, it has the functions such that the main function runs correctly with the following output.

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
EXAMPLE OUTPUT
constructor 1
0 0 0
0 0 0
0 0 0
constructor 2
1 2 3
4 5 6
7 8 9
copy constructor
1 2 3
4 5 6
7 8 9
operator =
1 2 3
4 5 6
7 8 9
getColumn
2
5
8
getRow
4 5 6
concatenateRows
0 0 0
0 0 0
0 0 0
1 2 3
4 5 6
7 8 9
concatenateColumns
0 0 0 1 2 3
0 0 0 4 5 6
0 0 0 7 8 9
reshape
0 0 2
0 0 5
0 0 8
0 1 3
0 4 6
0 7 9
transpose
1 4 7
2 5 8
3 6 9
operator +
2 4 6
8 10 12
14 16 18
operator +
11 12 13
14 15 16
17 18 19
operator -
0 2 4
-2 0 2
-4 -2 0
operator -
-9 -8 -7
-6 -5 -4
-3 -2 -1
operator *
66 78 90
78 93 108
90 108 126
operator *
2 4 6
8 10 12
14 16 18
max
9
min
1
sum
45
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename E>
class Matrix{
private:
int rows;
int columns;
vector<E> values;
public:
Matrix(int r, int c):rows(r),columns(c){
this->values.clear();
this->values.resize(r*c);
}
Matrix(int r, int c, vector<E>& v):rows(r),columns(c){
this->values.clear();
this->values.resize(r*c);
for(int i=0; i<r*c; i++)
values[i] = v[i];
}
Matrix(const Matrix & m)
{
this->columns = m.columns;
this->rows = m.rows;
this->values.clear();
this->values.resize(m.rows*m.columns);
for(int i=0; i<m.rows*m.columns; i++)
this->values[i] = m.values[i];
}
Matrix & operator = (const Matrix & m)
{
this->rows = m.rows;
this->columns = m.columns;
this->values.clear();
this->values.resize(m.rows*m.columns);
for(int i=0; i<m.rows*m.columns; i++)
this->values[i] = m.values[i];
return *this;
}
~Matrix()
{
this->values.clear();
}
void print()
{
for(int i=0; i<this->rows; i++)
{
for(int j=0; j<this->columns; j++)
cout << " " << values[i*this->columns+j];
cout << endl;
}
}
Matrix getColumn(int c)
{
Matrix rem(this->rows, 1);
for(int i=0; i<this->rows; i++)
rem.values[i] = this->values[i*this->columns+c-1];
return rem;
}
Matrix getRow(int r)
{
Matrix rem(1, this->columns);
for(int i=0; i<this->columns; i++)
rem.values[i] = this->values[(r-1)*this->columns+i];
return rem;
}
Matrix concatenateRows(const Matrix & m)
{
Matrix rem(this->rows+m.rows, this->columns);
for(int i=0; i<this->rows*this->columns; i++)
rem.values[i] = this->values[i];
for(int i=0; i<m.rows*m.columns; i++)
rem.values[this->rows*this->columns+i] = m.values[i];
return rem;
}
Matrix concatenateColumns(const Matrix & m)
{
Matrix rem(this->rows, this->columns+m.columns);
int z = 0;
for(int i=0; i<this->rows; i++)
{
for(int j=0; j<this->columns; j++)
rem.values[z++] = this->values[i*this->columns+j];
for(int j=0; j<m.columns; j++)
rem.values[z++] = m.values[i*m.columns+j];
}
return rem;
}
Matrix reshape(int r, int c)
{
Matrix rem(r, c);
E arr[r*c];
int z = 0;
for(int i=0; i<this->columns; i++)
{
for(int j=0; j<this->rows; j++)
{
arr[z++] = this->values[j*this->columns+i];
}
}
z = 0;
for(int i=0; i<c; i++)
{
for(int j=0; j<r; j++)
{
rem.values[j*c+i] = arr[z++];
}
}
return rem;
}
Matrix transpose()
{
Matrix rem(this->columns, this->rows);
for(int i=0; i<rem.rows; i++)
{
for(int j=0; j<rem.columns; j++)
{
rem.values[i*rem.columns+j] = this->values[j*this->columns+i];
}
}
return rem;
}
Matrix operator + (int n)
{
Matrix rem(this->rows, this->columns);
for(int i=0; i<this->rows*this->columns; i++)
{
rem.values[i] = this->values[i] + n;
}
return rem;
}
Matrix operator + (Matrix &n)
{
Matrix rem(this->rows, this->columns);
for(int i=0; i<this->rows*this->columns; i++)
{
rem.values[i] = this->values[i] + n.values[i];
}
return rem;
}
Matrix operator - (int n)
{
Matrix rem(this->rows, this->columns);
for(int i=0; i<this->rows*this->columns; i++)
{
rem.values[i] = this->values[i] - n;
}
return rem;
}
Matrix operator - (Matrix &n)
{
Matrix rem(this->rows, this->columns);
for(int i=0; i<this->rows*this->columns; i++)
{
rem.values[i] = this->values[i] - n.values[i];
}
return rem;
}
Matrix operator * (int n)
{
Matrix rem(this->rows, this->columns);
for(int i=0; i<this->rows*this->columns; i++)
{
rem.values[i] = this->values[i] * n;
}
return rem;
}
Matrix operator * (const Matrix & matrix2) const{
Matrix rem(this->rows, matrix2.columns);
for(int i=0; i<this->rows; i++)
{
for(int j=0; j<matrix2.columns; j++)
{
int sum = 0;
for(int x=0; x<this->columns; x++)
{
sum += this->values[i*this->columns+x]*matrix2.values[x*matrix2.columns+j];

}
rem.values[i*matrix2.columns+j] = sum;
}
}
return rem;
}
E& get(int r, int c)
{
int nr = r-1;
int nc = c-1;
return this->values[nr*this->columns+nc];
}
Matrix sum() const{
Matrix rem(1, this->columns);
if(this->rows == 1)
{
rem.columns = 1;
for(int i=0; i<this->columns; i++)
{
rem.values[0] += this->values[i];
}
}
else
{
for(int i=0; i<this->columns; i++)
{
for(int j=0; j<this->rows; j++)
{
rem.values[i] += this->values[j*this->columns+i];
}
}
}
return rem;
}
Matrix max()const {
Matrix rem(1, this->columns);
if(this->rows == 1)
{
rem.columns = 1;
for(int i=0; i<this->columns; i++)
{
rem.values[0] = rem.values[0]>this->values[i] ? rem.values[0] : this->values[i];
}
}
else
{
for(int i=0; i<this->columns; i++)
{
for(int j=0; j<this->rows; j++)
{
rem.values[i] = rem.values[i] > this->values[j*this->columns+i] ? rem.values[i]: this->values[j*this->columns+i];
}
}
}
return rem;
}
Matrix min()const {
Matrix rem(1, this->columns);
if(this->rows == 1)
{
rem.columns = 1;
rem.values[0] = this->values[0];
for(int i=0; i<this->columns; i++)
{
rem.values[0] = rem.values[0]<this->values[i] ? rem.values[0] : this->values[i];
}
}
else
{
for(int i=0; i<this->columns; i++)
{
rem.values[i] = this->values[i];
for(int j=0; j<this->rows; j++)
{
rem.values[i] = rem.values[i] < this->values[j*this->columns+i] ? rem.values[i]: this->values[j*this->columns+i];
}
}
}
return rem;
}
};

int main() {
cout << "constructor 1" << endl;
Matrix<double> matrix1(3, 3);
matrix1.print();

const double values1[] = {
1, 2, 3,
4, 5, 6,
7, 8, 9,
};
vector<double> values2;
for (int i = 0; i < 9; ++ i) {
values2.push_back(values1[i]);
}

cout << "constructor 2" << endl;
Matrix<double> matrix2(3, 3, values2);
matrix2.print();

cout << "copy constructor" << endl;
Matrix<double> matrix3 = matrix2;
matrix3.print();

cout << "operator =" << endl;
matrix3.get(1, 1) = 10.0;
matrix3 = matrix2;
matrix3.print();

cout << "getColumn" << endl;
matrix2.getColumn(2).print();
cout << "getRow" << endl;
matrix2.getRow(2).print();

cout << "concatenateRows" << endl;
matrix1.concatenateRows(matrix2).print();
cout << "concatenateColumns" << endl;
matrix1.concatenateColumns(matrix2).print();

cout << "reshape" << endl;
matrix1.concatenateColumns(matrix2).
reshape(6, 3).print();

cout << "transpose" << endl;
matrix2.transpose().print();

cout << "operator +" << endl;
(matrix2 + matrix2).print();
cout << "operator +" << endl;
(matrix2 + 10).print();
cout << "operator -" << endl;
(matrix2.transpose() - matrix2).print();
cout << "operator -" << endl;
(matrix2 - 10).print();

cout << "operator *" << endl;
(matrix2.transpose() * matrix2).print();
cout << "operator *" << endl;
(matrix2 * 2).print();

cout << "max" << endl;
cout << matrix2.max().max().get(1, 1) << endl;
cout << "min" << endl;
cout << matrix2.min().min().get(1, 1) << endl;
cout << "sum" << endl;
cout << matrix2.sum().sum().get(1, 1) << endl;
}

矩阵类 稀疏矩阵存储:

Design a class Sparse that implements interface Matrix: Sparse should has the following public object functions in addition:

A constructor Sparse(int rows, int column), which initializes all elements in the matrix to 0's.

A function Sparse Sparse::operator * (Sparse & sparse2), which returns the product of two sparse matrixes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
EXAMPLE INPUT
1000000 2000000 3000000

1 1 10
1 2000000 50
1000000 2000000 20

1 3000000 30
2000000 1 40
1 1 -10
EXAMPLE OUTPUT
(1,1,1900)
(1,3000000,300)
(1000000,1,800)
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
#include <iostream>
#include <vector>
using namespace std;

class Entry
{
public:
int row;
int column;
double value;
};

class Matrix
{
public:
virtual int size(int dimension) const = 0;

virtual void set(int row, int column,
double value) = 0;

virtual double get(int row, int column)
const = 0;

virtual void print() = 0;

};


class Sparse : public Matrix
{
private:
int rows;
int column;
vector<Entry> mmatrix;
public:
Sparse(int rows, int column)
{
this->rows = rows;
this->column = column;
}
virtual int size(int dimension) const
{
if(dimension == 1) return this->rows;
if(dimension == 2) return this->column;
}
virtual void set(int row, int column, double value)
{
int flag = 0;
for(int i=0; i<mmatrix.size(); i++)
{
if(mmatrix[i].row == row && mmatrix[i].column == column)
{
flag = 1;
mmatrix[i].value = value;
break;
}
}
if(!flag)
{
Entry rem;
rem.row = row;
rem.column = column;
rem.value = value;
mmatrix.push_back(rem);
}
}
virtual double get(int row, int column)const
{
for(int i=0; i<mmatrix.size(); i++)
{
if(mmatrix[i].row == row && mmatrix[i].column == column)
{
return mmatrix[i].value;
}
}
return -1;
}
virtual void print()
{
for(int i=0; i<mmatrix.size()-1; i++)
{
for(int j=0; j<mmatrix.size()-1-i; j++)
{
if(mmatrix[j].row>mmatrix[j+1].row)
{
Entry temp = mmatrix[j];
mmatrix[j] = mmatrix[j+1];
mmatrix[j+1] = temp;
}
else if(mmatrix[j].row==mmatrix[j+1].row)
{
if(mmatrix[j].column>mmatrix[j+1].column)
{
Entry temp = mmatrix[j];
mmatrix[j] = mmatrix[j+1];
mmatrix[j+1] = temp;
}
}
}
}
for(int i=0; i<mmatrix.size(); i++)
{
cout << "(" << mmatrix[i].row << "," << mmatrix[i].column << "," << mmatrix[i].value << ")" << endl;
}
}
Sparse operator * (Sparse &sparse2)
{
Sparse rem(this->rows, sparse2.column);
vector<Entry> ttt;
for(int i=0; i<this->mmatrix.size(); i++)
{
for(int j=0; j<sparse2.mmatrix.size(); j++)
{
if(this->mmatrix[i].column == sparse2.mmatrix[j].row)
{
Entry rrr;
rrr.row = this->mmatrix[i].row;
rrr.column = sparse2.mmatrix[j].column;
rrr.value = this->mmatrix[i].value * sparse2.mmatrix[j].value;
ttt.push_back(rrr);
}

}
}
int num[ttt.size()] = {0};
for(int i=0; i<ttt.size(); i++)
{
if(num[i]==0)
{
int sum = ttt[i].value;
num[i] = 1;
for(int j=0; j<ttt.size(); j++)
{
if(ttt[i].row == ttt[j].row && ttt[i].column == ttt[j].column && i != j)
{
sum += ttt[j].value;
num[j] = 1;
}
}
if(sum != 0)
{
Entry zzz;
zzz.row = ttt[i].row;
zzz.column = ttt[i].column;
zzz.value = sum;
rem.mmatrix.push_back(zzz);
}
}
}
return rem;
}
};


void print(Matrix & matrix) {
matrix.print();
}

void readAndSetElement(Matrix & matrix) {
int row;
int column;
double value;
cin >> row >> column >> value;
matrix.set(row, column, value);
}

void readAndSetMultipleElements(Matrix & matrix, int count) {
for (int i = 0; i < count; ++ i) {
readAndSetElement(matrix);
}
}

int main() {
int rows;
int columns;
int & rows2 = columns;
int columns2;

cin >> rows >> columns >> columns2;

Sparse sparse1(rows, columns);
readAndSetMultipleElements(sparse1, 3);

Sparse sparse2(rows2, columns2);
readAndSetMultipleElements(sparse2, 3);

Sparse sparse3 = sparse1 * sparse2;
print(sparse3);
}
Donate? comment?