|
| 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 System.Linq; |
| 10 | +using AngouriMath.Extensions; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Calculus |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// The integrals that answer in the exponential and logarithmic integrals: an exponential of a |
| 17 | + /// linear over a power of a linear, the Gaussian's odd negative moments, and a power of x over |
| 18 | + /// a power of a logarithm. |
| 19 | + /// https://github.com/asc-community/AngouriMath/issues/1501 |
| 20 | + /// </summary> |
| 21 | + [Trait("Area", "Calculus")] |
| 22 | + public sealed class ExponentialIntegralIntegrationTest |
| 23 | + { |
| 24 | + private static void DifferentiatesBack(string integrand, double[] points, params (string Name, string Value)[] pins) |
| 25 | + { |
| 26 | + var integral = integrand.ToEntity().Integrate("x"); |
| 27 | + Assert.DoesNotContain("integral(", integral.Stringize()); |
| 28 | + Entity Pinned(Entity e) => pins.Aggregate(e, (current, pin) => current.Substitute(pin.Name, pin.Value.ToEntity())); |
| 29 | + var derivative = Pinned(integral.Substitute("C", 0)).Differentiate("x"); |
| 30 | + var original = Pinned(integrand.ToEntity()); |
| 31 | + foreach (var at in points) |
| 32 | + { |
| 33 | + var got = derivative.Substitute("x", at).EvalNumerical(); |
| 34 | + var want = original.Substitute("x", at).EvalNumerical(); |
| 35 | + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); |
| 36 | + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart)); |
| 37 | + Assert.True(difference / scale < 1e-9, $"d/dx of {integral} is {got} at x = {at}, where {integrand} is {want}"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary>Either side of 0 and of every pole below.</summary> |
| 42 | + private static readonly double[] AroundZero = { -1.7, 0.35, 0.9, 1.45 }; |
| 43 | + |
| 44 | + /// <summary>Past 1, where every logarithm below is real and positive.</summary> |
| 45 | + private static readonly double[] PastOne = { 1.3, 1.9, 2.6, 3.4 }; |
| 46 | + |
| 47 | + private static readonly (string, string)[] Pins = |
| 48 | + { ("a", "1/3"), ("b", "2/3"), ("c", "5/4"), ("d", "1/2"), ("k", "2"), ("m", "1/2"), ("n", "3"), ("F", "2") }; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Under <c>u = c + d x</c>, each term is <c>u^m e^(k u)</c>, and <c>m = -1</c> is <c>Ei(k u)</c>. |
| 52 | + /// Rubi's 2.3, <c>F^(c (a + b x))/(d + e x)^n</c>. |
| 53 | + /// </summary> |
| 54 | + [Theory] |
| 55 | + [InlineData("e^x/x")] |
| 56 | + [InlineData("e^(2*x + 1)/(3*x + 4)")] |
| 57 | + [InlineData("e^x/x^2")] |
| 58 | + [InlineData("x*e^x/(x + 1)")] |
| 59 | + [InlineData("2^x/x^3")] |
| 60 | + [InlineData("(1 + x)^2*e^(-x)/x^2")] |
| 61 | + [InlineData("F^(c*(a + b*x))/(d + k*x)^2")] |
| 62 | + public void AnExponentialOfALinearOverAPowerOfALinear(string integrand) |
| 63 | + => DifferentiatesBack(integrand, AroundZero, Pins); |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// The Gaussian's odd negative moments end at <c>int e^(A x^2)/x = Ei(A x^2)/2</c>, and |
| 67 | + /// <c>x e^(-1/x^2)</c> is one of them under <c>u = 1/x</c>. |
| 68 | + /// </summary> |
| 69 | + [Theory] |
| 70 | + [InlineData("e^(x^2)/x")] |
| 71 | + [InlineData("e^(-x^2)/x^3")] |
| 72 | + [InlineData("x*e^(-1/x^2)")] |
| 73 | + [InlineData("x^3*e^(-1/x^2)")] |
| 74 | + public void AnOddNegativeMomentOfTheGaussian(string integrand) |
| 75 | + => DifferentiatesBack(integrand, AroundZero, Pins); |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Under <c>t = A + B ln(c x^r)</c>, <c>x^p (A + B L)^(-k)</c> is a constant times |
| 79 | + /// <c>e^((p + 1) t/(B r)) t^(-k)</c>, and a power of x inside, <c>d + e x^m</c>, is the same |
| 80 | + /// question under <c>u = x^m</c>. Rubi's 3.1.2, 3.3 and 3.4. |
| 81 | + /// </summary> |
| 82 | + [Theory] |
| 83 | + [InlineData("1/ln(x)")] |
| 84 | + [InlineData("x/ln(x)")] |
| 85 | + [InlineData("x^2/ln(x)^2")] |
| 86 | + [InlineData("x^m/(a + b*ln(c*x^n))")] |
| 87 | + [InlineData("1/(a + b*ln(c*x^n))^2")] |
| 88 | + [InlineData("(d + k*x)^2/ln(c*(d + k*x)^n)")] |
| 89 | + [InlineData("(1 + x)/ln(x)")] |
| 90 | + [InlineData("x^2/ln(c*(d + k*x^3)^n)")] |
| 91 | + [InlineData("x^8/ln(c*(d + k*x^3)^n)^2")] |
| 92 | + public void APowerOverAPowerOfALogarithm(string integrand) |
| 93 | + => DifferentiatesBack(integrand, PastOne, Pins); |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// The non-elementary neighbours the exponential ansatz declines, which <c>Ei</c> answers: |
| 97 | + /// <c>e^(e^x)</c> under <c>u = e^x</c>, and <c>e^x ln(x)</c> by parts. |
| 98 | + /// </summary> |
| 99 | + [Theory] |
| 100 | + [InlineData("e^(e^x)")] |
| 101 | + [InlineData("e^x/(1 + x)")] |
| 102 | + public void AnExponentialIntegralTheAnsatzDeclines(string integrand) |
| 103 | + => DifferentiatesBack(integrand, AroundZero, Pins); |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void TheExponentialTimesTheLogarithm() |
| 107 | + => DifferentiatesBack("e^x*ln(x)", PastOne, Pins); |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Where the exponential integrals cancel, the antiderivative is elementary, and is left to |
| 111 | + /// the rules that write it shortest. |
| 112 | + /// </summary> |
| 113 | + [Fact] |
| 114 | + public void WhereTheExponentialIntegralsCancelTheAnswerIsElementary() |
| 115 | + => Assert.Equal("e ^ x / (1 + x) + C", "e^x*x/(1 + x)^2".ToEntity().Integrate("x").Stringize()); |
| 116 | + |
| 117 | + /// <summary><c>int 1/ln(x) dx</c> is the logarithmic integral itself, since <c>Ei(ln y) = li(y)</c>.</summary> |
| 118 | + [Fact] |
| 119 | + public void TheReciprocalOfTheLogarithmIsTheLogarithmicIntegral() |
| 120 | + { |
| 121 | + Assert.Equal(MathS.Li("x") + "C", "1/ln(x)".ToEntity().Integrate("x")); |
| 122 | + Assert.Equal(MathS.Li("y"), MathS.Ei(MathS.Ln("y")).InnerSimplified); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments