|
| 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 limit at -oo is read at +oo with -x written for x, and the logarithms a limit gathers are |
| 16 | + /// gathered on the signs their operands hold on the approach. Those signs have to be read on |
| 17 | + /// the approach the rewritten expression is on: read against -oo, -x and 1 - x are positive, |
| 18 | + /// <c>ln(-x) + ln(1 - x)</c> was gathered into one logarithm, and |
| 19 | + /// <c>lim x->-oo ln(x) + ln(x + 1) - ln(x^2 + x)</c> came back 0. Both operands are negative |
| 20 | + /// there, so each logarithm carries <c>i pi</c> and the limit is <c>2 pi i</c>. |
| 21 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2022">#2022</a> |
| 22 | + /// </summary> |
| 23 | + [Trait("Area", "Calculus")] |
| 24 | + public sealed class LogarithmsGatheredOnTheirOwnApproachTest |
| 25 | + { |
| 26 | + [Theory] |
| 27 | + [InlineData("ln(x) + ln(x + 1) - ln(x^2 + x)", "-oo", "2 * i * pi")] |
| 28 | + [InlineData("ln(x) + ln(x - 1) - ln(x^2 - x)", "-oo", "2 * i * pi")] |
| 29 | + [InlineData("(ln(x) + ln(x + 1) - ln(x^2 + x)) * x / (x + 1)", "-oo", "2 * i * pi")] |
| 30 | + [InlineData("(ln(x) + ln(x + 1) - ln(x^2 + x))^2", "-oo", "-4 * pi^2")] |
| 31 | + [InlineData("ln(-x) + ln(1 - x) - ln(x^2 - x)", "+oo", "2 * i * pi")] |
| 32 | + // Where the operands are positive the logarithms do gather, on either side. |
| 33 | + [InlineData("ln(-x) + ln(-x - 1) - ln(x^2 + x)", "-oo", "0")] |
| 34 | + [InlineData("ln(-x) + ln(1 - x) - ln(x^2 - x)", "-oo", "0")] |
| 35 | + [InlineData("ln(x) + ln(x + 1) - ln(x^2 + x)", "+oo", "0")] |
| 36 | + [InlineData("ln(x^2) - 2 * ln(x)", "-oo", "-2 * i * pi")] |
| 37 | + [InlineData("ln(x - 1) + ln(x - 2) - ln(x^2 - 3 * x + 2)", "0", "2 * i * pi")] |
| 38 | + public void IsTheLimitOfTheLogarithmsAsTheyAre(string expression, string dest, string limit) |
| 39 | + { |
| 40 | + var want = limit.ToEntity().EvalNumerical(); |
| 41 | + AssertIs(want, expression.ToEntity().Limit("x", dest), expression, dest, "Limit"); |
| 42 | + AssertIs(want, $"limit({expression}, x, {dest})".ToEntity().Simplify(), expression, dest, "Simplify"); |
| 43 | + } |
| 44 | + |
| 45 | + private static void AssertIs(Entity.Number.Complex want, Entity limit, string expression, string dest, string how) |
| 46 | + { |
| 47 | + Assert.DoesNotContain("limit(", limit.Stringize()); |
| 48 | + var got = limit.EvalNumerical(); |
| 49 | + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); |
| 50 | + Assert.True(difference < 1e-9, $"{how}: lim x->{dest} {expression} is {limit} = {got}, where it is {want}"); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments