diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 79fe4b478..68ec92506 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -870,6 +870,20 @@ prime, `17` ([#1450](https://github.com/asc-community/AngouriMath/issues/1450)). | `"prime(25)".ToEntity().Evaled` | `prime * 25` — juxtaposition of a variable `prime` | `97`; `prime(n)` is the `n`-th prime, `NaN` off the positive whole numbers | | `"valuation(12, 2)".ToEntity().Evaled` | `UnrecognizedFunctionParseException` | `2`; `valuation(n, p)` is the `p`-adic valuation, `+oo` at `0` and `NaN` off the primes | +### A set-builder whose predicate is `NaN` is the empty set, and was `NaN` + +`{ x : 1/0 = x }` evaluated to `NaN`, and so did `{ x : x subset 2 }` once `subset` became a +keyword — a subset of a number is not a statement about any `x`. A predicate that is `NaN` holds +nowhere, so the set it describes is empty; the solver already answered `{ }` for `1/0 = x`, and +`Solve` on `x subset 2` cast the `NaN` set to a set and threw an `InvalidCastException`, which +the crash harness found. Both now agree with the solver +([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"{ x : 1/0 = x }".ToEntity().Evaled` | `NaN` | `{ }` | +| `"x subset 2".ToEntity().Solve("x")` | `x * subset ^ 2` had no solution: `{ }` — `subset` is new since | `{ }`, where the unreleased keyword threw | + ### `binomial(n, k)` is a function **Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled diff --git a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs index 8db344553..fa42dce6f 100644 --- a/Sources/AngouriMath/Functions/Continuous/Differentiation.cs +++ b/Sources/AngouriMath/Functions/Continuous/Differentiation.cs @@ -74,10 +74,13 @@ internal Entity DifferentiateOnce(Variable variable) /// /// The differentiated expression, or the Derivative node -- and zero for a node with no /// rule of its own that does not mention the variable: it is a constant in it, and the - /// derivative of a constant is not an open question. + /// derivative of a constant is not an open question. A set is not a constant, whatever + /// it mentions: { 1, 2 } + sin(x) is the set { 1 + sin(x), 2 + sin(x) }, and + /// its derivative is { cos(x) }, which the derivative node reaches elementwise and + /// a zero would flatten to the number cos(x). /// protected virtual Entity InnerDifferentiate(Variable variable) - => ContainsNode(variable) ? new Derivativef(this, variable, 1) : Integer.Zero; + => ContainsNode(variable) || this is Set ? new Derivativef(this, variable, 1) : Integer.Zero; /// /// Whether the node is a constant in , which the modulus, the diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs index a2ab797b6..8d4ead2cb 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Omni/Evaluation.Omni.Classes.cs @@ -88,6 +88,10 @@ protected override Entity InnerSimplify(bool isExact) return core.Evaled switch { Boolean(false) => Empty, + // A predicate that is not a statement at all -- `x subset 2`, a subset + // of a number -- is NaN, and holds nowhere; left in the set it would + // make the set NaN, and `Solve` then cast a number to a set and threw. + Number { IsNaN: true } => Empty, Boolean(true) when condition != Boolean.True => New(Var, condition), // No set names every value a symbol could take, so a predicate that // holds everywhere is left as written rather than asking for the set of diff --git a/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs b/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs index 285d45259..74d6cf1b7 100644 --- a/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs +++ b/Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs @@ -136,6 +136,17 @@ public void TheDerivativeOfAModulusNotShownRealIsLeftUnevaluated(string expr) public void AConstantInTheVariableDifferentiatesToZeroWhateverItsNode(string expr, string expected) => Assert.Equal(expected.ToEntity().Simplify(), expr.ToEntity().Differentiate(x).Simplify()); + // ...and a set is not a constant in the variable even when it does not mention it: + // { 1, 2 } + sin(x) is the set { 1 + sin(x), 2 + sin(x) }, whose derivative is the set + // { cos(x) }, which the zero of the rule above flattened to the number cos(x) -- the + // wiki's Differentiation page says { cos(x) }, and docsamples read the number. + [Theory] + [InlineData("{ 1, 2 } + sin(x)", "{ cos(x) }")] + [InlineData("{ 1, 2 }", "{ 0 }")] + [InlineData("[1, 2] * x", "[1, 2]")] + public void ASetDifferentiatesElementwiseNotToZero(string expr, string expected) + => Assert.Equal(expected.ToEntity(), expr.ToEntity().Differentiate(x).Simplify()); + [Theory] [InlineData("abs(i * x)", "abs(x)")] [InlineData("abs(x * i)", "abs(x)")] diff --git a/Sources/Tests/UnitTests/Core/Sets/SubsetTest.cs b/Sources/Tests/UnitTests/Core/Sets/SubsetTest.cs index 20c62ffe2..cddb63971 100644 --- a/Sources/Tests/UnitTests/Core/Sets/SubsetTest.cs +++ b/Sources/Tests/UnitTests/Core/Sets/SubsetTest.cs @@ -193,6 +193,21 @@ public void TheElevenStatementsAboutOneSet(string statement, string expected) } /// The nodes print as they parse, and the API builds them. + /// + /// x subset 2 is not a statement about any x, since 2 is not a set: the + /// predicate is NaN and holds nowhere, so the set it describes is empty -- and + /// Solve answers the empty set rather than throwing, which the crash harness + /// found it doing when the NaN predicate made the whole set-builder NaN. + /// + [Theory] + [InlineData("x subset 2")] + [InlineData("1/0 = x")] + public void APredicateThatIsNaNHoldsNowhere(string statement) + { + Assert.Equal(Empty, statement.ToEntity().Solve("x")); + Assert.Equal(Empty, $"{{ x : {statement} }}".ToEntity().Evaled); + } + [Fact] public void PrintingAndTheApi() {