diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 84ad8254e..6aa4bdb85 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1660,6 +1660,25 @@ arctangent, arccotangent or arccosecant whose argument diverges, which is now an | `integral(1/(2 + cos(x)), x, 0, pi)` | `integral(1 / (2 + cos(x)), x, 0, pi)` | `sqrt(3) * pi / 3` | | `limitleft(arctan(tan(x / 2)), x, pi)` | `limitleft(arctan(tan(x / 2)), x, pi)` | `pi / 2` | +### With the downcasting off, integration and limits compute on exact numbers + +With `DowncastingEnabled` off, `1/(1 + c^2*x^2)` integrated to `NaN + C` and `limit(sin(c*x)/x, x, 0)` +was `NaN`, where the default setting answers `arctan(c x)/c` and `c`. The zero test compared +against the literal `0`, and with the downcasting off no zero was equal to it. A whole number +parsed with the setting off was a decimal, so `x^2` was not a whole power to the integrator. +Integration and limits now compute as with the downcasting on, whatever the caller's setting, on +the input's decimals read as the exact rationals they are. With it on, nothing changes. With it +off, a decimal comes back as the rational it holds: `0.1 x` integrates to `1/10 * x ^ 2 / 2 + C`, +where it was `0.1 * x ^ 2 / 2 + C` +([#1490](https://github.com/asc-community/AngouriMath/issues/1490)). + +| Input, with the downcasting off | Was (2.5.0) | Now | +|---|---|---| +| `"1/(1 + c^2*x^2)".Integrate("x")` | `NaN + C` | the antiderivative | +| `"(a + b*arcsin(c*x))/sqrt(d - c^2*d*x^2)".Integrate("x")` | `integral((a + b * arcsin(c * x)) / (d - c ^ 2 * d * x ^ 2) ^ (1/2), x)` | the antiderivative | +| `limit(sin(c*x)/x, x, 0)` | `NaN` | `c` | +| `limit((1 - cos(c*x))/x^2, x, 0)` | `NaN` | `c ^ 2 / 2` | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 2021a6d53..a8e90a293 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -633,3 +633,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + +[Tests/UnitTests/Calculus/DowncastingOffCalculusTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs b/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs index 87c8a15b8..f0857ec8f 100644 --- a/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs +++ b/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs @@ -958,7 +958,17 @@ private sealed class IntegrationTransformation : Transformation public override Soundness Soundness => Soundness.SoundUnderAssumptions; protected override Entity? ApplyCore(Entity input) + => MathS.Settings.DowncastingEnabled.Value ? Integrate(input) : IntegrateExactly(input); + + private Entity? Integrate(Entity input) => Functions.Algebra.Integration.ComputeIndefiniteIntegral(input.InnerSimplified, variable)?.InnerSimplified; + + /// See . + private Entity? IntegrateExactly(Entity input) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(true); + return Integrate(ReadExactly(input)); + } } private sealed class LimitTransformation : Transformation @@ -984,10 +994,48 @@ internal LimitTransformation(Variable variable, Entity destination, ApproachFrom // limit stays a question rather than a wrong answer to one. // https://github.com/asc-community/AngouriMath/issues/1186 protected override Entity? ApplyCore(Entity input) - => LimitFunctional.ComputeLimit(input, variable, destination, side) is { } limit + => MathS.Settings.DowncastingEnabled.Value ? Limit(input, destination) : LimitExactly(input); + + private Entity? Limit(Entity input, Entity at) + => LimitFunctional.ComputeLimit(input, variable, at, side) is { } limit && (LimitFunctional.ReadingAnApproach || !LimitFunctional.IsAnIndeterminateForm(limit)) ? limit : null; + + /// See . + private Entity? LimitExactly(Entity input) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(true); + return Limit(ReadExactly(input), ReadExactly(destination)); + } } + + /// + /// with every decimal number read as the exact rational it is, for + /// a computation that runs with the downcasting on whatever the caller's setting. + /// + /// + /// With the downcasting off, a whole number parses as a decimal, so x^2 is not a + /// whole power to the integrator, and the zero test reads no literal zero as zero: + /// integration gave NaN for (a + b arcsin(c x))/sqrt(d - c^2 d x^2) and + /// limits gave it for sin(c x)/x at 0, where the default setting answers. Both are + /// computed as with the downcasting on, on the exact values the decimals hold, so that + /// the answer does not depend on the setting. The cost is that decimals come back as + /// rationals: the integral of 0.1 x is x^2/20. + /// https://github.com/asc-community/AngouriMath/issues/1490 + /// + private static Entity ReadExactly(Entity expr) + => expr.Replace(static node => node switch + { + Number.Rational => node, + Number.Real real => Exactly(real), + Number.Complex { RealPart: var re, ImaginaryPart: var im } => Number.Complex.Create(Exactly(re), Exactly(im)), + _ => node + }); + + private static Number.Real Exactly(Number.Real value) + => value is Number.Rational || !value.EDecimal.IsFinite + ? value + : Number.Rational.Create(PeterO.Numbers.ERational.FromEDecimal(value.EDecimal)); } } diff --git a/Sources/Tests/UnitTests/Calculus/DowncastingOffCalculusTest.cs b/Sources/Tests/UnitTests/Calculus/DowncastingOffCalculusTest.cs new file mode 100644 index 000000000..3b60b2a76 --- /dev/null +++ b/Sources/Tests/UnitTests/Calculus/DowncastingOffCalculusTest.cs @@ -0,0 +1,102 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using System.Linq; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Calculus +{ + /// + /// Integration and limits compute as with the downcasting on, whatever the caller's setting, + /// on the input's decimals read as the exact rationals they are. With it off, a whole number + /// parsed as a decimal and no literal zero read as zero, so an integral came back as + /// NaN or unevaluated, and so did a limit, where the default setting answers. + /// https://github.com/asc-community/AngouriMath/issues/1490 + /// + [Trait("Area", "Calculus")] + public sealed class DowncastingOffCalculusTest + { + private static readonly (string Name, string Value)[] Pins = { ("a", "1/3"), ("b", "2"), ("c", "1/2"), ("d", "3") }; + private static readonly double[] Points = { -0.7, -0.2, 0.3, 0.8 }; + + /// Parsed with the downcasting off, bypassing the parse cache, which the setting does not key. + private static Entity ParsedOff(string text) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(false); + return MathS.FromString(text, useCache: false); + } + + private static Entity IntegratedOff(Entity integrand) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(false); + return integrand.Integrate("x"); + } + + /// Checked with the default setting, with the parameters pinned. + private static void DifferentiatesBack(string integrand, Entity answer) + { + Assert.DoesNotContain("integral(", answer.Stringize()); + Assert.DoesNotContain("NaN", answer.Stringize()); + Entity Pinned(Entity e) => Pins.Aggregate(e, (current, pin) => current.Substitute(pin.Name, pin.Value.ToEntity())); + var derivative = Pinned(answer.Substitute("C", 0)).Differentiate("x"); + var original = Pinned(integrand.ToEntity()); + foreach (var at in Points) + { + var got = derivative.Substitute("x", at).EvalNumerical(); + var want = original.Substitute("x", at).EvalNumerical(); + var difference = Math.Abs((double)(got - want).RealPart) + Math.Abs((double)(got - want).ImaginaryPart); + Assert.True(difference < 1e-9, $"d/dx of {answer} is {got} at x = {at}, where {integrand} is {want}"); + } + } + + /// Parsed with the default setting and integrated with the downcasting off. + [Theory] + [InlineData("(a + b*arcsin(c*x))/sqrt(d - c^2*d*x^2)")] + [InlineData("1/sqrt(2 - 2*x^2)")] + [InlineData("1/(1 + c^2*x^2)")] + public void IntegratedWithTheDowncastingOff(string integrand) + => DifferentiatesBack(integrand, IntegratedOff(MathS.FromString(integrand, useCache: false))); + + /// Parsed and integrated with the downcasting off, where every number is a decimal. + [Theory] + [InlineData("1/sqrt(2 - 2*x^2)")] + [InlineData("x^2*sin(x)")] + [InlineData("(a + b*arcsin(c*x))/sqrt(d - c^2*d*x^2)")] + public void ParsedAndIntegratedWithTheDowncastingOff(string integrand) + => DifferentiatesBack(integrand, IntegratedOff(ParsedOff(integrand))); + + [Theory] + [InlineData("sin(c*x)/x", "c")] + [InlineData("(1 - cos(c*x))/x^2", "c^2/2")] + public void ALimitWithTheDowncastingOff(string expression, string value) + { + var limit = LimitOff(ParsedOff(expression)); + Assert.Equal(value.ToEntity().Simplify(), limit.Simplify()); + } + + private static Entity LimitOff(Entity expression) + { + using var _ = MathS.Settings.DowncastingEnabled.Set(false); + return expression.Limit("x", 0); + } + + /// + /// The cost: a decimal comes back as the rational it holds, 0.1 x integrating to + /// x^2/20 rather than 0.05 x^2. Read before any simplification, which with + /// the default setting would make the two equal. + /// + [Fact] + public void ADecimalComesBackAsTheRationalItIs() + { + var answer = IntegratedOff(ParsedOff("0.1 * x")); + Assert.All(answer.Nodes.OfType(), number => Assert.IsAssignableFrom(number)); + Assert.Equal("x ^ 2 / 20".ToEntity().Simplify(), answer.Substitute("C", 0).Simplify()); + } + } +}