Skip to content

Commit a189e62

Browse files
A shared symbol among a denominator sum's coefficients comes out in front of it (#1403)
* A shared symbol among a denominator sum's coefficients comes out in front of it tan(x)/(a + a csc(x)) came back unevaluated while tan(x)/(1 + csc(x)) was answered: under u = sin(x) the denominator is (a u + a)(1 - u^2), a symbolic linear beside a rational quadratic that shares its root, which the symbolic split declines, and under the half-angle it is a sextic with a in every coefficient, which nothing factors. A written sum below the bar whose coefficients share a symbol is now that symbol times a polynomial over the rationals -- the content, by the multivariate gcd of the coefficients, and the primitive part -- before the denominator is refactored, so the repeated factor is found and the rational machinery answers. Once, since a primitive factor has no content to take out. Rubi's family 4 sample: ten more rows, the powers of (a + a sin) and (a + a sec) among them; the 1774-problem suite is unchanged at 1706 with 0 wrong. Part of #718. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * The lowest power of the variable every term holds comes out of the sum as well a u^4 + b u^3 is u^3 (a u + b), what u = sin(x) makes of cot(x)^3/(a + b csc(x)), and neither the refactoring over the rationals nor the symbolic split reads a symbolic polynomial for the monomial it is a multiple of. Family 4: three more rows. Part of #718. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 3226805 commit a189e62

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,24 @@ rule that does the same for a polynomial under a square root, whose answers are
351351
| `"1/sqrt(a*cot(x)^2)".Integrate("x")` | `-sgn(tan(x)) ln(1/sqrt(1 + abs(tan(x))^2))/sqrt(a)` | `sgn(cot(x)) ln(1 + tan(x)^2)/(2 sqrt(a))` |
352352
| `"(b*tan(x)^2)^(5/2)".Integrate("x")` | `sgn(tan(x)) b^(5/2) ((abs(tan(x))^2)^2/2 - abs(tan(x))^2 + ln(abs(tan(x))^2 + 1))/2` | the same with `tan(x)` for `abs(tan(x))` |
353353

354+
### A shared symbol among a denominator sum's coefficients comes out in front of it
355+
356+
**Improvement, not silent.** `tan(x)/(a + a csc(x))` came back unevaluated while
357+
`tan(x)/(1 + csc(x))` was answered: under `u = sin(x)` the denominator is `(a u + a)(1 - u^2)`,
358+
a symbolic linear beside a rational quadratic that shares its root, which the symbolic split
359+
declines, and under the half-angle it is a sextic with `a` in every coefficient, which nothing
360+
factors. A written sum below the bar whose coefficients share a symbol is now that symbol times
361+
a polynomial over the rationals — `(a u + a)` is `a (u + 1)` — before the denominator is
362+
refactored, so the repeated factor is found and the rational machinery answers; and the lowest
363+
power of the variable every term holds comes out the same way.
364+
365+
| | Was | Is |
366+
|---|---|---|
367+
| `"tan(x)/(a + a*csc(x))".Integrate("x")` | unevaluated | `(-1/(2 (sin(x) + 1)) + 3/4 ln(sin(x) + 1) + 1/4 ln(sin(x) - 1))/(-a)` |
368+
| `"sec(x)^2/(a + a*csc(x))".Integrate("x")` | unevaluated | in `tan(x/2)`, over `a` |
369+
| `"x^2/((a*x + a)*(1 - x^2))".Integrate("x")` | unevaluated | the partial fractions over `(x + 1)^2 (1 - x)`, over `a` |
370+
| `"cot(x)^3/(a + b*csc(x))".Integrate("x")` | unevaluated | `-1/(b sin(x)) - a ln(sin(x))/b^2 + (b^2 - a^2) ln(a sin(x) + b)/(a b^2)`, once `a u^4 + b u^3` is read as `u^3 (a u + b)` |
371+
354372
### A condition the answer states on its own is no longer repeated beside it
355373

356374
**Improvement, not silent.** `Simplify` returned `(1 - ln(x))/x^2 provided not x = 0` for the

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
442442
[Tests/UnitTests/Calculus/CombinedRadicalsTest.cs]
443443
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
444444

445+
[Tests/UnitTests/Calculus/DenominatorContentTest.cs]
446+
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
447+
445448
[Tests/UnitTests/Calculus/SineOverASymbolicQuadraticTest.cs]
446449
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
447450

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,16 @@ is var (multiple, leftover)
621621
// under a second this way, with the chain behind it for a spelling this rule does
622622
// not answer on its own -- one irreducible factor to the first power is the table's.
623623
// Once: the spelling this produces refactors to itself.
624+
// A written sum with a symbol among its coefficients that they all share is that
625+
// symbol times a polynomial over the rationals, and is written so first: `(a u + a)`
626+
// beside `1 - u^2` shares a root with it, which the symbolic split declines, and a
627+
// symbolic sextic is nothing the refactoring below reads, where `a` times a rational
628+
// one is. Once: a primitive factor has no content to take out.
629+
if (WithTheContentOutOfEachSumFactor(denominator, x) is { } primitive
630+
&& (SolveByPartialFractions(numerator / primitive, x, integrateByParts)
631+
?? Integration.ComputeIndefiniteIntegral(numerator / primitive, x, integrateByParts)) is { } overPrimitives)
632+
return overPrimitives;
633+
624634
if (TryWriteInIrreducibleFactors(denominator, x) is { } refactored
625635
&& (SolveByPartialFractions(numerator / refactored, x, integrateByParts)
626636
?? Integration.ComputeIndefiniteIntegral(numerator / refactored, x, integrateByParts)) is { } overIrreducibles)
@@ -7535,6 +7545,79 @@ private static bool IsASumOfMonomials(Entity expr, Entity.Variable x)
75357545
return true;
75367546
}
75377547

