|
| 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.Numerics; |
| 9 | +using AngouriMath; |
| 10 | +using AngouriMath.Core.Exceptions; |
| 11 | +using AngouriMath.Extensions; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace AngouriMath.Tests.Common |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A quantifier, a subset and a cardinality over a finite set compile, in FastExpression and in the LINQ compiler, to |
| 18 | + /// what evaluation gives: a quantifier as the connective of its body at each member, a subset as the memberships of |
| 19 | + /// its members, and a cardinality as the number of distinct values among them. Each was refused, and over any other |
| 20 | + /// set still is. |
| 21 | + /// <a href="https://github.com/asc-community/AngouriMath/issues/2195">#2195</a> |
| 22 | + /// </summary> |
| 23 | + [Trait("Area", "Common")] |
| 24 | + public sealed class FiniteSetStatementCompilationTest |
| 25 | + { |
| 26 | + // Not at NaN: see AtNaNTheValueIsUndecidedOrTheVacuousTruth. |
| 27 | + private static readonly string[] points = { "-1", "0", "1/2", "1", "2", "5/2", "3", "4", "i", "1 + i", "+oo", "-oo" }; |
| 28 | + |
| 29 | + private static Complex Numeric(string value) => ((Entity.Number.Complex)value.ToEntity().Evaled).ToNumerics(); |
| 30 | + |
| 31 | + private static Entity At(string expression, string point) => expression.ToEntity().Substitute("x", point); |
| 32 | + |
| 33 | + private static bool Same(Complex expected, Complex actual) |
| 34 | + => double.IsNaN(expected.Real) || double.IsNaN(expected.Imaginary) |
| 35 | + ? double.IsNaN(actual.Real) || double.IsNaN(actual.Imaginary) |
| 36 | + : expected == actual; |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData("forall t in { 1, 2, 3 } : t < x")] |
| 40 | + [InlineData("exists t in { 1, 2, 3 } : t = x")] |
| 41 | + [InlineData("exists! t in { 1, 2, 3 } : t > x")] |
| 42 | + [InlineData("forall t in { 1, 2 } : exists s in { 2, 3 } : s > t + x")] |
| 43 | + [InlineData("forall t in { } : t = x")] |
| 44 | + [InlineData("exists t in { } : t = x")] |
| 45 | + [InlineData("{ x } subset [0; 3]")] |
| 46 | + [InlineData("{ x, 2 } subset ZZ")] |
| 47 | + [InlineData("{ } subset { x }")] |
| 48 | + public void AStatementOverAFiniteSetIsWhatEvaluationSays(string statement) |
| 49 | + { |
| 50 | + var fast = statement.ToEntity().Compile("x"); |
| 51 | + var linq = statement.ToEntity().Compile<Complex, bool?>("x"); |
| 52 | + var compared = 0; |
| 53 | + foreach (var a in points) |
| 54 | + { |
| 55 | + var evaluated = At(statement, a).Evaled; |
| 56 | + if (evaluated is not Entity.Boolean && !evaluated.IsNaN) |
| 57 | + continue; |
| 58 | + bool? expected = evaluated is Entity.Boolean truth ? (bool)truth : null; |
| 59 | + var x = Numeric(a); |
| 60 | + var value = fast.Call(x); |
| 61 | + bool? fastTruth = value == 1 ? true : value == 0 ? false : null; |
| 62 | + Assert.True(expected == fastTruth, $"{statement} at {a}: {expected?.ToString() ?? "none"} evaluated, {value} by FastExpression"); |
| 63 | + Assert.True(expected == linq(x), $"{statement} at {a}: {expected?.ToString() ?? "none"} evaluated, {linq(x)?.ToString() ?? "none"} by LINQ"); |
| 64 | + compared++; |
| 65 | + } |
| 66 | + Assert.True(compared >= points.Length / 2, $"only {compared} of {statement} decided"); |
| 67 | + } |
| 68 | + |
| 69 | + [Theory] |
| 70 | + [InlineData("card({ 1, x })")] |
| 71 | + [InlineData("card({ 1, 2, x, x^2 })")] |
| 72 | + [InlineData("card({ })")] |
| 73 | + public void ACardinalityIsWhatEvaluationSays(string expression) |
| 74 | + { |
| 75 | + var fast = expression.ToEntity().Compile("x"); |
| 76 | + var linq = expression.ToEntity().Compile<Complex, Complex>("x"); |
| 77 | + var compared = 0; |
| 78 | + foreach (var a in points) |
| 79 | + { |
| 80 | + if (At(expression, a).Evaled is not Entity.Number.Complex evaluated) |
| 81 | + continue; |
| 82 | + var expected = evaluated.ToNumerics(); |
| 83 | + var x = Numeric(a); |
| 84 | + Assert.True(Same(expected, fast.Call(x)), $"{expression} at {a}: {expected} evaluated, {fast.Call(x)} by FastExpression"); |
| 85 | + Assert.True(Same(expected, linq(x)), $"{expression} at {a}: {expected} evaluated, {linq(x)} by LINQ"); |
| 86 | + compared++; |
| 87 | + } |
| 88 | + Assert.True(compared >= points.Length / 2, $"only {compared} of {expression} evaluated"); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// At NaN the compiled value is undecided, or the vacuous truth of a quantifier over the empty set, where |
| 93 | + /// evaluation differs: a finite set drops a member whose value is NaN, so <c>{ NaN }</c> is <c>{ }</c> and |
| 94 | + /// <c>card({ 1, NaN })</c> is 1, and a NaN anywhere in a quantifier's body makes the quantifier NaN. |
| 95 | + /// </summary> |
| 96 | + [Theory] |
| 97 | + [InlineData("{ x } subset [0; 3]", double.NaN)] |
| 98 | + [InlineData("card({ 1, x })", double.NaN)] |
| 99 | + [InlineData("forall t in { } : t = x", 1)] |
| 100 | + [InlineData("exists t in { } : t = x", 0)] |
| 101 | + public void AtNaNTheValueIsUndecidedOrTheVacuousTruth(string expression, double expected) |
| 102 | + { |
| 103 | + var value = expression.ToEntity().Compile("x").Call(double.NaN); |
| 104 | + Assert.True(double.IsNaN(expected) ? double.IsNaN(value.Real) : value == expected, $"{expression} at NaN: {value}"); |
| 105 | + } |
| 106 | + |
| 107 | + [Theory] |
| 108 | + [InlineData("forall t in [0; 1] : t < x")] |
| 109 | + [InlineData("exists t in ZZ : t = x")] |
| 110 | + [InlineData("{ x } subset QQ")] |
| 111 | + [InlineData("card([0; x])")] |
| 112 | + public void OverAnyOtherSetItIsRefused(string expression) |
| 113 | + => Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile("x")); |
| 114 | + } |
| 115 | +} |
0 commit comments