Skip to content

Commit f62db4c

Browse files
atanh(tanh(a + b x)) under an integral is a logarithm linear in x, and is named so (#1456)
An inverse hyperbolic function of a hyperbolic one is spelled by the parser as a logarithm of a nested quotient -- atanh(tanh(u)) is 1/2 ln((1 + T)/(1 - T)) with T = (E - 1)/(E + 1) and E = e^(2u) -- which no integration rule read. Two routes, each asked as a question of its own: - SolveByCancellingInsideALogarithm: a logarithm whose argument is a quotient cancelled with the functions of x in it taken for indeterminates, so atanh(tanh(u)) is 1/2 ln(E) and acoth(tanh(u)) is 1/2 ln(-E). - SolveByNamingALogarithmLinearInTheVariable: a logarithm whose derivative in x is a constant k, read off the factors of its argument, is k x + c for a constant c, named and written back as ln(...) - k x in the answer -- the b x - atanh(tanh(a + b x)) Rubi's answers carry. The logarithm is not unwrapped to its exponent, which it equals only on a strip. Rubi's family 7: 187 to 194 of 270, 0 wrong, timeouts 54 to 44; the 1774-problem suite unchanged at 1706, 0 wrong, 0 timeout. Suite 12519 passed; the kernel gate passed on all 19 gated benchmarks. Part of #718. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 41d34f6 commit f62db4c

5 files changed

Lines changed: 204 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,24 @@ prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)).
870870
| `"prime(25)".ToEntity().Evaled` | `prime * 25` — juxtaposition of a variable `prime` | `97`; `prime(n)` is the `n`-th prime, `NaN` off the positive whole numbers |
871871
| `"valuation(12, 2)".ToEntity().Evaled` | `UnrecognizedFunctionParseException` | `2`; `valuation(n, p)` is the `p`-adic valuation, `+oo` at `0` and `NaN` off the primes |
872872

873+
### `atanh(tanh(a + b x))` under an integral is linear in `x`, and was an atom no rule read
874+
875+
An inverse hyperbolic function of a hyperbolic one is spelled by the parser as a logarithm of a
876+
nested quotient: `atanh(tanh(a + b x))` is `1/2 ln((1 + T)/(1 - T))` with `T = (E - 1)/(E + 1)`
877+
and `E = e^(2(a + b x))`, and `acoth(tanh(a + b x))` is `1/2 ln(1 + 2/(T - 1))`. No integration
878+
rule read the nested quotient. It is now cancelled with `E` for an indeterminate, to
879+
`1/2 ln(E)` and `1/2 ln(-E)`; and a logarithm whose derivative in `x` is a constant `k` is
880+
`k x + c` for a constant `c`, which the integrator names and writes back as `ln(E) - k x` in the
881+
answer — the same `b x - atanh(tanh(a + b x))` Rubi's answers carry. The logarithm is not
882+
unwrapped to `a + b x`, which it equals only on a strip
883+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
884+
885+
| Input | Was (2.5.0) | Now |
886+
|---|---|---|
887+
| `"x^m*atanh(tanh(a+b*x))^3".Integrate("x")` | no answer in two minutes | the antiderivative in 0.1 s, in `ln(e^(2(a + b x))) - 2 b x` |
888+
| `"1/(x*atanh(tanh(a+b*x)))".Integrate("x")` | left unevaluated | the antiderivative |
889+
| `"acoth(tanh(a+b*x))/x^2".Integrate("x")` | no answer in two minutes | `b/2 ln(x^2) - (ln(-e^(2(a + b x))) - 2 b x)/(2 x)`, up to the form |
890+
| `"1/(x*ln(e^(2*(a+b*x))))".Integrate("x")` | left unevaluated | the antiderivative |
873891
### A set-builder whose predicate is `NaN` is the empty set, and was `NaN`
874892

875893
`{ x : 1/0 = x }` evaluated to `NaN`, and so did `{ x : x subset 2 }` once `subset` became a

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
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
293293

294+
[Tests/UnitTests/Calculus/InverseHyperbolicOfAHyperbolicIntegralTest.cs]
295+
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
296+
294297
[AngouriMath/Functions/Boolean/ProofRecording.cs]
295298
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
296299

