-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymat.cpp
More file actions
156 lines (128 loc) · 3.22 KB
/
polymat.cpp
File metadata and controls
156 lines (128 loc) · 3.22 KB
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
#include <cstdio>
#include "polymat.hpp"
PolyMat::PolyMat() {
poly = nullptr;
rows = 0;
cols = 0;
}
PolyMat::PolyMat(int r, int c) {
rows = r;
cols = c;
poly = new Poly[r * c];
}
PolyMat::PolyMat(const PolyMat& x) {
rows = x.rows;
cols = x.cols;
poly = new Poly[rows * cols];
for (int i = 0; i < rows * cols; ++i)
poly[i] = x.poly[i];
}
PolyMat& PolyMat::operator=(const PolyMat& x) {
if (this == &x) return *this;
delete[] poly;
rows = x.rows;
cols = x.cols;
poly = new Poly[rows * cols];
for (int i = 0; i < rows * cols; ++i)
poly[i] = x.poly[i];
return *this;
}
PolyMat::~PolyMat() {
delete[] poly;
}
Poly& PolyMat::get(int r, int c) {
return poly[r * cols + c];
}
const Poly& PolyMat::get(int r, int c) const {
return poly[r * cols + c];
}
void PolyMat::reserve(int r, int c) {
if (r < rows && c < cols) return;
Poly* a = new Poly[r*c];
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
if (i < rows && j < cols)
a[i * c + j] = poly[i * cols + j];
else
a[i * c + j] = Poly();
}
}
delete[] poly;
poly = a;
rows = r;
cols = c;
}
void PolyMat::set(int r, int c, const Poly& x) {
if (r < rows && c < cols) {
poly[r * cols + c] = x;
}
else {
reserve(r+1, c+1);
poly[r * cols + c] = x;
rows = r+1;
cols = c+1;
}
}
void PolyMat::transpose(PolyMat& x) {
if (this == &x) {
PolyMat tmp = x;
transpose(tmp);
return;
}
reserve(x.cols, x.rows);
for (int i = 0; i < x.rows; i++)
for (int j = 0; j < x.cols; j++)
set(j, i, x.get(i, j));
}
void PolyMat::add(const PolyMat& x) {
if (x.rows > rows || x.cols > cols)
reserve(x.rows, x.cols);
for (int i = 0; i < x.rows; ++i)
for (int j = 0; j < x.cols; ++j)
poly[i * cols + j] += x.get(i,j);
}
void PolyMat::mul(const PolyMat& x, const PolyMat& y) {
if (x.cols != y.rows) return;
reserve(x.rows, y.cols);
for (int i = 0; i < x.rows; ++i) {
for (int j = 0; j < y.cols; ++j) {
Poly sum, temp;
for (int k = 0; k < x.cols; ++k) {
temp = x.get(i,k) * y.get(k,j);
sum += temp;
}
poly[i * cols + j] = sum;
}
}
}
PolyMat& PolyMat::operator +=(const PolyMat& x) {
add(x);
return *this;
}
PolyMat operator +(const PolyMat& x, const PolyMat& y) {
PolyMat result(x);
result += y;
return result;
}
PolyMat operator *(const PolyMat& x, const PolyMat& y) {
PolyMat result;
result.mul(x, y);
return result;
}
void PolyMat::println() {
printf("[");
for (int i = 0; i < rows; ++i) {
printf("[");
for (int j = 0; j < cols; ++j) {
poly[i * cols + j].print();
if (j < cols - 1) {
printf(",");
}
}
printf("]");
if (i < rows - 1) {
printf(",");
}
}
printf("]\n\n");
}