|
| 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 AngouriMath.Extensions; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace AngouriMath.Tests.Calculus |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A definite integral's value at a bound is the antiderivative's value there, and where that |
| 15 | + /// is undefined, its limit from inside the range, which is what an improper integral is. |
| 16 | + /// <c>-(x + 1) e^(-x)</c> at <c>+oo</c> is <c>-oo * 0</c>, so the integral of <c>x e^(-x)</c> |
| 17 | + /// over <c>[0, +oo)</c> was <c>NaN</c>; <c>x ln(x) - x</c> at 0 is <c>0 * -oo</c>, so the |
| 18 | + /// integral of <c>ln(x)</c> over <c>[0, 1]</c> was too. |
| 19 | + /// https://github.com/asc-community/AngouriMath/issues/1507 |
| 20 | + /// </summary> |
| 21 | + [Trait("Area", "Calculus")] |
| 22 | + public sealed class ImproperIntegralTest |
| 23 | + { |
| 24 | + private static Entity Definite(string integrand, string from, string to) |
| 25 | + => MathS.Integral(integrand.ToEntity(), "x", from.ToEntity(), to.ToEntity()).Simplify(); |
| 26 | + |
| 27 | + [Theory] |
| 28 | + [InlineData("x * e^(-x)", "0", "+oo", "1")] |
| 29 | + [InlineData("x^2 * e^(-x)", "0", "+oo", "2")] |
| 30 | + [InlineData("x^3 * e^(-x^2)", "0", "+oo", "1/2")] |
| 31 | + [InlineData("ln(x) / x^2", "1", "+oo", "1")] |
| 32 | + [InlineData("e^(-x) * sin(x)", "0", "+oo", "1/2")] |
| 33 | + [InlineData("x * e^x", "-oo", "0", "-1")] |
| 34 | + public void AnInfiniteBoundIsALimit(string integrand, string from, string to, string value) |
| 35 | + => Assert.Equal(value.ToEntity(), Definite(integrand, from, to)); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// A finite bound is approached from the side the other bound is on, so the range may |
| 39 | + /// run either way. |
| 40 | + /// </summary> |
| 41 | + [Theory] |
| 42 | + [InlineData("ln(x)", "0", "1", "-1")] |
| 43 | + [InlineData("x * ln(x)", "0", "1", "-1/4")] |
| 44 | + [InlineData("ln(x)", "1", "0", "1")] |
| 45 | + [InlineData("x * e^(-x)", "+oo", "0", "-1")] |
| 46 | + public void ABoundWhereTheAntiderivativeIsUndefinedIsALimit(string integrand, string from, string to, string value) |
| 47 | + => Assert.Equal(value.ToEntity(), Definite(integrand, from, to)); |
| 48 | + |
| 49 | + /// <summary>A divergent integral is <c>+oo</c> where the limit is.</summary> |
| 50 | + [Fact] |
| 51 | + public void ADivergentIntegralIsItsLimit() => Assert.Equal("+oo".ToEntity(), Definite("1/x^2", "0", "1")); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// And <c>NaN</c> where there is no limit: <c>-cos(x)</c> does not settle as <c>x</c> grows. |
| 55 | + /// </summary> |
| 56 | + [Fact] |
| 57 | + public void AnAntiderivativeWithoutALimitLeavesNaN() => Assert.Equal(MathS.NaN, Definite("sin(x)", "0", "+oo")); |
| 58 | + |
| 59 | + /// <summary>A bound the substitution answers is answered as it was.</summary> |
| 60 | + [Theory] |
| 61 | + [InlineData("e^(-x)", "0", "+oo", "1")] |
| 62 | + [InlineData("1/(x^2 + 1)", "-oo", "+oo", "pi")] |
| 63 | + [InlineData("x * e^(-x^2)", "0", "+oo", "1/2")] |
| 64 | + [InlineData("1/sqrt(x)", "0", "1", "2")] |
| 65 | + [InlineData("1/x", "1", "+oo", "+oo")] |
| 66 | + public void ABoundWithAValueKeepsIt(string integrand, string from, string to, string value) |
| 67 | + => Assert.Equal(value.ToEntity(), Definite(integrand, from, to)); |
| 68 | + } |
| 69 | +} |
0 commit comments