-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_formula.js
210 lines (185 loc) · 6.1 KB
/
complex_formula.js
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
'use strict';
// f should be a function that takes an array of arrays of Complex
// objects and returns an array of complex objects. If subformulas is
// falsy or empty, then when update is called, f will be passed an
// array of a single array containing the arguments of update
// converted to Complex objects. Otherwise, f will be passed the array
// of subresults from calling the subformulas with the arguments of
// update.
function ComplexFormula(f, subformulas) {
this._f = f;
this._subformulas = subformulas || [];
}
// This function takes in any number of objects and returns an array
// of Complex objects.
ComplexFormula.prototype.update = function() {
// Use a for loop as anything more clever prevents this use of
// arguments from being optimized out.
var inputs = [];
for (var i = 0; i < arguments.length; ++i) {
inputs.push(Complex.from(arguments[i]));
};
if (this._subformulas.length === 0) {
return this._f([inputs]);
}
var subresults = this._subformulas.map(function(subformula) {
return subformula.update.apply(subformula, inputs);
});
return this._f(subresults);
};
ComplexFormula.empty = new ComplexFormula(function() {
return [];
});
ComplexFormula.constant = function(o) {
var z = Complex.from(o);
return new ComplexFormula(function() {
return [ z ];
});
};
ComplexFormula.select = function(i) {
return new ComplexFormula(function(subresults) {
if (i < 0) {
i = subresults[0].length + i;
}
return [ subresults[0][i] ];
});
};
ComplexFormula.prototype._unaryOp = function(op) {
return new ComplexFormula(function(subresults) {
return subresults[0].map(function(x) { return op(x); });
}, [ this ]);
};
ComplexFormula.prototype.neg = function() {
return this._unaryOp(function(z) {
return z.neg();
});
};
ComplexFormula.prototype.conj = function() {
return this._unaryOp(function(z) {
return z.conj();
});
};
ComplexFormula.prototype.pow = function(p) {
return this._unaryOp(function(z) {
return z.pow(p);
});
};
// The returned formula returns an array of all p pth roots of all its
// arguments. Furthermore, as the formula is updated with new
// arguments that go around the origin, the returned roots remain in a
// stable order and vary continuously with respect to the input,
// assuming that the arguments don't go through zero.
ComplexFormula.prototype.root = function(p) {
var rotationCounters;
return new ComplexFormula(function(subresults) {
var zs = subresults[0];
if (rotationCounters === undefined) {
rotationCounters = zs.map(function() {
return new RotationCounter();
});
}
var results = [];
for (var i = 0; i < zs.length; ++i) {
var rotationCounter = rotationCounters[i];
var z = zs[i];
rotationCounter.update(z);
var k = rotationCounter.k();
for (var j = 0; j < p; ++j) {
results.push(z.root(p, (k + j) % p));
}
}
return results;
}, [ this ]);
};
ComplexFormula.from = function(o) {
if (o instanceof ComplexFormula) {
return o;
}
return ComplexFormula.constant(o);
};
ComplexFormula._getSubformulas = function(that, args) {
var subformulas = Array.prototype.map.call(args, function(arg) {
return ComplexFormula.from(arg);
});
if (that instanceof ComplexFormula) {
subformulas.unshift(that);
}
return subformulas;
};
ComplexFormula._multiOp = function(op, that, args) {
var subformulas = this._getSubformulas(that, args);
if (subformulas.length == 0) {
return ComplexFormula.empty;
}
return new ComplexFormula(function(subresults) {
return subresults[0].map(function(_, i) {
var opInputs = subresults.map(function(a) { return a[i]; });
return op.apply(null, opInputs);
});
}, subformulas);
};
ComplexFormula.plus = ComplexFormula.prototype.plus = function() {
return ComplexFormula._multiOp(Complex.plus, this, arguments);
};
ComplexFormula.minus = ComplexFormula.prototype.minus = function() {
return ComplexFormula._multiOp(Complex.minus, this, arguments);
};
ComplexFormula.times = ComplexFormula.prototype.times = function() {
return ComplexFormula._multiOp(Complex.times, this, arguments);
};
ComplexFormula.div = ComplexFormula.prototype.div = function() {
return ComplexFormula._multiOp(Complex.div, this, arguments);
};
ComplexFormula._multiOpAll = function(op, that, args) {
var subformulas = this._getSubformulas(that, args);
if (subformulas.length == 0) {
return ComplexFormula.empty;
}
return new ComplexFormula(function(subresults) {
var indices = subresults.map(function() { return 0; });
var results = [];
outer:
while (true) {
for (var i = 0; i < indices.length; ++i) {
if (indices[i] < subresults[i].length) {
break;
}
if ((i + 1) == indices.length) {
break outer;
}
indices[i] = 0;
++indices[i+1];
}
var opInputs = indices.map(function(i, j) { return subresults[j][i]; });
results.push(op.apply(null, opInputs));
++indices[0];
}
return results;
}, subformulas);
};
ComplexFormula.plusAll = ComplexFormula.prototype.plusAll = function() {
return ComplexFormula._multiOpAll(Complex.plus, this, arguments);
};
ComplexFormula.minusAll = ComplexFormula.prototype.minusAll = function() {
return ComplexFormula._multiOpAll(Complex.minus, this, arguments);
};
ComplexFormula.timesAll = ComplexFormula.prototype.timesAll = function() {
return ComplexFormula._multiOpAll(Complex.times, this, arguments);
};
ComplexFormula.divAll = ComplexFormula.prototype.divAll = function() {
return ComplexFormula._multiOpAll(Complex.div, this, arguments);
};
ComplexFormula.prototype.slice = function(begin, end) {
return new ComplexFormula(function(subresults) {
return subresults[0].slice(begin, end);
}, [ this ]);
};
ComplexFormula.concat = ComplexFormula.prototype.concat = function() {
var subformulas = ComplexFormula._getSubformulas(this, arguments);
if (subformulas.length == 0) {
return ComplexFormula.empty;
}
return new ComplexFormula(function(subresults) {
return Array.prototype.concat.apply([], subresults);
}, subformulas);
};