Skip to content
Open
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
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,23 @@ logarithms. Where nothing cancels, the integral diverges and is now `+oo`, where
| `integral((2x+1)/(x^4+2x^3+x^2-x-1), x, 1, +oo)` | unevaluated | a sum over roots = `0.64121…` |
| `integral(x^2/(x^3+x+1), x, 0, +oo)` | `NaN` | `+oo` |

### A definite integral is split at the solutions of an equation in a condition, and at exact points

**An answer where there was none, and exact where it was a decimal.** A definite integral was split at the boundaries
of a condition only when the condition compared the variable with a number. `(x^3 - x)/(x^2 - 1)` simplifies to
`x provided not x^2 - 1 = 0`, so its integral over `[-2, 2]` was left unevaluated. That condition fails at two points
only, and the integral is 0. An equation's solutions are now listed as boundaries, and a range with an infinite bound
is split as a finite one is. Each boundary is kept as written, so `piecewise(x provided x < sqrt(2), 1)` over `[0, 2]`
is `3 - sqrt(2)`, where it was `3 - 1.41421…`, a hundred-digit decimal
([#2654](https://github.com/asc-community/AngouriMath/issues/2654)).

| simplified | Was (2.10.0) | Now |
|---|---|---|
| `integral((x^3 - x)/(x^2 - 1), x, -2, 2)` | unevaluated | `0` |
| `integral(sin(x) cos(x)^0, x, 0, pi)` | unevaluated | `2` |
| `integral((-x - 1)/(x^3 + 3x^2 + x - 1), x, -oo, -3)` | unevaluated | `-1/4 * sqrt(2) * ln(…)` = `-0.62322…` |
| `integral(piecewise(x provided x < sqrt(2), 1), x, 0, 2)` | `3 - 1.41421…` | `3 - sqrt(2)` |

### A rational integral down to -oo is the reflection of one up to +oo

**An answer where there was none.** A sum over roots in an antiderivative was taken at `+oo` but not at `-oo`. There,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,40 +217,50 @@ private static bool IsABreak(Entity node, Variable x)

private static Entity? OverCaseBoundaries(Entity expr, Variable x, Entity from, Entity to)
{
if (from.Evaled is not Real lower || !lower.IsFinite || to.Evaled is not Real upper || !upper.IsFinite)
// An infinite bound is a range like any other here: -1/(x^2 + 2x - 1) provided x + 1 != 0, which
// (-x - 1)/(x^3 + 3x^2 + x - 1) simplifies to, was left unevaluated over (-oo, -3].
// https://github.com/asc-community/AngouriMath/issues/2654
if (from.Evaled is not Real lower || lower.IsNaN || to.Evaled is not Real upper || upper.IsNaN)
return null;
if (upper < lower)
return null;

var thresholds = new List<Real>();
// Each with the point as written, so that a piece ends at pi or at sqrt(2) and not at a decimal of it.
var thresholds = new List<(Entity At, Real Value)>();
foreach (var node in expr.Nodes)
switch (node)
{
case Piecewise piecewise:
foreach (var @case in piecewise.Cases)
if (!CollectThresholds(@case.Predicate, x, thresholds))
if (!CollectThresholds(@case.Predicate, x, thresholds, lower, upper))
return null;
break;
case Providedf provided:
if (!CollectThresholds(provided.Predicate, x, thresholds))
if (!CollectThresholds(provided.Predicate, x, thresholds, lower, upper))
return null;
break;
}

var points = new List<Real> { lower };
foreach (var threshold in thresholds.Where(t => lower < t && t < upper).OrderBy(t => t.EDecimal))
if (!points.Contains(threshold))
var points = new List<(Entity At, Real Value)> { (from, lower) };
foreach (var threshold in thresholds.Where(t => lower < t.Value && t.Value < upper).OrderBy(t => t.Value.EDecimal))
if (!points.Any(point => point.Value == threshold.Value))
points.Add(threshold);
if (upper > lower)
points.Add(upper);
points.Add((to, upper));

Entity total = Integer.Zero;
for (var i = 0; i + 1 < points.Count; i++)
{
var (start, end) = (points[i], points[i + 1]);
if (((Entity)start + end).Evaled is not Real twice)
var ((start, startValue), (end, endValue)) = (points[i], points[i + 1]);
var midpoint = (startValue.IsFinite, endValue.IsFinite) switch
{
(true, true) => ((Entity)startValue + endValue).Evaled is Real twice ? (Real)(twice / Integer.Create(2)).Evaled : null,
(true, false) => (Real)(startValue + Integer.One).Evaled,
(false, true) => (Real)(endValue - Integer.One).Evaled,
_ => Integer.Zero
};
if (midpoint is null)
return null;
var midpoint = (Real)(twice / Integer.Create(2)).Evaled;
var resolved = ResolvedAt(expr, x, midpoint);
if (resolved is null)
return null;
Expand All @@ -268,12 +278,12 @@ private static bool IsABreak(Entity node, Variable x)
/// compares <paramref name="x"/> with something that is not a number, or is a shape
/// this does not read.
/// </summary>
private static bool CollectThresholds(Entity predicate, Variable x, List<Real> thresholds)
private static bool CollectThresholds(Entity predicate, Variable x, List<(Entity At, Real Value)> thresholds, Real lower, Real upper)
{
if (!predicate.ContainsNode(x))
return true;
bool Both(Entity left, Entity right)
=> CollectThresholds(left, x, thresholds) && CollectThresholds(right, x, thresholds);
=> CollectThresholds(left, x, thresholds, lower, upper) && CollectThresholds(right, x, thresholds, lower, upper);
switch (predicate)
{
case Andf(var a, var b):
Expand All @@ -285,16 +295,29 @@ bool Both(Entity left, Entity right)
case Xorf(var a, var b):
return Both(a, b);
case Notf(var operand):
return CollectThresholds(operand, x, thresholds);
return CollectThresholds(operand, x, thresholds, lower, upper);
case ComparisonSign comparison when comparison.DirectChildren.Count == 2:
{
var (left, right) = (comparison.DirectChildren[0], comparison.DirectChildren[1]);
// An equation changes truth only at its solutions, which are listed where they can be: x^2 - 1 = 0
// at -1 and 1, cos(x) = 0 at every odd multiple of pi/2. The integral of x provided x^2 - 1 != 0,
// which is what (x^3 - x)/(x^2 - 1) simplifies to, was left unevaluated over [-2, 2], where between
// those points the condition holds and the integral is 0.
// https://github.com/asc-community/AngouriMath/issues/2654
if (comparison is Equalsf && left != x && right != x)
{
var solutions = new List<(Entity At, Real Value)>();
if (!AntiderivativeBreaks.ZerosOf(left - right, x, lower, upper, solutions))
return false;
thresholds.AddRange(solutions);
return true;
}
var other = left == x ? right : right == x ? left : null;
if (other is null || other.ContainsNode(x))
return false;
if (other.Evaled is not Real value || !value.IsFinite)
return false;
thresholds.Add(value);
thresholds.Add((other, value));
return true;
}
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// 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.Linq;
using AngouriMath.Extensions;
using Xunit;
using static AngouriMath.Entity;

namespace AngouriMath.Tests.Calculus
{
/// <summary>
/// <c>(x^3 - x)/(x^2 - 1)</c> simplifies to <c>x provided not x^2 - 1 = 0</c>, and the split of a definite integral at
/// a condition's boundaries read only a comparison of the variable with a number, so the integral over
/// <c>[-2, 2]</c> was left unevaluated where it is 0. An equation's solutions are its boundaries. They are kept as
/// written, as are a comparison's, so that a piece ends at <c>sqrt(2)</c> and not at a decimal of it.
/// https://github.com/asc-community/AngouriMath/issues/2654
/// </summary>
[Trait("Area", "Calculus")]
public sealed class AConditionThatFailsAtIsolatedPointsIsSplitAtThemTest
{
[Theory]
[InlineData("(x^3 - x)/(x^2 - 1)", "-2", "2", "0")]
[InlineData("(x^3 - x)/(x^2 - 1)", "0", "3", "9/2")]
[InlineData("(x^4 - 1)/(x^2 - 1)", "-2", "2", "28/3")]
[InlineData("sin(x) cos(x)^0", "0", "pi", "2")]
[InlineData("sin(x)^4 cos(x)^0", "0", "pi", "3/8 pi")]
[InlineData("piecewise(x provided x < sqrt(2), 1)", "0", "2", "3 - sqrt(2)")]
// Over an infinite range as well.
[InlineData("(x^3 - x)/(x^2 - 1) e^(-x^2)", "0", "+oo", "1/2")]
[InlineData("(x^2 + x)/(x + 1) e^(-x)", "0", "+oo", "1")]
public void TheIntegralIsExact(string integrand, string from, string to, string expected)
{
var integral = integrand.ToEntity().Integrate("x", from.ToEntity(), to.ToEntity()).Simplify();
Assert.DoesNotContain(integral.Nodes, node => node is Integralf);
// Exact: no decimal stands in for a point the range was split at.
Assert.DoesNotContain(integral.Nodes, node => node is Number.Real and not Number.Rational);
Assert.Equal(Number.Integer.Zero, (integral - expected.ToEntity()).Simplify());
}

/// <summary>Over an infinite range, checked against mpmath's quadrature at 30 digits.</summary>
[Fact]
public void DownToMinusInfinity()
{
var integral = "(-x - 1)/(x^3 + 3x^2 + x - 1)".ToEntity().Integrate("x", "-oo".ToEntity(), -3).Simplify();
Assert.True((integral - "-0.623225240140230513394020080251".ToEntity()).EvalNumerical().Abs() < 1e-12, $"was {integral}");
}

/// <summary>A condition that fails on an interval leaves no value there.</summary>
[Fact]
public void AConditionThatFailsOnAnIntervalAnswersNothing()
=> Assert.Contains("(x provided x > 1/2)".ToEntity().Integrate("x", 0, 1).Simplify().Nodes, node => node is Integralf);
}
}
Loading