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
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed

[Tests/UnitTests/Calculus/ProductOverSetTest.cs]
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

[AngouriMath/Functions/Output/ToString/UnicodeSpelling.cs]
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
50 changes: 49 additions & 1 deletion Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6180,7 +6180,55 @@ public static partial class Settings
/// </code>
/// </example>
public static Setting<bool> ExplicitParsingOnly { get; } = false;


/// <summary>
/// Makes <see cref="Entity.Stringize()"/> and <see cref="Entity.ToString()"/> print
/// mathematical symbols in place of their ASCII spellings. Off by default.
/// </summary>
/// <remarks>
/// <para>
/// With it on, <c>pi</c> prints as <c>π</c>, <c>e</c> as <c>ℯ</c>, <c>+oo</c> and
/// <c>-oo</c> as <c>+∞</c> and <c>−∞</c>, <c>&lt;=</c> <c>&gt;=</c> as <c>≤</c> <c>≥</c>,
/// <c>not a = b</c> as <c>a ≠ b</c>, <c>in</c> <c>subset</c> as <c>∈</c> <c>⊆</c>,
/// a congruence <c>a = b (mod n)</c> as <c>a ≡ b (mod n)</c>, <c>forall</c>
/// <c>exists</c> <c>exists!</c> as <c>∀</c> <c>∃</c> <c>∃!</c>, <c>\/</c> <c>/\</c> as
/// <c>∪</c> <c>∩</c>, <c>not</c> <c>and</c> <c>or</c> as <c>¬</c> <c>∧</c> <c>∨</c>, the
/// minus sign as <c>−</c> (U+2212), and an integer exponent in superscript digits:
/// <c>x ^ 2</c> as <c>x²</c>, <c>x ^ (-1)</c> as <c>x⁻¹</c>.
/// </para>
/// <para>
/// The parser reads every one of these back, so what is printed parses to the
/// expression that was printed, as the ASCII form does. Names, numbers and function
/// calls are printed as they are either way, and so is anything an exponent's
/// superscript would misplace: <c>x ^ 2 ^ 3</c> prints as <c>x ^ 2³</c>, and an
/// exponent followed by <c>!</c> keeps its <c>^</c>.
/// </para>
/// <para>
/// Only what is shown changes. <see cref="Entity.Latexize()"/>, the SymPy code, the
/// JSON form and every ordering the library takes by printed form stay as they are,
/// so that no answer depends on this setting.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1242">#1242</a>
/// </para>
/// </remarks>
/// <example>
/// <code>
/// using System;
/// using AngouriMath;
/// using static AngouriMath.MathS;
///
/// Entity expr = "x ^ 2 - 1 &lt;= pi";
/// using (Settings.UnicodeOutput.Set(true))
/// Console.WriteLine(expr);
/// Console.WriteLine(expr);
/// </code>
/// Prints
/// <code>
/// x² − 1 ≤ π
/// x ^ 2 - 1 &lt;= pi
/// </code>
/// </example>
public static Setting<bool> UnicodeOutput { get; } = false;

/// <summary>
/// That is how we perform newton solving when no analytical solution was found
/// in <see cref="Entity.Solve(Variable)"/> and <see cref="Entity.SolveEquation(Variable)"/>
Expand Down
33 changes: 33 additions & 0 deletions Sources/AngouriMath/Core/Antlr/AngouriMath.g
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ options
// So these are instance fields
public readonly CommonToken Multiply = new(Array.IndexOf(_LiteralNames, "'*'"), "*");
public readonly CommonToken Power = new(Array.IndexOf(_LiteralNames, "'^'"), "^");
public readonly CommonToken Minus = new(Array.IndexOf(_LiteralNames, "'-'"), "-");

// The token a Unicode spelling stands for. Each is a synonym of an ASCII token the parser
// already has, so the parser itself does not change and the two spellings cannot differ in
// anything but the glyph. https://github.com/asc-community/AngouriMath/issues/1242
int UnicodeSynonym(string glyph) => Array.IndexOf(_LiteralNames, glyph switch
{
"\u2264" => "'<='",
"\u2265" => "'>='",
"\u2260" => "'<>'",
"\u2208" => "'in'",
"\u222A" => "'\\/'",
"\u2229" => "'/\\'",
"\u00AC" => "'not'",
"\u2227" => "'and'",
"\u2228" => "'or'",
"\u2212" => "'-'",
"+\u221E" => "'+oo'",
_ => "'-oo'",
});
}
@parser::members
{
Expand Down Expand Up @@ -809,6 +829,19 @@ BOOLEAN: ('true' | 'True' | 'false' | 'False') ;
// otherwise be an identifier. https://github.com/asc-community/AngouriMath/issues/906
NAN: 'NaN' ;

/* The Unicode spellings MathS.Settings.UnicodeOutput prints: ≤ ≥ ≠ ∈ ∪ ∩ ¬ ∧ ∨, the minus sign
U+2212, and +∞ and −∞ (or -∞) for the two infinities. Each is lexed as the ASCII token it
stands for, so that it parses exactly as that token does. A bare ∞ is not among them, as a
bare oo is not: either infinity is written with its sign.
https://github.com/asc-community/AngouriMath/issues/1242 */
UNICODE_SYNONYM: ('\u2264' | '\u2265' | '\u2260' | '\u2208' | '\u222A' | '\u2229' | '\u00AC' | '\u2227' | '\u2228' | '\u2212' | ('+' | '-' | '\u2212') '\u221E')
{ Type = UnicodeSynonym(Text); } ;

/* An integer exponent written in superscript digits, x² or x⁻¹, which Parser.cs replaces by the
tokens ^ 2 or ^ - 1 before parsing, so that it binds exactly as the power written out does.
https://github.com/asc-community/AngouriMath/issues/1242 */
SUPERSCRIPT: '\u207B'? ('\u2070' | '\u00B9' | '\u00B2' | '\u00B3' | '\u2074'..'\u2079')+ ;

/* A subscript may have a subscript of its own: X_a_beta is X with the subscript a_beta, and prints as X_{a_{\beta}}.
https://github.com/asc-community/AngouriMath/issues/524 */
VARIABLE: ('a'..'z'|'A'..'Z'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF'|'\u212F')+ ('_' ('a'..'z'|'A'..'Z'|'0'..'9'|'\u0370'..'\u03FF'|'\u1F00'..'\u1FFF'|'\u0400'..'\u04FF')+)* ;
Expand Down
6 changes: 5 additions & 1 deletion Sources/AngouriMath/Core/Antlr/AngouriMath.interp

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions Sources/AngouriMath/Core/Antlr/AngouriMath.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ NUMBER=239
SPECIALSET=240
BOOLEAN=241
NAN=242
VARIABLE=243
COMMENT=244
WS=245
UNICODE_SYNONYM=243
SUPERSCRIPT=244
VARIABLE=245
COMMENT=246
WS=247
'['=1
']'=2
'!'=3
Expand Down
Loading
Loading