Skip to content

Commit a5ce1c5

Browse files
A constant with i in it is put in lowest terms when a rational function is split (#1795)
InLowestTermsOverTheSymbols reads a constant's numerator and denominator as polynomials over the rationals, and a coefficient with i in it is not one, so nothing in such a constant cancelled. Newton's iteration for the inverse modulo a power of a quadratic squares its iterate every round, and the split of 2 d^3 t^2/((t^2 - c - i d)^3 (t^2 - c + i d)) came out with d^572 in its coefficients: the antiderivative of (c + d tan(x))^(5/2)/(a + i a tan(x))^2 was 813,280 characters long and the default precision could not evaluate it. A constant with i among its coefficients is now read with i as a symbol j, j^2 = -1, written over its denominator times the conjugate, which is real, and the gcd of the real part, the imaginary part and the denominator divided out. The form is unique, so a zero comes out zero. The three answers are 71,266 to 83,614 characters now. Part of #1788. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 084c471 commit a5ce1c5

4 files changed

Lines changed: 213 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,23 @@ up to a third of such an answer, and of every answer built on one, as `1/(x (a +
418418
| `"1/(1 + q*x^2)".ToEntity().Integrate("x")` | four arms, the third `-2/(2 q x)` on `4 q = 0` | three, without it |
419419
| `"1/(x*(a + b*x))".ToEntity().Integrate("x")` | four arms, the second an arctangent on `-a^2 > 0` | three, without it |
420420

421+
### A constant with `i` in it is put in lowest terms when a rational function is split
422+
423+
**Shorter answers.** `(c + d tan(x))^(5/2)/(a + i a tan(x))^2` is `2 d^3 t^2/((t^2 - c - i d)^3 (t^2 - c + i d))`
424+
over `a^2` in `t = sqrt(c + d tan(x))`, and splitting that into partial fractions carries `c + i d` through
425+
every step of the inverse modulo the cube of its quadratic. The polynomial gcd reads coefficients in the
426+
rationals, and `i` is not one, so no step cancelled anything: the coefficients came out with `d^572` in them,
427+
and the answer at 813,000 characters, which the default precision could not evaluate. A constant with `i`
428+
among its coefficients is written as its real and imaginary parts over a real denominator now, the three
429+
without a common factor
430+
([#1788](https://github.com/asc-community/AngouriMath/issues/1788)).
431+
432+
| Input | Was (2.5.0) | Now |
433+
|---|---|---|
434+
| `"(c + d*tan(x))^(5/2)/(a + i*a*tan(x))^2".ToEntity().Integrate("x")` | `integral(...)`; 813,280 characters on the unreleased master | 83,614 characters |
435+
| `"(c + d*tan(x))^(3/2)/(a + i*a*tan(x))^2".ToEntity().Integrate("x")` | `integral(...)`; 470,119 characters on the unreleased master | 77,377 characters |
436+
| `"(c + d*tan(x))^(1/2)/(a + i*a*tan(x))^2".ToEntity().Integrate("x")` | `integral(...)`; 258,943 characters on the unreleased master | 71,266 characters |
437+
421438
### A polynomial with symbols in it that is a binomial or an even quartic in a shifted variable is integrated in it
422439

423440
**Answers where there were none.** `1/(c^2 x^3 + 3 b c x^2 + 3 b^2 x + 3 a b)` was left unevaluated,

‎Sources/AngouriMath/Functions/Algebra/Polynomials/PartialFractions.cs‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant)
639639
if (expandedAbove == Integer.Zero || expandedAbove.Evaled is Complex { IsZero: true } || IsZeroAtPinnedSymbols(expandedAbove))
640640
return Integer.Zero;
641641
var expandedBelow = Bare(below.Expand().InnerSimplified);
642+
// A number is folded already, and stays a number.
643+
if ((expandedAbove.Vars.Any() || expandedBelow.Vars.Any())
644+
&& (HoldsTheImaginaryUnit(expandedAbove) || HoldsTheImaginaryUnit(expandedBelow))
645+
&& InLowestTermsOverTheGaussianRationals(expandedAbove, expandedBelow) is { } gaussian)
646+
return gaussian;
642647
// With room for the coefficient a fourth-order block leaves: the series of
643648
// `x^4 (A + B x)/(1 + x^2)` at `-a/b` had `(a^2 + b^2)^3` in common at the third
644649
// order and eleven hundred nodes, and left uncancelled it carried into every
@@ -654,6 +659,117 @@ internal static Entity InLowestTermsOverTheSymbols(Entity constant)
654659
return WithThePrimitiveDenominator(expandedBelow == Integer.One ? expandedAbove : expandedAbove / expandedBelow);
655660
}
656661

662+
/// <summary>Whether a number off the real line is among the nodes of <paramref name="expr"/>.</summary>
663+
private static bool HoldsTheImaginaryUnit(Entity expr)
664+
=> expr.Nodes.Any(node => node is Complex number && number is not Real && !number.ImaginaryPart.IsZero);
665+
666+
/// <summary>
667+
/// <paramref name="above"/> over <paramref name="below"/>, with the imaginary unit among
668+
/// their coefficients, as <c>(P + i Q)/R</c> for polynomials <c>P</c>, <c>Q</c> and
669+
/// <c>R</c> over the rationals in the symbols, with no factor common to the three and
670+
/// <c>R</c> primitive; null where a side does not read as a polynomial so.
671+
/// </summary>
672+
/// <remarks>
673+
/// The polynomial gcd reads its coefficients in the rationals, and <c>i</c> is not one, so
674+
/// a constant with it in was left as the expansion gave it and nothing in it cancelled.
675+
/// Newton's iteration for the inverse modulo a power of a quadratic squares its iterate
676+
/// every round, and the split of <c>2 d^3 t^2/((t^2 - c - i d)^3 (t^2 - c + i d))</c>,
677+
/// which the root substitution makes of Rubi's 4.3.2.1
678+
/// <c>(c + d tan(x))^(5/2)/(a + i a tan(x))^2</c>, came out with <c>d^572</c> in its
679+
/// coefficients and an answer of 240,000 characters, which the default precision could
680+
/// not evaluate.
681+
/// https://github.com/asc-community/AngouriMath/issues/1788
682+
/// The unit is read as a symbol <c>j</c> with <c>j^2 = -1</c>, so each side is
683+
/// <c>A + j B</c> with <c>A</c> and <c>B</c> free of it; the denominator times its
684+
/// conjugate is real, <c>B0^2 + B1^2</c>, and what the three polynomials over the
685+
/// rationals then share is their gcd. With the denominator real and that gcd one the
686+
/// form is unique, so a constant that is zero comes out zero.
687+
/// </remarks>
688+
private static Entity? InLowestTermsOverTheGaussianRationals(Entity above, Entity below)
689+
{
690+
if ((long)above.Complexity + below.Complexity > 4096)
691+
return null;
692+
var unit = Variable.CreateUnique(above + below, "j");
693+
Entity WithTheUnitNamed(Entity side)
694+
=> side.Replace(node => node is Complex number && number is not Real ? number.RealPart + number.ImaginaryPart * unit : node);
695+
var namedAbove = WithTheUnitNamed(above);
696+
var namedBelow = WithTheUnitNamed(below);
697+
var variables = namedAbove.Vars.Concat(namedBelow.Vars).Distinct().OrderBy(v => v.Name, System.StringComparer.Ordinal).ToArray();
698+
if (variables.Length > MultivariatePolynomial.MaxVariables)
699+
return null;
700+
var indices = new Dictionary<Variable, int>(variables.Length);
701+
for (var i = 0; i < variables.Length; i++)
702+
indices[variables[i]] = i;
703+
if (MultivariatePolynomial.TryParse(namedAbove, indices) is not { } top
704+
|| MultivariatePolynomial.TryParse(namedBelow, indices) is not { } bottom || bottom.IsZero)
705+
return null;
706+
var (a0, a1) = RealAndImaginaryParts(top, indices[unit]);
707+
var (b0, b1) = RealAndImaginaryParts(bottom, indices[unit]);
708+
MultivariatePolynomial p, q, r;
709+
if (b1.IsZero)
710+
(p, q, r) = (a0, a1, b0);
711+
else if (a0.Multiply(b0) is { } a0b0 && a1.Multiply(b1) is { } a1b1
712+
&& a1.Multiply(b0) is { } a1b0 && a0.Multiply(b1) is { } a0b1
713+
&& b0.Multiply(b0) is { } b0b0 && b1.Multiply(b1) is { } b1b1)
714+
(p, q, r) = (a0b0.Add(a1b1), a1b0.Subtract(a0b1), b0b0.Add(b1b1));
715+
else
716+
return null;
717+
if (r.IsZero)
718+
return null;
719+
var order = new int[variables.Length];
720+
for (var i = 0; i < order.Length; i++)
721+
order[i] = i;
722+
if (PolynomialGcd.Gcd(p, q, order, 0) is not { } shared || PolynomialGcd.Gcd(shared, r, order, 0) is not { } common)
723+
return null;
724+
if (!common.IsConstant)
725+
{
726+
// Multiplied back, as the gcd's own cancellation is: nothing is divided out that
727+
// has not been seen to divide.
728+
if (p.DivideExact(common) is not { } reducedP || q.DivideExact(common) is not { } reducedQ
729+
|| r.DivideExact(common) is not { } reducedR
730+
|| reducedP.Multiply(common) is not { } backP || !backP.SameAs(p)
731+
|| reducedQ.Multiply(common) is not { } backQ || !backQ.SameAs(q)
732+
|| reducedR.Multiply(common) is not { } backR || !backR.SameAs(r))
733+
return null;
734+
(p, q, r) = (reducedP, reducedQ, reducedR);
735+
}
736+
var primitive = r.Normalized(out var scale);
737+
p = p.ScaleBy(scale);
738+
q = q.ScaleBy(scale);
739+
// `c - i d` rather than `c + i (-d)`: the imaginary part goes in with its sign taken
740+
// out where its first term, as written, is negative.
741+
var negative = !q.IsZero && q.Terms.OrderByDescending(term => term.Key).First().Value.Sign < 0;
742+
Entity numerator;
743+
if (q.IsZero)
744+
numerator = p.ToEntity(variables);
745+
else
746+
{
747+
var imaginary = MathS.i * (negative ? q.ScaleBy(ERational.FromInt32(-1)) : q).ToEntity(variables);
748+
numerator = p.IsZero ? (negative ? -imaginary : imaginary) : negative ? p.ToEntity(variables) - imaginary : p.ToEntity(variables) + imaginary;
749+
}
750+
return primitive.IsConstant ? numerator : numerator / primitive.ToEntity(variables);
751+
}
752+
753+
/// <summary>
754+
/// <paramref name="polynomial"/>, in which <paramref name="unit"/> stands for the
755+
/// imaginary unit, as <c>A + j B</c> with <c>j^2 = -1</c> and both free of it.
756+
/// </summary>
757+
private static (MultivariatePolynomial Real, MultivariatePolynomial Imaginary) RealAndImaginaryParts(MultivariatePolynomial polynomial, int unit)
758+
{
759+
var real = MultivariatePolynomial.Zero(polynomial.VariableCount);
760+
var imaginary = MultivariatePolynomial.Zero(polynomial.VariableCount);
761+
foreach (var pair in polynomial.CoefficientsIn(unit))
762+
{
763+
// j^k is 1, j, -1, -j as k is 0, 1, 2, 3 modulo 4.
764+
var term = pair.Key % 4 >= 2 ? pair.Value.ScaleBy(ERational.FromInt32(-1)) : pair.Value;
765+
if (pair.Key % 2 == 0)
766+
real = real.Add(term);
767+
else
768+
imaginary = imaginary.Add(term);
769+
}
770+
return (real, imaginary);
771+
}
772+
657773
/// <summary>
658774
/// Whether a constant in the symbols is zero as a value, decided as
659775
/// <see cref="InLowestTermsOverTheSymbols"/> decides it: over one bar, its numerator

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public sealed class ComplexCoefficientRationalIntegralTest
2626
{
2727
private static readonly double[] Points = { -0.7, 0.3, 0.9, 1.3, 2.2 };
2828

29-
private static void DifferentiatesBack(string integrand)
29+
private static void DifferentiatesBack(string integrand) => DifferentiatesBack(integrand, integrand.ToEntity().Integrate("x"));
30+
31+
private static void DifferentiatesBack(string integrand, Entity integral)
3032
{
31-
var integral = integrand.ToEntity().Integrate("x");
3233
Assert.DoesNotContain("integral(", integral.Stringize());
3334
var derivative = integral.Substitute("C", 0).Differentiate("x");
3435
Entity original = integrand.ToEntity();
@@ -80,6 +81,27 @@ private static void DifferentiatesBack(string integrand)
8081
[InlineData("1/(a + i*a*tan(c + d*x))^3")]
8182
public void APowerOfAPlusIATangent(string integrand) => DifferentiatesBack(integrand);
8283

84+
/// <summary>
85+
/// A root of a tangent sum over the square of <c>a + i a tan</c>: under the root it is
86+
/// <c>2 d^3 t^2/((t^2 - c - i d)^3 (t^2 - c + i d))</c> over <c>a^2</c>, and the split of that
87+
/// carries <c>c + i d</c> through the inverse modulo the cube of its quadratic. Nothing with
88+
/// <c>i</c> in it was put in lowest terms, and the coefficients came out with <c>d^572</c>
89+
/// in them: answers of 259,000, 470,000 and 813,000 characters, which the default precision
90+
/// could not evaluate.
91+
/// https://github.com/asc-community/AngouriMath/issues/1788
92+
/// </summary>
93+
[Theory]
94+
[InlineData("(c + d*tan(x))^(1/2)/(a + i*a*tan(x))^2")]
95+
[InlineData("(c + d*tan(x))^(3/2)/(a + i*a*tan(x))^2")]
96+
[InlineData("(c + d*tan(x))^(5/2)/(a + i*a*tan(x))^2")]
97+
public void ARootOfATangentSumOverTheSquareOfAPlusIATangent(string integrand)
98+
{
99+
var integral = integrand.ToEntity().Integrate("x");
100+
var length = integral.Stringize().Length;
101+
Assert.True(length < 150_000, $"the antiderivative of {integrand} is {length} characters long");
102+
DifferentiatesBack(integrand, integral);
103+
}
104+
83105
/// <summary>
84106
/// A power of a quotient of linears with the imaginary unit in it, beside a power of x the
85107
/// substitution for it does not take down to something answered. The scaling of the
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 System.Linq;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
using static AngouriMath.Entity.Number;
13+
14+
namespace AngouriMath.Tests.Common
15+
{
16+
/// <summary>
17+
/// A constant with the imaginary unit among its coefficients, in lowest terms over the
18+
/// symbols: its real and imaginary parts over a real denominator, the three without a common
19+
/// factor. The gcd reads coefficients in the rationals, and a constant with <c>i</c> in it was
20+
/// left as its expansion gave it, so nothing in it cancelled.
21+
/// https://github.com/asc-community/AngouriMath/issues/1788
22+
/// </summary>
23+
[Trait("Area", "Common")]
24+
public sealed class LowestTermsOverTheGaussianRationalsTest
25+
{
26+
private static Entity InLowestTerms(string constant)
27+
=> Functions.PartialFractions.InLowestTermsOverTheSymbols(constant.ToEntity());
28+
29+
/// <summary>A constant that is zero comes out zero, since the form is unique.</summary>
30+
[Theory]
31+
[InlineData("1/(c + i*d) - (c - i*d)/(c^2 + d^2)")]
32+
[InlineData("(c + i*d)*(c - i*d)/(c^2 + d^2) - 1")]
33+
[InlineData("2*i*d/((c + i*d) - (c - i*d)) - 1")]
34+
public void IsZero(string constant) => Assert.Equal(Integer.Zero, InLowestTerms(constant));
35+
36+
[Theory]
37+
[InlineData("(c^2 + d^2)/(c + i*d)", "c - i*d")]
38+
[InlineData("1/(2*i*d)^3", "i/(8*d^3)")]
39+
[InlineData("(c + i*d)^3/(c^2 + d^2)^2", "(c + i*d)/(c - i*d)^2")]
40+
[InlineData("(1 + 2*i*a)/(a - i)", "(-a + i*(2*a^2 + 1))/(a^2 + 1)")]
41+
public void IsOverARealDenominator(string constant, string value)
42+
{
43+
var lowest = InLowestTerms(constant);
44+
var (_, below) = Functions.SingleQuotient.Of(lowest);
45+
Assert.DoesNotContain(below.Nodes, node => node is Complex number && number is not Real);
46+
foreach (var (first, second) in new[] { (1.37, 0.83), (-2.1, 0.4) })
47+
{
48+
Entity Pinned(Entity e) => e.Substitute("a", first).Substitute("c", first).Substitute("d", second);
49+
var got = Pinned(lowest).EvalNumerical();
50+
var want = Pinned(value.ToEntity()).EvalNumerical();
51+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
52+
Assert.True(difference < 1e-12, $"{constant} is {lowest}, which is {got} where {value} is {want}");
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)