|
| 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 static AngouriMath.Entity; |
| 11 | +using static AngouriMath.Entity.Number; |
| 12 | +using static AngouriMath.Entity.Set; |
| 13 | + |
| 14 | +namespace AngouriMath.Functions.Algebra.AnalyticalSolving |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Equations in moduli: where one is real for every complex <c>x</c>, and how one in moduli of |
| 18 | + /// linear functions is solved over the reals. |
| 19 | + /// </summary> |
| 20 | + /// <remarks> |
| 21 | + /// Over the complex numbers <c>|x - 2| = |x - 3|</c> holds on the whole line |
| 22 | + /// <c>Re x = 5/2</c>: both sides are real whatever <c>x</c> is, so the equation is one real |
| 23 | + /// condition on the two real unknowns <c>x</c> is made of, and what solves it is a curve. A |
| 24 | + /// numerical search finds points of it and would answer them as the set. Over the reals the |
| 25 | + /// same equation is <c>x = 5/2</c>, found by cases: between two neighbouring kinks every |
| 26 | + /// modulus has one sign, and the equation there has no moduli. |
| 27 | + /// https://github.com/asc-community/AngouriMath/issues/1573 |
| 28 | + /// </remarks> |
| 29 | + internal static class ModulusSolver |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// Whether <paramref name="expr"/> is real for every complex <paramref name="x"/>, read off |
| 33 | + /// its structure: <paramref name="x"/> only ever inside a modulus, and around the moduli |
| 34 | + /// arithmetic with real numbers. A numerical probe could not tell this from a function |
| 35 | + /// that is real at the points it tried. |
| 36 | + /// </summary> |
| 37 | + internal static bool IsRealForEveryComplex(Entity expr, Variable x) |
| 38 | + => expr.ContainsNode(x) && RealWhereverItIs(expr, x); |
| 39 | + |
| 40 | + private static bool RealWhereverItIs(Entity expr, Variable x) |
| 41 | + => expr switch |
| 42 | + { |
| 43 | + _ when !expr.ContainsNode(x) => expr.Evaled is Real || expr.Evaled is Complex { ImaginaryPart.IsZero: true }, |
| 44 | + Absf => true, |
| 45 | + Sumf(var augend, var addend) => RealWhereverItIs(augend, x) && RealWhereverItIs(addend, x), |
| 46 | + Minusf(var minuend, var subtrahend) => RealWhereverItIs(minuend, x) && RealWhereverItIs(subtrahend, x), |
| 47 | + Mulf(var multiplier, var multiplicand) => RealWhereverItIs(multiplier, x) && RealWhereverItIs(multiplicand, x), |
| 48 | + Divf(var dividend, var divisor) => RealWhereverItIs(dividend, x) && RealWhereverItIs(divisor, x), |
| 49 | + Powf(var @base, Integer) => RealWhereverItIs(@base, x), |
| 50 | + _ => false, |
| 51 | + }; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// The real <paramref name="x"/> at which <paramref name="expr"/> is zero, where every |
| 55 | + /// modulus in it with <paramref name="x"/> inside is of a linear function with real |
| 56 | + /// coefficients; <see langword="null"/> otherwise, or where a piece is not solved. |
| 57 | + /// </summary> |
| 58 | + internal static Set? SolveOverTheReals(Entity expr, Variable x) |
| 59 | + { |
| 60 | + var moduli = new List<Entity>(); |
| 61 | + var kinks = new List<Real>(); |
| 62 | + foreach (var node in expr.Nodes) |
| 63 | + { |
| 64 | + if (node is not Absf(var argument) || !argument.ContainsNode(x)) |
| 65 | + continue; |
| 66 | + if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var intercept) |
| 67 | + || intercept.ContainsNode(x) || slope.Evaled is not Real { IsZero: false } |
| 68 | + || (-intercept / slope).InnerSimplified is not Real kink) |
| 69 | + return null; |
| 70 | + moduli.Add(argument); |
| 71 | + kinks.Add(kink); |
| 72 | + } |
| 73 | + if (moduli.Count == 0) |
| 74 | + return null; |
| 75 | + var ends = kinks.Distinct().OrderBy(kink => kink.EDecimal).ToList(); |
| 76 | + |
| 77 | + // The pieces between neighbouring kinks, closed where they end at one, and on each the |
| 78 | + // sign every modulus takes, read at a point inside it. |
| 79 | + Set answer = Set.Empty; |
| 80 | + for (var i = 0; i <= ends.Count; i++) |
| 81 | + { |
| 82 | + Entity left = i == 0 ? Real.NegativeInfinity : ends[i - 1]; |
| 83 | + Entity right = i == ends.Count ? Real.PositiveInfinity : ends[i]; |
| 84 | + Entity inside = i == 0 ? (Entity)ends[0] - 1 : i == ends.Count ? (Entity)ends[^1] + 1 : ((Entity)ends[i - 1] + ends[i]) / 2; |
| 85 | + var signs = new Dictionary<Entity, bool>(); |
| 86 | + foreach (var argument in moduli) |
| 87 | + if (!signs.ContainsKey(argument)) |
| 88 | + { |
| 89 | + if (argument.Substitute(x, inside).Evaled is not Real value) |
| 90 | + return null; |
| 91 | + signs[argument] = !value.IsNegative; |
| 92 | + } |
| 93 | + var withoutModuli = expr.Replace(node => node is Absf(var argument) && signs.TryGetValue(argument, out var nonNegative) |
| 94 | + ? (nonNegative ? argument : -argument) |
| 95 | + : node).InnerSimplified; |
| 96 | + Set piece = new Interval(left, i > 0, right, i < ends.Count); |
| 97 | + if (!withoutModuli.ContainsNode(x)) |
| 98 | + { |
| 99 | + if (withoutModuli.Evaled is Complex { IsZero: true }) |
| 100 | + answer = answer.Unite(piece); |
| 101 | + continue; |
| 102 | + } |
| 103 | + var solved = AnalyticalEquationSolver.Solve(withoutModuli, x); |
| 104 | + // An equation that holds everywhere on the piece has the whole piece for solutions: |
| 105 | + // |x - 1| + |x - 2| = 1 is every x from 1 to 2, the solver answering such an |
| 106 | + // identity with every number. |
| 107 | + if (solved == MathS.Sets.C) |
| 108 | + { |
| 109 | + answer = answer.Unite(piece); |
| 110 | + continue; |
| 111 | + } |
| 112 | + if (solved is not FiniteSet roots) |
| 113 | + return null; |
| 114 | + foreach (var root in roots) |
| 115 | + if (root.Evaled is Real value && Within(value, left, right)) |
| 116 | + answer = answer.Unite(new FiniteSet(root)); |
| 117 | + } |
| 118 | + return (Set)answer.InnerSimplified; |
| 119 | + } |
| 120 | + |
| 121 | + private static bool Within(Real value, Entity left, Entity right) |
| 122 | + => left.Evaled is Real low && right.Evaled is Real high |
| 123 | + && low.EDecimal.CompareTo(value.EDecimal) <= 0 && value.EDecimal.CompareTo(high.EDecimal) <= 0; |
| 124 | + } |
| 125 | +} |
0 commit comments