Skip to content

Commit 34cdde4

Browse files
Ask a folded exponential of a logarithm with its constant exponential named (#2009)
SolveByFoldingAnExponentialOfALogarithm writes e^(2a + 4 ln x) as (e^a x^2)^2, leaving e^a a coefficient, and dividing by e^(2a) then writes e^(2a(-1)): the two never cancelled. tanh(a + 2 ln x)^2/x^3 came back in six arms on the signs of 4 e^(2a) and 4 e^(-2a), 7,796 characters, and its derivative carried a condition on (e^(-28a))^2 that the evaluator takes for zero. Where the folded integrand is a rational function of x and every x-free exponential is a whole power of one, that exponential is also asked as a symbol k and substituted back, conditions included, and the shorter answer is kept: in k the factorization can split a biquadratic into arms on k's sign, as for coth(a + 2 ln x)/x^2, where the exponential's answer stays. All 728 rows of 6.3.x and 6.4.x and the 54 of 6.1.5, 6.2.5, 6.5.3 and 6.6.3 that reach the fold: 2 GAP -> OK, 20 answers shorter (7,796 -> 181 and 8,047 -> 202 characters among them), none longer, none lost. Chain pocket of 1,576 rows: verdicts identical; samples unchanged; 6.3.2:218 and 6.4.2:221 answered by Integrate itself where master needed Simplify, 5.9 s -> 144 ms and 16.9 s -> 99 ms. No row's allocation grew past 1.3x. Part of #233. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 375fbb4 commit 34cdde4

3 files changed

Lines changed: 171 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,24 @@ tanh keep the answers the exponential substitution gives them ([#233](https://gi
311311
| `"sin(tanh(a+b*x))".ToEntity().Integrate("x")` | `integral(...)` | `Si` and `Ci` of `1 - tanh(a + b x)` and `1 + tanh(a + b x)` |
312312
| `"tanh(x)/sqrt(a+b*tanh(x)^2+c*tanh(x)^4)".ToEntity().Integrate("x")` | `integral(...)` | a logarithm of the root, piecewise on the sign of `a + b + c` |
313313

314+
### A hyperbolic function of `a + 2 ln(x)` is integrated with `e^a` as a symbol too
315+
316+
**Answers where there were none.** `tanh(a + 2 ln(x))^2/x^3` was declined, and so were nineteen more of Rubi's 6.3.2,
317+
6.4.2 and 6.5.3, among them `coth(a + 2 ln(x))^2/x^3` and the odd powers of x beside `tanh(a + 2 ln(x))^k`. The
318+
unreleased master answered them through the fold of `e^(2 a + 4 ln(x))` to `e^(2 a) x^4`, in up to 8,047 characters: the
319+
arithmetic of `e^(2 a)` against the `e^(-2 a)` a division writes never cancelled, and `tanh(a + 2 ln(x))^2/x^3` came
320+
back in six arms on the signs of `4 e^(2 a)` and `4 e^(-2 a)`. Where the folded integrand is a rational function of x,
321+
`e^a` is now also asked as a symbol and written back, and the shorter answer is kept; where the symbol splits a
322+
biquadratic into arms on its sign, as for `coth(a + 2 ln(x))/x^2`, the exponential's answer stays
323+
([#233](https://github.com/asc-community/AngouriMath/issues/233)).
324+
325+
| Input | Was (2.5.0) | Now |
326+
|---|---|---|
327+
| `"tanh(a + 2*ln(x))^2/x^3".ToEntity().Integrate("x")` | `integral(...)` | 181 characters |
328+
| `"coth(a + 2*ln(x))^2/x^3".ToEntity().Integrate("x")` | `integral(...)` | 202 characters |
329+
| `"x^3*tanh(a + 2*ln(x))^2".ToEntity().Integrate("x")` | `integral(...)` | 134 characters |
330+
| `"tanh(a + 2*ln(x))/x^3".ToEntity().Integrate("x")` | `integral(...)` | 178 characters |
331+
314332
### A constant or an exponential evaluated at a higher precision than before has all of its digits
315333

316334
**More digits where there were a hundred.** `pi`, `e` and an exponential, evaluated once at one precision, went on
@@ -813,8 +831,7 @@ split. It is written `b (s + r)(s^2 - r s + r^2)` now, with `r = (a/b)^(1/3)`, w
813831
**Answers where there were none.** `1/(a x + b x/log(c x^n))` was left unevaluated. With x taken out of the sum below the
814832
bar it is `1/(x (a + b/log(c x^n)))`, a function of the logarithm beside its differential, which is answered. The same
815833
holds for the higher powers of the logarithm and for Rubi's 3.5:37
816-
([#718](https://github.com/asc-community/AngouriMath/issues/718)). `tanh(a + 2 log(x))^2/x^3`, which 2.5.0 left
817-
unevaluated, is answered by this route as well.
834+
([#718](https://github.com/asc-community/AngouriMath/issues/718)).
818835

819836
| Input | Was (2.5.0) | Now |
820837
|---|---|---|
@@ -823,7 +840,6 @@ unevaluated, is answered by this route as well.
823840
| `"1/(a*x+b*x/ln(c*x^n)^3)".ToEntity().Integrate("x")` | `integral(...)` | 514 characters |
824841
| `"1/(a*x+b*x/ln(c*x^n)^4)".ToEntity().Integrate("x")` | `integral(...)` | 337 characters |
825842
| `"(a*x+2*b*n*ln(c*x^n))/(a*x^2+b*x*ln(c*x^n)^2)".ToEntity().Integrate("x")` | `integral(...)` | 37 characters |
826-
| `"tanh(a+2*ln(x))^2/x^3".ToEntity().Integrate("x")` | `integral(...)` | 7,796 characters |
827843

828844
### A quotient over a sum of two powers of even quadratics is split over the roots of its binomial
829845

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26360,6 +26360,24 @@ when IsAnExponential(@base, inner) && !outer.ContainsNode(x) && outer.Evaled is
2636026360
/// is a rule here, asked as a question of its own so the closed rules meet it at the top.
2636126361
/// https://github.com/asc-community/AngouriMath/issues/718
2636226362
/// </summary>
26363+
/// <remarks>
26364+
/// The exponential the fold leaves free of the variable, <c>e^a</c> from
26365+
/// <c>a + b ln(c x^n)</c>, is asked as a symbol <c>k</c> and written back into the answer,
26366+
/// conditions and all: <c>d/dx F(x, k) = R(x, k)</c> for every <c>k</c>, so for
26367+
/// <c>k = e^a</c> too. As an exponential it was arithmetic the rules never finished --
26368+
/// the division by <c>e^(2 a)</c> writes <c>e^(-2 a)</c>, and the two do not cancel --
26369+
/// and <c>tanh(a + 2 ln(x))^2/x^3</c> (Rubi's 6.3.2) came back a piecewise of 7,796
26370+
/// characters, six arms on the signs of <c>4 e^(2 a)</c> and <c>4 e^(-2 a)</c>, with an
26371+
/// <c>e^(-56 a)</c> in its derivative's condition that a decimal evaluation takes for zero
26372+
/// at <c>a = 1.07</c>; in <c>k</c> it is 181. The two spellings are asked and the shorter
26373+
/// answer kept, since in <c>k</c> the factorisation reads the coefficient and splits
26374+
/// <c>k^2 x^4 - 1</c> into arms on the sign of <c>k</c>: <c>coth(a + 2 ln(x))/x^2</c> is
26375+
/// 3,577 characters in <c>k</c> and 413 as the exponential. Only where the folded
26376+
/// integrand is a rational function of x -- beside a symbolic power of x the name cost
26377+
/// four times the allocation for an answer as long -- and every such exponential is a
26378+
/// whole power of one of them, so that the relations between them survive the naming.
26379+
/// https://github.com/asc-community/AngouriMath/issues/233
26380+
/// </remarks>
2636326381
internal static Entity? SolveByFoldingAnExponentialOfALogarithm(Entity expr, Entity.Variable x, bool integrateByParts)
2636426382
{
2636526383
var folded = expr.Replace(node =>
@@ -26392,7 +26410,53 @@ when IsAnExponential(@base, inner) && !outer.ContainsNode(x) && outer.Evaled is
2639226410
? Number.Integer.One / (power.Equals(EInteger.FromInt32(-1)) ? @base : MathS.Pow(@base, Number.Integer.Create(-power)))
2639326411
: inside), exponent)
2639426412
: node);
26395-
return Integration.ComputeAsAQuestionOfItsOwn(folded, x, integrateByParts);
26413+
var answer = Integration.ComputeAsAQuestionOfItsOwn(folded, x, integrateByParts);
26414+
// Both spellings, and the shorter answer: in a name the factorisation reads the
26415+
// coefficient and can split a biquadratic over it into arms on its sign, which the
26416+
// exponential, an atom to it, is answered without.
26417+
if (!IsARationalFunction(folded, x) || AConstantExponentialNamed(folded, x) is not { } named
26418+
|| Integration.ComputeAsAQuestionOfItsOwn(named.Question, x, integrateByParts) is not { } inTheName
26419+
|| inTheName.Nodes.Any(node => node is Integralf))
26420+
return answer;
26421+
var writtenBack = inTheName.Substitute(named.Name, named.Value);
26422+
return answer is null || writtenBack.Complexity < answer.Complexity ? writtenBack : answer;
26423+
}
26424+
26425+
/// <summary>
26426+
/// <paramref name="expr"/> with every exponential free of <paramref name="x"/> that has a
26427+
/// symbol in its exponent written as a whole power of one name <c>k</c>, and the
26428+
/// <c>e^E</c> that <c>k</c> stands for; <see langword="null"/> where there is none, or
26429+
/// where no one of them has all the others as whole powers.
26430+
/// </summary>
26431+
private static (Entity Question, Variable Name, Entity Value)? AConstantExponentialNamed(Entity expr, Entity.Variable x)
26432+
{
26433+
var exponents = expr.Nodes
26434+
.Where(node => node is Powf(var @base, var exponent) && @base == MathS.e && !exponent.ContainsNode(x) && exponent.Vars.Any())
26435+
.Select(node => ((Powf)node).Exponent)
26436+
.Distinct()
26437+
.ToList();
26438+
foreach (var unit in exponents)
26439+
{
26440+
var powers = new Dictionary<Entity, Number.Integer>();
26441+
foreach (var exponent in exponents)
26442+
{
26443+
if (exponent == unit)
26444+
powers[exponent] = Number.Integer.One;
26445+
else if (Functions.PartialFractions.Bare((exponent / unit).Simplify()).Evaled is Number.Integer k && !k.IsZero && k.EInteger.CanFitInInt32())
26446+
powers[exponent] = k;
26447+
else
26448+
break;
26449+
}
26450+
if (powers.Count != exponents.Count)
26451+
continue;
26452+
var name = Variable.CreateUnique(expr, "k_fold");
26453+
var question = expr.Replace(node =>
26454+
node is Powf(var @base, var exponent) && @base == MathS.e && powers.TryGetValue(exponent, out var power)
26455+
? power == Number.Integer.One ? name : MathS.Pow(name, power)
26456+
: node);
26457+
return (question, name, MathS.Pow(MathS.e, unit));
26458+
}
26459+
return null;
2639626460
}
2639726461

2639826462
/// <summary>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
/// A hyperbolic function of <c>a + b ln(x)</c> folds to a rational function of <c>x</c> with
16+
/// <c>e^a</c> among its coefficients, and that exponential is also asked as a symbol and
17+
/// written back, the shorter answer kept. As an exponential its arithmetic did not cancel:
18+
/// <c>tanh(a + 2 ln(x))^2/x^3</c> came back a piecewise of 7,796 characters on the signs of
19+
/// <c>4 e^(2 a)</c> and <c>4 e^(-2 a)</c>. Rubi's 6.3.2 and 6.4.2.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/233">#233</a>
21+
/// </summary>
22+
[Trait("Area", "Calculus")]
23+
public sealed class AConstantExponentialBesideAFoldedLogarithmIntegralTest
24+
{
25+
private static readonly double[] Points = { 0.3, 0.6, 0.9, 1.4, 2.1 };
26+
27+
private static void DifferentiatesBack(string integrand, int longest)
28+
{
29+
var integral = integrand.ToEntity().Integrate("x");
30+
var text = integral.Stringize();
31+
Assert.DoesNotContain("integral(", text);
32+
Assert.True(text.Length <= longest, $"{text.Length} characters of answer for {integrand}");
33+
foreach (var a in new[] { -0.7, 0.6, 1.3 })
34+
{
35+
Entity Pinned(Entity e) => e.Substitute("a", a);
36+
var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x");
37+
var original = Pinned(integrand.ToEntity());
38+
var compared = 0;
39+
foreach (var at in Points)
40+
{
41+
var want = original.Substitute("x", at).EvalNumerical();
42+
var got = derivative.Substitute("x", at).EvalNumerical();
43+
if (want.IsNaN)
44+
continue;
45+
compared++;
46+
Assert.True(Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart)
47+
< 1e-9 * Math.Max(1, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)),
48+
$"d/dx of the antiderivative of {integrand} is {got} at x = {at}, a = {a}, where the integrand is {want}");
49+
}
50+
Assert.True(compared >= 5, $"only {compared} points could be compared for {integrand} at a = {a}");
51+
}
52+
}
53+
54+
/// <summary>
55+
/// An odd power of <c>x</c>, a function of <c>x^2</c> under <c>t = x^2</c>: in a symbol
56+
/// these are a few hundred characters at most, and as exponentials up to thousands.
57+
/// </summary>
58+
[Theory]
59+
[InlineData("tanh(a + 2*ln(x))^2/x^3")]
60+
[InlineData("coth(a + 2*ln(x))^2/x^3")]
61+
[InlineData("x^3*tanh(a + 2*ln(x))^2")]
62+
[InlineData("x^3*coth(a + 2*ln(x))^2")]
63+
[InlineData("x*tanh(a + 2*ln(x))^2")]
64+
[InlineData("tanh(a + 2*ln(x))^2/x")]
65+
[InlineData("coth(a + 2*ln(x))^2/x")]
66+
[InlineData("tanh(a + 2*ln(x))/x^3")]
67+
[InlineData("coth(a + 2*ln(x))/x^3")]
68+
[InlineData("x^3*tanh(a + 2*ln(x))")]
69+
[InlineData("x^3*coth(a + 2*ln(x))")]
70+
[InlineData("x*tanh(a + 2*ln(x))")]
71+
[InlineData("x*coth(a + 2*ln(x))")]
72+
public void AnOddPowerOfX(string integrand) => DifferentiatesBack(integrand, 400);
73+
74+
/// <summary>
75+
/// An even power of <c>x</c>, where the factorisation reads a symbol and splits the
76+
/// biquadratic over it into arms on its sign -- <c>coth(a + 2 ln(x))/x^2</c> is 3,577
77+
/// characters in the symbol and 413 as the exponential, which is kept.
78+
/// </summary>
79+
[Theory]
80+
[InlineData("coth(a + 2*ln(x))/x^2")]
81+
[InlineData("coth(a + 2*ln(x))^2/x^2")]
82+
[InlineData("coth(a + 2*ln(x))^2")]
83+
[InlineData("x^2*coth(a + 2*ln(x))^2")]
84+
[InlineData("tanh(a + 2*ln(x))^2")]
85+
public void AnEvenPowerOfX(string integrand) => DifferentiatesBack(integrand, 1300);
86+
}
87+
}

0 commit comments

Comments
 (0)