|
| 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 | + /// Integration and limits compute as with the downcasting on, whatever the caller's setting, |
| 17 | + /// on the input's decimals read as the exact rationals they are. With it off, a whole number |
| 18 | + /// parsed as a decimal and no literal zero read as zero, so an integral came back as |
| 19 | + /// <c>NaN</c> or unevaluated, and so did a limit, where the default setting answers. |
| 20 | + /// https://github.com/asc-community/AngouriMath/issues/1490 |
| 21 | + /// </summary> |
| 22 | + [Trait("Area", "Calculus")] |
| 23 | + public sealed class DowncastingOffCalculusTest |
| 24 | + { |
| 25 | + private static readonly (string Name, string Value)[] Pins = { ("a", "1/3"), ("b", "2"), ("c", "1/2"), ("d", "3") }; |
| 26 | + private static readonly double[] Points = { -0.7, -0.2, 0.3, 0.8 }; |
| 27 | + |
| 28 | + /// <summary>Parsed with the downcasting off, bypassing the parse cache, which the setting does not key.</summary> |
| 29 | + private static Entity ParsedOff(string text) |
| 30 | + { |
| 31 | + using var _ = MathS.Settings.DowncastingEnabled.Set(false); |
| 32 | + return MathS.FromString(text, useCache: false); |
| 33 | + } |
| 34 | + |
| 35 | + private static Entity IntegratedOff(Entity integrand) |
| 36 | + { |
| 37 | + using var _ = MathS.Settings.DowncastingEnabled.Set(false); |
| 38 | + return integrand.Integrate("x"); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary>Checked with the default setting, with the parameters pinned.</summary> |
| 42 | + private static void DifferentiatesBack(string integrand, Entity answer) |
| 43 | + { |
| 44 | + Assert.DoesNotContain("integral(", answer.Stringize()); |
| 45 | + Assert.DoesNotContain("NaN", answer.Stringize()); |
| 46 | + Entity Pinned(Entity e) => Pins.Aggregate(e, (current, pin) => current.Substitute(pin.Name, pin.Value.ToEntity())); |
| 47 | + var derivative = Pinned(answer.Substitute("C", 0)).Differentiate("x"); |
| 48 | + var original = Pinned(integrand.ToEntity()); |
| 49 | + foreach (var at in Points) |
| 50 | + { |
| 51 | + var got = derivative.Substitute("x", at).EvalNumerical(); |
| 52 | + var want = original.Substitute("x", at).EvalNumerical(); |
| 53 | + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); |
| 54 | + Assert.True(difference < 1e-9, $"d/dx of {answer} is {got} at x = {at}, where {integrand} is {want}"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary>Parsed with the default setting and integrated with the downcasting off.</summary> |
| 59 | + [Theory] |
| 60 | + [InlineData("(a + b*arcsin(c*x))/sqrt(d - c^2*d*x^2)")] |
| 61 | + [InlineData("1/sqrt(2 - 2*x^2)")] |
| 62 | + [InlineData("1/(1 + c^2*x^2)")] |
| 63 | + public void IntegratedWithTheDowncastingOff(string integrand) |
| 64 | + => DifferentiatesBack(integrand, IntegratedOff(MathS.FromString(integrand, useCache: false))); |
| 65 | + |
| 66 | + /// <summary>Parsed and integrated with the downcasting off, where every number is a decimal.</summary> |
| 67 | + [Theory] |
| 68 | + [InlineData("1/sqrt(2 - 2*x^2)")] |
| 69 | + [InlineData("x^2*sin(x)")] |
| 70 | + [InlineData("(a + b*arcsin(c*x))/sqrt(d - c^2*d*x^2)")] |
| 71 | + public void ParsedAndIntegratedWithTheDowncastingOff(string integrand) |
| 72 | + => DifferentiatesBack(integrand, IntegratedOff(ParsedOff(integrand))); |
| 73 | + |
| 74 | + [Theory] |
| 75 | + [InlineData("sin(c*x)/x", "c")] |
| 76 | + [InlineData("(1 - cos(c*x))/x^2", "c^2/2")] |
| 77 | + public void ALimitWithTheDowncastingOff(string expression, string value) |
| 78 | + { |
| 79 | + var limit = LimitOff(ParsedOff(expression)); |
| 80 | + Assert.Equal(value.ToEntity().Simplify(), limit.Simplify()); |
| 81 | + } |
| 82 | + |
| 83 | + private static Entity LimitOff(Entity expression) |
| 84 | + { |
| 85 | + using var _ = MathS.Settings.DowncastingEnabled.Set(false); |
| 86 | + return expression.Limit("x", 0); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// The cost: a decimal comes back as the rational it holds, <c>0.1 x</c> integrating to |
| 91 | + /// <c>x^2/20</c> rather than <c>0.05 x^2</c>. Read before any simplification, which with |
| 92 | + /// the default setting would make the two equal. |
| 93 | + /// </summary> |
| 94 | + [Fact] |
| 95 | + public void ADecimalComesBackAsTheRationalItIs() |
| 96 | + { |
| 97 | + var answer = IntegratedOff(ParsedOff("0.1 * x")); |
| 98 | + Assert.All(answer.Nodes.OfType<Entity.Number.Real>(), number => Assert.IsAssignableFrom<Entity.Number.Rational>(number)); |
| 99 | + Assert.Equal("x ^ 2 / 20".ToEntity().Simplify(), answer.Substitute("C", 0).Simplify()); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments