Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions Sources/AngouriMath/Functions/Continuous/Differentiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ internal Entity DifferentiateOnce(Variable variable)
/// <returns>
/// 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: <c>{ 1, 2 } + sin(x)</c> is the set <c>{ 1 + sin(x), 2 + sin(x) }</c>, and
/// its derivative is <c>{ cos(x) }</c>, which the derivative node reaches elementwise and
/// a zero would flatten to the number <c>cos(x)</c>.
/// </returns>
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;

/// <summary>
/// Whether the node is a constant in <paramref name="variable"/>, which the modulus, the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions Sources/Tests/UnitTests/Calculus/ModulusPhaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)")]
Expand Down
15 changes: 15 additions & 0 deletions Sources/Tests/UnitTests/Core/Sets/SubsetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ public void TheElevenStatementsAboutOneSet(string statement, string expected)
}

/// <summary>The nodes print as they parse, and the API builds them.</summary>
/// <summary>
/// <c>x subset 2</c> is not a statement about any <c>x</c>, since <c>2</c> is not a set: the
/// predicate is NaN and holds nowhere, so the set it describes is empty -- and
/// <c>Solve</c> answers the empty set rather than throwing, which the crash harness
/// found it doing when the NaN predicate made the whole set-builder NaN.
/// </summary>
[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()
{
Expand Down
Loading