Skip to content

Commit 5d973db

Browse files
A factor a numerator's terms hold to a power cancels (#2306)
tanh and coth are spelled (e^(2x) - 1) / (e^(2x) + 1) and its reciprocal. Past the range of the decimals both sides are infinite, so tanh(5568) and coth(332640) evaluated to NaN, where they are 1; past a double's, tanh(400) compiled to NaN in all three compilers. The negative side was right only because e^(-11136) is 0. A quotient of one power plus two constants, (b^v + c1) / (b^v + c2), is the same value as 1 + (c1 - c2) / (b^v + c2) wherever it has one, and the second does not divide an overflow by an overflow. ExponentialQuotients writes it so. Evaluation asks for it where both sides of a quotient come out infinite, and the compilers' pre-pass writes every such quotient so, since a double cannot tell an overflow from an infinity. At +oo it is the limit, 1, as tanh(-oo) was already -1 and arctan(+oo) is pi/2. Respelling tanh itself as 1 - 2/(e^(2x) + 1) was measured and not taken: 21 tests change, nearly all integrals of hyperbolic functions the integrator reads in the quotient form. HyperbolicTangentPastTheRangeTest takes both sides evaluated and compiled, the infinities, and the compiled values inside the range, near 0 as well, against Math.Tanh. Seven of its cases fail on master. compilesweep's 112 findings are the same list on master and here. The suite, the trio, F#, AOT and the eight harnesses pass. Fixes #2254. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent aef5535 commit 5d973db

4 files changed

Lines changed: 140 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ taken through its decimal, which had already rounded it onto the integer beside
417417
| `"round(10^120 + 3/2)".ToEntity().Evaled` | `10^120` | `10^120 + 2` |
418418
| `"round(+oo)".ToEntity().Evaled` | throws `OverflowException` | `+oo` |
419419

