diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 56773b199..1883ffaa3 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -723,6 +723,32 @@ irrational constants. Where two exponents written over different constants canno | `(2^x + 3^x) / (3^x + 4^x)` at `+oo` | unevaluated | `0` | | `x^10 * 2^x / 3^x` at `+oo` | unevaluated | `0` | +### A limit through a cosine, a sine or an error function of a vanishing argument is answered at infinity + +**Not silent: limits and definite integrals that were left unevaluated have values.** Gruntz's algorithm, which settles +an infinity less an infinity at `+oo` and so at every finite point moved out there, declined any expression holding a +sine, a cosine or an error function. It now expands one whose argument tends to a finite real number where it is +analytic (the sine, cosine, tangent, cotangent, secant and cosecant away from their poles, the arctangent, and `erf`, +`erfc` and `erfi`) as its Taylor series there, where every logarithm and every power that is not whole in the +expression is of something positive on the way, so that the function is real there; one whose argument does not settle +is declined as before. So +`ln(x) cos(x) - ln(x)` at 0 from the right, which was left unevaluated while the same expression with `e^x` for the +cosine was 0, is 0, and the integrals of `ln(x)` times the sine integral, the error functions and the sine over +`[0, 1]` are answered. Two-sided at 0 the limit is still left unevaluated, the logarithm not being real to the left. +Measured on master `70b17bba4` and on this branch; each value checked against mpmath +([#2690](https://github.com/asc-community/AngouriMath/issues/2690)). + +| Call | Was (`70b17bba4`) | Now | +|---|---|---| +| `"ln(x) * cos(x) - ln(x)".Limit("x", 0, ApproachFrom.Right)` | `limitright(ln(x) * cos(x) - ln(x), x, 0)` | `0` | +| `"ln(x) * cos(1/x) - ln(x)".Limit("x", "+oo")` | unevaluated | `0` | +| `"e^(2 * x) * (1 - cos(e^(-x)))".Limit("x", "+oo")` | unevaluated | `1/2` | +| `"integral(ln(x) * Si(x), x, 0, 1)".Simplify()` | `integral(Si(x) * ln(x), x, 0, 1)` | `eulergamma + 1 - (Si(1) + Ci(1) + cos(1))`, -0.2465736342 | +| `"integral(ln(x) * erf(x), x, 0, 1)".Simplify()` | `integral(ln(x) * erf(x), x, 0, 1)` | `(1 + (-1) / e) / sqrt(pi) - (pi ^ (-1/2) * (Ei(-1) - eulergamma) / 2 + erf(1))`, -0.2613483600 | +| `"integral(ln(x) * erfc(x), x, 0, 1)".Simplify()` | not done in 20 s | `(Ei(-1) / 2 - 1 + 1 / e + -eulergamma / 2) / sqrt(pi) - erfc(1)`, -0.7386516400 | +| `"integral(ln(x) * sin(x), x, 0, 1)".Simplify()` | `integral(sin(x) * ln(x), x, 0, 1)` | `Ci(1) - eulergamma`, -0.2398117420 | +| `"ln(x) * cos(x) - ln(x)".Limit("x", 0)` | `limit(ln(x) * cos(x) - ln(x), x, 0)` | unchanged | + ### A negative multiple of ln of a rational below 1 is turned round **Silent, in the printed form only.** `ln(1/b) = -ln(b)` matched a written quotient `1 / b`, never the literal diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/AsymptoticSeries.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/AsymptoticSeries.cs index aa1f50011..9c0cadff5 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/AsymptoticSeries.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/AsymptoticSeries.cs @@ -262,6 +262,53 @@ internal AsymptoticSeries WithoutConstant() return sum.WithOrder(requested).Multiply(Constant(scale)); } + /// + /// of the series, by its Taylor series at the series' constant term: f(c + d) = + /// sum f^(k)(c) d^k / k!, where d, the rest, starts above w^0. The caller has made sure + /// f is analytic at the limit of c. + /// + /// + /// f^(k)(c) is a coefficient, left unexpanded as exp(c) is in : c + /// may still hold a function of a slower class than w. Where no term of the rest is known, nothing beyond + /// its order is known of the result either. + /// https://github.com/asc-community/AngouriMath/issues/2690 + /// + internal AsymptoticSeries? Through(Func function, SeriesExponent requested) + { + var constant = Terms.TryGetValue(SeriesExponent.Zero, out var atZero) ? atZero : Integer.Zero; + var rest = WithoutConstant(); + if (rest.LeadingTerm() is var (_, least) && least.CompareTo(SeriesExponent.Zero) <= 0) + return null; + var t = Variable.CreateTemp(constant.Vars); + var derivative = function(t); + if (At(derivative) is not { } value) + return null; + var sum = Constant(value); + var term = Constant(1); + for (var i = 1; i < MaxExpansionTerms; i++) + { + term = term.Multiply(rest).Multiply(Constant(Rational.Create(1, i))); + if (term.LeadingTerm() is not var (_, at) || at.CompareTo(requested) >= 0) + break; + derivative = derivative.Differentiate(t).InnerSimplified; + if (At(derivative) is not { } coefficient) + return null; + sum = sum.Add(term.Multiply(Constant(coefficient))); + } + return sum.WithOrder(rest.Order.CompareTo(requested) < 0 ? rest.Order : requested); + + // A derivative written as a quotient whose divisor vanishes at the constant would read as no number there, + // and a coefficient that is no number is no series; where the constant still holds x it is left to the + // limit of the leading coefficient, which is taken afterwards. + Entity? At(Entity derivativeOfF) + { + var coefficient = derivativeOfF.Substitute(t, constant).InnerSimplified; + return constant.Vars.Any() || coefficient.Evaled is Complex { IsNaN: false, IsFinite: true } + ? coefficient + : null; + } + } + /// /// The logarithm of the series, given the value of log(w). The leading term /// has to be found, since log of something that might be zero is not something @@ -331,6 +378,28 @@ internal AsymptoticSeries WithoutConstant() return Expand(antilog, w, logarithmOfW, order)?.Logarithm(logarithmOfW, order) is { } logarithm ? logarithm.Multiply(Constant((1 / MathS.Ln(@base)).InnerSimplified)) : null; + // Only reached for a function Gruntz.Mrv has found analytic where its argument ends up. + // https://github.com/asc-community/AngouriMath/issues/2690 + case Sinf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Sin, order); + case Cosf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Cos, order); + case Tanf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Tan, order); + case Cotanf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Cotan, order); + case Secantf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Sec, order); + case Cosecantf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Cosec, order); + case Arctanf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Arctan, order); + case Erff(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erf, order); + case Erfcf(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erfc, order); + case Erfif(var argument): + return Expand(argument, w, logarithmOfW, order)?.Through(MathS.Erfi, order); default: return null; } diff --git a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs index 2fd0a637a..d4c8c44ae 100644 --- a/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs +++ b/Sources/AngouriMath/Functions/Continuous/Limits/Gruntz/Gruntz.cs @@ -42,9 +42,12 @@ namespace AngouriMath.Functions.Algebra /// /// The scope here is the exp-log functions: what can be built from x and the rationals /// with the four operations, exp and log. That is the class the algorithm is proved for, - /// because those functions are eventually monotone and so comparable at all. Anything - /// else -- a sine, an unknown function, a factorial -- makes this decline rather than - /// guess, since sin(x) has no limit at infinity and no comparability class either. + /// because those functions are eventually monotone and so comparable at all. A sine, a + /// cosine, a tangent, the arctangent or an error function of an argument that settles at a + /// point where it is analytic is read as its Taylor series there, which puts it in its + /// argument's class. Anything else -- a sine of an argument that does not settle, an unknown + /// function, a factorial -- makes this decline rather than guess, since sin(x) has no limit at + /// infinity and no comparability class either. /// /// internal static class Gruntz @@ -86,9 +89,45 @@ private static void Trace(string what, object? value) if (expr.Nodes.Any(node => LimitFunctional.TakesItsValueOnACut(node, x, Real.PositiveInfinity))) return null; expr = AsExponentials(expr, x); + if (expr.Nodes.Any(IsAnalyticFunction) && !IsRealOnTheWay(expr, x)) + return null; return LimitInf(expr, x); } + private static bool IsAnalyticFunction(Entity node) + => node is Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Erff or Erfcf or Erfif; + + /// + /// Whether every logarithm in is of something eventually positive, and every power with an + /// exponent that is not whole of a base that is, as the algorithm's reading of them as real needs. + /// + /// + /// The cut check above reads the value at the destination, and a sine of a vanishing argument is 0 there + /// whichever side it comes from. Asked only of an expression holding one of the functions + /// reads through their Taylor series, which before were declined whole: x^x at 0 from the left is + /// (-1/x)^(-1/x) at +oo, which is not real, and under a sine it was answered sin(1), and + /// sin(x)^(x - 1) there +oo, where the library leaves a side that is not real unevaluated. + /// https://github.com/asc-community/AngouriMath/issues/2690 + /// + private static bool IsRealOnTheWay(Entity e, Variable x) + { + foreach (var node in e.Nodes) + { + var positive = node switch + { + Logf(var @base, var antilog) => (@base.ContainsNode(x) ? @base : null, antilog.ContainsNode(x) ? antilog : null), + Powf(var @base, var power) when @base != MathS.e && !power.ContainsNode(x) && power.Evaled is not Integer + => (@base.ContainsNode(x) ? @base : null, (Entity?)null), + _ => ((Entity?)null, (Entity?)null) + }; + if (positive.Item1 is { } first && Sign(first, x) is not 1) + return false; + if (positive.Item2 is { } second && Sign(second, x) is not 1) + return false; + } + return true; + } + private static Entity? LimitInf(Entity e, Variable x) { if (!e.ContainsNode(x)) @@ -276,9 +315,16 @@ private static bool IsInfinite(Entity e) case Logf(var @base, var antilog) when !@base.ContainsNode(x): return Mrv(antilog, x); + // A function analytic where its argument ends up is a power series in how far the argument still + // is from there, so it is in no class of its own: it is in its argument's. + // https://github.com/asc-community/AngouriMath/issues/2690 + case Sinf or Cosf or Tanf or Cotanf or Secantf or Cosecantf or Arctanf or Erff or Erfcf or Erfif + when AnalyticWhereItsArgumentEndsUp(e, x) is { } argument: + return Mrv(argument, x); + default: - // Anything else -- a sine, an unknown function, a factorial -- has no - // comparability class this knows how to place. + // Anything else -- a sine of an argument that does not settle, an unknown + // function, a factorial -- has no comparability class this knows how to place. return null; } @@ -286,6 +332,57 @@ private static bool IsInfinite(Entity e) => Mrv(left, x) is { } a && Mrv(right, x) is { } b ? MrvMax(a, b, x) : null; } + /// + /// The argument of a trigonometric function, the arctangent or an error function, where it tends to a finite + /// real number at which the function is analytic, or . + /// + /// + /// + /// The algorithm is proved for the exp-log functions, and nothing in it needs more of a function analytic at + /// the limit of its argument: f(u) with u tending to c is the Taylor series of f at + /// c in u - c, which vanishes, so f(u) is in no class faster than u's, and its + /// series is that Taylor series composed with the series of u. A sine whose argument runs off to + /// infinity has no limit and no class, and is still declined. + /// + /// + /// The tangent and secant are analytic off the zeros of the cosine, the cotangent and cosecant off the zeros of + /// the sine, and those are only asked at a limit whose cosine or sine is clear of 0; the arctangent and the error + /// functions are analytic on the whole real line. Without this, a cosine held this out of every limit at + /// +oo it appeared in, and of every limit at a finite point moved out to +oo with it: ln(x) cos(x) - ln(x) at 0 from the right, which is ln(1/x) (1 - cos(1/x)) + /// there and tends to 0, was left unevaluated, where with e^x in place of the cosine it was answered. + /// https://github.com/asc-community/AngouriMath/issues/2690 + /// + /// + private static Entity? AnalyticWhereItsArgumentEndsUp(Entity e, Variable x) + { + var argument = e switch + { + Sinf(var u) => u, + Cosf(var u) => u, + Tanf(var u) => u, + Cotanf(var u) => u, + Secantf(var u) => u, + Cosecantf(var u) => u, + Arctanf(var u) => u, + Erff(var u) => u, + Erfcf(var u) => u, + Erfif(var u) => u, + _ => null + }; + if (argument is null || !argument.ContainsNode(x)) + return null; + if (LimitInf(argument, x) is not { } limit + || Bare(limit).Evaled is not Real { IsFinite: true, IsNaN: false } at) + return null; + const double clearOfZero = 1e-12; + return e switch + { + Tanf or Secantf => System.Math.Abs(System.Math.Cos(at.EDecimal.ToDouble())) > clearOfZero ? argument : null, + Cotanf or Cosecantf => System.Math.Abs(System.Math.Sin(at.EDecimal.ToDouble())) > clearOfZero ? argument : null, + _ => argument + }; + } + private static HashSet? MrvMax(HashSet f, HashSet g, Variable x) { if (f.Count == 0) return g; diff --git a/Sources/Tests/UnitTests/Calculus/GruntzTest.cs b/Sources/Tests/UnitTests/Calculus/GruntzTest.cs index dc8d3e5ae..c219cc13b 100644 --- a/Sources/Tests/UnitTests/Calculus/GruntzTest.cs +++ b/Sources/Tests/UnitTests/Calculus/GruntzTest.cs @@ -75,6 +75,20 @@ public void WhatNothingBeforeItCouldReach(string expression, string destination, public void TheOrdinaryOnesAreUnchanged(string expression, string destination, string expected) => AssertLimit(expression, destination, expected); + /// + /// A function analytic where its argument settles is the Taylor series there in what the argument still has to + /// go, and is expanded as one: in w = e^(-x), 1 - cos(e^(-x)) is w^2/2 - w^4/24 + .... A + /// cosine anywhere in the expression held the algorithm out of it, and each of these was left unevaluated. The + /// last is the quartic term, where the cosine and the exponential agree to the square. + /// https://github.com/asc-community/AngouriMath/issues/2690 + /// + [Theory] + [InlineData("e ^ (2 * x) * (1 - cos(e ^ (-x)))", "+oo", "1/2")] + [InlineData("ln(x) * (cos(1/x) - 1)", "+oo", "0")] + [InlineData("x * (cos(1/x) - e ^ (-1/(2 * x ^ 2))) * x ^ 3", "+oo", "-1/12")] + public void AFunctionAnalyticWhereItsArgumentSettlesIsItsSeries(string expression, string destination, string expected) => + AssertLimit(expression, destination, expected); + /// /// The algorithm is stated for the exp-log functions -- what can be built from x and /// the rationals with the four operations, exp and log -- because those are eventually diff --git a/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs b/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs index 1419fdaac..180b1bca4 100644 --- a/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ImproperIntegralTest.cs @@ -63,23 +63,29 @@ public void ABoundWhereTheAntiderivativeIsUndefinedIsALimit(string integrand, st /// /// And as written where the limit is not decided: undefined would say the integral does not exist, and /// ln(x) erf(x) over [0, 1], whose antiderivative is an infinity less an infinity at 0 that no rule - /// settles, was NaN where it is -0.2613. The limit there is asked within a budget, and the simplification, + /// settled, was NaN where it is -0.2613. The limit there is asked within a budget, and the simplification, /// which asks it again for each spelling of the integrand it tries, ran past two minutes without one. /// https://github.com/asc-community/AngouriMath/issues/2081 /// + /// + /// The rows were ln(x) erf(x) and ln(x) Si(x) until both were answered + /// (, https://github.com/asc-community/AngouriMath/issues/2690), + /// so the rows are now integrands whose antiderivative's limit at 0 is still not decided. That of + /// ln(x) Si(2x) holds ln(x) x Si(2x) beside ln(x) cos(2x) - Ci(2x)/2, and Gruntz's algorithm, + /// which settles such an infinity less an infinity, has no series for the sine integral. That of + /// ln(x) arccos(x) holds the logarithm of a quotient that is negative on the whole of (0, 1), so is + /// not real there. Both were left as written before the change as well. + /// [Theory] - [InlineData("ln(x) * erf(x)")] - [InlineData("ln(x) * Si(x)")] + [InlineData("ln(x) * Si(2 * x)")] + [InlineData("ln(x) * arccos(x)")] public void ABoundWhoseLimitIsNotDecidedLeavesTheIntegralAsWritten(string integrand) { Entity? result = null; var task = System.Threading.Tasks.Task.Run(() => result = Definite(integrand, "0", "1")); // The budget bounds the work, in limits; this wall is a backstop against a hang that the budget no longer // bounds, not a measure of time. The erf integral took fourteen seconds alone, ten of them one limit spending - // the whole budget, and a wall of sixty fired in the full suite on a loaded machine; it takes about four - // since Ei's limit at 0 is read (https://github.com/asc-community/AngouriMath/issues/2661). Written as - // erf(x) ln(x) it is answered (see ABoundAtTheExponentialIntegralsSingularityIsItsLimit), but the limit - // through this spelling's antiderivative is still not decided, and Simplify keeps the integral as written. + // the whole budget, and a wall of sixty fired in the full suite on a loaded machine. Assert.True(task.Wait(System.TimeSpan.FromMinutes(5)), $"the integral of {integrand} from 0 to 1 was still being simplified"); Assert.IsType(result); Assert.DoesNotContain(result!.Nodes, node => node == MathS.NaN); @@ -102,6 +108,28 @@ public void ABoundAtTheExponentialIntegralsSingularityIsItsLimit() Assert.True((value - "-0.2613483600213989642354593897".ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {value}"); } + /// + /// The antiderivative of the logarithm times a function analytic at 0 holds the logarithm against that function at + /// 0, an infinity less an infinity there: cos(x) ln(x) - Ci(x) for the sine integral, ln(x) x erf(x) + /// beside e^(-x^2) ln(x) and Ei(-x^2) for the error function. Gruntz's algorithm declined any expression + /// holding a cosine or an error function, and each integral was left as written. Each value is mpmath's + /// quadrature to thirty digits, and agrees with the closed form to fifty. + /// https://github.com/asc-community/AngouriMath/issues/2690 + /// + [Theory] + [InlineData("ln(x) * Si(x)", "1 + eulergamma - Si(1) - Ci(1) - cos(1)", "-0.246573634234758006398424035072904601")] + [InlineData("ln(x) * erf(x)", "(1 - Ei(-1)/2 - 1/e + eulergamma/2)/sqrt(pi) - erf(1)", "-0.261348360021398964235459389701221541")] + [InlineData("ln(x) * erfi(x)", "(Ei(1)/2 - 1 + e - eulergamma/2)/sqrt(pi) - erfi(1)", "-0.309215716588680854824099892575843036")] + [InlineData("ln(x) * erfc(x)", "(Ei(-1)/2 - 1 + 1/e - eulergamma/2)/sqrt(pi) - erfc(1)", "-0.738651639978601035764540610298778459")] + [InlineData("ln(x) * sin(x)", "Ci(1) - eulergamma", "-0.239811742000564725943865886193251661")] + public void ALogarithmTimesAnAnalyticFunctionIsAnswered(string integrand, string closedForm, string value) + { + var result = Definite(integrand, "0", "1"); + Assert.False(result.Nodes.Any(node => node is Entity.Integralf or Entity.Limitf), result.Stringize()); + Assert.True((result - closedForm.ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {result}"); + Assert.True((result - value.ToEntity()).EvalNumerical().Abs() < 1e-25, $"was {result}"); + } + /// A bound the substitution answers is answered as it was. [Theory] [InlineData("e^(-x)", "0", "+oo", "1")] diff --git a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs index 4fed0a081..cdfb0fcc6 100644 --- a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs @@ -840,6 +840,35 @@ public void Issue2680_AQuestionIsNotAskedAgainInsideItself(string equation) Assert.IsNotType(task.Result); } + // lim x->0+ ln(x) cos(x) - ln(x) is 0: the logarithm times 1 - cos(x), which vanishes like x^2. Read at +oo, where + // a limit at a finite point is moved, it is ln(1/x) (1 - cos(1/x)), and Gruntz's algorithm, the only rule that + // settles an infinity less an infinity there, declined any expression holding a cosine. Written with e^x in + // place of the cosine it was answered. Checked against mpmath at x = 1e-30 to 1e-240 (and 1e30 to 1e240). + // https://github.com/asc-community/AngouriMath/issues/2690 + [Theory] + [InlineData("ln(x) * cos(x) - ln(x)", "0+", "0")] + [InlineData("ln(x) * cos(x) + ln(1/x)", "0+", "0")] + [InlineData("ln(x) * cos(x) - ln(x) + 1", "0+", "1")] + [InlineData("ln(x)^3 * sec(x) - ln(x)^3", "0+", "0")] + [InlineData("ln(x)^3 * (sin(x)/x - 1)", "0+", "0")] + [InlineData("ln(x) * cos(x) - ln(x) * e^x", "0+", "0")] + [InlineData("ln(x) * sec(x) - ln(x) * (1 + x)^(1/3)", "0+", "0")] + [InlineData("ln(x) * cos(1/x) - ln(x)", "+oo", "0")] + [InlineData("ln(x)^2 * sec(1/x) - ln(x)^2", "+oo", "0")] + public void Issue2690_ALogarithmTimesACosineLessTheLogarithmIsZero(string expression, string destination, string expected) + { + var (to, side) = destination.EndsWith("+") + ? (destination.TrimEnd('+').ToEntity(), AngouriMath.Core.ApproachFrom.Right) + : (destination.ToEntity(), AngouriMath.Core.ApproachFrom.BothSides); + Assert.Equal(expected.ToEntity(), expression.ToEntity().Limit("x", to, side)); + } + + // Two-sided, the same difference is still left unevaluated: left of 0 the logarithm is not real, which is the + // convention x ln(x) and x^x at 0 are held to. + [Fact] + public void Issue2690_TwoSidedTheConventionForALogarithmAtZeroStands() + => Assert.IsType("ln(x) * cos(x) - ln(x)".ToEntity().Limit("x", 0)); + private static readonly PeterO.Numbers.EContext Unfloored = new(100, PeterO.Numbers.ERounding.HalfUp, -100000, 100000, false); // Real parts, evaluated without the exponent floor, of roots each checked to be real.