‎Sources/AngouriMath/Functions/Continuous/Integration/IndefiniteIntegralSolver.cs‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9802,6 +9802,106 @@ node is Powf(Powf(var @base, var inner), var outer)
98029802
return folded == expr ? null : Integration.ComputeAsAQuestionOfItsOwn(folded, x, integrateByParts);
98039803
}
98049804

9805+
/// <summary>
9806+
/// A logarithm whose argument is a quotient in <paramref name="x"/> that cancels with
9807+
/// the functions of <paramref name="x"/> in it taken for indeterminates, with the
9808+
/// argument cancelled. That is the spelling the parser gives an inverse hyperbolic
9809+
/// function of a hyperbolic one -- <c>atanh(tanh(u))</c> is
9810+
/// <c>1/2 ln((1 + T)/(1 - T))</c> with <c>T = (E - 1)/(E + 1)</c> and <c>E = e^(2u)</c>,
9811+
/// which as one quotient is <c>2E(E + 1)/(2(E + 1))</c>, and is <c>1/2 ln(E)</c>: an
9812+
/// atom whose derivative is <c>u'</c>, which every rule downstream reads, where the
9813+
/// nested quotient was read by none. <c>acoth(tanh(u))</c> is <c>1/2 ln(-E)</c> the
9814+
/// same way. Kept as the logarithm rather than unwrapped to <c>u</c>, since
9815+
/// <c>ln(e^(2u))</c> is <c>2u</c> only on a strip, and the logarithm is exact
9816+
/// everywhere -- the answer carries it as Rubi's carries <c>atanh(tanh(u))</c>.
9817+
/// Rubi's 7.3.7 and 7.4.1. Asked as a question of its own.
9818+
/// https://github.com/asc-community/AngouriMath/issues/718
9819+
/// </summary>
9820+
internal static Entity? SolveByCancellingInsideALogarithm(Entity expr, Entity.Variable x, bool integrateByParts)
9821+
{
9822+
var cancelled = expr.Replace(node =>
9823+
{
9824+
if (node is not Logf(var @base, var argument) || !argument.ContainsNode(x) || !argument.Nodes.Any(inner => inner is Divf))
9825+
return node;
9826+
var (above, below) = Functions.SingleQuotient.Of(argument);
9827+
if (below == Number.Integer.One)
9828+
return node;
9829+
var (top, bottom) = CancelledWithFunctionsAsIndeterminates(above, below, x, argument);
9830+
if (top == above && bottom == below)
9831+
return node;
9832+
// Simplified rather than normalised: the cancellation leaves the numeric
9833+
// content where it was, and `2E/2` is not the atom `E` until it is folded.
9834+
var written = (bottom == Number.Integer.One ? top : top / bottom).Simplify();
9835+
return written == argument ? node : new Logf(@base, written);
9836+
});
9837+
return cancelled == expr ? null : Integration.ComputeAsAQuestionOfItsOwn(cancelled, x, integrateByParts);
9838+
}
9839+
9840+
/// <summary>
9841+
/// A logarithm of <paramref name="x"/> whose derivative in <paramref name="x"/> is a
9842+
/// constant <c>k</c> is <c>k x + c</c> for a constant <c>c</c>, and is written so, with
9843+
/// <c>c</c> a name standing for <c>ln(...) - k x</c>, which the answer substitutes back.
9844+
/// <c>ln(e^(2(a + b x)))</c> -- which is what <c>atanh(tanh(a + b x))</c> is once its
9845+
/// quotient is cancelled -- has derivative <c>2b</c>, and is <c>2b x + c</c>: with that
9846+
/// name, <c>x^m atanh(tanh(a + b x))^3</c> is a polynomial times a power of a linear and
9847+
/// <c>1/(x atanh(tanh(a + b x)))</c> a rational function, which the rules answer at once
9848+
/// where the logarithm was an atom no rule read past the first power. The name is
9849+
/// exact: <c>c</c> is <c>ln(...) - k x</c> by definition, and a constant in <c>x</c>
9850+
/// because the derivative is. The logarithm is not unwrapped to its argument's
9851+
/// exponent, which it equals only on a strip; Rubi's answers carry
9852+
/// <c>b x - atanh(tanh(a + b x))</c> in the same way. Rubi's 7.3.7 and 7.4.1. Asked as
9853+
/// a question of its own. https://github.com/asc-community/AngouriMath/issues/718
9854+
/// </summary>
9855+
internal static Entity? SolveByNamingALogarithmLinearInTheVariable(Entity expr, Entity.Variable x, bool integrateByParts)
9856+
{
9857+
Logf? linear = null;
9858+
Entity? slope = null;
9859+
foreach (var node in expr.Nodes)
9860+
{
9861+
if (node is not Logf(var logBase, var argument) log || logBase.ContainsNode(x) || !argument.ContainsNode(x))
9862+
continue;
9863+
// The derivative of ln(prod f_i^(n_i)) is the sum of n_i f_i'/f_i, which for a
9864+
// factor e^v is v': read off the factors rather than differentiated and
9865+
// simplified, since the differentiation leaves E 2b / E as it is.
9866+
var (above, below) = Functions.SingleQuotient.Of(argument);
9867+
Entity sum = Number.Integer.Zero;
9868+
var read = true;
9869+
foreach (var (factor, sign) in Mulf.LinearChildren(above).Select(f => (f, 1)).Concat(Mulf.LinearChildren(below).Select(f => (f, -1))))
9870+
{
9871+
if (!factor.ContainsNode(x))
9872+
continue;
9873+
if (factor is not Powf(var @base, var exponent) || @base.ContainsNode(x))
9874+
{
9875+
read = false;
9876+
break;
9877+
}
9878+
var rate = exponent.Differentiate(x);
9879+
if (rate.ContainsNode(x) || rate is Derivativef)
9880+
{
9881+
read = false;
9882+
break;
9883+
}
9884+
sum += sign * rate * (@base == MathS.e ? Number.Integer.One : MathS.Ln(@base));
9885+
}
9886+
if (!read)
9887+
continue;
9888+
var derivative = sum.InnerSimplified;
9889+
if (derivative == Number.Integer.Zero || derivative.ContainsNode(x))
9890+
continue;
9891+
linear = log;
9892+
slope = logBase == MathS.e ? derivative : derivative / MathS.Ln(logBase);
9893+
break;
9894+
}
9895+
if (linear is null || slope is null)
9896+
return null;
9897+
var constant = Variable.CreateUnique(expr, "c");
9898+
var named = expr.Substitute(linear, slope * x + constant);
9899+
if (named == expr)
9900+
return null;
9901+
var answer = Integration.ComputeAsAQuestionOfItsOwn(named, x, integrateByParts);
9902+
return answer?.Substitute(constant, linear - slope * x);
9903+
}
9904+
98059905
/// <summary>
98069906
/// A whole power of a product in which the variable stands beside a constant, written
98079907
/// as the product of the powers: <c>(c x)^2</c> is <c>c^2 x^2</c>, exact for a whole

