|
| 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 | + /// The half-angle substitution with a constant common to the denominator's coefficients: it is |
| 16 | + /// divided out in lowest terms over the symbols, which the simplification does not do for |
| 17 | + /// <c>k^2/k^2</c>. Kept, it left a quadratic whose discriminant was not decidably zero, and |
| 18 | + /// <c>x/(k + k sin(x))^2</c> -- and Rubi's 5.1.4 <c>(a + b asin(c x))/((d + c d x)^(5/2) sqrt(f - c f x))</c>, |
| 19 | + /// which comes to it -- were answered <c>NaN</c>. |
| 20 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/718">#718</a> |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Calculus")] |
| 23 | + public sealed class AHalfAngleSubstitutionWithACommonConstantIntegralTest |
| 24 | + { |
| 25 | + private static void DifferentiatesBack(string integrand, params (string, double)[] pins) |
| 26 | + { |
| 27 | + var integral = integrand.ToEntity().Integrate("x"); |
| 28 | + Assert.DoesNotContain("integral(", integral.Stringize()); |
| 29 | + |
| 30 | + var pinned = integral.Substitute("C", 0); |
| 31 | + Entity original = integrand.ToEntity(); |
| 32 | + foreach (var (name, value) in pins) |
| 33 | + { |
| 34 | + pinned = pinned.Substitute(name, value); |
| 35 | + original = original.Substitute(name, value); |
| 36 | + } |
| 37 | + var derivative = pinned.Differentiate("x"); |
| 38 | + foreach (var at in new[] { -2.3, -0.7, 0.4, 1.9 }) |
| 39 | + { |
| 40 | + var got = derivative.Substitute("x", at).EvalNumerical(); |
| 41 | + var want = original.Substitute("x", at).EvalNumerical(); |
| 42 | + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); |
| 43 | + var scale = Math.Max(1.0, Math.Abs((double)want.RealPart) + Math.Abs((double)want.ImaginaryPart)); |
| 44 | + Assert.True(difference / scale < 1e-8, |
| 45 | + $"d/dx of the antiderivative of {integrand} is {got} at x = {at}, where the integrand is {want}"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // d with the sign that keeps the argument real at every sampled x, and |d| below 2/2.3^2 |
| 50 | + // so that the inverse sine and cosine stay inside [-1, 1]; off it the exact checks hold too. |
| 51 | + [Theory] |
| 52 | + [InlineData("x/(k+k*sin(x))^2")] |
| 53 | + [InlineData("1/(d*q+q*d*sin(x))^2")] |
| 54 | + [InlineData("x/(k-k*cos(x))^2")] |
| 55 | + [InlineData("(a+b*asin(q*x))/((d+q*d*x)^(5/2)*sqrt(f-q*f*x))")] |
| 56 | + public void IsAnsweredAndDifferentiatesBack(string integrand) |
| 57 | + { |
| 58 | + Assert.DoesNotContain("NaN", integrand.ToEntity().Integrate("x").Stringize()); |
| 59 | + DifferentiatesBack(integrand, ("a", 0.6), ("b", 1.25), ("q", 0.3), ("d", 0.75), ("f", 1.4), ("k", 1.5)); |
| 60 | + DifferentiatesBack(integrand, ("a", -0.75), ("b", 0.4), ("q", -0.4), ("d", -2), ("f", 0.5), ("k", -0.3)); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments