Skip to content

Commit a720972

Browse files
A power of the variable is a substitution even where it is written nowhere (#1135)
`int x / (x^4 + 1)` came back unevaluated. It wants `u = x^2`, and two separate things stopped it. Candidates were taken from the subexpressions that occur, and `x^2` occurs nowhere in `x / (x^4 + 1)`. So it was never offered. And had it been offered, substituting it would have replaced nothing: `x^4` is not written as `(x^2)^2`, and a substitution matches what is written, so the integrand would have kept its `x` and the candidate would have been rejected anyway. `int x^3 / (x^4 + 1)` worked throughout, because its substitution `u = x^4` does occur. That is what made the gap hard to see from the outside -- one of the pair works and the other does not, for a reason that is about syntax rather than about integration. So a power of the variable now offers its divisors as candidates, and substituting a power rewrites the other powers of the variable into powers of itself, which it can do exactly when its exponent divides theirs. Nothing is assumed by either: the caller still checks that no `x` survives, so a candidate that does not clear the variable is rejected as before. `x^2 / (x^4 + 1)` is exactly that case -- a bare `x` remains, it is still declined, and it stays open on the issue, since it needs the denominator factored over the reals rather than a substitution. Newly integrable, each verified by differentiating the answer and comparing with the integrand at nine sampled points rather than by reading the printed form: x/(x^4 + 1) arctan(x^2)/2 + C x/(x^4 - 1) x/(x^4 + 4) x^2/(x^6 + 1) x/(x^6 + 1) x^3/(x^8 + 1) x^3/(x^12 + 1) Eleven integrals taking the other paths -- a substitution that does occur, partial fractions, by parts, a logarithm -- are checked the same way and are unchanged. The full suite is green and takes the same time it did, so the extra candidates cost nothing measurable. Part of #233. Three of the five it lists were already answered; `sqrt(tan(x))` and `x^2 / (x^4 + 1)` remain.
1 parent 9923e4e commit a720972

4 files changed

Lines changed: 205 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4826,6 +4826,15 @@ And what cannot be compiled is named rather than failing obscurely.
48264826

48274827
Not behavioural changes, listed so that a reader working through this file has the whole picture.
48284828

4829+
- **Integrals wanting a substitution by a power of the variable that occurs nowhere.**
4830+
`int x / (x^4 + 1)` is `arctan(x^2)/2 + C`, and so for `x/(x^4 - 1)`, `x/(x^4 + 4)`,
4831+
`x^2/(x^6 + 1)`, `x/(x^6 + 1)`, `x^3/(x^8 + 1)` and `x^3/(x^12 + 1)`. Each was previously
4832+
returned unevaluated. Two things had to change: `x^2` is now offered as a candidate although
4833+
it is written nowhere in `x / (x^4 + 1)`, and substituting it rewrites `x^4` as `(x^2)^2` so
4834+
that there is something for it to replace. `int x^3 / (x^4 + 1)` worked throughout, its own
4835+
substitution being one that occurs, which is what made the gap hard to see. No integral that
4836+
was answered before is answered differently.
4837+
[#233](https://github.com/asc-community/AngouriMath/issues/233).
48294838
- **A modulus node.** `Modf`, `MathS.Mod`, the `%` operator on `Entity` in C# and F#, the `mod`
48304839
keyword in the parser, evaluation, simplification, differentiation, limits, both compilers, LaTeX
48314840
and SymPy export. `a % b` on `Entity` is *not* `int`'s `%` — it is floored, and its documentation

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
5151
[AngouriMath/Functions/Algebra/Polynomials/KroneckerFactorization.cs]
5252
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
5353

54+
[Tests/UnitTests/Calculus/PowerSubstitutionIntegralTest.cs]
55+
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
56+
5457
[AngouriMath/Functions/Algebra/Polynomials/FractionFreeDeterminant.cs]
5558
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
5659

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

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ arg is Entity.Powf(var y, var pow) ? // log(b, y^p) = ln(y^p) / ln(b) = ln(p) /
217217

218218
// Try to divide expr by duDx and check if result is independent of x
219219
// Replace all occurrences of u's expression with a temporary variable
220-
var integrandInU = (expr / duDx).Substitute(u, uSub).Simplify(1);
220+
var integrandInU = InTermsOf(expr / duDx, u, uSub, x).Simplify(1);
221221
if (integrandInU is Providedf(var innerExpr, _)) integrandInU = innerExpr; // TODO: singularities ignored but not handled properly
222222

223223
// If the result doesn't contain x anymore, we found a valid substitution
@@ -241,6 +241,53 @@ arg is Entity.Powf(var y, var pow) ? // log(b, y^p) = ln(y^p) / ln(b) = ln(p) /
241241
return null;
242242
}
243243

244+
/// <summary>
245+
/// <paramref name="expr"/> written in terms of <paramref name="uSub"/>, where that stands
246+
/// for <paramref name="u"/>.
247+
/// </summary>
248+
/// <remarks>
249+
/// <para>
250+
/// Ordinarily that is a plain substitution: the candidate occurs in the integrand, and
251+
/// replacing it is all that is wanted.
252+
/// </para>
253+
/// <para>
254+
/// <b>A power of the variable is the exception, and it is the one that mattered.</b>
255+
/// Substituting <c>u = x^2</c> into <c>x / (x^4 + 1)</c> replaces nothing, because
256+
/// <c>x^4</c> is not written as <c>(x^2)^2</c> and a substitution matches what is
257+
/// written. So the integrand kept its <c>x</c>, the candidate was rejected, and
258+
/// <c>int x / (x^4 + 1)</c> came back unevaluated while <c>int x^3 / (x^4 + 1)</c> —
259+
/// whose substitution does occur — did not.
260+
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
261+
/// </para>
262+
/// <para>
263+
/// So a power substitution rewrites the other powers of the variable into powers of
264+
/// itself, which it can do exactly when its exponent divides theirs. Nothing is assumed
265+
/// by it: the caller still checks that no <c>x</c> survives, so a rewrite that does not
266+
/// clear the variable leaves the candidate rejected as before — which is what happens to
267+
/// <c>x^2 / (x^4 + 1)</c>, where a bare <c>x</c> remains and the integral genuinely needs
268+
/// a factorisation this does not have.
269+
/// </para>
270+
/// </remarks>
271+
private static Entity InTermsOf(Entity expr, Entity u, Entity.Variable uSub, Entity.Variable x)
272+
{
273+
if (u is not Powf(var powerBase, Number.Integer exponent)
274+
|| powerBase != x
275+
|| !exponent.EInteger.CanFitInInt32())
276+
return expr.Substitute(u, uSub);
277+
var k = exponent.EInteger.ToInt32Checked();
278+
if (k < 2)
279+
return expr.Substitute(u, uSub);
280+
return expr.Replace(node =>
281+
node is Powf(var otherBase, Number.Integer otherExponent)
282+
&& otherBase == x
283+
&& otherExponent.EInteger.CanFitInInt32()
284+
&& otherExponent.EInteger.ToInt32Checked() is var n
285+
&& n >= k
286+
&& n % k == 0
287+
? (n == k ? uSub : MathS.Pow(uSub, n / k))
288+
: node);
289+
}
290+
244291
/// <summary>
245292
/// Finds potential substitution candidates u = g(x) from the expression.
246293
/// For example, common patterns to try:
@@ -261,6 +308,20 @@ private static IEnumerable<Entity> FindSubstitutionCandidates(Entity expr, Entit
261308
break;
262309
case Powf(var @base, var exp):
263310
if (@base == x) candidates.Add(node); // Power expressions x^n
311+
// And the roots of that power, which need not occur anywhere to be the
312+
// right substitution: `int x / (x^4 + 1)` wants u = x^2, and x^2 appears
313+
// nowhere in it. A divisor of the exponent is the condition for the
314+
// rewrite to be exact -- see InTermsOf -- so the candidates are exactly
315+
// the divisors, and each is still tested by whether it clears the
316+
// variable. https://github.com/asc-community/AngouriMath/issues/233
317+
if (@base == x
318+
&& exp is Number.Integer power
319+
&& power.EInteger.CanFitInInt32()
320+
&& power.EInteger.ToInt32Checked() is var n
321+
&& n > 2)
322+
for (var divisor = 2; divisor + divisor <= n; divisor++)
323+
if (n % divisor == 0)
324+
candidates.Add(MathS.Pow(x, divisor));
264325
// Exponential with non-trivial argument
265326
if (@base != x && @base.ContainsNode(x)) candidates.Add(@base);
266327
if (exp != x && exp.ContainsNode(x)) candidates.Add(exp);
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// Integrals that need a substitution by a <em>power of the variable</em> which does not occur
16+
/// anywhere in the integrand.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
18+
/// </summary>
19+
/// <remarks>
20+
/// <para>
21+
/// <c>int x / (x^4 + 1)</c> wants <c>u = x^2</c>, and <c>x^2</c> is written nowhere in it. Two
22+
/// things followed from that. The candidate was never offered, because candidates were taken
23+
/// from the subexpressions that occur; and had it been, substituting it would have replaced
24+
/// nothing, because <c>x^4</c> is not written as <c>(x^2)^2</c> and a substitution matches
25+
/// what is written. So the integrand kept its <c>x</c> and the candidate was rejected.
26+
/// </para>
27+
/// <para>
28+
/// <c>int x^3 / (x^4 + 1)</c> worked throughout, which is what made this hard to see: its
29+
/// substitution, <c>u = x^4</c>, does occur.
30+
/// </para>
31+
/// <para>
32+
/// Every case here is checked by differentiating the answer and comparing it with the
33+
/// integrand at sampled points, not by comparing printed forms. An antiderivative is only
34+
/// correct if it differentiates back, and these come out with radicals and arctangents whose
35+
/// printed shape says nothing about whether they do.
36+
/// </para>
37+
/// </remarks>
38+
[Trait("Area", "Calculus")]
39+
public sealed class PowerSubstitutionIntegralTest
40+
{
41+
/// <summary>
42+
/// The sample points, chosen away from the poles of the integrands below and on both
43+
/// sides of zero, since a substitution by an even power is where a sign is most easily
44+
/// lost.
45+
/// </summary>
46+
private static readonly double[] Points = { 0.3, 0.7, 1.3, 1.9, 2.6, 3.4, -0.4, -1.7, -2.9 };
47+
48+
private static void DifferentiatesBack(string integrand)
49+
{
50+
var integral = integrand.ToEntity().Integrate("x");
51+
Assert.DoesNotContain("integral(", integral.Stringize());
52+
53+
var derivative = integral.Differentiate("x");
54+
var original = integrand.ToEntity();
55+
var compared = 0;
56+
foreach (var at in Points)
57+
{
58+
var got = derivative.Substitute("x", at).EvalNumerical();
59+
var want = original.Substitute("x", at).EvalNumerical();
60+
if (got.IsNaN || want.IsNaN)
61+
continue;
62+
compared++;
63+
var difference = Math.Abs((double)(got - want).RealPart);
64+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart));
65+
Assert.True(difference / scale < 1e-9,
66+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, "
67+
+ $"where the integrand is {want}");
68+
}
69+
Assert.True(compared >= 5,
70+
$"only {compared} of {Points.Length} points were comparable for {integrand}, "
71+
+ "so this asserts almost nothing");
72+
}
73+
74+
/// <summary>
75+
/// The shape the issue is about: an odd power over an even one, where the substitution is
76+
/// the root of the denominator's power.
77+
/// </summary>
78+
[Theory]
79+
[InlineData("x/(x^4 + 1)")]
80+
[InlineData("x/(x^4 - 1)")]
81+
[InlineData("x/(x^4 + 4)")]
82+
[InlineData("x^2/(x^6 + 1)")]
83+
[InlineData("x/(x^6 + 1)")]
84+
[InlineData("x^3/(x^8 + 1)")]
85+
[InlineData("x^3/(x^12 + 1)")]
86+
public void APowerOfTheVariableThatOccursNowhereIsStillASubstitution(string integrand)
87+
=> DifferentiatesBack(integrand);
88+
89+
/// <summary>
90+
/// <c>int x / (x^4 + 1)</c> is <c>arctan(x^2)/2</c>, which is worth asserting as a form
91+
/// and not only as a derivative: it is the answer the issue's link gives, and getting a
92+
/// constant factor wrong would still differentiate back to something proportional.
93+
/// </summary>
94+
[Fact]
95+
public void TheAnswerIsTheOneTheIssueNames()
96+
{
97+
var integral = "x/(x^4 + 1)".ToEntity().Integrate("x").Simplify();
98+
Assert.Equal("arctan(x ^ 2) / 2 + C".ToEntity().Simplify(), integral);
99+
}
100+
101+
/// <summary>
102+
/// Nothing that integrated before integrates differently. These take the other paths —
103+
/// a substitution that does occur, partial fractions, by parts, a logarithm — so a
104+
/// change in the candidate list would show here.
105+
/// </summary>
106+
[Theory]
107+
[InlineData("x^3/(x^4 + 1)")]
108+
[InlineData("cos(x^2) * x")]
109+
[InlineData("sin(x) * e^x")]
110+
[InlineData("1/(x^2 + 1)")]
111+
[InlineData("1/(x^4 - 1)")]
112+
[InlineData("1/(x^2 - 1)")]
113+
[InlineData("x * ln(x)")]
114+
[InlineData("ln(x)")]
115+
[InlineData("x^2")]
116+
[InlineData("sin(x) * cos(x)")]
117+
[InlineData("e^x * x")]
118+
public void WhatIntegratedBeforeStillDoes(string integrand)
119+
=> DifferentiatesBack(integrand);
120+
121+
/// <summary>
122+
/// And the rewrite does not make a candidate succeed that should not.
123+
/// <c>x^2 / (x^4 + 1)</c> under <c>u = x^2</c> leaves a bare <c>x</c> behind, so it is
124+
/// still rejected — that integral needs the denominator factored over the reals, which
125+
/// this does not do, and it remains open on the issue.
126+
/// </summary>
127+
[Fact]
128+
public void AnIntegralThisDoesNotReachIsStillDeclined()
129+
=> Assert.Contains("integral(", "x^2/(x^4 + 1)".ToEntity().Integrate("x").Stringize());
130+
}
131+
}

0 commit comments

Comments
 (0)