Repository navigation
Expand file tree
/
Copy pathPredicateEntailment.cs
More file actions
280 lines (269 loc) · 15.9 KB
/
Copy pathPredicateEntailment.cs
File metadata and controls
280 lines (269 loc) · 15.9 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
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
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;
namespace AngouriMath.Functions
{
/// <summary>
/// Whether one predicate being true forces another to be true, decided structurally and
/// only where it can be proven.
/// </summary>
/// <remarks>
/// <para>
/// Written for <see cref="Entity.Piecewise"/>, which takes its first matching case and can
/// therefore drop any case whose predicate entails an earlier one: wherever the later
/// predicate holds the earlier one holds too, so the earlier case is taken and the later is
/// unreachable. That rule was already applied for predicates that are <em>equal</em>; this is
/// the same rule with a wider notion of "already covered".
/// </para>
/// <para>
/// <b>One-directional and deliberately incomplete.</b> Entailment between arbitrary predicates
/// is not decidable, so the only answers here are "proven" and "not proven", and the second is
/// answered <see langword="false"/>. A false negative costs a case that could have been
/// dropped, which is a longer answer; a false positive would delete a reachable case, which is
/// a wrong one. Everything below is therefore a sufficient condition, never a heuristic.
/// </para>
/// <para>
/// <b>Three-valued logic does not complicate it.</b> A case is *taken* only when its predicate
/// is true, so the only thing that has to hold is that a true antecedent forces a true
/// consequent. What either predicate does when it is <c>NaN</c> — over a non-real argument, say
/// — never arises, because a case with a <c>NaN</c> predicate is not taken either way.
/// </para>
/// <para>
/// Part of question I.3 of
/// <a href="https://github.com/asc-community/AngouriMath/issues/1212">#1212</a>, where
/// distributing a binder over a piecewise produces one case per subset of the conditions and
/// most of them are unreachable.
/// </para>
/// </remarks>
internal static class PredicateEntailment
{
/// <summary>
/// Whether <paramref name="stronger"/> being true forces <paramref name="weaker"/> to be
/// true. <see langword="false"/> wherever that is not proven, including where it is true
/// but not by one of the shapes below.
/// </summary>
internal static bool Entails(Entity stronger, Entity weaker)
{
if (stronger == weaker)
return true;
return (stronger, weaker) switch
{
// To force a conjunction is to force both halves of it.
(_, Andf(var left, var right)) => Entails(stronger, left) && Entails(stronger, right),
// A conjunction is at least as strong as either half.
(Andf(var left, var right), _) => Entails(left, weaker) || Entails(right, weaker),
// A disjunction forces something only if both halves do.
(Orf(var left, var right), _) => Entails(left, weaker) && Entails(right, weaker),
// Forcing either half of a disjunction forces the disjunction.
(_, Orf(var left, var right)) => Entails(stronger, left) || Entails(stronger, right),
_ => ComparisonEntails(stronger, weaker)
};
}
/// <summary>
/// Two comparisons of the same expression against numbers: <c>a > 2</c> forces
/// <c>a > 1</c>, and <c>a >= 2</c> forces <c>a > 1</c>, but not the other way
/// about. Both are read into the same shape first, so that <c>1 < a</c> and
/// <c>a > 1</c> are the one statement they are.
/// </summary>
private static bool ComparisonEntails(Entity stronger, Entity weaker)
{
if (!TryRead(stronger, out var strongSubject, out var strongAbove, out var strongBound, out var strongStrict)
|| !TryRead(weaker, out var weakSubject, out var weakAbove, out var weakBound, out var weakStrict))
return false;
// The same unknown, and both bounding it from the same side. A lower bound says
// nothing about an upper one.
if (strongSubject != weakSubject || strongAbove != weakAbove)
return false;
var comparison = strongBound.EDecimal.CompareTo(weakBound.EDecimal);
// Bounding from below: `subject > s` forces `subject > w` when s is at least w, and
// where the bounds are equal only if the stronger is no looser about the endpoint.
// Bounding from above the comparison is the other way round.
var atLeastAsTight = strongAbove ? comparison >= 0 : comparison <= 0;
if (!atLeastAsTight)
return false;
// At the same bound, `>=` does not force `>` -- they differ exactly at the endpoint.
return comparison != 0 || strongStrict || !weakStrict;
}
/// <summary>
/// A comparison as "this expression is above / below this number", or
/// <see langword="false"/> where it is not one. <paramref name="above"/> is whether the
/// subject is bounded from below, <paramref name="strict"/> whether the endpoint itself
/// is excluded.
/// </summary>
private static bool TryRead(Entity predicate, out Entity subject, out bool above, out Real bound, out bool strict)
{
subject = predicate;
above = false;
bound = Integer.Zero;
strict = false;
switch (predicate)
{
// subject > bound, subject >= bound
case Greaterf(var left, var right) when Number(right) is { } b:
(subject, above, bound, strict) = (left, true, b, true);
return true;
case GreaterOrEqualf(var left, var right) when Number(right) is { } b:
(subject, above, bound, strict) = (left, true, b, false);
return true;
// bound < subject, bound <= subject -- the same two, written the other way.
case Lessf(var left, var right) when Number(left) is { } b:
(subject, above, bound, strict) = (right, true, b, true);
return true;
case LessOrEqualf(var left, var right) when Number(left) is { } b:
(subject, above, bound, strict) = (right, true, b, false);
return true;
// subject < bound, subject <= bound
case Lessf(var left, var right) when Number(right) is { } b:
(subject, above, bound, strict) = (left, false, b, true);
return true;
case LessOrEqualf(var left, var right) when Number(right) is { } b:
(subject, above, bound, strict) = (left, false, b, false);
return true;
// bound > subject, bound >= subject
case Greaterf(var left, var right) when Number(left) is { } b:
(subject, above, bound, strict) = (right, false, b, true);
return true;
case GreaterOrEqualf(var left, var right) when Number(left) is { } b:
(subject, above, bound, strict) = (right, false, b, false);
return true;
default:
return false;
}
// A finite real literal, which is what a bound has to be for the comparison of two
// of them to mean anything. `+oo` is excluded deliberately: it is a value here, and
// reasoning about it as an endpoint is not what this is for.
static Real? Number(Entity what)
=> what.Evaled is Real { IsFinite: true } real ? real : null;
}
/// <summary>
/// <paramref name="predicate"/> with every conjunct that <paramref name="expression"/>
/// implies on its own taken off: a condition <c>not e = 0</c> says nothing where the
/// expression has no value at the zeros of <c>e</c> anyway. <see cref="Entity.Boolean.True"/>
/// where nothing is left.
/// </summary>
/// <remarks>
/// <para>
/// <c>(1 + ln(x)) x^x provided not x = 0</c> is <c>(1 + ln(x)) x^x</c>: <c>0^0</c> has no
/// value, so the condition excludes a point the expression does not reach, and a reader
/// is told twice what the expression says once.
/// https://github.com/asc-community/AngouriMath/issues/1394
/// </para>
/// <para>
/// <b>Decided by the expression's own domain condition, under the codomain.</b> A
/// <c>provided</c> says where the expression has a value, so a condition is redundant
/// exactly where the expression has none in the codomain the question is asked in --
/// and what an expression accepts under a codomain is what
/// <see cref="Entity.DomainConditionIn"/> already says: a quotient accepts a nonzero
/// divisor, a power a nonzero base or a positive exponent, a logarithm a nonzero
/// antilogarithm over the complex plane and a positive one over the reals, and so on
/// through every node. The condition is redundant where that domain condition already
/// excludes the zeros of <c>e</c>: a conjunct <c>not d = 0</c> or <c>d > 0</c> with
/// <c>d</c> vanishing at them -- <c>d</c> being <c>e</c> itself, a positive power of it, a
/// product holding it, a sine, tangent, arcsine or arctangent of such a thing, or a
/// polynomial in the variable <c>e</c> is with no constant term -- or a disjunction every
/// side of which does. Nothing here decides what a node accepts; that stays with the
/// node, and when a complex infinity is written
/// (https://github.com/asc-community/AngouriMath/issues/217) and a quotient's domain
/// condition changes under the codomains that admit it, this follows. Anything not
/// proven keeps its condition: a false negative is a redundant clause, a false positive
/// would claim a value at a point.
/// </para>
/// <para>
/// Decided on the expression as it stands, which matters: <c>x/x provided not x = 0</c>
/// simplifies to <c>1</c>, and <c>1</c> says nothing about zero, so the condition on the
/// simplified body is not redundant and stays.
/// </para>
/// </remarks>
internal static Entity WithoutWhatTheExpressionImplies(Entity expression, Entity predicate)
{
// Asked from the simplification of a `provided`, and the reading below simplifies
// polynomials, which can make a `provided` of their own: one level, and no more.
if (deciding)
return predicate;
deciding = true;
try
{
return Without(expression, predicate);
}
finally
{
deciding = false;
}
}
[System.ThreadStatic] private static bool deciding;
private static Entity Without(Entity expression, Entity predicate)
=> predicate switch
{
Andf(var left, var right) =>
(Without(expression, left), Without(expression, right)) switch
{
(Entity.Boolean { Value: true }, var rest) => rest,
(var rest, Entity.Boolean { Value: true }) => rest,
(var l, var r) => l == left && r == right ? predicate : new Andf(l, r),
},
Notf(Equalsf(var e, var zero)) when zero.Evaled is Complex { IsZero: true } && IsUndefinedAtTheZerosOf(expression, e) => Entity.Boolean.True,
Notf(Equalsf(var zero, var e)) when zero.Evaled is Complex { IsZero: true } && IsUndefinedAtTheZerosOf(expression, e) => Entity.Boolean.True,
_ => predicate,
};
/// <summary>
/// Whether <paramref name="expression"/> has no value wherever <paramref name="e"/> is
/// zero, by its own domain condition read in the ambient codomain
/// (<see cref="Entity.DomainConditionIn"/>): the condition excludes the zeros of
/// <paramref name="e"/> where a conjunct <c>not d = 0</c> or <c>d > 0</c> has <c>d</c>
/// vanishing with <c>e</c>, or a disjunction has every side excluding them --
/// <c>x^x</c>'s <c>not x = 0 or x > 0</c>.
/// </summary>
private static bool IsUndefinedAtTheZerosOf(Entity expression, Entity e)
=> ExcludesTheZerosOf(expression.DomainConditionIn(MathS.Settings.Codomain), e);
private static bool ExcludesTheZerosOf(Entity condition, Entity e)
=> condition switch
{
Andf(var left, var right) => ExcludesTheZerosOf(left, e) || ExcludesTheZerosOf(right, e),
Orf(var left, var right) => ExcludesTheZerosOf(left, e) && ExcludesTheZerosOf(right, e),
Notf(Equalsf(var d, var zero)) when zero.Evaled is Complex { IsZero: true } => VanishesWith(d, e),
Notf(Equalsf(var zero, var d)) when zero.Evaled is Complex { IsZero: true } => VanishesWith(d, e),
Greaterf(var d, var zero) when zero.Evaled is Complex { IsZero: true } => VanishesWith(d, e),
_ => false,
};
/// <summary>
/// Whether <paramref name="d"/> is zero wherever <paramref name="e"/> is: <c>d</c> is
/// <c>e</c>, a positive power of it, a product with such a factor, a sine, tangent,
/// arcsine or arctangent of such a thing, or -- for a variable <c>e</c> -- a polynomial
/// in it without a constant term.
/// </summary>
private static bool VanishesWith(Entity d, Entity e)
{
if (d == e)
return true;
// A function that is zero at zero, of something that vanishes: `sin(x)/x` is `0/0`
// at zero.
if (d is Sinf or Tanf or Arcsinf or Arctanf)
return VanishesWith(d.DirectChildren[0], e);
if (d is Powf(var @base, var power) && power.Evaled is Real { IsPositive: true })
return VanishesWith(@base, e);
if (d is Mulf)
return Mulf.LinearChildren(d).Any(factor => VanishesWith(factor, e));
if (e is Variable variable && d.ContainsNode(variable)
&& TreeAnalyzer.TryGetPolynomial(d, variable, out var polynomial) && polynomial.Count > 0)
return !polynomial.ContainsKey(PeterO.Numbers.EInteger.Zero)
&& polynomial.Keys.All(degree => degree.Sign > 0)
&& polynomial.Values.All(coefficient => !coefficient.ContainsNode(variable));
// A linear factor with a numeric root: `x + 1` vanishes with `x^2 + x`, since the
// latter is zero at -1. Read by substituting the root, which is cheap; a polynomial
// division here was asked on every quotient of every simplification inside the
// integrator and made a decline of `x/sin(x)` take a minute.
if (e is Sumf or Minusf && e.Vars.Count() == 1 && e.Vars.First() is var only && d.ContainsNode(only)
&& TreeAnalyzer.TryGetPolynomial(e, only, out var line) && line.Count == 2
&& line.TryGetValue(PeterO.Numbers.EInteger.One, out var slope) && line.TryGetValue(PeterO.Numbers.EInteger.Zero, out var intercept)
&& slope.Evaled is Complex { IsFinite: true } b && !b.IsZero && intercept.Evaled is Complex { IsFinite: true } a
&& TreeAnalyzer.TryGetPolynomial(d, only, out var polynomialInIt) && polynomialInIt.Values.All(coefficient => coefficient.Evaled is Complex))
return d.Substitute(only, (-a / b).Evaled).Evaled is Complex { IsZero: true };
return false;
}
}
}