Skip to content

Commit 0bb599e

Browse files
Four rewrite rules hold only where they are written to (#2434)
* Cancel a sign times a thing over its absolute value only for a real operand * Turn an inverse function of a numeric reciprocal only for a nonzero numerator * Decide a chain of comparisons by transitivity only where its operands are ordered * Gather a quotient of powers with a whole exponent factor only where it holds Closes #2428. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 86140d4 commit 0bb599e

11 files changed

Lines changed: 323 additions & 16 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ divisor answers at once. A two-sided limit is not changed. Found by limitcheck;
7979
| `"arcsin(-x * log(-1, x))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright(arcsin(-x * log(-1, x)), x, 0)` | `0` |
8080
| `"x * log(2 + x, x)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `0` after 27 s | `0` in 0.25 s |
8181

82+
### A quotient of powers whose exponents differ by a whole factor is gathered only where that holds
83+
84+
**Silent: `a^n / b^(c*n)` and `a^(c*n) / b^n` are no longer gathered into one power unless `n` is whole or both
85+
bases are positive reals**, the condition `a^n / b^n -> (a/b)^n` has carried since
86+
[#802](https://github.com/asc-community/AngouriMath/issues/802). The two rules gathering a whole factor `c` had no
87+
guard. At `x = 1, y = -1, n = 1/2` the quotient `x^n / y^(2n)` is `1 / (-1)^1 = -1`, while the gathered
88+
`(x / y^2)^n` is `1`. Found by the v3 definedness specification; no issue was filed.
89+
90+
| Input | Was (2.6.0) | Now |
91+
|---|---|---|
92+
| `"x^n/y^(2*n)".ToEntity().Simplify()` | `(x / y ^ 2) ^ n` | `x ^ n / y ^ (2 * n)` |
93+
| `"x^(2*n)/y^n".ToEntity().Simplify()` | `(x ^ 2 / y) ^ n` | `x ^ (2 * n) / y ^ n` |
94+
| `"2^n/3^(2*n)".ToEntity().Simplify()` | `(2/9) ^ n` | `(2/9) ^ n`, unchanged |
95+
8296
### A product with a power for a factor is read by the power's exponent's limit in a one-sided limit
8397

8498
**Silent: a one-sided limit of a product whose factors tend to 0 and to an infinity, one of them a power `u^v` whose
@@ -93,6 +107,22 @@ limitcheck; no issue was filed.
93107
| `"(tan(x) + 1)^(x^(x - 1))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `limitright((tan(x) + 1) ^ x ^ (x - 1), x, 0)` | `e` |
94108
| `"tan(x) * x^(x - 1)".ToEntity().Limit("x", 0, ApproachFrom.Right)` | `1` after 11 s | `1` in 0.21 s |
95109

110+
### A chain of comparisons is decided by transitivity only where its operands are ordered
111+
112+
**Silent: `(a > b and b > c) implies a > c`, and the same with `<`, is no longer simplified to `True` for symbols
113+
over the complex plane.** An order comparison has no truth value off the real line, so at `a = i, b = 1, c = 0`
114+
the implication is `NaN`, and the answer `True` gave it one. It is still `True` over the reals
115+
(`MathS.Settings.Codomain` set to `Domain.Real`) and for operands known to be real. A condition `a in RR` cannot be
116+
attached instead, as `x > x` attaches it ([#876](https://github.com/asc-community/AngouriMath/issues/876)): at
117+
`a = i, b = 0, c = 1` the conjunction is false and the implication is `True`. Found by the v3 definedness
118+
specification; no issue was filed.
119+
120+
| Input | Was (2.6.0) | Now |
121+
|---|---|---|
122+
| `"((a > b) and (b > c)) implies (a > c)".ToEntity().Simplify()` | `True` | `a > b and b > c implies a > c` |
123+
| `"((a < b) and (b < c)) implies (a < c)".ToEntity().Simplify()` | `True` | `a < b and b < c implies a < c` |
124+
| the same over `Domain.Real` | `True` | `True`, unchanged |
125+
96126
### A derivative keeps an exponent that is not rational exact
97127

98128
**Silent: the power rule writes a constant exponent that is not rational as it is, where it wrote its decimal
@@ -109,6 +139,19 @@ rational exponent is still written as its value. Found by limitcheck; no issue w
109139
| `"x^(abs(x)^arctan(2))".ToEntity().Limit("x", 0, ApproachFrom.Left)` | unevaluated | `1` |
110140
| `"x^(abs(x)^arctan(2))".ToEntity().Limit("x", 0, ApproachFrom.Right)` | unevaluated | `1` |
111141

142+
### An inverse function of a zero over a symbol is not turned into its reciprocal function
143+
144+
**Silent, through the rule registry only: `arcsin(0 / x)` is no longer rewritten to `arccsc(x / 0)` by
145+
`RewriteRules.Trigonometric`, nor `arccos(0 / x)` to `arcsec(x / 0)`.** The rules read `n / d` as `1 / (d / n)`,
146+
which needs a nonzero `n`; at `n = 0` the input is `0` (or `pi / 2`) wherever `x` is not `0`, and the answer has no
147+
value. `Simplify` folds `0 / x` before the rules are asked, so its answers do not change. Found by the v3
148+
definedness specification; no issue was filed.
149+
150+
| Input | Was (2.6.0) | Now |
151+
|---|---|---|
152+
| `Transformation.Rewriting(RewriteRules.Trigonometric).ApplyOrKeep("arcsin(0/x)")` | `arccsc(x / 0)` | `arcsin(0 / x)` |
153+
| `Transformation.Rewriting(RewriteRules.Trigonometric).ApplyOrKeep("arccos(0/x)")` | `arcsec(x / 0)` | `arccos(0 / x)` |
154+
112155
### A two-sided limit whose sides are undecided is left unevaluated
113156

114157
**Silent: a two-sided limit is left unevaluated, rather than `NaN`, where a side's limit stops at an indeterminate form
@@ -139,6 +182,19 @@ limit is still `NaN`. Found by limitcheck; no issue was filed.
139182
| `"1 / log(x^2, cos(x))".ToEntity().Limit("x", 0)` | `-oo` | `limit(1 / log(x ^ 2, cos(x)), x, 0)` |
140183
| `"1 / (x * arccot(x))".ToEntity().Limit("x", 0)` | `NaN` | `limit(1 / (x * arccotan(x)), x, 0)` |
141184

185+
### A sign times a thing over its absolute value cancels only for a real operand
186+
187+
**Silent, through the rule registry only: `sgn(x) * (y * x) / abs(x)` is no longer rewritten to
188+
`y provided not x = 0` by `RewriteRules.Common` for an `x` not known to be real.** `sgn(x)` is `x / abs(x)`, so the
189+
quotient is `y * x^2 / abs(x)^2`, which is `-y` at `x = i`. Over the reals (`MathS.Settings.Codomain` set to
190+
`Domain.Real`) and for an operand known to be real it still cancels. `Simplify` sorts the product before the rule is
191+
asked, so its answers do not change. Found by the v3 definedness specification; no issue was filed.
192+
193+
| Input | Was (2.6.0) | Now |
194+
|---|---|---|
195+
| `Transformation.Rewriting(RewriteRules.Common).ApplyOrKeep("sgn(x)*(y*x)/abs(x)")` | `y provided not x = 0` | `sgn(x) * y * x / abs(x)` |
196+
| the same over `Domain.Real` | `y provided not x = 0` | `y provided not x = 0`, unchanged |
197+
142198
### A limit at the jump of a step function is not answered by substitution
143199

144200
**Silent: a limit of an expression built on `floor`, `ceil`, `round` or `sgn`, where the step's argument arrives on one

‎Sources/AngouriMath/Core/Transformations/Matching/MatchedRules.cs‎

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,12 +2057,15 @@ is var (divided, remainder)
20572057
// larger the angle is.
20582058
growth: RewriteRuleGrowth.Collects),
20592059

2060+
// The four rules below are the definitions arccsc(z) = arcsin(1 / z) and its siblings,
2061+
// read through n / d = 1 / (d / n), which holds for a nonzero n. At n = 0 the left
2062+
// side is arcsin(0) = 0 and the right arccsc(d / 0), which has no value.
20602063
new MatchedRule(
20612064
"an-arcsine-of-a-numeric-reciprocal-is-an-arccosecant",
20622065
MatchPattern.Node<Arcsinf>(MatchPattern.Node<Divf>(MatchPattern.Any("n"), MatchPattern.Any("d"))),
20632066
bound => new Arccosecantf(bound["d"] / bound["n"]),
20642067
Soundness.SoundUnderAssumptions,
2065-
when: bound => bound["n"] is Number && bound["d"] is not Number,
2068+
when: bound => bound["n"] is Number && bound["n"] != Integer.Zero && bound["d"] is not Number,
20662069
description: "arcsin(1 / c) = arccosec(c), for a numeric c",
20672070
// One inverse function becomes another and the quotient stays a quotient with its
20682071
// operands the other way round, so the two are the same size whatever they are.
@@ -2073,7 +2076,7 @@ is var (divided, remainder)
20732076
MatchPattern.Node<Arccosf>(MatchPattern.Node<Divf>(MatchPattern.Any("n"), MatchPattern.Any("d"))),
20742077
bound => new Arcsecantf(bound["d"] / bound["n"]),
20752078
Soundness.SoundUnderAssumptions,
2076-
when: bound => bound["n"] is Number && bound["d"] is not Number,
2079+
when: bound => bound["n"] is Number && bound["n"] != Integer.Zero && bound["d"] is not Number,
20772080
description: "arccos(1 / c) = arcsec(c), for a numeric c",
20782081
// One inverse function becomes another and the quotient stays a quotient with its
20792082
// operands the other way round, so the two are the same size whatever they are.
@@ -2084,7 +2087,7 @@ is var (divided, remainder)
20842087
MatchPattern.Node<Arccosecantf>(MatchPattern.Node<Divf>(MatchPattern.Any("n"), MatchPattern.Any("d"))),
20852088
bound => new Arcsinf(bound["d"] / bound["n"]),
20862089
Soundness.SoundUnderAssumptions,
2087-
when: bound => bound["n"] is Number && bound["d"] is not Number,
2090+
when: bound => bound["n"] is Number && bound["n"] != Integer.Zero && bound["d"] is not Number,
20882091
description: "arccosec(1 / c) = arcsin(c), for a numeric c",
20892092
// One inverse function becomes another and the quotient stays a quotient with its
20902093
// operands the other way round, so the two are the same size whatever they are.
@@ -2095,7 +2098,7 @@ is var (divided, remainder)
20952098
MatchPattern.Node<Arcsecantf>(MatchPattern.Node<Divf>(MatchPattern.Any("n"), MatchPattern.Any("d"))),
20962099
bound => new Arccosf(bound["d"] / bound["n"]),
20972100
Soundness.SoundUnderAssumptions,
2098-
when: bound => bound["n"] is Number && bound["d"] is not Number,
2101+
when: bound => bound["n"] is Number && bound["n"] != Integer.Zero && bound["d"] is not Number,
20992102
description: "arcsec(1 / c) = arccos(c), for a numeric c",
21002103
// One inverse function becomes another and the quotient stays a quotient with its
21012104
// operands the other way round, so the two are the same size whatever they are.
@@ -2546,17 +2549,26 @@ is var (divided, remainder)
25462549
bound => Functions.Patterns.PushNotInside(Orf.LinearChildren((Orf)bound["chain"]), disjoin: false),
25472550
Soundness.Sound,
25482551
description: "not (a or b or ...) = (not a) and (not b) and ..."),
2552+
// Transitivity is a fact about an order, so the pair below fires only where the
2553+
// three operands are known to be ordered. Elsewhere the implication need not have a
2554+
// truth value -- at a = i, b = 1, c = 0 it is NaN -- and the ordered condition cannot
2555+
// be attached instead, as its siblings attach it: a comparison with no truth value
2556+
// inside a conjunction that is already false leaves the implication true, so at
2557+
// a = i, b = 0, c = 1 the chain of greaters is True and `True provided a in RR` is
2558+
// not. https://github.com/asc-community/AngouriMath/issues/876
25492559
new MatchedRule(
25502560
"a-chain-of-greaters-implies-its-own-ends",
25512561
MatchPattern.Node<Impliesf>(MatchPattern.Node<Andf>(MatchPattern.Node<Greaterf>(MatchPattern.Any("a"), MatchPattern.Any("b")), MatchPattern.Node<Greaterf>(MatchPattern.Any("b"), MatchPattern.Any("c"))), MatchPattern.Node<Greaterf>(MatchPattern.Any("a"), MatchPattern.Any("c"))),
25522562
bound => Entity.Boolean.True.Provided(bound["a"].DomainCondition).Provided(bound["b"].DomainCondition).Provided(bound["c"].DomainCondition),
25532563
Soundness.SoundUnderAssumptions,
2564+
when: bound => Functions.Patterns.AreOrdered(bound["a"], bound["b"], bound["c"]),
25542565
description: "(((a > b) and (b > c)) implies (a > c)) = True"),
25552566
new MatchedRule(
25562567
"a-chain-of-lesss-implies-its-own-ends",
25572568
MatchPattern.Node<Impliesf>(MatchPattern.Node<Andf>(MatchPattern.Node<Lessf>(MatchPattern.Any("a"), MatchPattern.Any("b")), MatchPattern.Node<Lessf>(MatchPattern.Any("b"), MatchPattern.Any("c"))), MatchPattern.Node<Lessf>(MatchPattern.Any("a"), MatchPattern.Any("c"))),
25582569
bound => Entity.Boolean.True.Provided(bound["a"].DomainCondition).Provided(bound["b"].DomainCondition).Provided(bound["c"].DomainCondition),
25592570
Soundness.SoundUnderAssumptions,
2571+
when: bound => Functions.Patterns.AreOrdered(bound["a"], bound["b"], bound["c"]),
25602572
description: "(((a < b) and (b < c)) implies (a < c)) = True"),
25612573
new MatchedRule(
25622574
"a-equals-with-zero-on-the-left-turns-round",
@@ -3084,6 +3096,13 @@ is var (divided, remainder)
30843096
MatchPattern.Any("n")))),
30853097
bound => new Powf(bound["a"] / new Powf(bound["b"], bound["c"]), bound["n"]),
30863098
Soundness.SoundUnderAssumptions,
3099+
// The guard of the quotient rule above, for the same reason: b ^ (c * n) is
3100+
// (b ^ c) ^ n, and a ^ n / (b ^ c) ^ n is (a / b ^ c) ^ n, only for a whole n or
3101+
// positive real bases. At a = 1, b = -1, n = 1/2 the quotient is
3102+
// 1 / (-1) ^ 1 = -1 and the gathered power (1 / 1) ^ (1/2) = 1.
3103+
when: bound => bound["n"] is Integer
3104+
|| (bound["a"].Evaled is Real { IsPositive: true }
3105+
&& bound["b"].Evaled is Real { IsPositive: true }),
30873106
description: "a ^ n / b ^ (c * n) = (a / b ^ c) ^ n, for a positive whole c",
30883107
// The exponent is matched twice and written once, and one power node goes: the delta
30893108
// is -(1 + |n|), at most -2 and further the larger the exponent is.
@@ -3100,6 +3119,11 @@ is var (divided, remainder)
31003119
MatchPattern.Node<Powf>(MatchPattern.Any("b"), MatchPattern.Any("n"))),
31013120
bound => new Powf(new Powf(bound["a"], bound["c"]) / bound["b"], bound["n"]),
31023121
Soundness.SoundUnderAssumptions,
3122+
// Guarded as its mirror is: at a = -1, b = 1, n = 1/2 the quotient is -1 and the
3123+
// gathered power ((-1) ^ 2 / 1) ^ (1/2) is 1.
3124+
when: bound => bound["n"] is Integer
3125+
|| (bound["a"].Evaled is Real { IsPositive: true }
3126+
&& bound["b"].Evaled is Real { IsPositive: true }),
31033127
description: "a ^ (c * n) / b ^ n = (a ^ c / b) ^ n, for a positive whole c",
31043128
// The exponent is matched twice and written once, and one power node goes with it:
31053129
// the delta is -(1 + |n|), the mirror of the rule that takes it into the divisor.
@@ -4015,7 +4039,12 @@ is var (divided, remainder)
40154039
MatchPattern.Node<Divf>(MatchPattern.Node<Mulf>(MatchPattern.Node<Signumf>(MatchPattern.Any("a")), MatchPattern.Node<Mulf>(MatchPattern.Any("b"), MatchPattern.Any("a"))), MatchPattern.Node<Absf>(MatchPattern.Any("a"))),
40164040
bound => bound["b"].Provided(!bound["a"].EqualTo(Integer.Zero)),
40174041
Soundness.SoundUnderAssumptions,
4018-
description: "(sgn(a) * (b * a)) / abs(a) = b, provided a is not zero",
4042+
// sgn(a) is a / abs(a), so sgn(a) * a / abs(a) is a^2 / abs(a)^2: 1 for a real a and
4043+
// not otherwise -- at a = i it is -1. Realness is a reading, not a condition on
4044+
// where either side is defined, so it is decided here, as the comparison rules
4045+
// decide it, and the rule does not fire on an operand not known to be real.
4046+
when: bound => Functions.Patterns.OrderedConditionFor(bound["a"]) == Entity.Boolean.True,
4047+
description: "(sgn(a) * (b * a)) / abs(a) = b, for a real a, provided a is not zero",
40194048
// Three copies of a go and one comes back inside the condition; the sign, the
40204049
// absolute value, the quotient and two products go against the three nodes the
40214050
// condition and its attachment add: the delta is -1 - 2|a|, at most -3.

