Skip to content

Commit 97fcdbc

Browse files
Integration and limits with the downcasting off compute as with it on, on exact numbers (#1516)
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: (a + b arcsin(c x))/sqrt(d - c^2 d x^2) integrated to NaN, and limit(sin(c x)/x, x, 0) was NaN, where the default setting answers. The integration and limit transformations now compute 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. The cost is that decimals come back as rationals: 0.1 x integrates to x^2/20. Closes #1490. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2880133 commit 97fcdbc

4 files changed

Lines changed: 173 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,25 @@ arctangent, arccotangent or arccosecant whose argument diverges, which is now an
16601660
| `integral(1/(2 + cos(x)), x, 0, pi)` | `integral(1 / (2 + cos(x)), x, 0, pi)` | `sqrt(3) * pi / 3` |
16611661
| `limitleft(arctan(tan(x / 2)), x, pi)` | `limitleft(arctan(tan(x / 2)), x, pi)` | `pi / 2` |
16621662

1663+
### With the downcasting off, integration and limits compute on exact numbers
1664+
1665+
With `DowncastingEnabled` off, `1/(1 + c^2*x^2)` integrated to `NaN + C` and `limit(sin(c*x)/x, x, 0)`
1666+
was `NaN`, where the default setting answers `arctan(c x)/c` and `c`. The zero test compared
1667+
against the literal `0`, and with the downcasting off no zero was equal to it. A whole number
1668+
parsed with the setting off was a decimal, so `x^2` was not a whole power to the integrator.
1669+
Integration and limits now compute as with the downcasting on, whatever the caller's setting, on
1670+
the input's decimals read as the exact rationals they are. With it on, nothing changes. With it
1671+
off, a decimal comes back as the rational it holds: `0.1 x` integrates to `1/10 * x ^ 2 / 2 + C`,
1672+
where it was `0.1 * x ^ 2 / 2 + C`
1673+
([#1490](https://github.com/asc-community/AngouriMath/issues/1490)).
1674+
1675+
| Input, with the downcasting off | Was (2.5.0) | Now |
1676+
|---|---|---|
1677+
| `"1/(1 + c^2*x^2)".Integrate("x")` | `NaN + C` | the antiderivative |
1678+
| `"(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 |
1679+
| `limit(sin(c*x)/x, x, 0)` | `NaN` | `c` |
1680+
| `limit((1 - cos(c*x))/x^2, x, 0)` | `NaN` | `c ^ 2 / 2` |
1681+
16631682
### `binomial(n, k)` is a function
16641683

16651684
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
633633

634634
[Tests/UnitTests/Calculus/IntegralAcrossABreakTest.cs]
635635
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
636+
637+
[Tests/UnitTests/Calculus/DowncastingOffCalculusTest.cs]
638+
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

‎Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,17 @@ private sealed class IntegrationTransformation : Transformation
958958
public override Soundness Soundness => Soundness.SoundUnderAssumptions;
959959

960960
protected override Entity? ApplyCore(Entity input)
961+
=> MathS.Settings.DowncastingEnabled.Value ? Integrate(input) : IntegrateExactly(input);
962+
963+
private Entity? Integrate(Entity input)
961964
=> Functions.Algebra.Integration.ComputeIndefiniteIntegral(input.InnerSimplified, variable)?.InnerSimplified;
965+
966+
/// <summary>See <see cref="ReadExactly"/>.</summary>
967+
private Entity? IntegrateExactly(Entity input)
968+
{
969+
using var _ = MathS.Settings.DowncastingEnabled.Set(true);
970+
return Integrate(ReadExactly(input));
971+
}
962972
}
963973

964974
private sealed class LimitTransformation : Transformation
@@ -984,10 +994,48 @@ internal LimitTransformation(Variable variable, Entity destination, ApproachFrom
984994
// limit stays a question rather than a wrong answer to one.
985995
// https://github.com/asc-community/AngouriMath/issues/1186
986996
protected override Entity? ApplyCore(Entity input)
987-
=> LimitFunctional.ComputeLimit(input, variable, destination, side) is { } limit
997+
=> MathS.Settings.DowncastingEnabled.Value ? Limit(input, destination) : LimitExactly(input);
998+
999+
private Entity? Limit(Entity input, Entity at)
1000+
=> LimitFunctional.ComputeLimit(input, variable, at, side) is { } limit
9881001
&& (LimitFunctional.ReadingAnApproach || !LimitFunctional.IsAnIndeterminateForm(limit))
9891002
? limit
9901003
: null;
1004+
1005+
/// <summary>See <see cref="ReadExactly"/>.</summary>
1006+
private Entity? LimitExactly(Entity input)
1007+
{
1008+
using var _ = MathS.Settings.DowncastingEnabled.Set(true);
1009+
return Limit(ReadExactly(input), ReadExactly(destination));
1010+
}
9911011
}
1012+
1013+
/// <summary>
1014+
/// <paramref name="expr"/> with every decimal number read as the exact rational it is, for
1015+
/// a computation that runs with the downcasting on whatever the caller's setting.
1016+
/// </summary>
1017+
/// <remarks>
1018+
/// With the downcasting off, a whole number parses as a decimal, so <c>x^2</c> is not a
1019+
/// whole power to the integrator, and the zero test reads no literal zero as zero:
1020+
/// integration gave <c>NaN</c> for <c>(a + b arcsin(c x))/sqrt(d - c^2 d x^2)</c> and
1021+
/// limits gave it for <c>sin(c x)/x</c> at 0, where the default setting answers. Both are
1022+
/// computed as with the downcasting on, on the exact values the decimals hold, so that
1023+
/// the answer does not depend on the setting. The cost is that decimals come back as
1024+
/// rationals: the integral of <c>0.1 x</c> is <c>x^2/20</c>.
1025+
/// https://github.com/asc-community/AngouriMath/issues/1490
1026+
/// </remarks>
1027+
private static Entity ReadExactly(Entity expr)
1028+
=> expr.Replace(static node => node switch
1029+
{
1030+
Number.Rational => node,
1031+
Number.Real real => Exactly(real),
1032+
Number.Complex { RealPart: var re, ImaginaryPart: var im } => Number.Complex.Create(Exactly(re), Exactly(im)),
1033+
_ => node
1034+
});
1035+
1036+
private static Number.Real Exactly(Number.Real value)
1037+
=> value is Number.Rational || !value.EDecimal.IsFinite
1038+
? value
1039+
: Number.Rational.Create(PeterO.Numbers.ERational.FromEDecimal(value.EDecimal));
9921040
}
9931041
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)