Skip to content

Commit e8cc9d8

Browse files
The substitution search drops a trigonometric candidate its quotient cannot be a function of (#1493)
A candidate u that takes one value at two points, where the integrand over du/dx takes two, is no substitution whatever the simplifier makes of it. A sine of a linear repeats at pi - theta, a cosine at -theta and a tangent a period on, so two evaluations with the symbols pinned settle it before the quotient is written in u and simplified, which is milliseconds per candidate. Rubi's 4.7.2, 4.3.10 and 4.4.10: the problems the integrator answers take 22% less time (154.2 -> 120.5 s and 153.4 -> 119.5 s, two interleaved pairs), with every verdict and every answer unchanged. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f3a2056 commit e8cc9d8

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15262,6 +15262,61 @@ private static bool TryWriteAPairOfLinearsOverARadicand(Entity expr, Entity.Vari
1526215262
};
1526315263
}
1526415264

15265+
/// <summary>
15266+
/// Whether <paramref name="expr"/> over <paramref name="derivative"/> takes two values at
15267+
/// two points where <paramref name="candidate"/>, a sine, cosine, tangent, cotangent,
15268+
/// secant or cosecant of a linear, takes one -- every other symbol pinned -- which no
15269+
/// function of the candidate does. False for any other candidate, and wherever a value
15270+
/// cannot be had or the candidate itself does not repeat there.
15271+
/// </summary>
15272+
private static bool TheQuotientDisagreesWhereTheCandidateRepeats(Entity expr, Entity derivative, Entity candidate, Entity.Variable x)
15273+
{
15274+
Entity? argument = candidate switch
15275+
{
15276+
Sinf(var a) => a, Cosf(var a) => a, Tanf(var a) => a,
15277+
Cotanf(var a) => a, Secantf(var a) => a, Cosecantf(var a) => a,
15278+
_ => null,
15279+
};
15280+
if (argument is null || !TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var intercept)
15281+
|| slope.Evaled is Number.Complex { IsZero: true })
15282+
return false;
15283+
Entity first = Number.Real.Create(EDecimal.FromString("0.37"));
15284+
Entity second = candidate switch
15285+
{
15286+
Sinf or Cosecantf => (MathS.pi - 2 * intercept) / slope - first,
15287+
Cosf or Secantf => -2 * intercept / slope - first,
15288+
_ => first + MathS.pi / slope,
15289+
};
15290+
var pinned = new Dictionary<Variable, Entity>();
15291+
var values = new[] { "1.37", "0.61", "2.23", "1.91", "0.83", "1.13" };
15292+
var index = 0;
15293+
foreach (var symbol in expr.Vars.Concat(derivative.Vars).Concat(candidate.Vars).Distinct())
15294+
if (symbol != x)
15295+
pinned[symbol] = Number.Real.Create(EDecimal.FromString(values[index++ % values.Length]));
15296+
Number.Complex? At(Entity function, Entity point)
15297+
{
15298+
var value = function.Substitute(x, point);
15299+
foreach (var pair in pinned)
15300+
value = value.Substitute(pair.Key, pair.Value);
15301+
try
15302+
{
15303+
return value.EvalNumerical() is Number.Complex { IsNaN: false } number && number.IsFinite ? number : null;
15304+
}
15305+
catch (Core.Exceptions.CannotEvalException) { return null; }
15306+
}
15307+
static bool Close(Number.Complex left, Number.Complex right, double tolerance)
15308+
{
15309+
var scale = System.Math.Max(1.0, ((Number.Real)left.Abs()).EDecimal.ToDouble());
15310+
return ((Number.Real)(left - right).Abs()).EDecimal.ToDouble() <= tolerance * scale;
15311+
}
15312+
if (At(candidate, first) is not { } candidateFirst || At(candidate, second) is not { } candidateSecond
15313+
|| !Close(candidateFirst, candidateSecond, 1e-9))
15314+
return false;
15315+
var quotient = expr / derivative;
15316+
return At(quotient, first) is { } quotientFirst && At(quotient, second) is { } quotientSecond
15317+
&& !Close(quotientFirst, quotientSecond, 1e-6);
15318+
}
15319+
1526515320
/// <summary>
1526615321
/// Whether <paramref name="expr"/>/<paramref name="reference"/> takes the same value at two
1526715322
/// points, every other symbol pinned to a fixed value -- a necessary condition for a
@@ -16415,6 +16470,16 @@ private static bool IsARationalFunctionOfExponentials(Entity expr, Entity.Variab
1641516470
if (!u.ContainsNode(x) || duDx.Evaled == 0)
1641616471
continue;
1641716472

16473+
// A candidate that takes one value at two points, where the quotient by its
16474+
// derivative takes two, is no substitution, whatever the simplifier makes of it:
16475+
// a function of u agrees wherever u does. A sine repeats at pi - theta, a cosine at
16476+
// -theta, a tangent a period on; two pinned evaluations settle it where writing the
16477+
// quotient in u and simplifying it is milliseconds per candidate, and the
16478+
// complement pass below is the same function spelled otherwise. The problems of
16479+
// Rubi's 4.7.2, 4.3.10 and 4.4.10 the integrator answers took a fifth less time.
16480+
if (!complementEvenPowers && TheQuotientDisagreesWhereTheCandidateRepeats(expr, duDx, u, x))
16481+
continue;
16482+
1641816483
// Try to express expr as h(u) * du/dx
1641916484
// If successful, integral becomes ∫h(u)du
1642016485
var uSub = Variable.CreateUnique(expr, "u_sub");

‎Sources/Tests/UnitTests/Calculus/IntegrationTest.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ public void TestAdvancedTrigSubstitution(string initial, string expected)
180180
Assert.Equal(MathS.Boolean.True, result.EqualTo(expectedResult).Simplify());
181181
}
182182