‎Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Common.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ internal static Integer DenominatorOf(Entity entity)
263263

264264
Mulf(Absf(var any1), Absf(var any2)) => new Absf(any1 * any2),
265265
Divf(Absf(var any1), Absf(var any2)) => new Absf(any1 / any2),
266-
Divf(Mulf(Signumf(var any1), Mulf(var any2, var any1a)), Absf(var any1b)) when any1 == any1a && any1a == any1b => any2.Provided(!any1.EqualTo(Integer.Zero)),
266+
// sgn(a) * a / abs(a) is 1 only for a real a: at a = i it is -1.
267+
Divf(Mulf(Signumf(var any1), Mulf(var any2, var any1a)), Absf(var any1b)) when any1 == any1a && any1a == any1b && OrderedCondition(any1) == Entity.Boolean.True => any2.Provided(!any1.EqualTo(Integer.Zero)),
267268
Mulf(Signumf(var any1), Absf(var any1a)) when any1 == any1a => any1,
268269
Mulf(Absf(var any1a), Signumf(var any1)) when any1 == any1a => any1,
269270

‎Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.EqualityInequality.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ internal static Entity OrderedConditionOf(Entity comparison, int operand)
136136
/// <inheritdoc cref="OrderedConditionOf"/>
137137
internal static Entity OrderedConditionFor(Entity entity) => OrderedCondition(entity);
138138

