From 3d205b3a5aa614b85bc372d94ce9c6c0a0189f8e Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 01:39:05 +0000 Subject: [PATCH] An even root written apart says where it holds when the integrand is real beyond it `e^atanh(x) sqrt(1 - x)` is `sqrt((1 + x)/(1 - x)) sqrt(1 - x)`, and the root of the quotient was written apart as `sqrt(1 + x)`, which is the integrand only for `x < 1`. Past `x = 1` the even root is not real, and the splitting took that to mean the integrand is not asked about there; but the integrand is real, the product of two imaginary factors, and of the other sign. Where the product under an even root is negative on an interval, the integrand is now evaluated at that interval's sample point in interval arithmetic. Unless it is decidedly not real there, the root still splits, and the answer says it holds where the root is real: `provided (1 + x)/(1 - x) >= 0`. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Integration/IndefiniteIntegralSolver.cs | 82 ++++++++++++++++--- .../Calculus/BinomialDifferentialTest.cs | 37 +++++++++ 2 files changed, 107 insertions(+), 12 deletions(-) diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index f9f7317fc..d5710759e 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -17093,12 +17093,49 @@ private static (Entity Root, Entity Beside)? CombinedAsAQuotient(List ba private static bool OnEveryRealInterval(List bases, Entity.Variable x, System.Func, bool> holds) => SignsOnEveryRealInterval(bases, x) is { } table && table.All(holds); + /// + /// , with the point of each interval the signs were read at. + /// + private static bool OnEveryRealIntervalAt(List bases, Entity.Variable x, System.Func, bool> holds) + => SamplesAndSignsOnEveryRealInterval(bases, x) is { } table && table.All(row => holds(row.At, row.Signs)); + + /// + /// Whether is decidedly not real at : its + /// imaginary part, in interval arithmetic, excludes zero with every other symbol pinned + /// to a positive value, to a negative one, and to each sign in turn. Where it cannot be + /// evaluated nothing is decided, and it is not. + /// + private static bool NotRealAt(Entity expr, Entity.Variable x, double at) + { + var value = expr.Substitute(x, at); + var symbols = value.Vars; + for (var pattern = 0; pattern < symbols.Count switch { 0 => 1, 1 => 2, _ => 4 }; pattern++) + { + var pinned = value; + for (var i = 0; i < symbols.Count; i++) + { + var negative = pattern == 1 || pattern == 2 && i % 2 == 1 || pattern == 3 && i % 2 == 0; + pinned = pinned.Substitute(symbols[i], (negative ? -1 : 1) * (1.37 + 0.61 * i)); + } + if (Numerics.IntervalEvaluation.Of(pinned) is not { IsFinite: true } interval || interval.Im.ContainsZero) + return false; + } + return true; + } + /// /// The signs of the polynomials , one list per interval between /// their real roots, in the order of ; /// where the roots cannot be had. /// private static List>? SignsOnEveryRealInterval(List bases, Entity.Variable x) + => SamplesAndSignsOnEveryRealInterval(bases, x)?.Select(row => row.Signs).ToList(); + + /// + /// , with the point of each interval the signs were + /// read at. + /// + private static List<(double At, List Signs)>? SamplesAndSignsOnEveryRealInterval(List bases, Entity.Variable x) { var roots = new List(); foreach (var @base in bases) @@ -17125,7 +17162,7 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S if (roots[i + 1] - roots[i] > 1e-9) samples.Add((roots[i] + roots[i + 1]) / 2); } - var table = new List>(); + var table = new List<(double At, List Signs)>(); foreach (var at in samples) { var signs = new List(); @@ -17135,7 +17172,7 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S return null; signs.Add(value < 0 ? -1 : value > 0 ? 1 : 0); } - table.Add(signs); + table.Add((at, signs)); } return table; } @@ -17272,7 +17309,8 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S // The degree bound is on what is left under the root, and where every // polynomial factor comes out nothing polynomial is left. var oddRoot = !exponent.ERational.Denominator.IsEven; - var everyFactorComesOut = oddRoot || AnEvenRootOfAProductSplits(above, below, exponent.ERational.Denominator); + var onlyWhereTheRootIsReal = false; + var everyFactorComesOut = oddRoot || AnEvenRootOfAProductSplits(above, below, exponent.ERational.Denominator, out onlyWhereTheRootIsReal); if (everyFactorComesOut || OfModestDegreeOrNotAPolynomial(above) && OfModestDegreeOrNotAPolynomial(below)) { // On either side of the bar: `sqrt(sin(x)^5/cos(x))` is `sin(x)^2 sqrt(sin(x)/cos(x))`. @@ -17324,7 +17362,14 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S oddAbove = oddAbove * factor; } if (taken.ContainsNode(x)) + { + if (onlyWhereTheRootIsReal) + { + var real = new GreaterOrEqualf(@base, Number.Integer.Zero); + assumed = assumed == Entity.Boolean.True ? real : assumed & real; + } return MathS.Pow(oddBelow == Number.Integer.One ? oddAbove : oddAbove / oddBelow, exponent) * taken; + } } // And a polynomial every monomial of which an even power of x divides: the root // of `x^4 + x^2` is `|x| sqrt(x^2 + 1)`, exactly, since `x^2` is not negative -- @@ -17422,16 +17467,22 @@ Entity WithANegativeConstantInsideAFactor(Entity product) // wherever the root is real: the polynomial factors are real, and on each interval // between their roots the product's argument is pi times the sum of the n_i of the // negative ones. Where that sum is odd the product is negative and its even root is - // not real, so the integrand is not asked about there; where it is even the root is - // real and positive, and the product of the principal powers agrees with it exactly - // when their phases, pi n_i / q each, add up to a whole turn: the sum a multiple of - // 2q. Not of q: `sqrt(t^2)` is `|t|`, and `t^(2/2)` is `t`, half a turn out below - // zero -- which Timofeev's `sqrt(tan(x) tan(2x))` found. The constant must be - // positive, since a negative one would take the phase the other way. + // not real, so the integrand is not asked about there -- unless another factor is + // imaginary there too and the integrand is real all the same. Then the root still + // splits, and `onlyWhereTheRootIsReal` has the answer say it holds where the root is + // real: `e^atanh(x) sqrt(1 - x)` is `sqrt((1 + x)/(1 - x)) sqrt(1 - x)`, real past + // `x = 1`, where written apart it is `sqrt(1 + x)` with the other sign + // (https://github.com/asc-community/AngouriMath/issues/1664). Where the sum is even + // the root is real and positive, and the product of the principal powers agrees with + // it exactly when their phases, pi n_i / q each, add up to a whole turn: the sum a + // multiple of 2q. Not of q: `sqrt(t^2)` is `|t|`, and `t^(2/2)` is `t`, half a turn + // out below zero -- which Timofeev's `sqrt(tan(x) tan(2x))` found. The constant must + // be positive, since a negative one would take the phase the other way. // `((x - 1)^3 (x + 2)^5)^(1/4)` splits: above 1 both are positive, below -2 both // negative with 3 + 5 a multiple of 8, and between the product is negative. - bool AnEvenRootOfAProductSplits(Entity above, Entity below, EInteger q) + bool AnEvenRootOfAProductSplits(Entity above, Entity below, EInteger q, out bool onlyWhereTheRootIsReal) { + onlyWhereTheRootIsReal = false; var factors = new List<(Entity Polynomial, EInteger Times)>(); foreach (var side in new[] { above, below }) foreach (var factor in Mulf.LinearChildren(side)) @@ -17449,14 +17500,21 @@ bool AnEvenRootOfAProductSplits(Entity above, Entity below, EInteger q) } if (factors.Count < 2) return false; - return OnEveryRealInterval(factors.Select(f => f.Polynomial).ToList(), x, signs => + var realBeyondTheRoot = false; + var splits = OnEveryRealIntervalAt(factors.Select(f => f.Polynomial).ToList(), x, (at, signs) => { var negativeTimes = EInteger.Zero; for (var i = 0; i < signs.Count; i++) if (signs[i] < 0) negativeTimes = negativeTimes.Add(factors[i].Times); - return !negativeTimes.IsEven || negativeTimes.Remainder(q.ShiftLeft(1)).IsZero; + if (negativeTimes.IsEven) + return negativeTimes.Remainder(q.ShiftLeft(1)).IsZero; + if (!NotRealAt(expr, x, at)) + realBeyondTheRoot = true; + return true; }); + onlyWhereTheRootIsReal = splits && realBeyondTheRoot; + return splits; } // Q^r for a Q positive at every real x: a monomial `c x^(2k)` is `c^r x^(2kr)` for x > 0. diff --git a/Sources/Tests/UnitTests/Calculus/BinomialDifferentialTest.cs b/Sources/Tests/UnitTests/Calculus/BinomialDifferentialTest.cs index ad836ca3c..e4ba4dac3 100644 --- a/Sources/Tests/UnitTests/Calculus/BinomialDifferentialTest.cs +++ b/Sources/Tests/UnitTests/Calculus/BinomialDifferentialTest.cs @@ -187,6 +187,43 @@ public void APowerOfAQuotientIsWrittenApart(string integrand) } } + /// + /// Where the root of the quotient is not real the integrand can still be, and there an + /// answer from the root written apart must not stand. e^atanh(x) sqrt(1 - x) is + /// sqrt((1 + x)/(1 - x)) sqrt(1 - x), and written apart as sqrt(1 + x) it + /// is the integrand only for x < 1. Past x = 1 it is the product of two + /// imaginary factors, real and of the other sign, and the answer was wrong there. Now the + /// answer says it holds where the root is real, and claims nothing past it. + /// https://github.com/asc-community/AngouriMath/issues/1664 + /// + [Theory] + [InlineData("e^atanh(x)*(1-x)^(1/2)")] + [InlineData("e^atanh(x)/(1-x)^(1/2)")] + [InlineData("((1+x)/(1-x))^(1/2)*(1-x)^(1/2)")] + public void AnAnswerFromARootWrittenApartHoldsWhereItIsGiven(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + var derivative = integral.Substitute("C", 0).Differentiate("x"); + var original = integrand.ToEntity(); + foreach (var at in new[] { 0.29, -0.61 }) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)(got - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + foreach (var at in new[] { 1.43, 3.17 }) + { + var want = original.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)want.ImaginaryPart) < 1e-12, $"the integrand at x = {at} is {want}, not real"); + var got = derivative.Substitute("x", at).Evaled; + Assert.True(got.IsNaN || got is Entity.Number.Complex number + && Math.Abs((double)(number - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)), + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + } + /// /// An even power on either side of the bar comes out of the root as well, since it is /// not negative: sqrt(sin(x)/cos(x)^5) is sqrt(sin(x)/cos(x))/cos(x)^2 and