From 23a46459f0de8a7623f8d4a01ff2f2dc062cd4eb Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 21 Sep 2026 14:55:38 +0000 Subject: [PATCH] acsch(c x) and asech(c x) are integrated with a symbolic c, and |c| differentiates to 0 in x x acsch(2 x) was answered and x acsch(c x) declined, for four reasons: (c x)^2 was read as a power of something that is not a polynomial where (2 x)^2 had been folded -- a whole power of a product with a symbolic constant beside the variable is written as the product of the powers, as a rule of its own; the root of (1 + c^2 x^2)/(c^2 x^2) was not written apart because c^2 is not a number -- a parameter's even power is taken for positive, the answer says so with provided c^2 > 0, and (c^2)^(-1/2) is written 1/|c|; the step by parts differentiated |c| to derivative(|c|, x) -- the modulus, the sign, the floor and their kin, and any node with no rule of its own, differentiate to 0 where they do not mention the variable; and q^(-1/2) above and sqrt(q) below were two atoms to the cancellation -- a negative fractional power goes below the bar where the same radicand also stands under a positive one. SolveAsPolynomialTerm gathers constants from any position among the factors once the two-operand reading has declined. Rubi's family 7: 162 -> 187 of 270, 0 wrong; the 1774 unchanged at 1706, 0 wrong, 0 timeout. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 29 ++++ Sources/.editorconfig | 3 + .../Functions/Continuous/Differentiation.cs | 39 +++-- .../Integration/IndefiniteIntegralSolver.cs | 156 +++++++++++++++++- .../Integration/Integration.Definition.cs | 3 + .../InverseHyperbolicSecantIntegralTest.cs | 71 ++++++++ .../UnitTests/Calculus/ModulusPhaseTest.cs | 19 +++ 7 files changed, 303 insertions(+), 17 deletions(-) create mode 100644 Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index f29d8c30a..717b975cb 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -788,6 +788,35 @@ written | `"forall n in ZZ+ : n! >= 2^(n - 1)".ToEntity().Evaled` | `UnhandledParseException` | `True` | | `"forall n in ZZ+ /\\ [4; +oo) : n^2 - 2 n - 1 > 0".ToEntity().Evaled` | `UnhandledParseException` | `True` — decided about `4 + t` over `ZZ*` | +### `acsch(c x)` and `asech(c x)` are integrated with a symbolic `c`, and `|c|` differentiates to `0` in `x` + +Three defects behind one symptom: `x acsch(2 x)` was answered and `x acsch(c x)` declined. The +parser spells the inverse hyperbolic cosecant `ln(1/(c x) + sqrt(1/(c x)^2 + 1))`, and (1) `(c x)^2` +was read as a power of something that is not a polynomial where `(2 x)^2` had been folded to +`4 x^2` — a whole power of a product with a symbolic constant beside the variable is now written +as the product of the powers, as a rule of its own; (2) the root of `(1 + c^2 x^2)/(c^2 x^2)` was +not written apart as `sqrt(1 + c^2 x^2)/(|c| |x|)` because `c^2` is not a number — a parameter's +even power is taken for positive now, and the answer says so, `provided c^2 > 0`, which holds +exactly for a real non-zero `c`; (3) the step by parts differentiated `|c|` to +`derivative(|c|, x)`, since the modulus rule asks whether its argument is real before it asks +whether the argument mentions the variable — **the modulus, the sign, the floor, the ceiling, +the rounding, a limit, a sum, a product, an applied function and any node with no rule of its +own differentiate to `0` where they do not mention the variable**, with no condition, which also +removes the `provided not c in ZZ` that `floor(c) + x` carried. A node with a rule of its own +keeps it: `d/dy piecewise(x provided a, 1/x)` still says where `1/x` exists. And a negative fractional power is put below the bar before two +quotients are cancelled as polynomials in their radicals, so that `q^(-1/2)` above and `sqrt(q)` +below are one indeterminate. Rubi's 7.5.1 and 7.6.1 +([#718](https://github.com/asc-community/AngouriMath/issues/718)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"abs(c) * x".ToEntity().Differentiate("x")` | `derivative(abs(c), x) * x + abs(c)` | `abs(c)` | +| `"floor(c) + x".ToEntity().Differentiate("x")` | `1 provided not c in ZZ` | `1` | +| `"c!".ToEntity().Differentiate("x")` | `derivative(c!, x)` | `0` | +| `"x * acsch(c * x)".ToEntity().Integrate("x")` | `integral(x * ln(1 / (c * x) + sqrt(1 / (c * x) ^ 2 + 1)), x)` | an antiderivative, `provided c^2 > 0` | +| `"x^2 * asech(c * x)".ToEntity().Integrate("x")` | `integral(...)`, the same | an antiderivative | +| `"x / sqrt(1 + 1/(c*x)^2)".ToEntity().Integrate("x")` | `integral(x / sqrt(1 + 1 / (c * x) ^ 2), x)` | an antiderivative, `provided c^2 > 0` | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 8a2013edd..b3ae3d874 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -291,6 +291,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/NumberTheory/ResidueClasses.cs] 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 +[Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs] +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 + [Tests/UnitTests/Core/InductionTest.cs] 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 diff --git a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs index 73af23d26..8db344553 100644 --- a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs +++ b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs @@ -71,9 +71,22 @@ internal Entity DifferentiateOnce(Variable variable) /// Internal differentiation function /// /// To derive over - /// The differentiated expressoin or the Derivative node + /// + /// The differentiated expression, or the Derivative node -- and zero for a node with no + /// rule of its own that does not mention the variable: it is a constant in it, and the + /// derivative of a constant is not an open question. + /// protected virtual Entity InnerDifferentiate(Variable variable) - => new Derivativef(this, variable, 1); + => ContainsNode(variable) ? new Derivativef(this, variable, 1) : Integer.Zero; + + /// + /// Whether the node is a constant in , which the modulus, the + /// sign, the floor and their kin ask before their own rule: |c| is a constant in + /// x whether or not c is known to be real, and floor(c) one with no + /// condition on c. |c| x was differentiated to derivative(|c|, x) x + |c|, + /// which every rule downstream read as a function of x. + /// + private bool IsConstantIn(Variable variable) => !ContainsNode(variable); partial record Variable { @@ -361,7 +374,7 @@ partial record Factorialf /// /// protected override Entity InnerDifferentiate(Variable variable) - => MathS.Derivative(this, variable, 1); + => base.InnerDifferentiate(variable); } #pragma warning disable IDE0054 // Use compound assignment @@ -371,7 +384,7 @@ partial record Derivativef protected override Entity InnerDifferentiate(Variable variable) => Var == variable ? this with { Iterations = Iterations + 1 } - : MathS.Derivative(this, variable, 1); + : base.InnerDifferentiate(variable); } partial record Integralf @@ -384,7 +397,7 @@ protected override Entity InnerDifferentiate(Variable variable) => ? to.InnerDifferentiate(variable) * Expression.Substitute(variable, to) - from.InnerDifferentiate(variable) * Expression.Substitute(variable, from) : Expression - : MathS.Derivative(this, variable, 1); + : base.InnerDifferentiate(variable); } #pragma warning restore IDE0054 // Use compound assignment @@ -396,7 +409,7 @@ protected override Entity InnerDifferentiate(Variable variable) => // See https://math.stackexchange.com/a/1048570/627798: // The derivative itself is a limit, so we can exchange limits if possible. -- Happypig375 - MathS.Derivative(this, variable); + base.InnerDifferentiate(variable); } partial record Signumf @@ -412,7 +425,8 @@ partial record Signumf // https://github.com/asc-community/AngouriMath/issues/1186 /// protected override Entity InnerDifferentiate(Variable variable) - => TreeAnalyzer.IsRealValued(Argument, variable) + => IsConstantIn(variable) ? Integer.Zero + : TreeAnalyzer.IsRealValued(Argument, variable) ? Integer.Zero.Provided(!Argument.EqualTo(Integer.Zero)) : MathS.Derivative(this, variable); } @@ -431,7 +445,8 @@ partial record Absf // https://github.com/asc-community/AngouriMath/issues/1186 /// protected override Entity InnerDifferentiate(Variable variable) - => TreeAnalyzer.TryTakeNumericModulusOut(Argument, out var withoutThePhase) + => IsConstantIn(variable) ? Integer.Zero + : TreeAnalyzer.TryTakeNumericModulusOut(Argument, out var withoutThePhase) ? withoutThePhase.InnerDifferentiate(variable) : TreeAnalyzer.IsRealValued(Argument, variable) ? MathS.Signum(Argument).Provided(!Argument.EqualTo(Integer.Zero)) * Argument.InnerDifferentiate(variable) @@ -447,14 +462,14 @@ partial record Floorf // has Providedf to say more with. /// protected override Entity InnerDifferentiate(Variable variable) - => Integer.Zero.Provided(!Argument.In(MathS.Sets.Z)); + => IsConstantIn(variable) ? Integer.Zero : Integer.Zero.Provided(!Argument.In(MathS.Sets.Z)); } partial record Ceilf { /// protected override Entity InnerDifferentiate(Variable variable) - => Integer.Zero.Provided(!Argument.In(MathS.Sets.Z)); + => IsConstantIn(variable) ? Integer.Zero : Integer.Zero.Provided(!Argument.In(MathS.Sets.Z)); } partial record Roundf @@ -463,7 +478,7 @@ partial record Roundf // jumps where the argument is exactly a half, not where it is an integer. /// protected override Entity InnerDifferentiate(Variable variable) - => Integer.Zero.Provided(!(Argument - Rational.Create(1, 2)).In(MathS.Sets.Z)); + => IsConstantIn(variable) ? Integer.Zero : Integer.Zero.Provided(!(Argument - Rational.Create(1, 2)).In(MathS.Sets.Z)); } partial record Providedf @@ -516,7 +531,7 @@ protected override Entity InnerDifferentiate(Variable variable) arg => MathS.Derivative(this, arg) * arg.Differentiate(variable) ).Aggregate((Entity)0, (a, b) => a + b), - _ => MathS.Derivative(this, variable) + _ => base.InnerDifferentiate(variable) }; } } diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs index 6f3873990..0cde22ba5 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs @@ -397,6 +397,37 @@ private static (Entity Numerator, Entity Denominator) CancelledWithFunctionsAsIn { if (!denominator.ContainsNode(x)) return (numerator, denominator); + // A negative fractional power is the reciprocal of the positive one, and goes + // below the bar so that `q^(-1/2)` above and `sqrt(q)` below are one atom and not + // two: the derivative of `ln(1/(c x) + sqrt(q))` by parts is such a quotient. + // Only where it joins two spellings of one root: a reciprocal root on its own is + // left where it is, since moving every one of them multiplied the other quotients + // through and fed a search -- Rubi's `x^5 atanh(x sqrt(p)/sqrt(d + p x^2))` went + // from a decline in 4 s to none. Where the two spellings do occur the joining is + // what lets the remainder be read, and the same row still runs out its budget: the + // remainder, read, is `x^6/sqrt(d + p x^2)` up to a constant, and that search is + // the cost of the capability rather than of this step. + var both = numerator.Nodes.Concat(denominator.Nodes) + .Where(node => node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && radicand.ContainsNode(x)) + .Select(node => (Radicand: ((Powf)node).Base, Negative: ((Number.Rational)((Powf)node).Exponent).ERational.IsNegative)) + .GroupBy(pair => pair.Radicand) + .Where(group => group.Any(pair => pair.Negative) && group.Any(pair => !pair.Negative)) + .Select(group => group.Key) + .ToList(); + Entity ReciprocalsBelow(Entity side) => side.Replace(node => + node is Powf(var radicand, Number.Rational r) && r is not Number.Integer && r.ERational.IsNegative && both.Contains(radicand) + ? 1 / MathS.Pow(radicand, -r) + : node); + if (both.Count > 0) + { + var (aboveOfTop, belowOfTop) = Functions.SingleQuotient.Of(ReciprocalsBelow(numerator)); + var (aboveOfBottom, belowOfBottom) = Functions.SingleQuotient.Of(ReciprocalsBelow(denominator)); + if (belowOfTop != Number.Integer.One || belowOfBottom != Number.Integer.One) + { + numerator = aboveOfTop * belowOfBottom; + denominator = belowOfTop * aboveOfBottom; + } + } var atoms = new Dictionary(); // Each name unique against the expression and against the atoms named before it. var named = numerator + denominator; @@ -1116,14 +1147,41 @@ private static bool TryReadAsQuotient(Entity expr, out Entity numerator, out Ent } } + // The constant factors gathered from every position among the factors, where a product + // read as two operands sees neither of `x^2 * b` and `L` as constant: `x^2 (a + b L)` + // splits into `a x^2` and `x^2 * b * L`, and the `b` inside the left product was + // integrated around rather than taken out, by parts against `b L` with the remainder + // one level deeper than the row without `a + b` -- five minutes where that took one + // second. Only after the two-operand reading has declined: gathering first sent + // `1/((a + b x)^2 (c + d x)^(3/2))` down another path and its answer grew from 700 + // characters to 24,000. Asked one level down, as before, and not as a question of its + // own: that reading made every sub-integrand's constant multiple a top-level question, + // and the rules scoped to the top then fired everywhere. + private static Entity? WithTheConstantFactorsOut(Entity.Mulf product, Entity.Variable x, bool integrateByParts) + { + // A constant that is the number 1 is a constant found, not none: `1 * x` is x. + Entity? constant = null; + Entity? rest = null; + foreach (var factor in Mulf.LinearChildren(product)) + if (!factor.ContainsNode(x)) + constant = constant is null ? factor : constant * factor; + else + rest = rest is null ? factor : rest * factor; + if (constant is null || rest is null) + return null; + return Integration.ComputeIndefiniteIntegral(rest, x, integrateByParts)?.Pipe(i => constant * i); + } + internal static Entity? SolveAsPolynomialTerm(Entity expr, Entity.Variable x, bool integrateByParts = true) => expr switch { - Entity.Mulf(var m1, var m2) => + // The constant factors wherever they stand among the factors: `x^2 * b * L` holds + // its `b` inside the left product, and read as two operands neither is constant. + Entity.Mulf(var m1, var m2) product => !m1.ContainsNode(x) ? Integration.ComputeIndefiniteIntegral(m2, x, integrateByParts)?.Pipe(i => m1 * i) : !m2.ContainsNode(x) ? Integration.ComputeIndefiniteIntegral(m1, x, integrateByParts)?.Pipe(i => m2 * i) : - null, + WithTheConstantFactorsOut(product, x, integrateByParts), Entity.Divf(var div, var over) => // A denominator that is partly constant is split first, and the order matters. @@ -9744,6 +9802,30 @@ node is Powf(Powf(var @base, var inner), var outer) return folded == expr ? null : Integration.ComputeAsAQuestionOfItsOwn(folded, x, integrateByParts); } + /// + /// A whole power of a product in which the variable stands beside a constant, written + /// as the product of the powers: (c x)^2 is c^2 x^2, exact for a whole + /// power over the complex numbers. That is the spelling the parser gives the inverse + /// hyperbolic secant and cosecant -- acsch(c x) is + /// ln(1/(c x) + sqrt(1/(c x)^2 + 1)) -- and every rule that reads a polynomial in + /// x read (c x)^2 as a power of something that is not one: x acsch(c x) + /// was declined where x acsch(2 x) was answered, the numeric coefficient having + /// been folded on the way. A symbolic constant beside the variable only: a numeric one + /// is folded by the rules already, and rewriting for it re-asked every sub-integrand + /// with a (2 u)^3 in it -- Welz's 67 went from 4 s to 34. Asked as a question of + /// its own. Rubi's 7.5.1 and 7.6.1. + /// https://github.com/asc-community/AngouriMath/issues/718 + /// + internal static Entity? SolveByDistributingWholePowersOfProducts(Entity expr, Entity.Variable x, bool integrateByParts) + { + var distributed = expr.Replace(node => + node is Powf(Mulf(var l, var r) product, Number.Integer power) && product.ContainsNode(x) + && (!l.ContainsNode(x) && l.Vars.Any() || !r.ContainsNode(x) && r.Vars.Any()) + ? MathS.Pow(l, power) * MathS.Pow(r, power) + : node); + return distributed == expr ? null : Integration.ComputeAsAQuestionOfItsOwn(distributed, x, integrateByParts); + } + internal static Entity? SolveAPolynomialTimesARationalFunctionOfAnExponential(Entity expr, Entity.Variable x, bool integrateByParts) { // The polynomial factors above the bar, and the rest, which holds x in exponents only. @@ -12842,6 +12924,10 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S if (!Integration.AnsweringTheQuestionAskedOrOneBelow) return null; var tookAPowerOfX = false; + // A parameter's even power taken for positive -- `c^2 x^2` below the bar, which is + // how `acsch(c x)` and `asech(c x)` arrive, is positive for a real `c` -- and the + // answer says so: `provided c^2 > 0`, which holds exactly for a real non-zero `c`. + Entity assumed = Entity.Boolean.True; var written = expr.Replace(node => { if (node is not Powf(var @base, Number.Rational exponent) || exponent is Number.Integer || !@base.ContainsNode(x)) @@ -12849,7 +12935,8 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S // As one quotient where it is a sum with a quotient in it: `1 - 1/x^2` is // `(x^2 - 1)/x^2`, the derivative of the arcsecant as it is written. var (above, below) = Functions.SingleQuotient.Of(AsOneQuotient(@base)); - if (below != Number.Integer.One && below.ContainsNode(x) && IsPositiveForReal(below, x) && OfModestDegree(above)) + if (below != Number.Integer.One && below.ContainsNode(x) && OfModestDegree(above) + && (IsPositiveForReal(below, x) || IsPositiveForARealParameter(below, x, out assumed, assumed))) return MathS.Pow(above, exponent) * PowerOfAPositive(below, -exponent); // A polynomial with rational roots is the product of its linear factors, and // the product is what the splitting below reads: `(1 - x^2)^(1/4)` is @@ -12954,7 +13041,8 @@ private static bool OnEveryRealInterval(List bases, Entity.Variable x, S var forPositive = Integration.ComputeIndefiniteIntegral(Functions.PartialFractions.Bare(written), x, integrateByParts: true); if (forPositive is null || forPositive.Nodes.Any(node => node == MathS.NaN)) return null; - return tookAPowerOfX ? ExtendedByParity(expr, forPositive, x) : forPositive; + var extended = tookAPowerOfX ? ExtendedByParity(expr, forPositive, x) : forPositive; + return extended?.Provided(assumed); // What is set free under the root is a polynomial of degree four at most: the // shapes the rules behind this read stop there, and a root of a sextic or a @@ -13054,15 +13142,73 @@ Entity PowerOfAPositive(Entity positive, Number.Rational exponent) { tookAPowerOfX = true; var power = MathS.Pow(x, (Number.Integer.Create(degree) * exponent).InnerSimplified); + // A parameter's even power to a fractional exponent is a power of its + // modulus, `(c^2)^(-1/2)` being `1/|c|`, which is one atom wherever it + // stands rather than two spellings of it. + var ofTheCoefficient = MathS.Pow(coefficient, exponent); + // The coefficient as the polynomial reader writes it, `1 * c^2 * 1^2`: + // its numeric factors together, its parameters' even powers as moduli. + Entity numeric = Number.Integer.One; + Entity symbolic = Number.Integer.One; + var readable = true; + foreach (var factor in Mulf.LinearChildren(coefficient)) + if (factor.Evaled is Number.Real { IsPositive: true } value) + numeric = numeric * value; + else if (factor is Powf(Variable parameter, Number.Integer { EInteger.IsEven: true } even) && even.EInteger.Sign > 0) + symbolic = symbolic * MathS.Pow(MathS.Abs(parameter), (even * exponent).InnerSimplified); + else + readable = false; + if (readable && symbolic != Number.Integer.One) + ofTheCoefficient = (numeric.InnerSimplified == Number.Integer.One ? symbolic : MathS.Pow(numeric.InnerSimplified, exponent) * symbolic); return coefficient.Evaled is Number.Integer { IsZero: false } one && one.EInteger.Equals(EInteger.One) ? power - : MathS.Pow(coefficient, exponent) * power; + : ofTheCoefficient * power; } } return MathS.Pow(positive, exponent); } } + /// + /// Whether the polynomial in is positive at + /// every real but possibly zero once its parameters are real: every + /// monomial an even power, every coefficient a positive number or an even power of a + /// symbol, or a product of those. The coefficients taken for positive are returned as + /// the condition c^2 > 0 and ..., conjoined with , + /// which is exactly the statement that each such parameter is real and not zero. + /// + private static bool IsPositiveForARealParameter(Entity expr, Entity.Variable x, out Entity assumed, Entity already) + { + assumed = already; + if (!TreeAnalyzer.TryGetPolynomial(expr, x, out var monomials) || monomials.Count == 0) + return false; + var conditions = new List(); + foreach (var pair in monomials) + { + if (!pair.Key.IsEven) + return false; + var symbolic = false; + foreach (var factor in Mulf.LinearChildren(pair.Value)) + { + if (factor.Evaled is Number.Real { IsPositive: true }) + continue; + if (factor is Powf(Variable, Number.Integer { EInteger.IsEven: true } n) && n.EInteger.Sign > 0) + { + symbolic = true; + continue; + } + return false; + } + if (symbolic) + conditions.Add(new Greaterf(pair.Value.InnerSimplified, Number.Integer.Zero)); + } + if (conditions.Count == 0) + return false; + foreach (var condition in conditions.Distinct()) + assumed = assumed == Entity.Boolean.True ? condition : assumed & condition; + return true; + } + /// /// Whether the polynomial in is positive at /// every real but possibly zero, for the plain reason that every diff --git a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs index 2e34871d9..5b740b587 100644 --- a/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs +++ b/Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs @@ -459,6 +459,9 @@ private static Entity Normalized(Entity expr, Entity.Variable x) => // An exponential of a multiple of a logarithm is a power of the argument, which is // how every inverse hyperbolic function under an exponential arrives. if ((answer = IndefiniteIntegralSolver.SolveByFoldingAnExponentialOfALogarithm(expr, x, integrateByParts)) is { }) return answer; + // A whole power of a product of a constant and the variable, as the product of + // the powers, which is how the inverse hyperbolic secant and cosecant arrive. + if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfProducts(expr, x, integrateByParts)) is { }) return answer; // A polynomial times a rational function of exponentials, by parts against the // whole rational function, before anything splits the sum: the general parts rule // takes each term on its own, and each term's antiderivative keeps a logarithm the diff --git a/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs new file mode 100644 index 000000000..e7e2afbe8 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/InverseHyperbolicSecantIntegralTest.cs @@ -0,0 +1,71 @@ +// +// 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 System.Linq; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// acsch(c x) and asech(c x) with a symbolic c: the parser spells them + /// ln(1/(c x) + sqrt(1/(c x)^2 ± 1)), and three things stood between that and an + /// answer -- (c x)^2 read as a power of something that is not a polynomial, the + /// root of (1 ± c^2 x^2)/(c^2 x^2) not written apart because c^2 was not a + /// number, and |c| differentiated to derivative(|c|, x) by parts -- where the + /// same rows with c = 2 were answered. Rubi's 7.5.1 and 7.6.1. + /// #718 + /// + [Trait("Area", "Calculus")] + public sealed class InverseHyperbolicSecantIntegralTest + { + /// Where asech(3 x / 2) is real, 0 < c x <= 1, and acsch too. + private static readonly double[] Points = { 0.15, 0.25, 0.4, 0.55, 0.62 }; + + private static void DifferentiatesBack(string integrand) + { + var integral = integrand.ToEntity().Integrate("x"); + Assert.DoesNotContain("integral(", integral.Stringize()); + // A condition on a parameter, `c^2 > 0`, holds at the c the rows are checked at. + var bare = integral.Substitute("C", 0).Substitute("c", "3/2".ToEntity()); + var derivative = bare.Differentiate("x"); + var original = integrand.ToEntity().Substitute("c", "3/2".ToEntity()); + var compared = 0; + foreach (var at in Points) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + if (got.IsNaN || want.IsNaN) + continue; + compared++; + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart)); + Assert.True(difference / scale < 1e-9, + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); + } + Assert.True(compared >= 4, $"only {compared} of {Points.Length} points were comparable for {integrand}"); + } + + [Theory] + [InlineData("x*acsch(c*x)")] + [InlineData("x^2*acsch(c*x)")] + [InlineData("x^2*asech(c*x)")] + [InlineData("x/sqrt(1 + 1/(c*x)^2)")] + public void AnInverseHyperbolicSecantOrCosecantWithASymbolicCoefficient(string integrand) => DifferentiatesBack(integrand); + + [Fact] + public void TheAnswerSaysTheParameterIsReal() + { + // c^2 > 0 is exactly "c is real and not zero", which is what taking sqrt(c^2 x^2) + // for |c| |x| assumed. + var integral = "x/sqrt(1 + 1/(c*x)^2)".ToEntity().Integrate("x"); + Assert.Contains(integral.Nodes, node => node is Providedf(_, var predicate) && predicate.Nodes.Any(inner => inner == "c^2 > 0".ToEntity())); + } + } +} diff --git a/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs b/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs index e5472ef12..285d45259 100644 --- a/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs @@ -117,6 +117,25 @@ public void TheDerivativeOfTheModulusOfAnImaginaryQuotientHasTheRightSign(string public void TheDerivativeOfAModulusNotShownRealIsLeftUnevaluated(string expr) => Assert.IsType(expr.ToEntity().Differentiate(x)); + // ...but a modulus, a sign, a floor or a limit that does not mention the variable is a + // constant in it, whatever its argument is, and the derivative of a constant is 0 with + // no condition: |c| x was differentiated to derivative(|c|, x) x + |c|, which every + // rule downstream read as a function of x. https://github.com/asc-community/AngouriMath/issues/718 + [Theory] + [InlineData("abs(c)", "0")] + [InlineData("abs(c) * x", "abs(c)")] + [InlineData("sgn(c) * x^2", "2 sgn(c) x")] + [InlineData("floor(c) + x", "1")] + [InlineData("ceil(c) * x", "ceil(c)")] + [InlineData("round(c) * x", "round(c)")] + [InlineData("c!", "0")] + [InlineData("limit(y^2, y, c) * x", "c^2")] + [InlineData("derivative(g, y) * x", "derivative(g, y)")] + [InlineData("apply(f, y)", "0")] + [InlineData("sum(k, k, 1, n) * x", "sum(k, k, 1, n)")] + public void AConstantInTheVariableDifferentiatesToZeroWhateverItsNode(string expr, string expected) + => Assert.Equal(expected.ToEntity().Simplify(), expr.ToEntity().Differentiate(x).Simplify()); + [Theory] [InlineData("abs(i * x)", "abs(x)")] [InlineData("abs(x * i)", "abs(x)")]