-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSummand.cpp
215 lines (198 loc) · 5.68 KB
/
Summand.cpp
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
//
// Copyright © 2017 Lennart Oymanns. All rights reserved.
//
#include <algorithm>
#include "ComplexNumber.hpp"
#include "Factor.hpp"
#include "Number.hpp"
#include "Summand.hpp"
#include "UnaryMinus.hpp"
using namespace Equation;
static bool comparator_sum(NodePtr &first, NodePtr &second) {
auto cplx1 = complex_number(first);
if (!cplx1.second) {
return false;
}
auto cplx2 = complex_number(second);
if (!cplx2.second) {
return false;
}
if (cplx1.first.imag() == NumberRepr(0l) &&
cplx2.first.imag() != NumberRepr(0l)) {
return true;
}
return false;
}
void Summand::ToStream(std::ostream &s) {
int i = 0;
for (auto &e : op1) {
if (i != 0) {
if (e->Type() == Node::Type_t::UnaryMinus) {
s << " - ";
auto un = std::static_pointer_cast<UnaryMinus>(e);
un->ToStreamAbs(s);
} else if (e->Type() == Node::Type_t::Factor) {
auto factor = std::static_pointer_cast<Factor>(e);
auto pre = factor->Data().begin();
if ((*pre)->Type() == Node::Type_t::Number) {
auto nb = std::static_pointer_cast<Number>(*pre);
if (nb->GetValue() < NumberRepr(0l)) {
s << " ";
e->ToStream(s);
} else {
s << " + ";
e->ToStream(s);
}
} else {
s << " + ";
e->ToStream(s);
}
} else if (e->Type() == Node::Type_t::Number) {
auto nb = std::static_pointer_cast<Number>(e);
auto r = nb->GetValue();
if (nb->GetValue() < NumberRepr(0l)) {
s << " - ";
r *= NumberRepr(-1l);
} else {
s << " + ";
}
s << r.String();
} else {
s << " + ";
e->ToStream(s);
}
} else {
e->ToStream(s);
}
i += 1;
}
}
void Summand::ToLatex(std::ostream &s) {
int i = 0;
for (auto &e : op1) {
if (i != 0) {
if (e->Type() == Node::Type_t::UnaryMinus) {
s << " - ";
auto un = std::static_pointer_cast<UnaryMinus>(e);
un->ToStreamAbs(s);
} else if (e->Type() == Node::Type_t::Factor) {
auto factor = std::static_pointer_cast<Factor>(e);
auto pre = factor->Data().begin();
if ((*pre)->Type() == Node::Type_t::Number) {
auto nb = std::static_pointer_cast<Number>(*pre);
if (nb->GetValue() < NumberRepr(0l)) {
s << " ";
e->ToLatex(s);
} else {
s << " + ";
e->ToLatex(s);
}
} else {
s << " + ";
e->ToLatex(s);
}
} else if (e->Type() == Node::Type_t::Number) {
auto nb = std::static_pointer_cast<Number>(e);
auto r = nb->GetValue();
if (nb->GetValue() < NumberRepr(0l)) {
s << " - ";
r *= NumberRepr(-1l);
} else {
s << " + ";
}
r.ToLatex(s);
} else {
s << " + ";
e->ToLatex(s);
}
} else {
e->ToLatex(s);
}
i += 1;
}
}
std::pair<NumberRepr, NodePtr> clone_expr_wo_coeff(NodePtr n) {
if (n->Type() == Node::Type_t::Factor) {
auto factor = std::static_pointer_cast<Factor>(n);
auto it_first = factor->Data().begin();
std::shared_ptr<Factor> factor_new;
NodePtr result;
auto type = (*it_first)->Type();
if (type == Node::Type_t::Number) {
factor_new = std::static_pointer_cast<Factor>(factor->clone());
factor_new->Data().pop_front();
if (factor_new->Data().size() == 1) {
result = factor_new->Data().front();
} else if (factor_new->Data().size() == 0) {
return std::pair<NumberRepr, NodePtr>(NumberRepr(1l),
(*it_first)->clone());
} else {
result = factor_new;
}
const auto coeff = std::static_pointer_cast<Number>(*it_first);
return std::pair<NumberRepr, NodePtr>(coeff->GetValue(), result);
}
return std::pair<NumberRepr, NodePtr>(NumberRepr(1l), n->clone());
}
if (n->Type() == Node::Type_t::UnaryMinus) {
auto un = std::static_pointer_cast<UnaryMinus>(n);
auto res = clone_expr_wo_coeff(un->Data());
res.first *= NumberRepr(-1l);
return res;
}
return std::pair<NumberRepr, NodePtr>(NumberRepr(1l), n->clone());
}
void Summand::simplify() {
std::list<std::pair<NumberRepr, NodePtr> > polynom;
for (auto &e : op1) {
auto nom = clone_expr_wo_coeff(e);
auto pos = std::find_if(polynom.begin(), polynom.end(),
[&nom](const std::pair<NumberRepr, NodePtr> &n) {
return n.second->equals(nom.second);
});
if (pos == polynom.end()) {
polynom.push_back(nom);
} else {
pos->first += nom.first;
}
}
// fill new summands
op1.clear();
for (auto &e : polynom) {
if (e.first == NumberRepr(1l)) {
op1.push_back(e.second);
} else if (e.first == NumberRepr(0l)) {
continue;
} else {
auto coeff = std::make_shared<Number>(e.first);
std::shared_ptr<Factor> factor;
if (e.second->Type() == Node::Type_t::Factor) {
factor = std::static_pointer_cast<Factor>(e.second);
factor->Data().push_front(coeff);
} else {
factor = std::make_shared<Factor>(coeff);
factor->AddOp1(e.second);
}
op1.push_back(factor);
}
}
}
void Summand::Eval(NodePtr *base, std::shared_ptr<State> state, bool numeric) {
TwoOp::Eval(base, state, numeric);
if ((*base).get() != this) {
return;
}
if (base && (*base)->Type() != Node::Type_t::Summand) {
return;
}
simplify();
if (base && op1.size() == 0) {
base->reset(new Number(0l));
return;
}
if (base && op1.size() == 1) {
*base = op1.front();
return;
}
op1.sort(comparator_sum);
}