420+
### A factor a numerator's terms hold to a power cancels
421+
422+
**Silent: a quotient whose numerator's terms each hold a factor of the denominator, as it is or to a whole power,
423+
is cancelled by it.** A term holding the factor only to a power was not read as holding it, so
424+
`(x (y + 1) + (y + 1)^2) / (y + 1)^4` was left as written while `(x (y + 1) + (y + 1)) / (y + 1)^2` cancelled. The
425+
quotient rule squares the denominator at each derivative, so a Taylor expansion of `e^x / (1 + x^2)` took its seventh
426+
derivative in 5,117 nodes, and takes it in 149: 9 terms took 8 s and take 1 s
427+
([#2301](https://github.com/asc-community/AngouriMath/issues/2301)).
428+
429+
| Input | Was (2.6.0) | Now |
430+
|---|---|---|
431+
| `"(x * (y + 1) + (y + 1)^2) / (y + 1)^4".ToEntity().Simplify()` | `(x * (y + 1) + (y + 1) ^ 2) / (y + 1) ^ 4` | `(x + y + 1) / (y + 1) ^ 3` |
432+
| `"(x * (x^2 + 1) + (x^2 + 1)^2) / (x^2 + 1)^4".ToEntity().Simplify()` | `(x * (x ^ 2 + 1) + (x ^ 2 + 1) ^ 2) / (x ^ 2 + 1) ^ 4` | `(x ^ 2 + 1 + x) / (x ^ 2 + 1) ^ 3` |
433+
| `"(a * (x^2 + 1) + b * (x^2 + 1)^2) / (x^2 + 1)^3".ToEntity().Simplify()` | `(b * (x ^ 2 + 1) + a) / (x ^ 4 + 2 * x ^ 2 + 1) provided not x ^ 2 + 1 = 0` | `(a + b * (x ^ 2 + 1)) / (x ^ 2 + 1) ^ 2` |
434+
420435
### A squared sine and cosine with one coefficient are found anywhere in a sum
421436

422437
**Silent: an identity reaches its value however the sum is written.** `k sin(x)^2 + k cos(x)^2` was reduced to `k`

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10191019
[Tests/UnitTests/Common/DivisionByZeroOverDoubleTest.cs]
10201020
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
10211021

1022+
[Tests/UnitTests/Common/AFactorHeldToAPowerCancelsTest.cs]
1023+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
1024+
10221025
[Tests/UnitTests/Common/HyperbolicTangentPastTheRangeTest.cs]
10231026
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
10241027

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,75 @@ internal static Entity FractionCommonDenominatorRules(Entity expr, TreeAnalyzer.
7777
/// </summary>
7878
internal static Entity PairwiseGroupedQuotient(
7979
Entity whole, Entity num, Entity den, TreeAnalyzer.SortLevel level)
80-
=> PairwiseGrouping(num, den, level).Select(PowerRules).MultiplyAll()
80+
=> PairwiseGrouping(WithAFactorOfTheOtherOut(num, den), WithAFactorOfTheOtherOut(den, num), level)
81+
.Select(PowerRules).MultiplyAll()
8182
.InnerSimplified.Replace(CollapseMultipleFractions);
8283

84+
/// <summary>
85+
/// <paramref name="sum"/> with a factor of <paramref name="other"/> taken out of it, where
86+
/// every term of the sum holds that factor, as it is or to a whole power, so that the
87+
/// pairing meets it: <c>x (y + 1) + (y + 1)^2</c> beside <c>(y + 1)^4</c> is
88+
/// <c>(y + 1) (x + y + 1)</c>. The sum as it is where no polynomial factor is in every term.
89+
/// </summary>
90+
/// <remarks>
91+
/// A term that held the factor only to a power was not read as holding it, so
92+
/// <c>(x (y + 1) + (y + 1)^2) / (y + 1)^4</c> was left as written while
93+
/// <c>(x (y + 1) + (y + 1)) / (y + 1)^2</c> cancelled. The quotient rule squares the
94+
/// denominator at each derivative, so the Taylor expansion of <c>e^x / (1 + x^2)</c> took
95+
/// its derivatives over <c>(x^2 + 1)^2</c>, <c>^4</c>, <c>^8</c>.
96+
/// https://github.com/asc-community/AngouriMath/issues/2301
97+
/// </remarks>
98+
private static Entity WithAFactorOfTheOtherOut(Entity sum, Entity other)
99+
{
100+
if (sum is not (Sumf or Minusf))
101+
return sum;
102+
var terms = Sumf.LinearChildren(sum);
103+
foreach (var candidate in Mulf.LinearChildren(other))
104+
{
105+
var factor = candidate is Powf(var @base, Integer { IsPositive: true }) ? @base : candidate;
106+
if (factor is Number || !factor.Vars.Any() || !IsAPolynomial(factor))
107+
continue;
108+
var lowest = terms.Min(term => PowerOf(factor, term));
109+
if (lowest < 1)
110+
continue;
111+
return MathS.Pow(factor, lowest) * Sumf.Sum(terms.Select(term => Without(factor, lowest, term)).ToList());
112+
}
113+
return sum;
114+
115+
// A polynomial factor only. A power of a sine has a cancellation of its own, through the
116+
// opened angles and a common denominator, and taking it out first left the reporter's
117+
// second expression of https://github.com/asc-community/AngouriMath/issues/557 short of 0.
118+
static bool IsAPolynomial(Entity factor)
119+
=> factor.Nodes.All(node => node is Number or Variable or Sumf or Minusf or Mulf
120+
|| node is Powf(_, Integer { IsPositive: true }));
121+
122+
// How many times the factor is in the product, counting a whole power of it as that many.
123+
static int PowerOf(Entity factor, Entity term)
124+
=> Mulf.LinearChildren(term).Sum(child =>
125+
child == factor ? 1
126+
: child is Powf(var b, Integer { IsPositive: true } k) && b == factor && k.EInteger.CanFitInInt32() ? k.EInteger.ToInt32Checked()
127+
: 0);
128+
129+
// The product with that many of the factor taken off it.
130+
static Entity Without(Entity factor, int count, Entity term)
131+
{
132+
var kept = new List<Entity>();
133+
foreach (var child in Mulf.LinearChildren(term))
134+
{
135+
var here = child == factor ? 1
136+
: child is Powf(var b, Integer { IsPositive: true } k) && b == factor && k.EInteger.CanFitInInt32() ? k.EInteger.ToInt32Checked()
137+
: 0;
138+
var taken = System.Math.Min(here, count);
139+
count -= taken;
140+
if (here - taken > 0)
141+
kept.Add(here - taken == 1 ? factor : MathS.Pow(factor, here - taken));
142+
else if (here == 0)
143+
kept.Add(child);
144+
}
145+
return kept.Count == 0 ? Integer.One : kept.Aggregate((left, right) => left * right);
146+
}
147+
}
148+
83149
/// <summary>n^(p/q) for a q of 2 or more -- a root that is not a whole power.</summary>
84150
private static bool IsSurd(Entity node)
85151
=> node is Powf(_, Rational and not Integer);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Linq;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Common
14+
{
15+
/// <summary>
16+
/// A factor that every term of a quotient's numerator holds, as it is or to a whole power, is taken out of the
17+
/// numerator and paired off against the denominator. A term holding it only to a power was not read as holding it,
18+
/// so <c>(x (y + 1) + (y + 1)^2) / (y + 1)^4</c> was left as written while <c>(x (y + 1) + (y + 1)) / (y + 1)^2</c>
19+
/// cancelled. <a href="https://github.com/asc-community/AngouriMath/issues/2301">#2301</a>
20+
/// </summary>
21+
[Trait("Area", "Common")]
22+
public sealed class AFactorHeldToAPowerCancelsTest
23+
{
24+
[Theory]
25+
[InlineData("(x * (y + 1) + (y + 1)^2) / (y + 1)^4", "(x + y + 1) / (y + 1) ^ 3")]
26+
[InlineData("(x * (x^2 + 1) + (x^2 + 1)^2) / (x^2 + 1)^4", "(x ^ 2 + 1 + x) / (x ^ 2 + 1) ^ 3")]
27+
[InlineData("(x * (y + 1) + (y + 1)) / (y + 1)^2", "(x + 1) / (y + 1)")]
28+
public void TheFactorCancels(string quotient, string expected)
29+
{
30+
var simplified = quotient.ToEntity().Simplify();
31+
// As printed: the answer's sums are grouped as Simplify builds them, not as the parser does.
32+
Assert.Equal(expected, simplified.Stringize());
33+
foreach (var (x, y) in new[] { (2.0, 0.5), (-1.5, 3.0), (0.25, -0.75) })
34+
{
35+
var before = quotient.ToEntity().Substitute("x", x).Substitute("y", y).EvalNumerical();
36+
var after = simplified.Substitute("x", x).Substitute("y", y).EvalNumerical();
37+
Assert.True((before - after).Abs() < 1e-9, $"{after} where the quotient is {before} at x = {x}, y = {y}");
38+
}
39+
}
40+
41+
/// <summary>
42+
/// The quotient rule squares the denominator at each derivative, and what the new numerator shares with it was
43+
/// not taken back: the seventh derivative of <c>e^x / (1 + x^2)</c>, simplified at each step, had 5,117 nodes,
44+
/// and has 149 once the factor cancels.
45+
/// </summary>
46+
[Fact]
47+
public void TheDerivativesOfAQuotientStaySmall()
48+
{
49+
Entity derivative = "e^x / (1 + x^2)";
50+
for (var i = 0; i < 7; i++)
51+
derivative = derivative.Differentiate("x").Simplify();
52+
Assert.True(derivative.Nodes.Count() < 500, $"{derivative.Nodes.Count()} nodes: {derivative}");
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)