Skip to content

Commit 2eeb163

Browse files
π is pi and the script ℯ is e (#1608)
π (U+03C0) was a free variable, a Greek letter like any other, so sin(π) did not simplify; the script ℯ (U+212F) was not a letter and did not parse. Both are spellings of the constants now, read by the one lookup the parser and the binders share: π is pi, one object with it, and ℯ is e. The Cyrillic е and х stay letters, variables that print like e and x. Closes #1260. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 67c422a commit 2eeb163

7 files changed

Lines changed: 414 additions & 332 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,20 @@ quotient or whole power with a matrix in it to GenericTensor's operation of that
752752
| `"[[x, 2 x], [x + 1, x^2]]".ToEntity().Compile<double, GenTensor<double, DoubleOperations>>("x")` | `UncompilableNodeException` | a delegate; at `3` the matrix `[[3, 6], [4, 9]]` |
753753
| `"A * B".ToEntity().Compile<GenTensor<…>, GenTensor<…>, GenTensor<…>>("A", "B")` | `UncompilableNodeException` | the matrix product |
754754
| `"[[1, 0]] * [[a, b], [c, d]] * [[0], [1]]".ToEntity().Compile<double, double, double, double, double>("a", "b", "c", "d")` | `2` at `(1, 2, 3, 4)` — simplified to the scalar first | unchanged |
755+
### `π` is `pi` and the script `ℯ` is `e`
756+
757+
`π` (U+03C0) was a free variable, a Greek letter like any other, so `sin(π)` did not simplify; the
758+
script `ℯ` (U+212F) was not a letter and did not parse. Both are now spellings of the constants, as
759+
mathematics writes them: `π` is `pi`, one object with it, and `ℯ` is `e`. A caller who used `π` as a
760+
variable's name now has the constant. The Cyrillic `е` and `х` stay letters, variables that print
761+
like `e` and `x` ([#1260](https://github.com/asc-community/AngouriMath/issues/1260)).
762+
763+
| Input | Was (2.5.0) | Now |
764+
|---|---|---|
765+
| `"sin(π)".ToEntity().Simplify()` | `sin(π)` | `0` |
766+
| `"cos(π / 3)".ToEntity().Simplify()` | `cos(π / 3)` | `1/2` |
767+
| `"ln(ℯ)".ToEntity().Simplify()` | `UnhandledParseException` | `1` |
768+
755769
### `lcm` is a function, and a congruence is solved to a residue class
756770

757771
`lcm(a, b, ...)` is a node, `Entity.Lcmf`, by the conventions of `gcd`: non-negative, `0` where

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ BOOLEAN: ('true' | 'True' | 'false' | 'False') ;
708708
// otherwise be an identifier. https://github.com/asc-community/AngouriMath/issues/906
709709
NAN: 'NaN' ;
710710

711-
VARIABLE: ('a'..'z'|'A'..'Z'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF')+ ('_' ('a'..'z'|'A'..'Z'|'0'..'9'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF')+)? ;
711+
VARIABLE: ('a'..'z'|'A'..'Z'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF'|'\u212F')+ ('_' ('a'..'z'|'A'..'Z'|'0'..'9'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF')+)? ;
712712

713713
COMMENT: ('//' ~[\r\n]* ('\r'? '\n')? | '/*' .*? '*/') -> skip ;
714714

‎Sources/AngouriMath/Core/Antlr/AngouriMathLexer.cs‎

Lines changed: 326 additions & 326 deletions
Large diffs are not rendered by default.

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

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

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,21 @@ public void Deconstruct(out string name)
7070
[ConstantField] internal static readonly Constant pi = NamedConstants[nameof(pi)];
7171
[ConstantField] internal static readonly Constant e = NamedConstants[nameof(e)];
7272

73+
/// <summary>
74+
/// The other spellings of a constant's name, each read as the constant itself: <c>π</c>
75+
/// (U+03C0) is <c>pi</c> and the script <c>ℯ</c> (U+212F) is <c>e</c>, as mathematics
76+
/// writes them. The Cyrillic <c>е</c> is not among them: it is a letter of an alphabet the
77+
/// grammar admits on purpose, and reading it as a constant would be the same surprise the
78+
/// other way round. https://github.com/asc-community/AngouriMath/issues/1260
79+
/// </summary>
80+
[ConstantField] private static readonly IReadOnlyDictionary<string, string> ConstantSpellings =
81+
new Dictionary<string, string> { { "\u03C0", nameof(pi) }, { "\u212F", nameof(e) } };
82+
83+
/// <summary>The name a spelling stands for: <c>π</c> is <c>pi</c>, and any other name is itself.</summary>
84+
private static string Spelled(string name) => ConstantSpellings.TryGetValue(name, out var constant) ? constant : name;
85+
7386
/// <summary>Is this name one the language reads as a mathematical constant?</summary>
74-
internal static bool IsConstantName(string name) => ConstantList.ContainsKey(name);
87+
internal static bool IsConstantName(string name) => ConstantList.ContainsKey(Spelled(name));
7588

7689
/// <summary>
7790
/// A name as the language reads it: a constant where the name is one, a variable
@@ -80,7 +93,7 @@ public void Deconstruct(out string name)
8093
/// <a href="https://github.com/asc-community/AngouriMath/issues/984">#984</a>
8194
/// </summary>
8295
internal static Variable CreateVariableOrConstant(string name)
83-
=> NamedConstants.TryGetValue(name, out var constant) ? constant : new Variable(name);
96+
=> NamedConstants.TryGetValue(Spelled(name), out var constant) ? constant : new Variable(name);
8497

8598
/// <summary>
8699
/// Extracts this <see cref="Variable"/>'s name and index

‎Sources/AngouriMath/Docs/Usage/Syntax.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ as `NaNx` or `NaN_1`, is still a variable, because the lexer takes the longest m
133133

134134
A name is **one or more letters**, optionally followed by `_` and one or more letters or digits:
135135
`x`, `xy`, `x_1`, `x_a`, `α`, `ω_1`, `Θ_1`, `альфа`, `x_ω`. Letters are `a`–`z`, `A`–`Z`, Greek
136-
(U+0370–U+03FF and U+1F00–U+1FFF) and Cyrillic (U+0400–U+04FF); no other script is one, so `fi` is
137-
a lexer error.
136+
(U+0370–U+03FF and U+1F00–U+1FFF), Cyrillic (U+0400–U+04FF) and the script `ℯ` (U+212F); no other
137+
script is one, so `fi` is a lexer error.
138+
139+
`pi` and `e` are the constants, and so are the spellings mathematics writes them in: `π` (U+03C0)
140+
is `pi` and the script `ℯ` is `e`, so `sin(π)` is `0` and `ln(ℯ)` is `1`. The Cyrillic `е`
141+
(U+0435) is a letter, not the constant, and so is the Cyrillic `х` (U+0445) beside the Latin `x`:
142+
they are variables that print like `e` and `x`, and `х + x` has two
143+
([#1260](https://github.com/asc-community/AngouriMath/issues/1260)).
138144

139145
Four things that look like names and are not:
140146

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 System.Linq;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Core
13+
{
14+
/// <summary>
15+
/// <c>π</c> and the script <c>ℯ</c> are the constants <c>pi</c> and <c>e</c>, as mathematics
16+
/// writes them, where <c>π</c> was a free variable and <c>ℯ</c> did not parse. The Cyrillic
17+
/// <c>е</c> and <c>х</c> are letters of an alphabet the grammar admits, and stay variables.
18+
/// https://github.com/asc-community/AngouriMath/issues/1260
19+
/// </summary>
20+
[Trait("Area", "Core")]
21+
public sealed class ConstantSpellingTest
22+
{
23+
[Theory]
24+
[InlineData("sin(π)", "0")]
25+
[InlineData("cos(π / 3)", "1/2")]
26+
[InlineData("π - pi", "0")]
27+
[InlineData("ln(ℯ)", "1")]
28+
[InlineData("ℯ^2 / e^2", "1")]
29+
public void TheConstantsReadInTheirSpellings(string written, string simplified)
30+
=> Assert.Equal(simplified.ToEntity(), written.ToEntity().Simplify());
31+
32+
[Fact]
33+
public void ASpellingIsTheConstantItself()
34+
{
35+
Assert.Equal("pi".ToEntity(), "π".ToEntity());
36+
Assert.Equal("e".ToEntity(), "ℯ".ToEntity());
37+
}
38+
39+
[Fact]
40+
public void CyrillicLettersAreVariables()
41+
{
42+
// The Cyrillic e (U+0435) is a letter, not the constant, and x (U+0445) is another
43+
// letter from the Latin x: two variables that print alike.
44+
Assert.IsType<Entity.Variable>("е".ToEntity());
45+
Assert.Equal("ln(е)".ToEntity(), "ln(е)".ToEntity().Simplify());
46+
Assert.Equal(2, "х + x".ToEntity().Vars.Count());
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)