|
| 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.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using PeterO.Numbers; |
| 11 | +using static AngouriMath.Entity; |
| 12 | +using static AngouriMath.Entity.Number; |
| 13 | +using static AngouriMath.Entity.Set; |
| 14 | + |
| 15 | +namespace AngouriMath.Functions.Algebra |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// The points strictly inside a numeric range where an antiderivative can stop being |
| 19 | + /// continuous, so that a definite integral is taken through it piece by piece. |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// <para> |
| 23 | + /// <c>F(b) - F(a)</c> is the integral only where <c>F</c> is continuous on <c>[a, b]</c>. A pole |
| 24 | + /// of the integrand between the bounds is a point where <c>F</c> is not even defined: the |
| 25 | + /// integral of <c>1/x^2</c> over <c>[-1, 1]</c> came back as <c>-1/x</c> at 1 less <c>-1/x</c> |
| 26 | + /// at -1, which is <c>-2</c>, a negative answer for a positive integrand whose integral |
| 27 | + /// diverges. An antiderivative can also jump where the integrand is continuous, and the |
| 28 | + /// answer through it is then wrong by the jump: <c>arccot(x)</c> jumps at 0 in this library's |
| 29 | + /// convention, and a half-angle substitution writes <c>tan(x/2)</c>. |
| 30 | + /// </para> |
| 31 | + /// <para> |
| 32 | + /// The places read are where a node of the antiderivative stops being continuous: the zeros |
| 33 | + /// of a denominator, of a base raised to a negative power, of a logarithm's argument, of the |
| 34 | + /// argument of <c>sgn</c>, <c>arccot</c>, <c>arcsec</c> and <c>arccsc</c>, and the zeros of the |
| 35 | + /// cosine or the sine under a tangent, secant, cotangent or cosecant of a linear argument. |
| 36 | + /// Where one of them cannot be listed -- a root with a symbol in it, a periodic argument |
| 37 | + /// that is not linear, more breaks than <see cref="MostBreaks"/> -- the caller is told so, |
| 38 | + /// and integrates as it did before. |
| 39 | + /// </para> |
| 40 | + /// <para> |
| 41 | + /// A break is carried as its exact point, where the one-sided limits are taken, and its |
| 42 | + /// decimal value, which only orders the breaks and places them in the range. |
| 43 | + /// https://github.com/asc-community/AngouriMath/issues/1508 |
| 44 | + /// </para> |
| 45 | + /// </remarks> |
| 46 | + internal static class AntiderivativeBreaks |
| 47 | + { |
| 48 | + /// <summary> |
| 49 | + /// More breaks than this in one range are not listed: a tangent over a thousand |
| 50 | + /// periods has no integral to find, and listing its poles would be all the work. |
| 51 | + /// </summary> |
| 52 | + private const int MostBreaks = 64; |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// The points strictly between <paramref name="lower"/> and <paramref name="upper"/>, |
| 56 | + /// ascending, where <paramref name="antiderivative"/> may break, or |
| 57 | + /// <see langword="null"/> where one of them cannot be listed. |
| 58 | + /// </summary> |
| 59 | + internal static List<(Entity At, Real Value)>? Inside(Entity antiderivative, Variable x, Real lower, Real upper) |
| 60 | + { |
| 61 | + var breaks = new List<(Entity At, Real Value)>(); |
| 62 | + // A subexpression repeated in the antiderivative is read once. |
| 63 | + var read = new HashSet<Entity>(); |
| 64 | + foreach (var node in antiderivative.Nodes) |
| 65 | + { |
| 66 | + if (!read.Add(node)) |
| 67 | + continue; |
| 68 | + var listed = node switch |
| 69 | + { |
| 70 | + Divf(_, var denominator) when denominator.ContainsNode(x) => ZerosOf(denominator, x, lower, upper, breaks), |
| 71 | + Powf(var @base, Real { IsNegative: true }) when @base.ContainsNode(x) => ZerosOf(@base, x, lower, upper, breaks), |
| 72 | + Logf(_, var antilogarithm) when antilogarithm.ContainsNode(x) => ZerosOf(antilogarithm, x, lower, upper, breaks), |
| 73 | + Signumf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks), |
| 74 | + Arccotanf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks), |
| 75 | + Arcsecantf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks), |
| 76 | + Arccosecantf(var argument) when argument.ContainsNode(x) => ZerosOf(argument, x, lower, upper, breaks), |
| 77 | + Tanf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks), |
| 78 | + Secantf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks), |
| 79 | + Cotanf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks), |
| 80 | + Cosecantf(var argument) when argument.ContainsNode(x) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks), |
| 81 | + _ => true |
| 82 | + }; |
| 83 | + if (!listed || breaks.Count > MostBreaks) |
| 84 | + return null; |
| 85 | + } |
| 86 | + breaks.Sort((a, b) => a.Value.CompareTo(b.Value)); |
| 87 | + // The same point from two nodes -- a pole of -ln(cos(x)) is also a zero of the |
| 88 | + // cosine's -- is one break. |
| 89 | + var distinct = new List<(Entity At, Real Value)>(); |
| 90 | + foreach (var point in breaks) |
| 91 | + if (distinct.Count == 0 || !Coincide(distinct[distinct.Count - 1].Value, point.Value)) |
| 92 | + distinct.Add(point); |
| 93 | + return distinct; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Adds the real zeros of <paramref name="expression"/> strictly inside the range, and |
| 98 | + /// says whether they could all be listed. A cosine or a sine is zero on a periodic set, |
| 99 | + /// which the solver writes with an integer parameter, so it is listed across the range |
| 100 | + /// instead: <c>-ln(cos(x))</c>, the antiderivative of <c>tan(x)</c>, has no tangent in it |
| 101 | + /// to read. The modulus and a positive power are zero where what they hold is, a product |
| 102 | + /// where one of its factors is, and a quotient where its numerator is. |
| 103 | + /// </summary> |
| 104 | + private static bool ZerosOf(Entity expression, Variable x, Real lower, Real upper, List<(Entity At, Real Value)> breaks) |
| 105 | + => expression switch |
| 106 | + { |
| 107 | + Cosf(var argument) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: true, breaks), |
| 108 | + Sinf(var argument) => PeriodicZeros(argument, x, lower, upper, halfPeriodShift: false, breaks), |
| 109 | + Absf(var inner) => ZerosOf(inner, x, lower, upper, breaks), |
| 110 | + Powf(var inner, Real { IsPositive: true }) => ZerosOf(inner, x, lower, upper, breaks), |
| 111 | + Mulf(var left, var right) => |
| 112 | + (!left.ContainsNode(x) || ZerosOf(left, x, lower, upper, breaks)) |
| 113 | + && (!right.ContainsNode(x) || ZerosOf(right, x, lower, upper, breaks)), |
| 114 | + // A quotient is zero where its numerator is; its denominator is read as one. |
| 115 | + Divf(var numerator, _) => !numerator.ContainsNode(x) || ZerosOf(numerator, x, lower, upper, breaks), |
| 116 | + _ => SolvedZeros(expression, x, lower, upper, breaks) |
| 117 | + }; |
| 118 | + |
| 119 | + /// <summary>The real zeros the solver lists for <paramref name="expression"/>, strictly inside the range.</summary> |
| 120 | + private static bool SolvedZeros(Entity expression, Variable x, Real lower, Real upper, List<(Entity At, Real Value)> breaks) |
| 121 | + { |
| 122 | + if (expression.SolveEquation(x) is not FiniteSet roots) |
| 123 | + return false; |
| 124 | + foreach (var root in roots) |
| 125 | + { |
| 126 | + if (root.ContainsNode(x) || root.Vars.Any()) |
| 127 | + return false; |
| 128 | + switch (root.Evaled) |
| 129 | + { |
| 130 | + case Real { EDecimal.IsFinite: true } value: |
| 131 | + if (value > lower && value < upper) |
| 132 | + breaks.Add((root, value)); |
| 133 | + break; |
| 134 | + case Complex { ImaginaryPart.IsZero: false }: |
| 135 | + break; |
| 136 | + default: |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Adds the zeros of the cosine (<paramref name="halfPeriodShift"/>) or the sine of a |
| 145 | + /// linear argument strictly inside the range: where <c>a x + b</c> is <c>pi/2 + k pi</c>, |
| 146 | + /// or <c>k pi</c>, for a whole <c>k</c>. |
| 147 | + /// </summary> |
| 148 | + private static bool PeriodicZeros(Entity argument, Variable x, Real lower, Real upper, bool halfPeriodShift, List<(Entity At, Real Value)> breaks) |
| 149 | + { |
| 150 | + if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var a, out var b) |
| 151 | + || a.Evaled is not Real { EDecimal.IsFinite: true, IsZero: false } slope |
| 152 | + || b.Evaled is not Real { EDecimal.IsFinite: true } intercept |
| 153 | + || MathS.pi.Evaled is not Real pi) |
| 154 | + return false; |
| 155 | + if (!lower.EDecimal.IsFinite || !upper.EDecimal.IsFinite) |
| 156 | + return false; |
| 157 | + // x = (c + k pi - b)/a, so k runs over the whole numbers between the ends mapped back. |
| 158 | + var shift = halfPeriodShift ? pi / 2 : Integer.Zero; |
| 159 | + var atLower = (slope * lower + intercept - shift) / pi; |
| 160 | + var atUpper = (slope * upper + intercept - shift) / pi; |
| 161 | + var (from, to) = atLower < atUpper ? (atLower, atUpper) : (atUpper, atLower); |
| 162 | + var first = from.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Floor)).ToEInteger(); |
| 163 | + var last = to.EDecimal.RoundToIntegerNoRoundedFlag(EContext.CliDecimal.WithRounding(ERounding.Ceiling)).ToEInteger(); |
| 164 | + if ((last - first).CompareTo(EInteger.FromInt32(MostBreaks)) > 0) |
| 165 | + return false; |
| 166 | + for (var k = first; k.CompareTo(last) <= 0; k += EInteger.One) |
| 167 | + { |
| 168 | + var at = ((halfPeriodShift ? MathS.pi / 2 : Integer.Zero) + Integer.Create(k) * MathS.pi - b) / a; |
| 169 | + if (at.InnerSimplified.Evaled is Real { EDecimal.IsFinite: true } value && value > lower && value < upper) |
| 170 | + breaks.Add((at.InnerSimplified, value)); |
| 171 | + } |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Two decimal values of the same point, reached by different exact spellings. |
| 177 | + /// </summary> |
| 178 | + private static bool Coincide(Real a, Real b) |
| 179 | + => a.EDecimal.Subtract(b.EDecimal).Abs().CompareTo(EDecimal.Create(1, -30)) < 0; |
| 180 | + } |
| 181 | +} |
0 commit comments