Skip to content
Open
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
23 changes: 23 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,29 @@ already did. With downcasting on, nothing changes ([#2622](https://github.com/as
| `(-30.5)^(1/3)` | `1.562… + 2.705…i` | `-3.124…` |
| `(-30.5)^(1/2)` | `5.522…i` | unchanged |

### A union or a product that opens with a set difference or a mod prints in brackets

**Silent**, in what is printed. A union printed its right operand flat when that operand was a
union, and a product its right operand when it was a product or a quotient, because both operators
are associative. But `\` shares a precedence level with `\/`, and `mod` with `*` and `/`, and the
parser folds each level to the left, so an operand that *opens* with one of them was read back
with the outer operator inside it: the printed form named a different set or number. The entity
was always right; `Stringize` and, for unions, `Latexize` now bracket such an operand.
[#2758](https://github.com/asc-community/AngouriMath/issues/2758)

| Expression | Before (`578680eed`) | Now |
|---|---|---|
| `A \/ ((B \ C) \/ D)` | `A \/ B \ C \/ D`, read back as `((A \/ B) \ C) \/ D` | `A \/ (B \ C \/ D)` |
| `{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })` | `{ 1 } \/ { 2 } \ { 1 } \/ { 3 }`, which is `{ 2, 3 }` | `{ 1 } \/ ({ 2 } \ { 1 } \/ { 3 })` |
| Simplify of `({ -1, 1 } \ [-2; +oo)) \/ (QQ \/ { pi })` | `{ pi } \/ { -1, 1 } \ [-2; +oo) \/ QQ`, which has no `pi` | `{ pi } \/ ({ -1, 1 } \ [-2; +oo) \/ QQ)` |
| `Latexize` of `A \/ ((B \ C) \/ D)` | `A \cup B \setminus C \cup D` | `A \cup \left(B \setminus C \cup D\right)` |
| `2 * ((3 mod 2) * 4)` | `2 * 3 mod 2 * 4`, which is 0 | `2 * (3 mod 2 * 4)`, which is 8 |
| `x * ((y mod z) / w)` | `x * y mod z / w` | `x * (y mod z / w)` |
| `-((y mod z) * w)` | `-y mod z * w`, read as `((-y) mod z) * w` | `-(y mod z * w)` |

A right operand made only of unions, or only of products and quotients, still prints flat. The LaTeX
of a product already bracketed each `\bmod` factor and is unchanged.

### A singular matrix is not inverted because its determinant is not written as 0

**Not silent: a wrong answer is now no answer.** `Matrix.Inverse`, `Matrix.Pow` with a negative exponent, `M^(-1)` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ partial record Unionf
// CSharpMath.Evaluation reads \cup and \setminus at one precedence level and folds
// them to the left, exactly as the grammar here does, so a \setminus on the right
// needs bracketing or it comes back as the outer operator. Union with union is
// associative and stays flat.
// associative and stays flat -- unless it opens with a \setminus, which the flat
// form would hand the outer union to (#2758).
private protected override string LatexizeNode()
=> $@"{Left.Latexize(Left.LatexPriority < LatexPriority)} \cup {Right.Latexize(Right.LatexPriority < LatexPriority || Right is SetMinusf)}";
=> $@"{Left.Latexize(Left.LatexPriority < LatexPriority)} \cup {Right.Latexize(Right.LatexPriority < LatexPriority || Right.LatexPriority == LatexPriority && Right.OpensWithANonAssociativeOperator(latex: true))}";
}

partial record Intersectionf
Expand Down
36 changes: 36 additions & 0 deletions Sources/AngouriMath/Functions/Output/ToString.Definition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,41 @@ private string StringizeAsWritten()
protected internal string Stringize(bool parenthesesRequired) =>
(parenthesesRequired && !PrintsItsCodomain) || MathS.Diagnostic.OutputExplicit
? $"({Stringize()})" : Stringize();

/// <summary>
/// Whether this, printed flat, would open with a <c>\</c> or a <c>mod</c> at its own
/// precedence level -- the non-associative operators that share a level with the
/// associative <c>\/</c> and <c>*</c>.
/// </summary>
/// <remarks>
/// An associative operator prints an operand of its own level on the right without
/// brackets, and the grammar folds the flat form to the left. That re-associates only the
/// operators along the operand's left edge, the ones printed without brackets of their
/// own, so it is sound exactly when none of them is a <c>\</c> or a <c>mod</c>.
/// <c>A \/ ((B \ C) \/ D)</c> printed as <c>A \/ B \ C \/ D</c> came back as
/// <c>((A \/ B) \ C) \/ D</c>, and <c>2 * ((3 mod 2) * 4)</c> printed as
/// <c>2 * 3 mod 2 * 4</c> came back as 0 where it is 8.
/// https://github.com/asc-community/AngouriMath/issues/2758
/// </remarks>
internal bool OpensWithANonAssociativeOperator(bool latex = false)
{
var level = latex ? LatexPriority : Priority;
Entity? leftmost = this switch
{
Set.SetMinusf or Modf => null,
Set.Unionf union => union.Left,
// -x prints as a unary minus on its own operand, which brackets itself if it opens with a mod.
Mulf(Integer(-1), _) when !MathS.Diagnostic.OutputExplicit => this,
Mulf product => product.Multiplier,
Divf quotient => quotient.Dividend,
_ => this
};
if (leftmost is null)
return true;
if (ReferenceEquals(leftmost, this) || leftmost.PrintsItsCodomain)
return false;
return (latex ? leftmost.LatexPriority : leftmost.Priority) == level
&& leftmost.OpensWithANonAssociativeOperator(latex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public partial record Mulf
// Multiplication is associative, so a product or a quotient on the right may stay
// unbracketed -- but `mod` shares this precedence level and is not associative, so a
// `mod` on the right must be bracketed or it is re-read as the outer operator:
// `2 * (3 mod 2)` is 2 and `(2 * 3) mod 2` is 0.
// `2 * (3 mod 2)` is 2 and `(2 * 3) mod 2` is 0. So must a product or a quotient that
// opens with one: `2 * ((3 mod 2) * 4)` is 8, and `2 * 3 mod 2 * 4` is 0. #2758
private protected override string StringizeNode()
{
var multiplicand = Multiplicand.Stringize(Multiplicand.Priority < Priority || Multiplicand is Modf);
var multiplicand = Multiplicand.Stringize(Multiplicand.Priority < Priority || Multiplicand.Priority == Priority && Multiplicand.OpensWithANonAssociativeOperator());
if (Multiplier is not Integer(-1) || MathS.Diagnostic.OutputExplicit)
return Multiplier.Stringize(Multiplier.Priority < Priority) + " * " + multiplicand;
// A negation of something printed with a sign of its own is bracketed: -(-2/3), not --2/3, which is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ partial record Unionf
// and `\` share one precedence level and are folded by one loop in the grammar, so
// a *set difference* on the right must be bracketed or it is re-read as the outer
// operator: `{ 1, 2 } \/ ({ 3 } \ { 1, 2 })` is { 1, 2, 3 } and
// `({ 1, 2 } \/ { 3 }) \ { 1, 2 }` is { 3 }.
// `({ 1, 2 } \/ { 3 }) \ { 1, 2 }` is { 3 }. So must a union that *opens* with a set
// difference: `A \/ ((B \ C) \/ D)` flat is `((A \/ B) \ C) \/ D`, and
// `{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })` would lose its 1. #2758
private protected override string StringizeNode()
=> $@"{Left.Stringize(Left.Priority < Priority)} \/ {Right.Stringize(Right.Priority < Priority || Right is SetMinusf)}";
=> $@"{Left.Stringize(Left.Priority < Priority)} \/ {Right.Stringize(Right.Priority < Priority || Right.Priority == Priority && Right.OpensWithANonAssociativeOperator())}";
/// <inheritdoc/>
public override string ToString() => Stringize();
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Tests/UnitTests/Convenience/LatexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ [Fact] public void SetMinusOfUnionOnTheRight()
=> Test($@"{As} \setminus \left({Bs} \cup {Cs}\right)", A.SetSubtract(B.Unite(C)));
[Fact] public void UnionOfSetMinusOnTheRight()
=> Test($@"{As} \cup \left({Bs} \setminus {Cs}\right)", A.Unite(B.SetSubtract(C)));
// https://github.com/asc-community/AngouriMath/issues/2758: a union on the right that
// opens with a \setminus re-associates the \setminus, not only the union.
[Fact] public void UnionOfAUnionOpeningWithSetMinusOnTheRight()
=> Test($@"{As} \cup \left({Bs} \setminus {Cs} \cup {As}\right)", A.Unite(B.SetSubtract(C).Unite(A)));
[Fact] public void UnionOfAUnionOpeningWithUnionOnTheRightStaysFlat()
=> Test($@"{As} \cup {Bs} \cup {Cs} \cup \left({As} \setminus {Bs}\right)", A.Unite(B.Unite(C).Unite(A.SetSubtract(B))));
[Fact] public void UnionOfUnionOnTheRightStaysFlat()
=> Test($@"{As} \cup {Bs} \cup {Cs}", A.Unite(B.Unite(C)));
[Fact] public void IntersectionOfIntersectionOnTheRightStaysFlat()
Expand Down
29 changes: 29 additions & 0 deletions Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ public void TheNodesWithoutAnOperatorSpellingPrintAsTheirFunction(string source)
[InlineData("x mod (y * z)")]
[InlineData("x / (y mod z)")]
[InlineData("x mod (y mod z)")]
// An associative operator's right operand of its own level is printed flat, which
// re-associates every operator along that operand's left edge -- including a `\` or a
// `mod` the operand merely opens with. https://github.com/asc-community/AngouriMath/issues/2758
[InlineData(@"A \/ ((B \ C) \/ D)")]
[InlineData(@"A \/ (((B \ C) \/ D) \/ E)")]
[InlineData(@"x in A \/ ((B \ C) \/ D)")]
[InlineData("x * ((y mod z) * w)")]
[InlineData("x * ((y mod z) / w)")]
[InlineData("x * (((y mod z) * w) / v)")]
[InlineData("-((y mod z) * w)")]
// `provided` is the mirror case: it folds to the *right*, so it is the left operand that
// mis-associates. Its value survives either reading -- `(x provided p) provided q` and
// `x provided (p provided q)` are both `x` exactly when `p` and `q` hold -- which is why
Expand All @@ -221,12 +231,30 @@ public void TheNodesWithoutAnOperatorSpellingPrintAsTheirFunction(string source)
[InlineData(@"{ 1, 2 } \/ ({ 3 } \ { 1, 2 })")]
[InlineData("2 * (3 mod 2)")]
[InlineData("(3 in { 3 }) in BB")]
[InlineData(@"{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })")]
[InlineData("2 * ((3 mod 2) * 4)")]
[InlineData("-((3 mod 2) * 4)")]
public void AMisreadGroupingWouldChangeTheValue(string source)
{
var original = source.ToEntity();
Assert.Equal(original.Evaled, Print(original).ToEntity().Evaled);
}

/// <summary>
/// Simplify's own answers are what reach a reader, and it puts a union in front of a set
/// difference on its own: <c>(B \ C) \/ (D \/ { pi })</c> became <c>{ pi } \/ B \ C \/ D</c>,
/// from which <c>C</c> can take <c>pi</c> out again.
/// https://github.com/asc-community/AngouriMath/issues/2758
/// </summary>
[Theory]
[InlineData(@"(B \ C) \/ (D \/ { pi })")]
[InlineData(@"({ -1, 1 } \ [-2; +oo)) \/ (QQ \/ { pi })")]
public void ASimplifiedSetReadsBackAsItself(string source)
{
var simplified = source.ToEntity().Simplify();
Assert.Equal(simplified, Print(simplified).ToEntity());
}

/// <summary>
/// The other half of the rule, pinned so that it is a decision rather than an oversight:
/// an operator that <em>is</em> associative prints flat, because the bracketing then
Expand All @@ -244,6 +272,7 @@ public void AMisreadGroupingWouldChangeTheValue(string source)
[InlineData("true xor (false xor true)", "True xor False xor True")]
[InlineData(@"{ 1 } \/ ({ 2 } \/ { 3 })", @"{ 1 } \/ { 2 } \/ { 3 }")]
[InlineData(@"{ 1, 2 } /\ ({ 2, 3 } /\ { 2 })", @"{ 1, 2 } /\ { 2, 3 } /\ { 2 }")]
[InlineData(@"{ 1 } \/ (({ 2 } \/ { 3 }) \/ ({ 1 } \ { 2 }))", @"{ 1 } \/ { 2 } \/ { 3 } \/ ({ 1 } \ { 2 })")]
public void AnAssociativeOperatorPrintsFlatAndKeepsItsValue(string source, string expectedFlat)
{
var original = source.ToEntity();
Expand Down
Loading