-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsums.dart
272 lines (253 loc) · 8.03 KB
/
sums.dart
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
import 'term.dart';
import 'constants.dart';
import 'unknowns.dart';
import 'products.dart';
/// A helper object that assists in the accumulation of a number of Term objects that
/// are being added together and in simplifying the resulting summation.
class TermAccumulator {
List<Term> terms = [];
Constant constant = zero;
/// Accumulate a Term object, consolidating all constant terms into a single constant
/// and combining any objects which can be added directly to each other and also combine
/// any compatible divisions.
void accumulate(Term term, bool isNegated) {
if (term is Constant) {
if (isNegated) {
constant -= term;
} else {
constant += term;
}
} else if (term is Sum) {
for (var addend in term.addends) {
accumulate(addend, isNegated);
}
} else {
for (int i = 0; i < terms.length; i++) {
Term sum = terms[i].addDirect(term, isNegated);
if (sum != null) {
if (sum is Constant) {
constant += sum;
terms.removeAt(i);
} else {
terms[i] = sum;
}
return;
}
}
if (isNegated) term = -term;
terms.add(term);
}
}
/// Perform simplifications and return the best representation of the result.
///
/// Simplifications include a constant term that overwhelms the sum such as
/// infinities and nan, and the reduction of the list of terms into a single
/// term.
Term getResult() {
if (terms.length == 0 || constant.overwhelmsSums()) {
return constant;
}
if (constant != zero) {
terms.add(constant);
}
if (terms.length == 1) {
return terms[0];
}
return Sum(terms);
}
}
/// A Term object representing the sum of a number of other Term objects.
class Sum extends Term {
final List<Term> addends;
Sum(List<Term> terms) : addends = List.unmodifiable(terms);
/// A helper method to add a list of Term objects and return a simplified result.
static Term addList(List<Term> terms) {
TermAccumulator accumulator = TermAccumulator();
for (var term in terms) accumulator.accumulate(term, false);
return accumulator.getResult();
}
/// A helper method to add two Term objects and return a simplified result.
static Term add(Term a, Term b) {
TermAccumulator accumulator = TermAccumulator();
accumulator.accumulate(a, false);
accumulator.accumulate(b, false);
return accumulator.getResult();
}
/// A helper method to subtract two Term objects and return a simplified result.
static Term sub(Term first, Term second) {
TermAccumulator accumulator = TermAccumulator();
accumulator.accumulate(first, false);
accumulator.accumulate(second, true);
return accumulator.getResult();
}
/// A helper method to find the greatest common factor of unknowns that is common to
/// all elements of the summation.
///
/// This method helps to reduce long fractions to simpler divisions by comparing the
/// remainders of numerators and denominators in Division objects to each other.
Term commonFactor() {
List<Term> common;
for (var term in addends) {
if (term is Unknown) {
if (common != null && !common.contains(term)) return one;
common = [term];
} else if (term is Product) {
List<Term> oldCommon = common;
common = [];
for (var factor in term.factors) {
if (factor is Unknown) {
if (oldCommon == null) {
common.add(factor);
} else {
for (var cTerm in oldCommon) {
if (cTerm.equals(factor)) {
common.add(factor);
oldCommon.remove(cTerm);
break;
}
}
}
}
}
if (common.length == 0) break;
} else {
return one;
}
}
if (common == null || common.length == 0) return one;
if (common.length == 1) return common[0];
return Product(factors: common);
}
@override
bool negatesGracefully() {
bool hadNegative = false;
bool wasUngraceful = false;
for (var term in addends) {
wasUngraceful == wasUngraceful || !term.negatesGracefully();
hadNegative == hadNegative && term.isNegative();
}
return hadNegative || !wasUngraceful;
}
@override
Term operator -() {
TermAccumulator accumulator = TermAccumulator();
for (var term in addends) accumulator.accumulate(term, true);
return accumulator.getResult();
}
@override bool isNegative() => false;
@override bool equals(Term other) {
if (other is Sum) {
List<Term> oAddends = other.addends;
if (oAddends.length != addends.length) return false;
List<bool> used = List.filled(addends.length, false);
for (var term in oAddends) {
bool foundIt = false;
for (int i = 0; i < addends.length; i++) {
if (!used[i] && term.equals(addends[i])) {
used[i] = foundIt = true;
break;
}
}
if (!foundIt) return false;
}
return true;
}
return false;
}
@override Term addDirect(Term other, bool isNegated) => null;
/// Compare the single Unknown object to the factors of the Product Term object to
/// determine which should appear first in the String output.
///
/// The list is sorted by the names of the Unknown terms so that similar terms will appear
/// in similar locations in any lists.
///
/// The intention is to make the many summations more readable for human eyes.
static int _compareProductUnknown(Unknown a, Product b) {
for (var factor in b.factors) {
if (factor is Unknown) return a.name.compareTo(factor.name);
if (factor is! Constant) return -1;
}
return -1;
}
/// Compare the factors of the Product Term objects to determine which should appear first in
/// the String output.
///
/// The list is sorted by the names of the Unknown terms so that similar terms will appear
/// in similar locations in any lists.
///
/// The intention is to make the many summations more readable for human eyes.
static int _compareProducts(Product a, Product b) {
int ai = 0, bi = 0;
while (ai < a.factors.length && bi < b.factors.length) {
if (a.factors[ai] is! Unknown) ai++;
else if (b.factors[bi] is! Unknown) bi++;
else {
Unknown au = a.factors[ai++];
Unknown bu = b.factors[bi++];
int comp = au.name.compareTo(bu.name);
if (comp != 0) return comp;
}
}
return 0;
}
/// A Comparator method to create a (hopefully) pleasing ordering of the summation
/// terms when the object is converted to a string.
static int _sortOrder(Term a, Term b) {
if (a is Constant) {
if (b is Constant) return a.compareTo(b);
return 1;
}
if (b is Constant) {
return -1;
}
if (a is Unknown) {
if (b is Unknown) return a.name.compareTo(b.name);
if (b is Product) return _compareProductUnknown(a, b);
return -1;
}
if (b is Unknown) {
if (a is Product) return -_compareProductUnknown(b, a);
return 1;
}
if (a is Product) {
if (b is Product) return _compareProducts(a, b);
return -1;
}
if (b is Product) {
return 1;
}
return 0;
}
List<Term> __sortedAddends;
List<Term> get _sortedAddends => __sortedAddends ??= [...addends]..sort(_sortOrder);
@override bool startsWithMinus() => false;
@override
String toString() {
String ret = '(';
String add = '';
for (Term term in _sortedAddends) {
if (!term.startsWithMinus()) ret += add;
ret += '$term';
add = '+';
}
return ret+')';
}
@override String toOutline() {
String ret = 'Sum(';
String add = '';
int nTerms = 0;
for (Term term in addends) {
if (term is Product || term is Unknown) {
nTerms++;
} else {
if (!term.startsWithMinus()) ret += add;
ret += term.toOutline();
add = ' + ';
}
}
if (nTerms > 0) {
ret += '$add$nTerms terms';
}
return ret + ')';
}
}