Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>See <see cref="ReadExactly"/>.</summary>
private Entity? IntegrateExactly(Entity input)
{
using var _ = MathS.Settings.DowncastingEnabled.Set(true);
return Integrate(ReadExactly(input));
}
}

private sealed class LimitTransformation : Transformation
Expand All @@ -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;

/// <summary>See <see cref="ReadExactly"/>.</summary>
private Entity? LimitExactly(Entity input)
{
using var _ = MathS.Settings.DowncastingEnabled.Set(true);
return Limit(ReadExactly(input), ReadExactly(destination));
}
}

/// <summary>
/// <paramref name="expr"/> 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.
/// </summary>
/// <remarks>
/// With the downcasting off, a whole number parses as a decimal, so <c>x^2</c> is not a
/// whole power to the integrator, and the zero test reads no literal zero as zero:
/// integration gave <c>NaN</c> for <c>(a + b arcsin(c x))/sqrt(d - c^2 d x^2)</c> and
/// limits gave it for <c>sin(c x)/x</c> 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 <c>0.1 x</c> is <c>x^2/20</c>.
/// https://github.com/asc-community/AngouriMath/issues/1490
/// </remarks>
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));
}
}
102 changes: 102 additions & 0 deletions Sources/Tests/UnitTests/Calculus/DowncastingOffCalculusTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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
/// <c>NaN</c> or unevaluated, and so did a limit, where the default setting answers.
/// https://github.com/asc-community/AngouriMath/issues/1490
/// </summary>
[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 };

/// <summary>Parsed with the downcasting off, bypassing the parse cache, which the setting does not key.</summary>
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");
}

/// <summary>Checked with the default setting, with the parameters pinned.</summary>
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}");
}
}

/// <summary>Parsed with the default setting and integrated with the downcasting off.</summary>
[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)));

/// <summary>Parsed and integrated with the downcasting off, where every number is a decimal.</summary>
[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);
}

/// <summary>
/// The cost: a decimal comes back as the rational it holds, <c>0.1 x</c> integrating to
/// <c>x^2/20</c> rather than <c>0.05 x^2</c>. Read before any simplification, which with
/// the default setting would make the two equal.
/// </summary>
[Fact]
public void ADecimalComesBackAsTheRationalItIs()
{
var answer = IntegratedOff(ParsedOff("0.1 * x"));
Assert.All(answer.Nodes.OfType<Entity.Number.Real>(), number => Assert.IsAssignableFrom<Entity.Number.Rational>(number));
Assert.Equal("x ^ 2 / 20".ToEntity().Simplify(), answer.Substitute("C", 0).Simplify());
}
}
}
Loading