From cb1cd28a2f991320d3e309be8f7c6fab58a4c549 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 4 Oct 2026 07:31:24 +0000 Subject: [PATCH] A quadratic's coefficients are named for the odd half power's solve A polynomial times an odd half power of a quadratic is reduced with one linear solve in the coefficients, and a quotient or a product of symbols among them was carried through the elimination as written: sqrt(a + b x + g^2 x^2/f^2) came back in an answer of 188,000 characters after three seconds, and (a + b x + c x^2/f)^(3/2) in one of 478,000 after eight. Each such coefficient is named for the solve and written back in what it gives; the answers are a thousand characters, in a tenth of a second. Part of #718. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 +++++++ .../Integration/IndefiniteIntegralSolver.cs | 34 +++++++++++-- ...undCoefficientsOfAQuadraticIntegralTest.cs | 51 +++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/CompoundCoefficientsOfAQuadraticIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index e9ae2c6a1..82067813a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -659,6 +659,23 @@ poles of `tan(x/2)`, as every half-angle answer. | `"1/(a+b*cos(x)+c*cos(x)^2)".Integrate("x")` | unevaluated | the cosine form | | `"csc(x)^2/(a-a*sin(x)^2)".Integrate("x")` | unevaluated | `-cot(x)/a` and two terms rational in `tan(x/2)` | +### A polynomial times an odd half power of a quadratic with compound coefficients is answered shortly + +**Answers where there were none, and short ones where the unreleased master's were not.** +`sqrt(a + b x + g^2 x^2/f^2)` was declined by 2.5.0. The unreleased master answers it by reducing +a polynomial times an odd half power of the quadratic with one linear solve in the coefficients, +and carried as written, a quotient or a product of symbols among them came back in answers of 40,000 +to 480,000 characters, after up to eight seconds, which took up to a minute more to differentiate +back. Each such coefficient is named for the solve now and written back in what it gives, and the +answers are a thousand characters, in a tenth of a second +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"sqrt(a + b*x + g^2*x^2/f^2)".ToEntity().Integrate("x")` | `integral(...)` | the root times a linear, and an arcsine or a logarithm by the sign of `g^2/f^2` | +| `"(a + b*x + c*x^2/f)^(3/2)".ToEntity().Integrate("x")` | `integral(...)` | the same with the power | +| `"x^2*sqrt(a*f + b*x + c*x^2/f)".ToEntity().Integrate("x")` | `integral(...)` | the same with a cubic in front of the root | + ### A linear over a quadratic beside the root of another quadratic is integrated `(g + h x)/(A sqrt(B))`, `A` and `B` two different quadratics, was left unevaluated wherever a diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 9965d1608..f294d9b65 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -13752,8 +13752,34 @@ static Dictionary Scaled(Dictionary poly, En if (coefficient.Evaled is Number.Complex and not Number.Real) return null; - var q = new Dictionary { [EInteger.Zero] = c, [EInteger.One] = b, [EInteger.FromInt32(2)] = a }; - var qPrime = new Dictionary { [EInteger.Zero] = b, [EInteger.One] = (2 * a).InnerSimplified }; + // Each coefficient that is neither a number nor a symbol is named for the solve and + // written back into what it gives. Carried through the elimination as written, + // `g^2/f^2` as the leading coefficient of `sqrt(a + b x + g^2 x^2/f^2)` came back in an + // answer of 188,000 characters after four seconds, where a symbol in its place gives a + // short one at once. + var names = new List<(Variable Name, Entity Value)>(); + Entity Named(Entity value) + { + if (value is Number or Variable) + return value; + foreach (var (name, named) in names) + if (named == value) + return name; + var fresh = Variable.CreateUnique(expr, "k_named" + names.Count); + names.Add((fresh, value)); + return fresh; + } + Entity Back(Entity value) + { + foreach (var (name, named) in names) + value = value.Substitute(name, named); + return value; + } + var (namedA, namedB, namedC) = (Named(a), Named(b), Named(c)); + pRead = pRead.ToDictionary(pair => pair.Key, pair => Named(pair.Value)); + + var q = new Dictionary { [EInteger.Zero] = namedC, [EInteger.One] = namedB, [EInteger.FromInt32(2)] = namedA }; + var qPrime = new Dictionary { [EInteger.Zero] = namedB, [EInteger.One] = (2 * namedA).InnerSimplified }; Dictionary PowerOfQ(int j) { var result = new Dictionary { [EInteger.Zero] = Number.Integer.One }; @@ -13794,12 +13820,12 @@ Dictionary PowerOfQ(int j) Entity r = Number.Integer.Zero; for (var k = 0; k <= degreeOfR; k++) { - var value = values[k].InnerSimplified; + var value = Back(values[k]).InnerSimplified; if (value.Evaled is Number.Complex { IsZero: true }) continue; r = r + value * (k == 0 ? Number.Integer.One : k == 1 ? x : MathS.Pow(x, k)); } - var kValue = values[degreeOfR + 1].InnerSimplified; + var kValue = Back(values[degreeOfR + 1]).InnerSimplified; var root = MathS.Pow(radicand, Number.Rational.Create(1, 2)); var powerOfQInFront = positive ? root : MathS.Pow(radicand, Number.Rational.Create(-(2 * j + 1), 2)); Entity reduced = r == Number.Integer.Zero ? Number.Integer.Zero : r * powerOfQInFront; diff --git a/Sources/Tests/UnitTests/Calculus/CompoundCoefficientsOfAQuadraticIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/CompoundCoefficientsOfAQuadraticIntegralTest.cs new file mode 100644 index 000000000..fb2c7c8f8 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/CompoundCoefficientsOfAQuadraticIntegralTest.cs @@ -0,0 +1,51 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// A polynomial times an odd half power of a quadratic whose coefficients are quotients or + /// products of symbols. The reduction solves a linear system in those coefficients, and + /// carried as written they came back in answers of 40,000 to 480,000 characters, after up to + /// eight seconds; named for the solve and written back, the answers are a thousand. The length is + /// what is checked, beside the derivative, since a time is not something a test can hold. + /// #718 + /// + [Trait("Area", "Calculus")] + public sealed class CompoundCoefficientsOfAQuadraticIntegralTest + { + [Theory] + [InlineData("sqrt(a + b*x + g^2*x^2/f^2)")] + [InlineData("d + g*x + f*sqrt(a + b*x + g^2*x^2/f^2)")] + [InlineData("x/sqrt(a + b*x + g^2*x^2/f^2)")] + [InlineData("(a + b*x + c*x^2/f)^(3/2)")] + [InlineData("x^2*sqrt(a*f + b*x + c*x^2/f)")] + public void IsAnsweredShortly(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + var written = integral.Stringize(); + Assert.DoesNotContain("integral(", written); + Assert.True(written.Length < 5000, $"the antiderivative of {integrand} is {written.Length} characters long"); + Entity Pinned(Entity e) => e.Substitute("a", 1.3).Substitute("b", 0.7).Substitute("c", 0.6) + .Substitute("d", 1.9).Substitute("f", 1.1).Substitute("g", 0.8); + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + foreach (var at in new[] { 0.3, 0.8, 1.5, 2.4 }) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart) + < 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}"); + } + } + } +}