forked from silq-lang/silq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limits.d
313 lines (308 loc) · 9.2 KB
/
limits.d
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Written in the D programming language
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
import dexpr, util;
import std.algorithm, std.array;
import asymptotics;
static struct ExpLim{
DExpr expression;
DExpr limit;
}
static struct Case(T){
DExpr condition;
T expression;
auto map(alias a)(){ return Case!(typeof(a(expression)))(a(expression),condition,limit); }
auto addCondition(DExpr c){ return Case!T(condition*c,expression); }
}
auto compress(Case!ExpLim[] explims){
foreach(ref el;explims){
el.condition=el.condition.simplify(one);
el.expression.limit=el.expression.limit.maybe!(e=>e.simplify(one));
}
Case!ExpLim[][DExpr] byLimit;
Case!ExpLim[] result;
foreach(el;explims) byLimit[el.expression.limit]~=el;
foreach(k,v;byLimit){
auto condition=zero, expression=zero, limit=zero;
if(k&&k.isInfinite()){
// TODO: improve
result~=v;
}else{
foreach(el;v){
assert(el.expression.limit == k);
condition=condition+el.condition;
expression=expression+el.condition*el.expression.expression;
if(el.expression.limit !is null) limit=limit+el.condition*el.expression.limit;
else limit=null;
}
result~=Case!ExpLim(condition,ExpLim(expression,limit));
}
}
return result;
}
auto expandMap(alias a)(Case!ExpLim[][] r){
alias T=typeof(a(ExpLim[].init));
Case!T[] result;
void go(int i,DExpr cond,ExpLim[] explims){ // TODO: this seems inefficient
if(i==r.length){
result~=Case!T(cond,a(explims));
return;
}
r[i]=compress(r[i]);
foreach(k;r[i]) go(i+1,cond*k.condition,explims~k.expression);
}
go(0,one,[]);
return result;
}
auto expandFlatMap(alias a)(Case!ExpLim[][] r){
Case!ExpLim[] result;
void go(int i,DExpr cond,ExpLim[] explims){ // TODO: this seems inefficient
if(i==r.length){
result~=a(explims).map!(a=>a.addCondition(cond)).array;
return;
}
r[i]=compress(r[i]);
foreach(k;r[i]) go(i+1,cond*k.condition,explims~k.expression);
}
go(0,one,[]);
return result;
}
DExpr getLimit(DVar v,DExpr e,DExpr x,DExpr facts=one)in{assert(isInfinite(e));}body{ // TODO: handle iverson brackets in some way
e=e.simplify(facts);
x=x.simplify(facts);
Case!ExpLim[] doIt(DVar v,DExpr e,DExpr x){
if(!x.hasFreeVar(v)) return [Case!ExpLim(one,ExpLim(x,x))];
if(x == v) return [Case!ExpLim(one,ExpLim(x,e))];
x=x.polyNormalize(v).simplify(facts);
if(auto p=cast(DPlus)x){
DExpr handlePlusImpl(ExpLim[] c){
bool simplified=false;
DExpr finite=zero;
DExprSet unsupported;
DExprSet inf;
DExprSet minf;
foreach(s;c){
auto l=s.limit;
if(!l){ unsupported.insert(s.expression); continue; }
l=l.simplify(facts);
if(l == dInf){ inf.insert(s.expression); continue; }
if(l == -dInf){ minf.insert(s.expression); continue; }
if(l.hasLimits()){ unsupported.insert(l); continue; }
finite = finite+l;
simplified = true;
}
if(!unsupported.length){
if(!inf.length && !minf.length) return finite;
if(inf.length && !minf.length) return dInf;
if(minf.length && !inf.length) return -dInf;
auto infAsymp=asymptoticNormalize(v,dPlus(inf));
auto minfAsymp=asymptoticNormalize(v,dPlus(minf));
if(growsFasterThanNormalized(v,infAsymp,minfAsymp))
return dInf;
if(growsFasterThanNormalized(v,minfAsymp,infAsymp))
return -dInf;
}
if(simplified) return finite+dLim(v,e,dPlus(unsupported)+dPlus(inf)+dPlus(minf));
return null;
}
ExpLim handlePlus(ExpLim[] c){ return ExpLim(p,handlePlusImpl(c)); }
Case!ExpLim[][] r;
foreach(s;p.summands){
auto cur=doIt(v,e,s);
if(!cur.length) return [];
r~=cur;
}
return expandMap!handlePlus(r);
}
if(auto m=cast(DMult)x){
Case!ExpLim[][] r;
auto ow=m.splitMultAtVar(v);
foreach(s;ow[1].factors){
r~=doIt(v,e,s);
if(!r[$-1].length) return [];
}
DExpr replaceDeltasByIvrs(DExpr e){
auto h=e.getHoles!(x=>cast(DDelta)x,DDelta);
auto r=h.expr;
foreach(hole;h.holes){
r=r.substitute(hole.var,dEqZ(hole.expr.var).simplify(facts));
}
return r;
}
// TODO: this is a hack and not generally correct:
// (It might be fine for the cases that this is actually called with though. This should still be fixed.)
auto owZNoDeltas=replaceDeltasByIvrs(ow[0]);
auto owZneqZ=dNeqZ(owZNoDeltas).simplify(facts);
Case!ExpLim[] handleMult(ExpLim[] c){
bool simplified=false;
DExpr finite=one;
DExprSet unsupported;
DExprSet inf;
bool sign=0;
DExprSet zro;
foreach(f;c){
auto l=f.limit;
if(!l){ unsupported.insert(f.expression); continue; }
l=l.simplify(facts);
if(l.isInfinite()){
sign ^= l == -dInf;
inf.insert(f.expression);
continue;
}
if(l == zero){
zro.insert(f.expression);
continue;
}
if(l.hasLimits()){ unsupported.insert(l); continue; }
finite=finite*l;
simplified=true;
}
//dw(v," ",inf," ",zro," ",finite," ",unsupported);
if(!unsupported.length){
if(!inf.length && !zro.length) return [Case!ExpLim(one,ExpLim(m,finite*ow[0]))];
if(inf.length && !zro.length){
auto lZ=dLtZ(finite*owZNoDeltas).simplify(facts);
auto eqZ=dEqZ(finite*owZNoDeltas).simplify(facts);
auto gZ=dGtZ(finite*owZNoDeltas).simplify(facts);
Case!ExpLim[] r;
if(lZ != zero)
r~=Case!ExpLim(lZ,ExpLim(m,sign?dInf:-dInf));
if(eqZ != zero)
r~=Case!ExpLim(eqZ,ExpLim(m,zero));
if(gZ != zero)
r~=Case!ExpLim(gZ,ExpLim(m,sign?-dInf:dInf));
return r;
}
if(zro.length && !inf.length) return [Case!ExpLim(one,ExpLim(m,zero))];
// Bernoulli-De l'Hôpital.
/+static int nesting = 0;
enum nestingLimit=5; // TODO: probably something like this is necessary.
++nesting; scope(exit) --nesting;
if(nesting>nestingLimit) return null;+/
if(inf.length && zro.length){
auto f=dMult(inf), g=1/dMult(zro);
return doIt(v,e,finite*dDiff(v,f)/dDiff(v,g));
}
}
return [Case!ExpLim(owZneqZ,ExpLim(m,null))];
// TODO: repeated simplification ugly, how to do without?
}
auto res=expandFlatMap!handleMult(r);
return res;
}
if(auto p=cast(DPow)x){
DExpr handlePow(DExpr l0,DExpr l1){
if(!l0 || !l1) return null;
l0=l0.simplify(facts);
l1=l1.simplify(facts);
if(l0.isInfinite()&&l1.isInfinite()){
if(l1 == -dInf||l0 == -dInf) return null;
return dInf;
}
if(l0.hasLimits()||l1.hasLimits()) return null;
if(l0 == dE && l1 == -dInf) return zero;
if(l1 == dInf||l1 == -dInf){
if(l1 == dInf && dBounded!"()"(l0,mone,one).simplify(facts) == one)
return zero;
if(l1 == -dInf && dBounded!"[]"(l0,mone,one).simplify(facts) == zero)
return zero;
return null;
}
if(l0 == -dInf){
if(dGeZ(l1).simplify(facts) == zero)
return zero;
if(auto c=l1.isInteger()){
assert(c.c>=0 && c.c.den==1);
if(c.c.num==0) return one;
return c.c.num%2?-dInf:dInf;
}
return null;
}
if(l0 == dInf){
if(dLeZ(l1).simplify(facts) == zero)
return dInf;
if(dGeZ(l1).simplify(facts) == zero)
return zero;
return null;
}
// pow is discontinuous at (0,0).
auto bad=(dEqZ(l0)*dEqZ(l1)).simplify(facts);
if(bad != zero) return null; // TODO!
return l0^^l1;
}
auto l0=doIt(v,e,p.operands[0]);
auto l1=doIt(v,e,p.operands[1]);
Case!ExpLim[] r;
foreach(el0;l0)
foreach(el1;l1)
r~=Case!ExpLim(el0.condition*el1.condition,
ExpLim(p,handlePow(el0.expression.limit,el1.expression.limit)));
return r;
}
if(auto g=cast(DGaussInt)x){
DExpr handleGaussInt(DExpr l){
if(!l) return null;
if(l.hasLimits()) return null;
if(l == -dInf) return zero;
if(l == dInf) return dΠ^^(one/2);
return dGaussInt(l);
}
auto l=doIt(v,e,g.x);
Case!ExpLim[] r;
foreach(el;l){
r~=Case!ExpLim(el.condition,ExpLim(g,handleGaussInt(el.expression.limit)));
}
return r;
}
if(auto g=cast(DGaussIntInv)x){
DExpr handleGaussIntInv(DExpr l){
if(!l) return null;
if(l.hasLimits()) return null;
if(l == -dInf||l == dInf) return null;
if(l == zero) return -dInf;
if(l == (dΠ^^(one/2)).simplify(one)) return dInf;
return dGaussIntInv(l);
}
auto l=doIt(v,e,g.x);
Case!ExpLim[] r;
foreach(el;l){
r~=Case!ExpLim(el.condition,ExpLim(g,handleGaussIntInv(el.expression.limit)));
}
return r;
}
if(auto ivr=cast(DIvr)x){
DExpr handleIvr(DExpr l){
if(!l) return null;
if(l.hasLimits()) return null;
if(l == -dInf||l==dInf){
with(DIvr.Type){
if(ivr.type==neqZ) return one;
if(ivr.type==eqZ) return zero;
else{
assert(ivr.type==leZ);
return l==-dInf?one:zero;
}
}
}
auto bad=dEqZ(l).simplify(one);
if(bad != zero) return null; // TODO?
return dIvr(ivr.type,l);
}
auto l=doIt(v,e,ivr.e);
Case!ExpLim[] r;
foreach(el;l){
r~=Case!ExpLim(el.condition,ExpLim(ivr,handleIvr(el.expression.limit)));
}
return r;
}
return [];
}
auto k=doIt(v,e,x);
if(!k.length) return null;
foreach(c;k) if(c.condition!=zero && !c.expression.limit) return null;
DExprSet summands;
foreach(c;k){
if(c.condition==zero||c.expression.limit==zero) continue;
DPlus.insert(summands,c.condition*c.expression.limit);
}
return dPlus(summands).simplify(facts);
}