|
| 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.Extensions; |
| 10 | +using Xunit; |
| 11 | +using static AngouriMath.Entity; |
| 12 | + |
| 13 | +namespace AngouriMath.Tests.Algebra |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// <c>Solve</c> of <c>f(x) in S</c>: the members of a listed <c>S</c> each as an equation, |
| 17 | + /// an interval as its bounds. It answered the <b>empty set</b> for every one of these, and |
| 18 | + /// for every statement it had no arm for -- a claim that no <c>x</c> exists, made about |
| 19 | + /// <c>x^2 in (0; 1)</c>. A statement the solver cannot read is the set of <c>x</c> with the |
| 20 | + /// property now, left as written. The reference's pre-images (Sullivan and Mackey, |
| 21 | + /// Ex 7.3.10, §7.3.5 Try 1) are this. |
| 22 | + /// <see href="https://github.com/asc-community/AngouriMath/issues/1409"/> |
| 23 | + /// </summary> |
| 24 | + [Trait("Area", "Algebra")] |
| 25 | + public sealed class MembershipSolvingTest |
| 26 | + { |
| 27 | + private static Set Solved(string statement) => statement.ToEntity().Solve("x"); |
| 28 | + |
| 29 | + /// <summary>Ex 7.3.10: the pre-images of <c>x^2</c>; the book prints <c>(-1, 1)</c> for the last, which is wrong at <c>0</c>.</summary> |
| 30 | + [Theory] |
| 31 | + [InlineData("x^2 in {1}", "{-1, 1}")] |
| 32 | + [InlineData("x^2 in {1, 4}", "{-2, -1, 1, 2}")] |
| 33 | + [InlineData("x^2 in (-oo; 0)", "{}")] |
| 34 | + [InlineData("x^2 in [0; +oo)", "RR")] |
| 35 | + [InlineData("x^2 in (0; 1)", "(-1; 0) \\/ (0; 1)")] |
| 36 | + [InlineData("x + 1 in [2; 3]", "[1; 2]")] |
| 37 | + [InlineData("2 x in (0; 4]", "(0; 2]")] |
| 38 | + public void APreImageIsSolvedThroughTheMembersOrTheBounds(string statement, string expected) |
| 39 | + { |
| 40 | + // Compared at points rather than as printed forms: the solver writes a union of |
| 41 | + // intervals in its own order, and the same set two ways is the same set. |
| 42 | + var solved = Solved(statement); |
| 43 | + var wanted = (Set)expected.ToEntity().Evaled; |
| 44 | + foreach (var at in new[] { -3.0, -2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0 }) |
| 45 | + { |
| 46 | + var point = (Entity)at; |
| 47 | + Assert.True(solved.TryContains(point, out var got), $"{solved} does not decide {at}"); |
| 48 | + Assert.True(wanted.TryContains(point, out var want), $"{wanted} does not decide {at}"); |
| 49 | + Assert.True(got == want, $"{statement} solved as {solved}: {at} is {(got ? "in" : "out")}, and should be {(want ? "in" : "out")}"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary>A statement with no arm is the set of <c>x</c> with the property, not the empty set.</summary> |
| 54 | + [Fact] |
| 55 | + public void WhatIsNotReadIsLeftAsWritten() |
| 56 | + { |
| 57 | + Assert.Equal("{ x : x^2 in ZZ }".ToEntity(), Solved("x^2 in ZZ")); |
| 58 | + Assert.Equal("{ x : x^2 in QQ }".ToEntity(), Solved("x^2 in QQ")); |
| 59 | + // And a quantifier that asked the solver no longer reads that emptiness as a proof: |
| 60 | + // forall x in RR : x^2 in ZZ was True. |
| 61 | + Assert.Equal(Boolean.False, "forall x in RR : x^2 in ZZ".ToEntity().Evaled); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary>The trigonometric equation's family is the pre-image of <c>{0}</c> under the sine.</summary> |
| 65 | + [Fact] |
| 66 | + public void AMemberEquationKeepsItsFamily() |
| 67 | + => Assert.Contains("n_1", Solved("sin(x) in {0}").Stringize()); |
| 68 | + } |
| 69 | +} |
0 commit comments