7548+
/// <summary>
7549+
/// <paramref name="denominator"/> with the content of every written sum among its
7550+
/// factors taken out in front of it: <c>(a u + a)(1 - u^2)</c> is <c>a (u + 1)(1 - u^2)</c>,
7551+
/// and <c>-a t^6 - 2a t^5 - a t^4 + a t^2 + 2a t + a</c> is <c>a</c> times the
7552+
/// polynomial over the rationals. Null where no factor has one.
7553+
/// </summary>
7554+
/// <remarks>
7555+
/// The content is the gcd of the coefficients as polynomials in the symbols, and a
7556+
/// factor whose coefficients share one is a constant times a smaller polynomial: with
7557+
/// the constant written inside, <c>(a u + a)</c> was a symbolic linear whose root the
7558+
/// factor <c>1 - u^2</c> beside it shares, which the symbolic split declines, and the
7559+
/// sextic the half-angle makes of <c>tan(x)/(a + a csc(x))</c> was a polynomial with a
7560+
/// symbol in every coefficient, which nothing factors -- where over the rationals it is
7561+
/// <c>(t - 1)(t + 1)^3 (t^2 + 1)</c>. Rubi's <c>a + a csc(x)</c> and the like are what
7562+
/// this is for.
7563+
/// https://github.com/asc-community/AngouriMath/issues/718
7564+
/// </remarks>
7565+
private static Entity? WithTheContentOutOfEachSumFactor(Entity denominator, Entity.Variable x)
7566+
{
7567+
var changed = false;
7568+
Entity product = Number.Integer.One;
7569+
foreach (var factor in Mulf.LinearChildren(denominator))
7570+
{
7571+
// Whole powers only: `(c a + d a x)^(1/3)` is `a^(1/3) (c + d x)^(1/3)` for a
7572+
// positive `a` and not for a negative one, and a symbol is neither.
7573+
var (@base, power) = factor is Powf(var b, Number.Integer p) ? (b, p) : (factor, Number.Integer.One);
7574+
if (@base is not (Sumf or Minusf) || !@base.ContainsNode(x) || !@base.Vars.Any(v => v != x)
7575+
|| !TreeAnalyzer.TryGetPolynomial(@base, x, out var read) || read.Count < 2
7576+
|| read.Keys.Any(degree => degree.Sign < 0) || read.Values.Any(coefficient => coefficient.ContainsNode(x)))
7577+
{
7578+
product *= factor;
7579+
continue;
7580+
}
7581+
var variables = @base.Vars.OrderBy(v => v.Name, System.StringComparer.Ordinal).ToList();
7582+
if (variables.Count > MultivariatePolynomial.MaxVariables)
7583+
{
7584+
product *= factor;
7585+
continue;
7586+
}
7587+
var indices = new Dictionary<Variable, int>();
7588+
for (var i = 0; i < variables.Count; i++)
7589+
indices[variables[i]] = i;
7590+
var rest = Enumerable.Range(0, variables.Count).Where(i => variables[i] != x).ToList();
7591+
// The lowest power of x every term holds comes out too: `a u^4 + b u^3` is
7592+
// `u^3 (a u + b)`, which is what the sine substitution makes of `cot^3/(a + b csc)`,
7593+
// and neither the refactoring over the rationals nor the symbolic split reads
7594+
// a symbolic polynomial for the monomial it is a multiple of.
7595+
var lowest = read.Keys.Min()!;
7596+
if (MultivariatePolynomial.TryParse(@base, indices) is not { } polynomial
7597+
|| Functions.PolynomialGcd.ContentIn(polynomial, indices[x], rest, 0) is not { } content
7598+
|| content.IsConstant && lowest.IsZero
7599+
|| polynomial.DivideExact(content) is not { } primitive)
7600+
{
7601+
product *= factor;
7602+
continue;
7603+
}
7604+
var monomial = lowest.IsZero ? Number.Integer.One : lowest.Equals(EInteger.One) ? x : MathS.Pow(x, Number.Integer.Create(lowest));
7605+
var primitivePart = primitive.ToEntity(variables);
7606+
if (!lowest.IsZero && TreeAnalyzer.PolynomialLongDivision(primitivePart, monomial, genericCase: true, inTermsOf: x) is var (divided, remainder)
7607+
&& (remainder is Divf(var top, _) ? top : remainder).Evaled is Number.Complex { IsZero: true })
7608+
primitivePart = divided.InnerSimplified;
7609+
else if (!lowest.IsZero)
7610+
{
7611+
product *= factor;
7612+
continue;
7613+
}
7614+
Entity constantPart = content.IsConstant ? monomial : lowest.IsZero ? content.ToEntity(variables) : content.ToEntity(variables) * monomial;
7615+
product *= power == Number.Integer.One ? constantPart * primitivePart : MathS.Pow(constantPart, power) * MathS.Pow(primitivePart, power);
7616+
changed = true;
7617+
}
7618+
return changed ? product : null;
7619+
}
7620+
75387621
private static Entity? TryWriteInIrreducibleFactors(Entity denominator, Entity.Variable x)
75397622
{
75407623
// Only where the written bases, each taken once, share a factor among them or
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
/// A written sum below the bar whose coefficients share a symbol is that symbol times a
16+
/// polynomial over the rationals, and is read so: <c>(a u + a)(1 - u^2)</c> is
17+
/// <c>a (u + 1)(1 - u^2)</c>, whose repeated factor the refactoring over the rationals then
18+
/// finds; and the lowest power of the variable every term holds comes out the same way,
19+
/// <c>a u^4 + 2 u^3</c> being <c>u^3 (a u + 2)</c>. Rubi's <c>tan(x)/(a + a csc(x))</c> and
20+
/// <c>cot(x)^3/(a + b csc(x))</c>, declined for the shared root under <c>u = sin(x)</c>, for
21+
/// a symbolic sextic under the half-angle, and for a symbolic quartic that is a multiple of
22+
/// <c>u^3</c>.
23+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
24+
/// </summary>
25+
[Trait("Area", "Calculus")]
26+
public sealed class DenominatorContentTest
27+
{
28+
private static readonly double[] Points = { 0.4, 1.1, 2.3, 3.6, 5.2 };
29+
30+
private static void DifferentiatesBack(string integrand, double constant)
31+
{
32+
var integral = integrand.ToEntity().Integrate("x");
33+
Assert.DoesNotContain("integral(", integral.Stringize());
34+
Assert.DoesNotContain("NaN", integral.Stringize());
35+
36+
var derivative = integral.Substitute("C", 0).Substitute("a", constant).Differentiate("x");
37+
var original = integrand.ToEntity().Substitute("a", constant);
38+
foreach (var at in Points)
39+
{
40+
var got = derivative.Substitute("x", at).EvalNumerical();
41+
var want = original.Substitute("x", at).EvalNumerical();
42+
var difference = Math.Abs((double)(got - want).RealPart)
43+
+ Math.Abs((double)(got - want).ImaginaryPart);
44+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
45+
Assert.True(difference / scale < 1e-8,
46+
$"d/dx of the antiderivative of {integrand} at a = {constant} is {got} at x = {at}, "
47+
+ $"where the integrand is {want}");
48+
}
49+
}
50+
51+
[Theory]
52+
[InlineData("tan(x)/(a + a*csc(x))")]
53+
[InlineData("sec(x)^2/(a + a*csc(x))")]
54+
[InlineData("x^2/((a*x + a)*(1 - x^2))")]
55+
[InlineData("1/((2*a*x + 4*a)*(x + 2)^2)")]
56+
[InlineData("cot(x)^3/(2 + a*csc(x))")]
57+
[InlineData("(1 - x^2)/(a*x^4 + 2*x^3)")]
58+
public void TheContentComesOutOfTheSum(string integrand)
59+
{
60+
DifferentiatesBack(integrand, 2);
61+
DifferentiatesBack(integrand, -3);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)