|
| 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 AngouriMath; |
| 9 | +using AngouriMath.Core; |
| 10 | +using static AngouriMath.Entity; |
| 11 | + |
| 12 | +namespace AngouriMath.Mcp; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Answers two questions the raw library does not: "what should I watch out for in this |
| 16 | +/// expression?" and "is this answer real, or merely correct?". |
| 17 | +/// |
| 18 | +/// AngouriMath has no step engine — there is no derivation output anywhere in the library — |
| 19 | +/// so the honest substitute is to check a chain of working the caller supplies, and say |
| 20 | +/// which step broke. See <see cref="CheckSteps"/>. |
| 21 | +/// </summary> |
| 22 | +internal static class Analysis |
| 23 | +{ |
| 24 | + // Spread across the real line, including negatives and a point near zero, because the |
| 25 | + // whole point here is to FIND the places an expression stops being real or defined. |
| 26 | + private static readonly Entity[] Probes = |
| 27 | + ["-2.7", "-1.3", "-0.4", "0.6", "1.9", "3.2"]; |
| 28 | + |
| 29 | + public sealed record Caution(string Where, string Risk); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Structural hazards, found by walking the tree. These are the classic ways an |
| 33 | + /// expression means less than it appears to. |
| 34 | + /// </summary> |
| 35 | + public static List<Caution> Hazards(Entity e) |
| 36 | + { |
| 37 | + var found = new List<Caution>(); |
| 38 | + |
| 39 | + foreach (var node in e.Nodes) |
| 40 | + { |
| 41 | + switch (node) |
| 42 | + { |
| 43 | + case Divf: |
| 44 | + var divisor = node.DirectChildren.Count > 1 |
| 45 | + ? node.DirectChildren[1].Stringize() |
| 46 | + : "?"; |
| 47 | + found.Add(new Caution(node.Stringize(), |
| 48 | + $"division: undefined where {divisor} = 0. Any cancellation of this " + |
| 49 | + "term silently widens the domain unless a `provided` guard survives.")); |
| 50 | + break; |
| 51 | + |
| 52 | + case Logf: |
| 53 | + found.Add(new Caution(node.Stringize(), |
| 54 | + "logarithm: real only for a positive argument. Combining " + |
| 55 | + "ln(a)+ln(b) into ln(a*b) widens the domain and is a documented " + |
| 56 | + "source of extraneous roots.")); |
| 57 | + break; |
| 58 | + |
| 59 | + case Powf pow when pow.Exponent is not Number.Integer: |
| 60 | + found.Add(new Caution(node.Stringize(), |
| 61 | + "fractional or symbolic power: the principal branch is taken, so " + |
| 62 | + "(-8)^(1/3) is a complex number, not -2. sqrt(x^2) is abs(x), not x.")); |
| 63 | + break; |
| 64 | + |
| 65 | + case Arcsinf or Arccosf: |
| 66 | + found.Add(new Caution(node.Stringize(), |
| 67 | + "inverse trig: real only for an argument in [-1, 1], and the " + |
| 68 | + "principal branch is returned — the full solution family is periodic.")); |
| 69 | + break; |
| 70 | + |
| 71 | + case Factorialf: |
| 72 | + found.Add(new Caution(node.Stringize(), |
| 73 | + "factorial: defined via the gamma function, so it returns values for " + |
| 74 | + "non-integers and poles at the negative integers.")); |
| 75 | + break; |
| 76 | + |
| 77 | + case Absf: |
| 78 | + found.Add(new Caution(node.Stringize(), |
| 79 | + "absolute value: not differentiable at 0, and it hides a sign change " + |
| 80 | + "that matters when solving.")); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return found; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary>Domain guards AngouriMath itself worked out, pulled out of the tree.</summary> |
| 89 | + public static List<string> Conditions(Entity simplified) => |
| 90 | + simplified.Nodes |
| 91 | + .OfType<Providedf>() |
| 92 | + .Select(p => p.Predicate.Stringize()) |
| 93 | + .Distinct() |
| 94 | + .ToList(); |
| 95 | + |
| 96 | + public sealed record RealityReport( |
| 97 | + List<string> ComplexAt, List<string> UndefinedAt, int Sampled); |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Where does this expression stop being a real number? A result can be perfectly |
| 101 | + /// correct and still be useless as a length, a resistance or a time. |
| 102 | + /// </summary> |
| 103 | + public static RealityReport Reality(Entity e) |
| 104 | + { |
| 105 | + var complexAt = new List<string>(); |
| 106 | + var undefinedAt = new List<string>(); |
| 107 | + var variables = e.Vars.ToArray(); |
| 108 | + var sampled = 0; |
| 109 | + |
| 110 | + // With no free variables there is a single value to inspect. |
| 111 | + if (variables.Length == 0) |
| 112 | + { |
| 113 | + Inspect(e, "(constant)"); |
| 114 | + return new RealityReport(complexAt, undefinedAt, 1); |
| 115 | + } |
| 116 | + |
| 117 | + foreach (var probe in Probes) |
| 118 | + { |
| 119 | + var substituted = e; |
| 120 | + foreach (var variable in variables) |
| 121 | + substituted = substituted.Substitute(variable, probe); |
| 122 | + |
| 123 | + sampled++; |
| 124 | + Inspect(substituted, string.Join(" = ", variables.Select(v => v.Stringize())) + |
| 125 | + " = " + probe.Stringize()); |
| 126 | + } |
| 127 | + |
| 128 | + return new RealityReport(complexAt, undefinedAt, sampled); |
| 129 | + |
| 130 | + void Inspect(Entity at, string label) |
| 131 | + { |
| 132 | + try |
| 133 | + { |
| 134 | + if (!at.EvaluableNumerical) { undefinedAt.Add(label); return; } |
| 135 | + |
| 136 | + var value = at.EvalNumerical(); |
| 137 | + var real = value.RealPart.EDecimal.ToDouble(); |
| 138 | + var imaginary = value.ImaginaryPart.EDecimal.ToDouble(); |
| 139 | + |
| 140 | + if (double.IsNaN(real) || double.IsNaN(imaginary)) undefinedAt.Add(label); |
| 141 | + else if (Math.Abs(imaginary) > 1e-12) complexAt.Add(label); |
| 142 | + } |
| 143 | + catch |
| 144 | + { |
| 145 | + undefinedAt.Add(label); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary>Force an expression to be real-valued: anything complex becomes NaN.</summary> |
| 151 | + public static Entity AsReal(Entity e) => |
| 152 | + e.Replace(node => node.WithCodomain(Domain.Real)); |
| 153 | + |
| 154 | + public sealed record StepCheck( |
| 155 | + int Index, string From, string To, bool? Equal, string Method, string? Difference); |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Check a chain of working, one step at a time, and report the first step that changes |
| 159 | + /// the value. |
| 160 | + /// |
| 161 | + /// This is the useful form of "show me the steps". The library cannot produce a |
| 162 | + /// derivation — but a model is perfectly good at PROPOSING one and unreliable at |
| 163 | + /// EXECUTING it, so having it write the steps and having this check each transition |
| 164 | + /// puts each side on the task it is actually good at. A wrong step is located, not just |
| 165 | + /// detected. |
| 166 | + /// </summary> |
| 167 | + public static List<StepCheck> CheckSteps(List<Entity> steps) |
| 168 | + { |
| 169 | + var checks = new List<StepCheck>(); |
| 170 | + |
| 171 | + for (var i = 1; i < steps.Count; i++) |
| 172 | + { |
| 173 | + var from = steps[i - 1]; |
| 174 | + var to = steps[i]; |
| 175 | + |
| 176 | + Entity difference; |
| 177 | + try { difference = (from - to).Simplify(); } |
| 178 | + catch { difference = from - to; } |
| 179 | + |
| 180 | + if (Numeric.IsZero(difference)) |
| 181 | + { |
| 182 | + checks.Add(new StepCheck(i, from.Stringize(), to.Stringize(), |
| 183 | + true, "exact", null)); |
| 184 | + continue; |
| 185 | + } |
| 186 | + |
| 187 | + var numerically = Numeric.Equal(difference, 0); |
| 188 | + checks.Add(new StepCheck( |
| 189 | + i, from.Stringize(), to.Stringize(), |
| 190 | + numerically, |
| 191 | + numerically is null ? "inconclusive" : "numeric", |
| 192 | + numerically == true ? null : difference.Stringize())); |
| 193 | + } |
| 194 | + |
| 195 | + return checks; |
| 196 | + } |
| 197 | +} |
0 commit comments