-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyops.cpp
More file actions
101 lines (87 loc) · 2.05 KB
/
Copy pathpolyops.cpp
File metadata and controls
101 lines (87 loc) · 2.05 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
// Copyright year qingxuan pei pqx97@bu.edu
// Copyright year cagri yoruk cyoruk@bu.edu
#include <vector>
using namespace std;
//include "polyops.cpp"
typedef vector<double> Poly;
Poly add_poly(const Poly &a,const Poly &b) {
vector<double> a1;
vector<double> b1;
vector<double> vec;
int len1 = a.size();
int len2 = b.size();
if (len1 < len2) {
for (int i = 0; i < len2; i++) {
a1.push_back(b.at(i));
}
for (int i = 0; i < len1; i++) {
b1.push_back(a.at(i));
}
int swap1 = len1;
len1 = len2;
len2 = swap1;
}
else {
for (int i = 0; i < len1; i++) {
a1.push_back(a.at(i));
}
for (int i = 0; i < len2; i++) {
b1.push_back(b.at(i));
}
}
for(int i = 0;i < len2;i++){
double sum = a1.at(i) + b1.at(i);
vec.push_back(sum);
}
for(int i = len2;i<len1;i++){
vec.push_back(a1.at(i));
}
while(vec.at(vec.size()-1) == 0 and vec.size()>1){
vec.pop_back();
}
return vec;
}
Poly multiply_poly(const Poly &a,const Poly &b) {
int len1 = a.size();
int len2 = b.size();
double sum = 0;
Poly m;
for(int i = 0;i<len1 + len2 - 1; i++){
m.push_back(0);
}
//vector < vector<double> > vec;
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (i + j < len1 + len2 - 1) {
double right = a.at(i) * b.at(j);
m.at(i + j) += right;
}
}
}
while(m.at(m.size()-1) == 0 and m.size()>1){
m.pop_back();
}
return m;
/*
for (int i = 0; i < len1 + len2 - 1; i++) {
m.pop_back();
return m;
}
*/
}
int main()
{
int Alen,Blen;
cin >> Alen >> Blen;
Poly A(Alen,0),B(Blen,0);
for (auto& e : A)
cin >> e;
for (auto& e : B)
cin >> e;
for (auto e : add_poly(A,B))
cout << e << " ";
cout << endl;
for (auto e : multiply_poly(A,B))
cout << e << " ";
cout << endl;
}