From 374bbee8bd0893fe6b3180edbde92aa5db06b323 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 03:08:11 +0000 Subject: [PATCH 1/2] A product of powers of quotients of linears is written over each linear once `e^atanh(a x) sqrt(c - c/(a x))` is `sqrt((1 + a x)/(1 - a x)) sqrt(c (a x - 1)/(a x))`, and the root `1/a` stands in both, with exponents `-1/2` and `1/2`. The substitution search did not finish on it. Written over each linear once it is `K sqrt(a x + 1)/sqrt(x)`, and `K` -- the integrand over that product -- has a zero logarithmic derivative, so it is constant wherever it is defined and stands in front of the answer, as Rubi writes `x^p (c + d/x)^p/(1 + c x/d)^p` in front of its. `SolveByWritingEachLinearOnce` reads the integrand through products, quotients, powers and the exponential of a multiple of a logarithm into linears whose roots are rational in the symbols, sums each root's exponents, and fires where a root stands in two bases under powers that are not whole, or in a root of a quotient with x below the bar and another base, and at most two linears are left under such powers. Each linear takes the sign that is positive on an interval where the integrand is real, so that the product is real there; each such sign is an arm saying where it holds, found with the other symbols pinned to either sign, and an arm for negative x is integrated in `t = -x`, where the power of the variable is one the rules read. The product is asked as a quotient, and without this rule. Every arm or none. With every power whole, a root of a quotient shared with another base is enough -- `e^(2 acoth(a x))/(c - c/(a^2 x^2))^2` is `K x^4/((a x + 1)(a x - 1)^3)`, which the partial fractions answer at once -- and `K` is a constant, with one arm and no condition. First in the chain, at the question asked: the polynomial term writes `1/u` as `u^(-1)` and asks that a level down. Two tests pinned the conditioned answers the linear-radical step gives for integrands this rule now answers on both sides; they assert that instead, and the step's condition stays pinned on `1/sqrt(x + x^(3/2))`. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../Integration/IndefiniteIntegralSolver.cs | 344 ++++++++++++++++++ .../Integration/Integration.Definition.cs | 5 + .../EachLinearWrittenOnceIntegralTest.cs | 82 +++++ .../LinearRadicalSubstitutionIntegralTest.cs | 22 +- 4 files changed, 441 insertions(+), 12 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/EachLinearWrittenOnceIntegralTest.cs diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index b1ceaa86b..d4ed3acab 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -18585,6 +18585,350 @@ private static bool TryWriteAPairOfLinearsOverARadicand(Entity expr, Entity.Vari }; } + /// Set while asks its product. + [System.ThreadStatic] private static bool writingEachLinearOnce; + + /// + /// A product of powers of quotients of polynomials in whose roots + /// are rational in the symbols, where one root stands in two of the bases, written over + /// each linear once: K (x - r_1)^(e_1) ... (x - r_k)^(e_k), with e_i the sum + /// of the exponents r_i has in the bases. K is the integrand over that + /// product, and its logarithmic derivative is zero -- each base is its leading coefficient + /// times its linears, so both sides have the same sum e_i/(x - r_i) -- so it is + /// constant wherever it is defined and stands in front of the answer, the way Rubi writes + /// x^p (c + d/x)^p/(1 + c x/d)^p in front of its. + /// + /// + /// + /// e^atanh(a x) sqrt(c - c/(a x)) is sqrt((1 + a x)/(1 - a x)) sqrt(c (a x - 1)/(a x)), + /// and the root 1/a stands in both bases, with exponents -1/2 and 1/2. + /// Written once it is gone: the integrand is K sqrt(a x + 1)/sqrt(x), which the + /// rule for two linear radicals answers in milliseconds, where the substitution search ran + /// out of time. Rubi's 7.3.6 and 7.4.2. https://github.com/asc-community/AngouriMath/issues/718 + /// + /// + /// Only where it helps: a root stands in two bases under powers that are not whole -- + /// sqrt(c - a c x)/sqrt(1 - a x) -- or in a root of a quotient with x below the bar, + /// sqrt(c - c/(a x)), and in another base; and at most two linears are left under a + /// power that is not whole, which is what the rule for two linear radicals reads. A root + /// beside a whole power of one of its linears -- sqrt(x)/(x (x + 1)), and + /// (1 - a^2 x^2)^(3/2)/((1 - a x)^2 (c + d x)), which the rules behind this one write + /// over the radicand -- is read by the rules as it is written, and is answered without a + /// K. + /// + /// + internal static Entity? SolveByWritingEachLinearOnce(Entity expr, Entity.Variable x, bool integrateByParts) + { + // Not while answering its own product, see below; and only where something of x is + // under a power that is not whole, or is the exponential of a logarithm -- looked for + // before anything is read, since this is the first rule every question is asked of. + if (writingEachLinearOnce || !Integration.AnsweringTheQuestionAsked + || !expr.Nodes.Any(node => node is Powf(var @base, var exponent) && @base.ContainsNode(x) && exponent is Number.Rational and not Number.Integer + || node is Powf(var e, var power) && e == MathS.e && power.Nodes.Any(inner => inner is Logf))) + return null; + // Each root, with the sum of its exponents, the bases it stands in, and whether one of + // them is under a power that is not whole; and each base, with its roots, whether it + // is under such a power, and whether it is a quotient with x below the bar. + var roots = new List<(Entity Root, Number.Rational Exponent, HashSet Bases, bool UnderARoot)>(); + var bases = new List<(HashSet Roots, bool UnderARoot, bool AQuotient)>(); + if (!Read(expr, Number.Integer.One)) + return null; + // With every power whole the integrand is a rational function, and a root of a quotient + // shared with another base is the same help: `e^(2 acoth(a x))/(c - c/(a^2 x^2))^2` is + // `K x^4/((a x + 1)(a x - 1)^3)`, which the partial fractions answer at once, where + // as written it ran out of time; and `K` is a constant there, with no arms to it. + var wholePowersOnly = !bases.Any(written => written.UnderARoot); + if (!roots.Any(root => root.Bases.Count(which => bases[which].UnderARoot) >= 2 + || root.Bases.Any(which => (bases[which].UnderARoot || wholePowersOnly) && bases[which].AQuotient) && root.Bases.Count >= 2) + || roots.Count(root => root.Exponent is not Number.Integer) > 2) + return null; + // Each linear with the root's denominator for its leading coefficient: `a x + 1` for + // the root `-1/a`, as the integrand writes it, where `x + 1/a` is the same up to `K`. + // Every root's, those whose exponents add up to zero too: the integrand changes there. + var linears = new List<(Entity Linear, Number.Rational Exponent)>(); + foreach (var (root, exponent, _, _) in roots) + { + var (above, below) = Functions.SingleQuotient.Of(root); + var linear = root == Number.Integer.Zero ? x + : (above is Number.Real { IsNegative: true } negative ? below * x + (-negative).InnerSimplified : below * x - above).InnerSimplified; + linears.Add((linear, exponent)); + } + // And with the sign that is positive on an interval where the integrand is real: there + // the product is real too, `K` taking the imaginary unit the other sign would leave, + // and the rules answer a product of linears where it is real. `e^atanh(a x) sqrt(c - c/(a x))` + // is real between `-1/a` and 0 for a positive `c`, where `sqrt(a x + 1)/sqrt(x)` has + // no value in the answer the rules give and `sqrt(a x + 1)/sqrt(-x)` has one; and for + // a negative `c` it is real between 0 and `1/a`, where `sqrt(a x + 1)/sqrt(x)` is the + // one. Each such sign is an arm, saying where it holds -- where every linear under a + // power that is not whole is not negative, which is where its product is real, and + // where its answer holds whatever the symbols are -- and the arms are found with the + // other symbols pinned to either sign. + var arms = new List(); + var tried = new HashSet(); + foreach (var (found, pinned, low, high) in IntervalsWhereItIsReal(expr, linears.Select(written => written.Linear).ToList(), x)) + { + var signs = wholePowersOnly ? linears.Select(_ => 1).ToArray() : found; + if (!tried.Add(string.Join(",", linears.Select((written, i) => written.Exponent == Number.Integer.Zero ? 0 : signs[i])))) + continue; + // As a quotient, the negative powers below the bar, which is how the rules read it: + // `(a x + 1)^(-1/2) (1 - a x)^(-1) x^(3/2)` was declined where + // `x^(3/2)/(sqrt(a x + 1) (1 - a x))` is answered. + Entity above = Number.Integer.One, below = Number.Integer.One; + Entity holdsWhere = Entity.Boolean.True; + for (var i = 0; i < linears.Count; i++) + { + var (linear, exponent) = linears[i]; + if (exponent == Number.Integer.Zero) + continue; + if (signs[i] < 0) + linear = linear is Variable ? -linear + : TreeAnalyzer.TryGetPolyLinear(linear, x, out var slope, out var intercept) ? ((-intercept).InnerSimplified - slope * x).InnerSimplified + : (-linear).InnerSimplified; + if (exponent is Number.Rational { ERational.Sign: < 0 }) + { + var positive = (Number.Rational)(-exponent); + below = below == Number.Integer.One ? (positive == Number.Integer.One ? linear : MathS.Pow(linear, positive)) + : below * (positive == Number.Integer.One ? linear : MathS.Pow(linear, positive)); + } + else + above = above == Number.Integer.One ? (exponent == Number.Integer.One ? linear : MathS.Pow(linear, exponent)) + : above * (exponent == Number.Integer.One ? linear : MathS.Pow(linear, exponent)); + if (exponent is not Number.Integer) + { + var notNegative = new GreaterOrEqualf(linear, Number.Integer.Zero); + holdsWhere = holdsWhere == Entity.Boolean.True ? notNegative : holdsWhere & notNegative; + } + } + var product = below == Number.Integer.One ? above : above / below; + // The product is asked with this rule out of the way: the rules in front of it write + // `sqrt(x + 1)/x^(5/2)` as `sqrt(x^2 + x)/x^3`, a root of a product beside a power + // of one of its linears, and asked of this rule again that was its own question with + // arms of its own, one fewer each time. + // Where the arm is for negative x, in t = -x, with the power of x a power of t: the + // rules read `x^(1/2)` and not `(-x)^(1/2)`, and `sqrt(-x) (1 - a x)^2/(1 + a x)^(3/2)` + // ran out of time where `sqrt(t) (1 + a t)^2/(1 - a t)^(3/2)` takes milliseconds. + var reflected = linears.Any(written => written.Linear == x && written.Exponent != Number.Integer.Zero) + && signs[linears.FindIndex(written => written.Linear == x)] < 0; + var t = reflected ? Variable.CreateUnique(expr, "t") : x; + Entity? integrated; + writingEachLinearOnce = true; + try + { + integrated = Integration.ComputeAsAQuestionOfItsOwn(reflected ? product.Substitute(x, -t).InnerSimplified : product, t, integrateByParts); + } + finally + { + writingEachLinearOnce = false; + } + if (reflected && integrated is not null) + integrated = -integrated.Substitute(t, -x); + // Every arm or none: an interval left without one would have no answer where the + // rules behind this one may give one. + if (integrated is null || integrated.Nodes.Any(node => node == MathS.NaN)) + return null; + var arm = expr / product * integrated; + // Checked at two points of that interval, with the symbols pinned as they were there. + var inside = new[] { low + (high - low) / 3, low + 2 * (high - low) / 3 } + .Select(at => at.ToString("R", System.Globalization.CultureInfo.InvariantCulture)).ToArray(); + if (!Functions.PartialFractions.HoldsAtSampledPoints(pinned(arm.Differentiate(x)), pinned(expr), x, inside)) + return null; + arms.Add(new Providedf(arm, holdsWhere)); + } + return arms.Count switch + { + 0 => null, + 1 => arms[0], + _ => MathS.Piecewise(arms), + }; + + // A constant in lowest terms: numbers by their arithmetic, which is exact and cheap, and + // quotients in the symbols by the polynomial gcd. This rule is the first every + // question is asked of, and most constants it meets are numbers. + static Entity Reduced(Entity constant) + => constant.Vars.Any() ? Functions.PartialFractions.InLowestTermsOverTheSymbols(constant) : constant.InnerSimplified; + + // `factor` to the power `times`, read into the table through products, quotients and + // powers of them, the power of a product taken as the product of the powers and the + // exponential of a multiple of a logarithm as the power it is, `e^(1/2 ln q)` as + // `q^(1/2)` -- which is how the inverse hyperbolic functions under an exponential + // arrive. Each of those has the logarithmic derivative it is read with, whatever the + // branches, and `K` takes what the branches make of it. False where a factor of x is + // anything else. + bool Read(Entity factor, Number.Rational times) + { + if (!factor.ContainsNode(x)) + return true; + switch (factor) + { + case Mulf: + return Mulf.LinearChildren(factor).All(child => Read(child, times)); + case Divf(var above, var below): + return Read(above, times) && Read(below, (Number.Rational)(-times)); + case Powf(var @base, Number.Rational exponent): + return Read(@base, (Number.Rational)(times * exponent)); + case Powf(var @base, var exponent) when @base == MathS.e && AMultipleOfALogarithm(exponent) is var (multiple, argument): + return Read(argument, (Number.Rational)(times * multiple)); + case Variable or Sumf or Minusf: + var index = bases.Count; + var underARoot = times is not Number.Integer; + var (top, bottom) = Functions.SingleQuotient.Of(AsOneQuotient(factor)); + bases.Add((new HashSet(), underARoot, bottom.ContainsNode(x))); + return WithItsRoots(top, times, index, underARoot) && WithItsRoots(bottom, (Number.Rational)(-times), index, underARoot); + default: + return false; + } + } + + // `c ln(q)` as `(c, q)` for a rational `c` -- a product of rationals and one natural + // logarithm -- and null otherwise. + (Number.Rational Multiple, Entity Argument)? AMultipleOfALogarithm(Entity exponent) + { + Number.Rational multiple = Number.Integer.One; + Entity? argument = null; + foreach (var factor in Mulf.LinearChildren(exponent)) + switch (factor) + { + case Number.Rational rational: + multiple = (Number.Rational)(multiple * rational); + break; + case Logf(var logarithmBase, var antilogarithm) when logarithmBase == MathS.e && argument is null: + argument = antilogarithm; + break; + default: + return null; + } + return argument is null ? null : (multiple, argument); + } + + // The roots of a polynomial in x, each its multiplicity times `exponent` into the + // table: a power of x, a linear, and a quadratic with no linear term whose roots are + // a monomial and its negative. False for anything else. + bool WithItsRoots(Entity polynomial, Number.Rational exponent, int @base, bool underARoot) + { + if (!polynomial.ContainsNode(x)) + return true; + if (!TreeAnalyzer.TryGetPolynomial(polynomial, x, out var read) || read.Count == 0 + || read.Values.Any(coefficient => coefficient.ContainsNode(x))) + return false; + var lowest = read.Keys.Min()!; + var highest = read.Keys.Max()!; + if (lowest.Sign < 0) + return false; + Entity CoefficientOf(EInteger power) => read.TryGetValue(power, out var coefficient) ? coefficient : Number.Integer.Zero; + if (lowest.Sign > 0) + Add(Number.Integer.Zero, (Number.Rational)(exponent * Number.Integer.Create(lowest))); + var degree = highest.Subtract(lowest); + if (degree.IsZero) + return true; + if (degree.Equals(EInteger.One)) + { + Add(Reduced(-CoefficientOf(lowest) / CoefficientOf(highest)), exponent); + return true; + } + if (degree.Equals(EInteger.FromInt32(2)) + && Reduced(CoefficientOf(lowest.Add(EInteger.One))) == Number.Integer.Zero + && AMonomialSquareRoot(-CoefficientOf(lowest) / CoefficientOf(highest)) is { } root) + { + Add(root, exponent); + Add(Reduced(-root), exponent); + return true; + } + return false; + + void Add(Entity root, Number.Rational times) + { + for (var i = 0; i < roots.Count; i++) + if (roots[i].Root == root || Reduced(roots[i].Root - root) == Number.Integer.Zero) + { + roots[i].Bases.Add(@base); + roots[i] = (roots[i].Root, (Number.Rational)(roots[i].Exponent + times), roots[i].Bases, roots[i].UnderARoot || underARoot); + bases[@base].Roots.Add(i); + return; + } + roots.Add((root, times, new HashSet { @base }, underARoot)); + bases[@base].Roots.Add(roots.Count - 1); + } + } + + // For each way of pinning the other symbols -- all positive, all negative, and each + // sign in turn -- the signs of `linears` on every interval between their roots where + // `expr` may be real, with the pinning and that interval, the outer ones a unit wide. + static IEnumerable<(int[] Signs, System.Func Pinned, double Low, double High)> IntervalsWhereItIsReal(Entity expr, List linears, Entity.Variable x) + { + static double? ValueOf(Entity e) + => Numerics.IntervalEvaluation.Of(e) is { IsFinite: true, Im.IsZero: true } value ? (value.Re.Low + value.Re.High) / 2 : null; + var symbols = expr.Vars.Where(symbol => symbol != x).ToList(); + for (var pattern = 0; pattern < (symbols.Count switch { 0 => 1, 1 => 2, _ => 4 }); pattern++) + { + var pins = new Dictionary(); + for (var i = 0; i < symbols.Count; i++) + { + var negative = pattern == 1 || pattern == 2 && i % 2 == 1 || pattern == 3 && i % 2 == 0; + pins[symbols[i]] = (negative ? -1 : 1) * (1.37 + 0.61 * i); + } + System.Func pinned = e => pins.Count == 0 ? e : e.Substitute(pins); + var pinnedLinears = linears.Select(pinned).ToList(); + var places = new List(); + foreach (var linear in pinnedLinears) + { + if (!TreeAnalyzer.TryGetPolyLinear(linear, x, out var slope, out var intercept) + || ValueOf(slope) is not { } a || a == 0 || ValueOf(intercept) is not { } b) + yield break; + places.Add(-b / a); + } + places = places.Distinct().OrderBy(place => place).ToList(); + var bounds = new List { places[0] - 1 }; + bounds.AddRange(places); + bounds.Add(places[^1] + 1); + var integrand = pinned(expr); + for (var i = 0; i + 1 < bounds.Count; i++) + { + var (low, high) = (bounds[i], bounds[i + 1]); + if (high - low < 1e-9) + continue; + var at = (low + high) / 2; + if (Numerics.IntervalEvaluation.Of(integrand.Substitute(x, at)) is not { IsFinite: true } value || !value.Im.ContainsZero) + continue; + var signs = new int[linears.Count]; + var read = true; + for (var j = 0; j < linears.Count && read; j++) + if (ValueOf(pinnedLinears[j].Substitute(x, at)) is { } sign && sign != 0) + signs[j] = System.Math.Sign(sign); + else + read = false; + if (read) + yield return (signs, pinned, low, high); + } + } + } + + // The square root of a quotient of monomials in the symbols with a positive square + // rational coefficient and even powers -- `1/a^2` is `(1/a)^2` -- and null otherwise. + static Entity? AMonomialSquareRoot(Entity constant) + { + var (above, below) = Functions.SingleQuotient.Of(Functions.PartialFractions.InLowestTermsOverTheSymbols(constant)); + return RootOf(above) is { } top && RootOf(below) is { } bottom ? top / bottom : null; + + static Entity? RootOf(Entity monomial) + { + Entity root = Number.Integer.One; + foreach (var factor in Mulf.LinearChildren(monomial)) + switch (factor) + { + case Number.Integer { EInteger: var whole } when whole.Sign > 0 && whole.Sqrt() is var floor && floor.Multiply(floor).Equals(whole): + root *= Number.Integer.Create(floor); + break; + case Powf(Variable symbol, Number.Integer { EInteger: var power }) when power.Sign > 0 && power.IsEven: + root *= MathS.Pow(symbol, Number.Integer.Create(power.Divide(EInteger.FromInt32(2)))); + break; + default: + return null; + } + return root; + } + } + } + /// /// Whether over takes two values at /// two points where , a sine, cosine, tangent, cotangent, diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 5889a488e..dec7b78a0 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -606,6 +606,11 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // dropped it re-enabled integration by parts one level below the call that // switched it off -- which is a cycle, since by parts calls back into here. // `x * ln(x)` went round it until the stack ran out. + // A product of powers of quotients of linears with one linear in two of them, written + // over each linear once with the constant that takes in front. First, since it answers + // the question asked only, and the polynomial term below writes `1/u` as `u^(-1)` and + // asks that a level down: `1/(e^atanh(a x) (c - c/(a x))^(3/2))` reached it nowhere. + if ((answer = IndefiniteIntegralSolver.SolveByWritingEachLinearOnce(expr, x, integrateByParts)) is { }) return answer; if ((answer = IndefiniteIntegralSolver.SolveAsPolynomialTerm(expr, x, integrateByParts)) is { }) return answer; // A product of powers of the variable with a power of a constant multiple of it // among them, `(c x)^m x^n`, by the power rule with the written power kept as diff --git a/Sources/Tests/UnitTests/Calculus/EachLinearWrittenOnceIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/EachLinearWrittenOnceIntegralTest.cs new file mode 100644 index 000000000..6f96cfd1d --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/EachLinearWrittenOnceIntegralTest.cs @@ -0,0 +1,82 @@ +// +// 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 product of powers of quotients of linears in which one linear stands in two of them, + /// written over each linear once with a constant K in front: e^atanh(a x) sqrt(c - c/(a x)) + /// is sqrt((1 + a x)/(1 - a x)) sqrt(c (a x - 1)/(a x)), and written once it is + /// K sqrt(a x + 1)/sqrt(x). Rubi's 7.3.6 and 7.4.2, which ran out of time. + /// #718 + /// + [Trait("Area", "Calculus")] + public sealed class EachLinearWrittenOnceIntegralTest + { + /// + /// On both sides of every root, so that K is checked wherever it takes another + /// value: the derivative of the answer against the integrand wherever the integrand is + /// real, with a and c pinned. + /// + private static readonly double[] Points = { -3.1, -1.7, -0.9, -0.45, -0.2, 0.15, 0.4, 0.95, 1.6, 2.9 }; + + [Theory] + [InlineData("e^atanh(a*x)*sqrt(c-c/(a*x))", "a=1.37,c=2.11")] + [InlineData("e^atanh(a*x)*sqrt(c-c/(a*x))", "a=1.37,c=-2.11")] + [InlineData("e^(3*atanh(a*x))*(c-c/(a*x))^(5/2)", "a=0.71,c=1.9")] + [InlineData("(c-c/(a*x))^(3/2)/e^atanh(a*x)", "a=1.37,c=2.11")] + [InlineData("e^atanh(a*x)*sqrt(c-c/(a*x))/x^2", "a=1.37,c=2.11")] + [InlineData("sqrt(c-c/(a*x))/(e^(2*atanh(a*x))*x^2)", "a=1.37,c=2.11")] + [InlineData("e^(2*acoth(a*x))*sqrt(c-c/(a^2*x^2))", "a=1.37,c=2.11")] + [InlineData("e^(2*acoth(a*x))/(c-c/(a^2*x^2))^(3/2)", "a=1.37,c=-2.11")] + [InlineData("e^atanh(x)*(1-x)^(1/2)", "")] + [InlineData("e^atanh(a*x)*sqrt(c-a*c*x)", "a=1.37,c=2.11")] + [InlineData("x*sqrt(c-a*c*x)/e^(3*atanh(a*x))", "a=3.1,c=0.7")] + [InlineData("sqrt(x)/(sqrt(1/x + 1)*sqrt(1 - x^2))", "")] + [InlineData("sqrt((x - 1)/(x + 1))/x", "")] + [InlineData("x*sqrt(c - a*c*x)/(1 + a*x)^(3/2)*(1 - a*x)^(-1/2)", "a=3.1,c=0.7")] + [InlineData("1/(e^atanh(a*x)*(c-c/(a*x))^(3/2))", "a=1.37,c=2.11")] + [InlineData("x*sqrt(c-c/(a*x))/e^(3*atanh(a*x))", "a=1.37,c=2.11")] + [InlineData("x*sqrt(c-c/(a*x))/e^(3*atanh(a*x))", "a=1.37,c=-2.11")] + [InlineData("e^(2*acoth(a*x))/(c-c/(a^2*x^2))^2", "a=1.37,c=2.11")] + [InlineData("1/(e^(2*atanh(a*x))*(c-c/(a^2*x^2))^4)", "a=1.37,c=-2.11")] + public void IsAnsweredWithEachLinearOnce(string integrand, string pins) + { + var integral = integrand.ToEntity().Integrate("x").Substitute("C", 0); + Assert.DoesNotContain("integral(", integral.Stringize()); + Assert.DoesNotContain("NaN", integral.Stringize()); + Entity Pin(Entity e) + { + foreach (var pin in pins.Split(',', StringSplitOptions.RemoveEmptyEntries)) + { + var parts = pin.Split('='); + e = e.Substitute(parts[0], double.Parse(parts[1], System.Globalization.CultureInfo.InvariantCulture)); + } + return e; + } + var derivative = Pin(integral).Differentiate("x"); + var original = Pin(integrand.ToEntity()); + var compared = 0; + foreach (var at in Points) + { + var want = original.Substitute("x", at).EvalNumerical(); + if (want.IsNaN || Math.Abs((double)want.ImaginaryPart) > 1e-12 * Math.Max(1, Math.Abs((double)want.RealPart))) + continue; + var got = derivative.Substitute("x", at).EvalNumerical(); + compared++; + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + Assert.True(difference < 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}"); + } + Assert.True(compared >= 2, $"only {compared} points where {integrand} is real"); + } + } +} diff --git a/Sources/Tests/UnitTests/Calculus/LinearRadicalSubstitutionIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/LinearRadicalSubstitutionIntegralTest.cs index a88f6d822..910eaaa36 100644 --- a/Sources/Tests/UnitTests/Calculus/LinearRadicalSubstitutionIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/LinearRadicalSubstitutionIntegralTest.cs @@ -85,7 +85,9 @@ private static void DifferentiatesBack(string integrand) /// above a x = 1 is the product of two imaginary factors -- while the answer built /// for a real u is not its antiderivative: with a = 3.1, c = 0.7 at /// x = 0.59 its derivative was 14% off, and it shipped as an answer. Now it is - /// NaN there, which is no claim, and right where the condition holds. + /// NaN there, which is no claim, and right where the condition holds. Rubi's integrand + /// is written over each linear once before that step is reached + /// (), and is answered on both sides. /// https://github.com/asc-community/AngouriMath/issues/718 /// [Fact] @@ -97,24 +99,19 @@ public void TheEvenRootStepSaysWhereItHolds() var integrand = "x * sqrt(c - a*c*x) / e^(3 * atanh(a*x))".ToEntity(); var integral = integrand.Integrate("x"); Assert.DoesNotContain("integral(", integral.Stringize()); - Assert.Contains("provided c - a * c * x >= 0", integral.Stringize()); var pinned = integral.Substitute("C", 0).Substitute("a", 3.1).Substitute("c", 0.7); var derivative = pinned.Differentiate("x"); var original = integrand.Substitute("a", 3.1).Substitute("c", 0.7); - // Inside the condition, a x < 1: the derivative is the integrand. - foreach (var at in new[] { 0.05, 0.17, 0.31 }) + // On both sides of a x = 1: the integrand is real at 0.59 too, 3.1 * 0.59 being above 1. + foreach (var at in new[] { 0.05, 0.17, 0.31, 0.59, 0.9 }) { var got = derivative.Substitute("x", at).EvalNumerical(); var want = original.Substitute("x", at).EvalNumerical(); - Assert.False(got.IsNaN, $"no answer at x = {at}, inside the condition"); + Assert.True(Math.Abs((double)want.ImaginaryPart) < 1e-12, $"the integrand at x = {at} is {want}, not real"); + Assert.False(got.IsNaN, $"no answer at x = {at}"); Assert.True(Math.Abs((double)(got - want).RealPart) < 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart)), $"d/dx of the antiderivative is {got} at x = {at}, where the integrand is {want}"); } - // Beyond it the integrand is real, 3.1 * 0.59 being above 1, and the answer claims - // nothing rather than something wrong. - var outside = original.Substitute("x", 0.59).EvalNumerical(); - Assert.True(Math.Abs((double)outside.ImaginaryPart) < 1e-12, $"the integrand at x = 0.59 is {outside}, not real"); - Assert.True(derivative.Substitute("x", 0.59).Evaled.IsNaN, "the answer claims a derivative outside its condition"); } /// @@ -191,12 +188,13 @@ public void TheSearchsEvenRootSaysWhereItHolds() /// is negative. sqrt(cos(x))/sqrt(1 + sec(x)) is real where the cosine is negative, /// two of its roots imaginary there; under t = cos(x) and u = sqrt(t) the /// product of roots below the bar was written with sqrt(u^4) = u^2, and the answer - /// was negated wherever the cosine is negative. Rubi's 4.5.1.2, and the same in t. + /// was negated wherever the cosine is negative. Rubi's 4.5.1.2. The same in t, + /// sqrt(t)/(sqrt(1/t + 1) sqrt(1 - t^2)), is written over each linear once and + /// answered on both sides (). /// https://github.com/asc-community/AngouriMath/issues/1581 /// [Theory] [InlineData("sqrt(cos(x))/sqrt(1 + sec(x))", "provided cos(x) >= 0", new[] { 0.3, 0.9, -0.7 }, 2.4)] - [InlineData("sqrt(x)/(sqrt(1/x + 1)*sqrt(1 - x^2))", "provided x >= 0", new[] { 0.2, 0.5, 0.8 }, -0.6)] public void AnEvenRootTakenRealWithoutASignSaysWhereItHolds(string written, string condition, double[] inside, double outsideAt) { var integrand = written.ToEntity(); From 841df7e881df236f5c3d9c5897194693fb501e4d Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 2 Oct 2026 11:39:44 +0000 Subject: [PATCH 2/2] A breaking-changes entry for a product of powers of quotients of linears Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b5268044d..2b91f6bf1 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -975,6 +975,25 @@ and this is the same answer reached through the even root |---|---|---| | `"sqrt(x)/(sqrt(1/x + 1)*sqrt(1 - x^2))".Integrate("x")` | `integral(...)` | an antiderivative in `sqrt(x)`, `provided x >= 0` | +### A product of powers of quotients of linears is written over each linear once + +**Improvement, not silent.** `e^atanh(a x) sqrt(c - c/(a x))` is +`sqrt((1 + a x)/(1 - a x)) sqrt(c (a x - 1)/(a x))`, in which the linear `1 - a x` stands twice, under +two roots, and the substitution search took one root at a time and ran out of time. The integrand is +now written over each linear once, `K sqrt(a x + 1)/sqrt(x)`, where `K` is the integrand over that +product: its logarithmic derivative is zero, so it is constant on every interval where it is defined, +and it stands in front of the answer as Rubi's does. Each sign the linears take where the integrand is +real is an arm of a piecewise, saying where it holds. Rubi's 7.3.6 and 7.4.2 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"e^atanh(a*x)*sqrt(c-c/(a*x))".Integrate("x")` | left unevaluated | the antiderivative, a piecewise with an arm for each interval where it is real | +| `"1/(e^atanh(a*x)*(c-c/(a*x))^(3/2))".Integrate("x")` | left unevaluated | the same | +| `"e^(2*acoth(a*x))/(c-c/(a^2*x^2))^2".Integrate("x")` | left unevaluated | `K` times the antiderivative of `x^4/((a x + 1)(a x - 1)^3)` | +| `"x*sqrt(c - a*c*x)/e^(3*atanh(a*x))".Integrate("x")` | left unevaluated | the antiderivative on both sides of `a x = 1`, where `master` gave one `provided c - a c x >= 0` | +| `"sqrt(x)/(sqrt(1/x + 1)*sqrt(1 - x^2))".Integrate("x")` | left unevaluated | the antiderivative on both sides of `x = 0`, where `master` gave one `provided x >= 0` | + ### The inverse hyperbolic tangent's answer is given on its domain, and says so `1/((1 - a^2 x^2)^(5/2) artanh(a x))` was left unevaluated, although the substitution `a x = tanh(u)`