139+
/// <summary>
140+
/// Whether every operand is known to be ordered, so that a chain of comparisons between
141+
/// them can be decided by transitivity. Asked rather than attached: see the rule
142+
/// <c>a-chain-of-greaters-implies-its-own-ends</c>.
143+
/// </summary>
144+
internal static bool AreOrdered(params Entity[] operands)
145+
=> operands.All(operand => OrderedCondition(operand) == True);
146+
139147
/// <summary>
140148
/// Whether pushing a <c>not</c> through this conjunction or disjunction leaves the
141149
/// expression no more complex: at most one operand may be something other than a
@@ -195,10 +203,10 @@ internal static Entity PushNotInside(IEnumerable<Entity> operands, bool disjoin)
195203
=> PushNotInside(Orf.LinearChildren(a), disjoin: false),
196204

197205
Impliesf(Andf(Greaterf(var any1, var any2), Greaterf(var any2a, var any3)), Greaterf(var any1a, var any3a))
198-
when any1 == any1a && any2 == any2a && any3 == any3a => True.Provided(any1.DomainCondition).Provided(any2.DomainCondition).Provided(any3.DomainCondition),
206+
when any1 == any1a && any2 == any2a && any3 == any3a && AreOrdered(any1, any2, any3) => True.Provided(any1.DomainCondition).Provided(any2.DomainCondition).Provided(any3.DomainCondition),
199207