183+
/// <summary>
184+
/// Substitutions under a sine, cosine, tangent or secant of a linear that the search's
185+
/// repeat test keeps: each integrand over the candidate's derivative is a function of the
186+
/// candidate, so it agrees where the candidate repeats -- at pi - theta for a sine, at
187+
/// -theta for a cosine, a period on for a tangent -- and is written in it.
188+
/// </summary>
189+
[Theory]
190+
[InlineData("sin(3*x + 1)^4 * cos(3*x + 1)")]
191+
[InlineData("cos(2*x)^5 * sin(2*x)")]
192+
[InlineData("tan(2*x)^3 * sec(2*x)^2")]
193+
[InlineData("cos(x)/(1 + sin(x)^2)")]
194+
[InlineData("sec(a*x + b)^2 * tan(a*x + b)^5")]
195+
[InlineData("sin(x) * cos(x)^3/(1 + cos(x)^4)")]
196+
public void ASubstitutionTheRepeatTestKeeps(string integrand)
197+
{
198+
var integral = integrand.Integrate("x").Substitute("C", 0);
199+
Assert.DoesNotContain("integral(", integral.Stringize());
200+
Entity Pin(Entity e) => e.Substitute("a", 0.7).Substitute("b", 0.3);
201+
var derivative = Pin(integral).Differentiate("x");
202+
var original = Pin(integrand.ToEntity());
203+
foreach (var at in new[] { 0.2, 0.5, 0.9, 1.3 })
204+
{
205+
var got = derivative.Substitute("x", at).EvalNumerical();
206+
var want = original.Substitute("x", at).EvalNumerical();
207+
var difference = (double)((Entity.Number.Real)(got - want).Abs()).EDecimal.ToDouble();
208+
Assert.True(difference < 1e-9, $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
209+
}
210+
}
211+
183212
[Theory]
184213
[InlineData("abs(x)", "signum(x) * x^2 / 2 + C")]
185214
[InlineData("abs(x + 0)", "signum(x) * x^2 / 2 + C")]

0 commit comments

Comments
 (0)