|
| 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 sum with the square root of a quadratic below the bar, cleared by its conjugate. |
| 16 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a> |
| 17 | + /// </summary> |
| 18 | + /// <remarks> |
| 19 | + /// Rubi's <c>1/(a c + b c x^2 + d sqrt(a + b x^2))</c> and its <c>x^2</c> and <c>1/x^2</c> |
| 20 | + /// neighbours had no antiderivative: Euler's substitution declines a symbolic leading |
| 21 | + /// coefficient. Each answer is differentiated back with the constants pinned on both signs of |
| 22 | + /// <c>b</c> and of <c>a c^2 - d^2</c>, at points where the root is real. |
| 23 | + /// </remarks> |
| 24 | + [Trait("Area", "Calculus")] |
| 25 | + public sealed class TheConjugateOfASumWithARootBelowTheBarIntegralTest |
| 26 | + { |
| 27 | + private static void DifferentiatesBack(string integrand, double[] points, params (string, double)[] pins) |
| 28 | + { |
| 29 | + var integral = integrand.ToEntity().Integrate("x"); |
| 30 | + Assert.DoesNotContain("integral(", integral.Stringize()); |
| 31 | + Assert.DoesNotContain("NaN", integral.Stringize()); |
| 32 | + |
| 33 | + var derivative = integral.Substitute("C", 0).Differentiate("x"); |
| 34 | + Entity original = integrand.ToEntity(); |
| 35 | + foreach (var (name, value) in pins) |
| 36 | + { |
| 37 | + derivative = derivative.Substitute(name, value); |
| 38 | + original = original.Substitute(name, value); |
| 39 | + } |
| 40 | + |
| 41 | + var compared = 0; |
| 42 | + foreach (var at in points) |
| 43 | + { |
| 44 | + var got = derivative.Substitute("x", at).EvalNumerical(); |
| 45 | + var want = original.Substitute("x", at).EvalNumerical(); |
| 46 | + if (got.IsNaN || want.IsNaN) |
| 47 | + continue; |
| 48 | + compared++; |
| 49 | + var difference = Math.Abs((double)(got - want).RealPart) |
| 50 | + + Math.Abs((double)(got - want).ImaginaryPart); |
| 51 | + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart)); |
| 52 | + Assert.True(difference / scale < 1e-8, |
| 53 | + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, " |
| 54 | + + $"where the integrand is {want}"); |
| 55 | + } |
| 56 | + Assert.True(compared >= 3, |
| 57 | + $"only {compared} of {points.Length} points were comparable for {integrand}"); |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [InlineData("1/(a*c+b*c*x^2+d*sqrt(a+b*x^2))")] |
| 62 | + [InlineData("x^2/(a*c+b*c*x^2+d*sqrt(a+b*x^2))")] |
| 63 | + [InlineData("1/(x^2*(a*c+b*c*x^2+d*sqrt(a+b*x^2)))")] |
| 64 | + [InlineData("1/(c*sqrt(a+b*x^2)+d)")] |
| 65 | + public void SymbolicConstants(string integrand) |
| 66 | + { |
| 67 | + var points = new[] { 0.3, 0.7, 1.1, 1.3 }; |
| 68 | + DifferentiatesBack(integrand, points, ("a", 2), ("b", 3), ("c", 1.25), ("d", 1)); |
| 69 | + DifferentiatesBack(integrand, points, ("a", 2), ("b", 3), ("c", 0.5), ("d", 3)); |
| 70 | + DifferentiatesBack(integrand, points, ("a", 2), ("b", -1), ("c", 1), ("d", 0.5)); |
| 71 | + DifferentiatesBack(integrand, points, ("a", 2), ("b", 3), ("c", 1), ("d", -0.5)); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void ARootBesideXOverTheirDifference() |
| 76 | + => DifferentiatesBack("x*sqrt(2-x^2)/(x-sqrt(2-x^2))", new[] { -0.5, 0.3, 1.2, 1.3 }); |
| 77 | + } |
| 78 | +} |
0 commit comments