200208
Impliesf(Andf(Lessf(var any1, var any2), Lessf(var any2a, var any3)), Lessf(var any1a, var any3a))
201-
when any1 == any1a && any2 == any2a && any3 == any3a => True.Provided(any1.DomainCondition).Provided(any2.DomainCondition).Provided(any3.DomainCondition),
209+
when any1 == any1a && any2 == any2a && any3 == any3a && AreOrdered(any1, any2, any3) => True.Provided(any1.DomainCondition).Provided(any2.DomainCondition).Provided(any3.DomainCondition),
202210

203211
Equalsf(var zero, var anyButZero) when IsZero(zero) && !IsZero(anyButZero) => anyButZero.EqualTo(zero),
204212
Greaterf(var zero, var anyButZero) when IsZero(zero) && !IsZero(anyButZero) => anyButZero < zero,

‎Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.Power.cs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,21 @@ internal static bool IsWholeOrAnOddRoot(Number n) =>
154154
// These read that pair back. Restricted to a whole c so that nothing gains a
155155
// root it did not have -- b^c goes into the base, rather than the exponent
156156
// being divided. https://github.com/asc-community/AngouriMath/issues/740
157+
// And guarded as the rule above is, since gathering the exponent is the same move:
158+
// at {1} = 1, {2} = -1, n = 1/2 the quotient is 1 / (-1) ^ 1 = -1 and the gathered
159+
// power (1 / 1) ^ (1/2) is 1.
157160
Divf(Powf(var any1, var any3), Powf(var any2, Mulf(Integer { IsPositive: true } const1, var any3a)))
158-
when any3 == any3a => new Powf(any1 / new Powf(any2, const1), any3),
161+
when any3 == any3a
162+
&& (any3 is Integer
163+
|| (any1.Evaled is Real { IsPositive: true }
164+
&& any2.Evaled is Real { IsPositive: true }))
165+
=> new Powf(any1 / new Powf(any2, const1), any3),
159166
Divf(Powf(var any1, Mulf(Integer { IsPositive: true } const1, var any3)), Powf(var any2, var any3a))
160-
when any3 == any3a => new Powf(new Powf(any1, const1) / any2, any3),
167+
when any3 == any3a
168+
&& (any3 is Integer
169+
|| (any1.Evaled is Real { IsPositive: true }
170+
&& any2.Evaled is Real { IsPositive: true }))
171+
=> new Powf(new Powf(any1, const1) / any2, any3),
161172

162173
// x / x^n
163174
Divf(var any1, Powf(var any1a, var any2)) when any1 == any1a => new Powf(any1, 1 - any2),

0 commit comments

Comments
 (0)