‎Sources/AngouriMath/Functions/Continuous/Integration/Integration.Definition.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ private static Entity Normalized(Entity expr, Entity.Variable x) =>
462462
// A whole power of a product of a constant and the variable, as the product of
463463
// the powers, which is how the inverse hyperbolic secant and cosecant arrive.
464464
if ((answer = IndefiniteIntegralSolver.SolveByDistributingWholePowersOfProducts(expr, x, integrateByParts)) is { }) return answer;
465+
// A logarithm of a quotient that cancels with the functions in it as
466+
// indeterminates, which is how an inverse hyperbolic function of a hyperbolic
467+
// one arrives: atanh(tanh(u)) is 1/2 ln(e^(2u)) once its quotient is cancelled.
468+
if ((answer = IndefiniteIntegralSolver.SolveByCancellingInsideALogarithm(expr, x, integrateByParts)) is { }) return answer;
469+
// A logarithm whose derivative is a constant k is k x + c, and is named so.
470+
if ((answer = IndefiniteIntegralSolver.SolveByNamingALogarithmLinearInTheVariable(expr, x, integrateByParts)) is { }) return answer;
465471
// A polynomial times a rational function of exponentials, by parts against the
466472
// whole rational function, before anything splits the sum: the general parts rule
467473
// takes each term on its own, and each term's antiderivative keeps a logarithm the
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Calculus
13+
{
14+
/// <summary>
15+
/// <c>atanh(tanh(a + b x))</c> and <c>acoth(tanh(a + b x))</c> under an integral: the
16+
/// parser spells them <c>1/2 ln((1 + T)/(1 - T))</c> with <c>T = (E - 1)/(E + 1)</c> and
17+
/// <c>E = e^(2(a + b x))</c>, a nested quotient no rule read; cancelled with <c>E</c> for
18+
/// an indeterminate it is <c>1/2 ln(E)</c>, and that logarithm's derivative is the constant
19+
/// <c>2b</c>, so it is <c>2b x + c</c> for a constant <c>c</c> the answer writes back as
20+
/// <c>ln(E) - 2b x</c>. Rubi's 7.3.7 and 7.4.1, whose answers carry
21+
/// <c>b x - atanh(tanh(a + b x))</c> the same way.
22+
/// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a>
23+
/// </summary>
24+
[Trait("Area", "Calculus")]
25+
public sealed class InverseHyperbolicOfAHyperbolicIntegralTest
26+
{
27+
private static readonly double[] Points = { 0.3, 0.7, 1.1, 1.9, 2.6 };
28+
29+
/// <summary>
30+
/// The antiderivative differentiated back against the integrand at real points, with
31+
/// <c>a = 1/3</c> and <c>b = 3/2</c>, where <c>atanh(tanh(a + b x))</c> is <c>a + b x</c>
32+
/// and <c>acoth(tanh(a + b x))</c> is <c>a + b x + i pi/2</c>.
33+
/// </summary>
34+
private static void DifferentiatesBack(string integrand)
35+
{
36+
var integral = integrand.ToEntity().Integrate("x");
37+
Assert.DoesNotContain("integral(", integral.Stringize());
38+
var bare = integral.Substitute("C", 0).Substitute("a", "1/3".ToEntity()).Substitute("b", "3/2".ToEntity());
39+
var derivative = bare.Differentiate("x");
40+
var original = integrand.ToEntity().Substitute("a", "1/3".ToEntity()).Substitute("b", "3/2".ToEntity());
41+
var compared = 0;
42+
foreach (var at in Points)
43+
{
44+
var got = derivative.Substitute("x", at).EvalNumerical();
45+
var want = original.Substitute("x", at).EvalNumerical();
46+
if (got.IsNaN || want.IsNaN)
47+
continue;
48+
compared++;
49+
var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart);
50+
var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart));
51+
Assert.True(difference / scale < 1e-9,
52+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}");
53+
}
54+
Assert.True(compared >= 4, $"only {compared} of {Points.Length} points were comparable for {integrand}");
55+
}
56+
57+
[Theory]
58+
[InlineData("atanh(tanh(a+b*x))^2/sqrt(x)")]
59+
[InlineData("x^m*atanh(tanh(a+b*x))^3")]
60+
[InlineData("1/(x*atanh(tanh(a+b*x)))")]
61+
[InlineData("acoth(tanh(a+b*x))/x^2")]
62+
[InlineData("acoth(tanh(a+b*x))^3/x")]
63+
[InlineData("1/(x*acoth(tanh(a+b*x))^2)")]
64+
public void AnInverseHyperbolicOfAHyperbolicIsLinearInTheVariable(string integrand)
65+
=> DifferentiatesBack(integrand.Replace("x^m", "x^2"));
66+
67+
/// <summary>
68+
/// The two steps on their own: the cancelled logarithm, and a logarithm whose
69+
/// derivative is a constant named as a linear.
70+
/// </summary>
71+
[Theory]
72+
[InlineData("ln((1 + (e^(2*x) - 1)/(e^(2*x) + 1))/(1 - (e^(2*x) - 1)/(e^(2*x) + 1)))/(2*x)")]
73+
[InlineData("1/(x*ln(e^(2*(a+b*x))))")]
74+
[InlineData("x/ln(e^(a+b*x))")]
75+
public void ALogarithmOfACancellingQuotientAndALogarithmLinearInTheVariable(string integrand) => DifferentiatesBack(integrand);
76+
}
77+
}

0 commit comments

Comments
 (0)