Skip to content

Commit e9aee49

Browse files
The Iverson bracket is a function: iverson(P) is 1 where P holds and 0 where it does not (#1567)
iverson(P), MathS.Iverson, is Entity.Iversonf. A decided statement gives 1 or 0, an undecided one keeps the node, a statement with no truth value gives NaN, and a number, which is not a statement, is left as written as a connective leaves one. So a count is a sum of brackets: sum(iverson(k divides 12), k, 1, 12) is 6, and Sullivan and Mackey's Ex 8.3.18 counts 12 outcomes where the book says 15. LaTeX is the double bracket [\![P]\!], SymPy a Piecewise; an equation in the node is left unsolved, the parser is regenerated for the name, and the public API baseline carries the node. Closes #1478. Part of #1409. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 8b05ef5 commit e9aee49

27 files changed

Lines changed: 1267 additions & 942 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,21 @@ carried a condition the piecewise read as no value, so the sum was `0` at `n = 0
904904
| `"sum(binomial(n, k), k, 0, n)".ToEntity().Evaled` | `UnhandledParseException` | `piecewise((2 ^ n) provided (n >= 0), 0 provided True)` |
905905
| `"sum(n! / (k! (n - k)!) (-1)^k, k, 0, n)".ToEntity().Evaled` | `piecewise(0 provided True)` — wrong at `n = 0` | `piecewise(1 provided (n = 0), 0 provided True)` |
906906

907+
### `iverson(P)` is the Iverson bracket, and was a product
908+
909+
`iverson(P)` is `1` where the statement `P` holds and `0` where it does not, and is kept as written
910+
where `P` is not decided, so a count is a sum of brackets: `sum(iverson(k divides 12), k, 1, 12)` is
911+
`6`. A number is not a statement and is left as written; a statement with no truth value, such as
912+
`i > 0`, gives `NaN`. It prints as `[\![P]\!]` in LaTeX and as a `Piecewise` for SymPy
913+
([#1478](https://github.com/asc-community/AngouriMath/issues/1478)). In 2.5.0 the name was read as a
914+
variable multiplying its argument, and a product with a truth value is `NaN`.
915+
916+
| Input | Was (2.5.0) | Now |
917+
|---|---|---|
918+
| `"iverson(2 > 1)".ToEntity().Simplify()` | `NaN` — `iverson * (2 > 1)` | `1` |
919+
| `"iverson(x > 0)".ToEntity().Simplify()` | `iverson * (x > 0)` | `iverson(x > 0)` |
920+
| `"sum(sum(sum(iverson(r + b + g = 4), g, 0, 3), b, 0, 3), r, 0, 3)".ToEntity().Simplify()` | `NaN` | `12` |
921+
907922
### A statement about a sum up to `n` is decided by induction
908923

909924
`forall n in ZZ+ : sum(f, k, a, n) = g` is proved from the least member of the set: the statement

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
225225
[Tests/UnitTests/Convenience/BinomialTest.cs]
226226
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
227227

228+
[Tests/UnitTests/Convenience/IversonTest.cs]
229+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
230+
228231
[Tests/UnitTests/Common/ExponentialOfALogarithmTest.cs]
229232
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
230233

@@ -336,6 +339,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
336339
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.ErrorFunctions.Classes.cs]
337340
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
338341

342+
[AngouriMath/Core/Entity/Continuous/Entity.Continuous.Iverson.Classes.cs]
343+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
344+
339345
[Tests/UnitTests/Core/ProofRecordingTest.cs]
340346
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
341347

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,33 @@ public static Entity VectorLength(Matrix v)
13671367
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13681368
public static Entity Binomial(Entity n, Entity k) => new Binomialf(n, k);
13691369

1370+
/// <summary>The Iverson bracket <c>iverson(P)</c>: <c>1</c> where the statement holds, <c>0</c> where it does not</summary>
1371+
/// <param name="statement">The statement</param>
1372+
/// <returns>The <see cref="Entity.Iversonf"/> node</returns>
1373+
/// <remarks>
1374+
/// A statement that is not decided is kept as the node. A count is a sum of these:
1375+
/// <c>sum(iverson(k divides 12), k, 1, 12)</c> is the number of divisors of <c>12</c>.
1376+
/// https://github.com/asc-community/AngouriMath/issues/1478
1377+
/// </remarks>
1378+
/// <example>
1379+
/// <code>
1380+
/// using System;
1381+
/// using static AngouriMath.MathS;
1382+
///
1383+
/// Console.WriteLine(Iverson("3 divides 12").Simplify());
1384+
/// Console.WriteLine(Iverson("2 > 3").Simplify());
1385+
/// Console.WriteLine(Iverson("x > 0").Simplify());
1386+
/// </code>
1387+
/// Prints
1388+
/// <code>
1389+
/// 1
1390+
/// 0
1391+
/// iverson(x > 0)
1392+
/// </code>
1393+
/// </example>
1394+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1395+
public static Entity Iverson(Entity statement) => new Iversonf(statement);
1396+
13701397
/// <summary>The error function, <c>erf(z) = 2/sqrt(pi) int_0^z e^(-t^2) dt</c></summary>
13711398
/// <param name="a">The argument, any complex number</param>
13721399
/// <returns>The <see cref="Entity.Erff"/> node</returns>

‎Sources/AngouriMath/Core/Antlr/AngouriMath.g‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ atom returns[Entity value]
596596
| 'gcd(' args = function_arguments ')' { AssertAtLeast("gcd", 1, $args.list.Count); $value = $args.list.Aggregate((a, b) => MathS.Gcd(a, b)); }
597597
| 'lcm(' args = function_arguments ')' { AssertAtLeast("lcm", 1, $args.list.Count); $value = $args.list.Aggregate((a, b) => MathS.Lcm(a, b)); }
598598
| 'binomial(' args = function_arguments ')' { Assert("binomial", 2, $args.list.Count); $value = MathS.Binomial($args.list[0], $args.list[1]); }
599+
/* The Iverson bracket, 1 where a statement holds and 0 where it does not: a count written as
600+
a sum. https://github.com/asc-community/AngouriMath/issues/1478 */
601+
| 'iverson(' args = function_arguments ')' { Assert("iverson", 1, $args.list.Count); $value = MathS.Iverson($args.list[0]); }
599602
/* The error functions. https://github.com/asc-community/AngouriMath/issues/1501 */
600603
| 'erf(' args = function_arguments ')' { Assert("erf", 1, $args.list.Count); $value = MathS.Erf($args.list[0]); }
601604
| 'erfc(' args = function_arguments ')' { Assert("erfc", 1, $args.list.Count); $value = MathS.Erfc($args.list[0]); }

‎Sources/AngouriMath/Core/Antlr/AngouriMath.interp‎

Lines changed: 3 additions & 1 deletion
Large diffs are not rendered by default.

‎Sources/AngouriMath/Core/Antlr/AngouriMath.tokens‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ T__177=178
179179
T__178=179
180180
T__179=180
181181
T__180=181
182-
NEWLINE=182
183-
NUMBER=183
184-
SPECIALSET=184
185-
BOOLEAN=185
186-
NAN=186
187-
VARIABLE=187
188-
COMMENT=188
189-
WS=189
182+
T__181=182
183+
NEWLINE=183
184+
NUMBER=184
185+
SPECIALSET=185
186+
BOOLEAN=186
187+
NAN=187
188+
VARIABLE=188
189+
COMMENT=189
190+
WS=190
190191
'!'=1
191192
'^'=2
192193
'-'=3
@@ -353,19 +354,20 @@ WS=189
353354
'gcd('=164
354355
'lcm('=165
355356
'binomial('=166
356-
'erf('=167
357-
'erfc('=168
358-
'erfi('=169
359-
'Ei('=170
360-
'li('=171
361-
'Si('=172
362-
'Ci('=173
363-
'Shi('=174
364-
'Chi('=175
365-
'trunc('=176
366-
'conjugate('=177
367-
'domain('=178
368-
'piecewise('=179
369-
'apply('=180
370-
'lambda('=181
371-
'NaN'=186
357+
'iverson('=167
358+
'erf('=168
359+
'erfc('=169
360+
'erfi('=170
361+
'Ei('=171
362+
'li('=172
363+
'Si('=173
364+
'Ci('=174
365+
'Shi('=175
366+
'Chi('=176
367+
'trunc('=177
368+
'conjugate('=178
369+
'domain('=179
370+
'piecewise('=180
371+
'apply('=181
372+
'lambda('=182
373+
'NaN'=187

0 commit comments

Comments
 (0)