@@ -5699,14 +5699,19 @@ node is Powf(var q, Number.Rational r) && r is not Number.Integer && q != first
56995699 /// <c>x</c> left the logarithm's derivative as a quotient of radicals.
57005700 /// The way back writes <c>e^u</c> as <c>L + sqrt(L^2 + 1)</c> and <c>e^(-u)</c> as
57015701 /// <c>sqrt(L^2 + 1) - L</c>, on the principal branch.
5702+ /// <c>arsech(L)</c> and <c>arcsch(L)</c> are the <c>arcosh</c> and <c>arsinh</c> of
5703+ /// <c>1/L</c>, and so <c>u</c> under <c>L = sech(u)</c> and <c>L = csch(u)</c>, with
5704+ /// <c>e^u</c> as <c>1/L + sqrt(1/L^2 - 1)</c> and <c>1/L + sqrt(1/L^2 + 1)</c>: Rubi's
5705+ /// <c>1/(x^2 (a + b arcsch(c x)))</c> is <c>-c cosh(u)/(a + b u)</c>, which the
5706+ /// hyperbolic sine and cosine integrals answer.
57025707 /// https://github.com/asc-community/AngouriMath/issues/718
57035708 /// </summary>
57045709 internal static Entity? SolveByInverseHyperbolicSubstitution(Entity expr, Entity.Variable x, bool integrateByParts)
57055710 {
57065711 // The one inverse hyperbolic function in the integrand, read off its logarithm.
57075712 Entity? inverse = null;
57085713 Entity? argument = null;
5709- var kind = 0; // 1 sinh, 2 cosh, 3 tanh
5714+ var kind = 0; // 1 sinh, 2 cosh, 3 tanh, 4 sech, 5 csch
57105715 foreach (var node in expr.Nodes)
57115716 {
57125717 if (node is not Logf(var @base, var antilogarithm) || @base != MathS.e || !antilogarithm.ContainsNode(x))
@@ -5723,14 +5728,25 @@ node is Powf(var q, Number.Rational r) && r is not Number.Integer && q != first
57235728 if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var offset) || slope is null || offset is null
57245729 || TreeAnalyzer.IsZero(slope) || slope.Evaled is Number.Complex { IsZero: true })
57255730 return null;
5731+ // The secant's and the cosecant's are taken only where they land on what the rules for
5732+ // exponentials close: `x^m` beside a function of the inverse alone, `m <= -2`, and the
5733+ // argument `c x`. Under `x = sech(u)/c` that is `cosh(u)^(-m - 2) sinh(u)` times the
5734+ // function of u, a polynomial in `e^u` and `e^(-u)` -- `1/(x^2 (a + b arcsch(c x)))` is
5735+ // `-c cosh(u)/(a + b u)` -- where a power of x above the bar is a power of `cosh(u)` below
5736+ // it: `x^3 arsech(a x)^2` is `u^2 sinh(u)/cosh(u)^5` over `-a^4`. By parts in x answers
5737+ // such integrands in a fraction of a second, where the searches in u held their test
5738+ // past five minutes.
5739+ if (kind >= 4 && (!TreeAnalyzer.IsZero(offset) || APowerOfXBesideAFunctionOfTheInverse(expr, inverse, x) is not { } degree || degree > -2))
5740+ return null;
57265741 // The tangent's logarithm carries a half in front: `ln((1 + L)/(1 - L))/2` is u, and
57275742 // the logarithm alone is 2u.
57285743 var u = Variable.CreateUnique(expr, "u_inv");
57295744 var expU = MathS.Pow(MathS.e, u);
57305745 var expMinusU = MathS.Pow(MathS.e, -u);
57315746 var sinh = (expU - expMinusU) / 2;
57325747 var cosh = (expU + expMinusU) / 2;
5733- Entity argumentInU, dArgument, radicandBase, root;
5748+ Entity argumentInU, dArgument;
5749+ Entity? radicandBase, root;
57345750 switch (kind)
57355751 {
57365752 case 1:
@@ -5739,9 +5755,18 @@ node is Powf(var q, Number.Rational r) && r is not Number.Integer && q != first
57395755 case 2:
57405756 (argumentInU, dArgument, radicandBase, root) = (cosh, sinh, MathS.Sqr(argument) - 1, sinh);
57415757 break;
5742- default :
5758+ case 3 :
57435759 (argumentInU, dArgument, radicandBase, root) = (sinh / cosh, 1 / MathS.Sqr(cosh), 1 - MathS.Sqr(argument), 1 / cosh);
57445760 break;
5761+ // The principal arsech is not negative, so the root of `1 - sech(u)^2` is
5762+ // `tanh(u)`; the arcsch takes either sign, and the root of `1 + csch(u)^2` is
5763+ // `|coth(u)|`, which is no radical this route removes.
5764+ case 4:
5765+ (argumentInU, dArgument, radicandBase, root) = (1 / cosh, -sinh / MathS.Sqr(cosh), 1 - MathS.Sqr(argument), sinh / cosh);
5766+ break;
5767+ default:
5768+ (argumentInU, dArgument, radicandBase, root) = (1 / sinh, -cosh / MathS.Sqr(sinh), (Entity?)null, (Entity?)null);
5769+ break;
57455770 }
57465771 var logarithmInU = kind == 3 ? 2 * u : u;
57475772
@@ -5750,6 +5775,8 @@ node is Powf(var q, Number.Rational r) && r is not Number.Integer && q != first
57505775 .Substitute(inverse, logarithmInU)
57515776 .Replace(node =>
57525777 {
5778+ if (radicandBase is null || root is null)
5779+ return node;
57535780 if (node is Powf(var wholeBase, Number.Integer whole) && whole.EInteger.CompareTo(EInteger.FromInt32(2)) >= 0
57545781 && whole.EInteger.CompareTo(EInteger.FromInt32(64)) <= 0 && wholeBase.ContainsNode(x)
57555782 && TreeAnalyzer.TryGetPolyQuadratic(wholeBase, x, out _, out _, out _)
@@ -5820,6 +5847,12 @@ node is Powf(var @base, Number.Rational power) && power is not Number.Integer &&
58205847 case 2:
58215848 (plus, minus) = (argument + MathS.Sqrt(MathS.Sqr(argument) - 1), argument - MathS.Sqrt(MathS.Sqr(argument) - 1));
58225849 break;
5850+ case 4:
5851+ (plus, minus) = (1 / argument + MathS.Sqrt(1 / MathS.Sqr(argument) - 1), 1 / argument - MathS.Sqrt(1 / MathS.Sqr(argument) - 1));
5852+ break;
5853+ case 5:
5854+ (plus, minus) = (1 / argument + MathS.Sqrt(1 / MathS.Sqr(argument) + 1), MathS.Sqrt(1 / MathS.Sqr(argument) + 1) - 1 / argument);
5855+ break;
58235856 default:
58245857 // e^(-u) as the reciprocal, not as the root of the reciprocal: off the real
58255858 // domain the principal roots of a quotient and of its reciprocal are not
@@ -5849,6 +5882,8 @@ node is Powf(var @base, Number.Rational power) && power is not Number.Integer &&
58495882 {
58505883 2 => new[] { "1.43", "3.17", "2.2", "5.1" },
58515884 3 => new[] { "0.29", "-0.61", "0.13", "-0.4" },
5885+ // Inside (0, 1] for a slope up to about four, where the principal arsech is real.
5886+ 4 => new[] { "0.07", "0.11", "0.19", "0.23" },
58525887 _ => null,
58535888 };
58545889 var derivative = back.Differentiate(x);
@@ -5860,10 +5895,51 @@ node is Powf(var @base, Number.Rational power) && power is not Number.Integer &&
58605895 }
58615896
58625897 /// <summary>
5863- /// The argument and the kind (1 <c>arsinh</c>, 2 <c>arcosh</c>, 3 <c>artanh</c>) of the
5864- /// inverse hyperbolic function whose logarithm has <paramref name="antilogarithm"/>
5865- /// as its argument -- <c>L + sqrt(L^2 + 1)</c>, <c>L + sqrt(L^2 - 1)</c> or
5866- /// <c>(1 + L)/(1 - L)</c> for a linear <c>L</c> -- else null.
5898+ /// The <c>m</c> for which <paramref name="expr"/> is <c>x^m</c> times a function of
5899+ /// <paramref name="inverse"/> alone, else null: with the inverse written as a symbol, such an
5900+ /// integrand at <c>2x</c> is <c>2^m</c> times itself wherever it is evaluated, and two points
5901+ /// with every other symbol pinned say whether it is.
5902+ /// </summary>
5903+ private static int? APowerOfXBesideAFunctionOfTheInverse(Entity expr, Entity inverse, Entity.Variable x)
5904+ {
5905+ var w = Variable.CreateUnique(expr, "w_inv");
5906+ var inW = expr.Substitute(inverse, w);
5907+ if (!inW.ContainsNode(x))
5908+ return 0;
5909+ using var _ = MathS.Settings.DowncastingEnabled.Set(false);
5910+ var pins = new[] { "0.61", "1.37", "2.23", "0.83", "1.91" };
5911+ var index = 0;
5912+ foreach (var symbol in inW.Vars.Where(symbol => symbol != x).ToList())
5913+ inW = inW.Substitute(symbol, Number.Real.Create(EDecimal.FromString(pins[index++ % pins.Length])));
5914+ double? RatioAt(string at)
5915+ {
5916+ var point = EDecimal.FromString(at);
5917+ if (inW.Substitute(x, Number.Real.Create(point.Multiply(2))).Evaled is not Number.Complex doubled
5918+ || inW.Substitute(x, Number.Real.Create(point)).Evaled is not Number.Complex single
5919+ || single.IsZero || doubled.IsNaN || single.IsNaN)
5920+ return null;
5921+ // Read by its parts, since with the downcasting off a quotient on the real line is
5922+ // still a Complex.
5923+ var ratio = doubled / single;
5924+ var real = ratio.RealPart.EDecimal;
5925+ return real.IsFinite && real.Sign > 0 && ratio.ImaginaryPart.EDecimal.Abs().CompareTo(real.Multiply(EDecimal.FromString("1e-30"))) <= 0
5926+ ? real.ToDouble() : null;
5927+ }
5928+ if (RatioAt("0.37") is not { } first || RatioAt("0.53") is not { } second)
5929+ return null;
5930+ var degree = System.Math.Log(first, 2);
5931+ var rounded = System.Math.Round(degree);
5932+ if (System.Math.Abs(degree - rounded) > 1e-9 || System.Math.Abs(System.Math.Log(second, 2) - rounded) > 1e-9)
5933+ return null;
5934+ return (int)rounded;
5935+ }
5936+
5937+ /// <summary>
5938+ /// The argument and the kind (1 <c>arsinh</c>, 2 <c>arcosh</c>, 3 <c>artanh</c>,
5939+ /// 4 <c>arsech</c>, 5 <c>arcsch</c>) of the inverse hyperbolic function whose logarithm
5940+ /// has <paramref name="antilogarithm"/> as its argument -- <c>L + sqrt(L^2 + 1)</c>,
5941+ /// <c>L + sqrt(L^2 - 1)</c>, <c>(1 + L)/(1 - L)</c>, <c>1/L + sqrt(1/L^2 - 1)</c> or
5942+ /// <c>1/L + sqrt(1/L^2 + 1)</c> for a linear <c>L</c> -- else null.
58675943 /// </summary>
58685944 private static (Entity Argument, int Kind)? ReadAnInverseHyperbolic(Entity antilogarithm, Entity.Variable x)
58695945 {
@@ -5880,6 +5956,28 @@ private static (Entity Argument, int Kind)? ReadAnInverseHyperbolic(Entity antil
58805956 if (IsTheSameQuadraticOnceSimplified(radicand, MathS.Sqr(linear) - 1, x))
58815957 return (linear, 2);
58825958 }
5959+ // The secant's and the cosecant's are the cosine's and the sine's of the
5960+ // reciprocal: `arsech(L)` is `ln(1/L + sqrt(1/L^2 - 1))` and `arcsch(L)` is
5961+ // `ln(1/L + sqrt(1/L^2 + 1))`.
5962+ foreach (var (reciprocal, radical) in new[] { (left, right), (right, left) })
5963+ {
5964+ if (!TryReadAHalfPower(radical, out var radicand, out var numeratorOfHalf) || numeratorOfHalf != 1 || !reciprocal.ContainsNode(x))
5965+ continue;
5966+ // Read off the bar where it is written as one, since the simplification leaves
5967+ // `1/(1/(c x))` as it is, and the answer would carry it.
5968+ var linear = reciprocal switch
5969+ {
5970+ Divf(var one, var denominator) when one == Number.Integer.One => denominator,
5971+ Powf(var @base, Number.Integer minusOne) when minusOne == Number.Integer.MinusOne => @base,
5972+ _ => (1 / reciprocal).InnerSimplified,
5973+ };
5974+ if (!TreeAnalyzer.TryGetPolyLinear(linear, x, out var slope, out _) || slope is null || TreeAnalyzer.IsZero(slope))
5975+ continue;
5976+ if (VanishesOnceSimplified(radicand - (1 / MathS.Sqr(linear) - 1)))
5977+ return (linear, 4);
5978+ if (VanishesOnceSimplified(radicand - (1 / MathS.Sqr(linear) + 1)))
5979+ return (linear, 5);
5980+ }
58835981 return null;
58845982 }
58855983 if (antilogarithm is Divf(var above, var below)
0 commit comments