Skip to content

Commit d44599f

Browse files
The real and the imaginary part are functions, Re and Im (#2185)
Re(z) and Im(z) are Entity.Ref and Entity.Imf, written as Mathematica, Maple and ISO 80000-2 write them, and accepted as SymPy's re and im and MATLAB's real and imag; they print Re and Im and draw upright, \operatorname{Re}. Both are real, Im(3 + 4i) being 4 and not 4i, and real-linear: carried through sums, differences, and products and quotients by a real number. The conjugate keeps the real part and turns the imaginary one. Not through a product of two symbols or through i, which mix the parts. What is known to be real is its own real part, and has a value exactly where that has one; its imaginary part is not taken as 0, since a codomain says what a node's values are and not where it has one. Both differentiate along the real line. re and im were left as products while the library had neither, two letters being short enough to be a variable's name. They are the functions now, and the name without a bracket is still a variable; that change of reading is recorded as breaking. The MCP server's examples of a silent product move to beta(z), and its misspelling hints for re and im are gone. Part of #2145. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 4fe3219 commit d44599f

35 files changed

Lines changed: 1514 additions & 877 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ the expression's own domain as its condition ([#2142](https://github.com/asc-com
286286
| `"(1 - cos(a))/(1 + cos(a))/tan(a/2)^2".Simplify()` | `(1 - cos(a)) / ((cos(a) + 1) * tan(a / 2) ^ 2)` | `1 provided not cos(a) + 1 = 0 and not tan(a / 2) = 0 and not cos(a / 2) = 0` |
287287
| `"(cos(2*x) - 1)^2 + sin(2*x)^2 - 4*sin(x)^2".Simplify()` ([#2133](https://github.com/asc-community/AngouriMath/issues/2133)) | `2 + (-2) * cos(2 * x) + (-4) * sin(x) ^ 2` | `0` |
288288

289+
### `re` and `im` are the real and the imaginary part
290+
291+
**Not silent: `re(…)` and `im(…)`, and `real(…)` and `imag(…)`, are now the real and the imaginary part, where they were
292+
read as products, and they print as `Re(…)` and `Im(…)`, as Mathematica, Maple and ISO 80000-2 write them.** `re(z)` was the variable `re` times `z`. They were left as products while the library had neither
293+
function, since two letters is short enough to be somebody's variable; the name without a bracket is still a variable.
294+
Both are real, `im(3 + 4i)` being `4`, real-linear, and turned by the conjugate as they should be
295+
([#2145](https://github.com/asc-community/AngouriMath/issues/2145)).
296+
297+
| Input | Was (2.6.0) | Now |
298+
|---|---|---|
299+
| `"re(z)".ToEntity()` | `re * z` | `Re(z)` |
300+
| `"im(3 + 4i)".ToEntity().Simplify()` | `(3 + 4i) * im` | `4` |
301+
| `"re * x".ToEntity()` | `re * x` | `re * x` |
302+
289303
### `conjugate`, `conj` and `arg` are functions
290304

291305
**Not silent: these names followed by a bracket are now the complex conjugate and the principal argument, where they

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
243243
[Tests/UnitTests/Algebra/SolveTest/FractionalPowerRangeTest.cs]
244244
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
245245

246+
[Tests/UnitTests/Convenience/RealAndImaginaryPartTest.cs]
247+
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
248+
246249
[Tests/UnitTests/Convenience/IversonTest.cs]
247250
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
248251

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,48 @@ public static Entity VectorLength(Matrix v)
14421442
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14431443
public static Entity Arg(Entity z) => new Argf(z);
14441444

1445+
/// <summary>The real part <c>re(z)</c>: <c>a</c> for <c>z = a + b i</c></summary>
1446+
/// <param name="z">The argument, any complex number</param>
1447+
/// <returns>The <see cref="Entity.Ref"/> node</returns>
1448+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2145</remarks>
1449+
/// <example>
1450+
/// <code>
1451+
/// using System;
1452+
/// using static AngouriMath.MathS;
1453+
///
1454+
/// Console.WriteLine(Re("3 + 4i").Simplify());
1455+
/// Console.WriteLine(Re("a + conj(b)").Simplify());
1456+
/// </code>
1457+
/// Prints
1458+
/// <code>
1459+
/// 3
1460+
/// Re(a) + Re(b)
1461+
/// </code>
1462+
/// </example>
1463+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1464+
public static Entity Re(Entity z) => new Ref(z);
1465+
1466+
/// <summary>The imaginary part <c>im(z)</c>: the real number <c>b</c> for <c>z = a + b i</c></summary>
1467+
/// <param name="z">The argument, any complex number</param>
1468+
/// <returns>The <see cref="Entity.Imf"/> node</returns>
1469+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2145</remarks>
1470+
/// <example>
1471+
/// <code>
1472+
/// using System;
1473+
/// using static AngouriMath.MathS;
1474+
///
1475+
/// Console.WriteLine(Im("3 + 4i").Simplify());
1476+
/// Console.WriteLine(Im("conj(z)").Simplify());
1477+
/// </code>
1478+
/// Prints
1479+
/// <code>
1480+
/// 4
1481+
/// -Im(z)
1482+
/// </code>
1483+
/// </example>
1484+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1485+
public static Entity Im(Entity z) => new Imf(z);
1486+
14451487
/// <summary>The aleph number <c>aleph(k)</c>, the size of an infinite set</summary>
14461488
/// <param name="index">The index, a whole number from zero</param>
14471489
/// <returns>The <see cref="Entity.Alephf"/> node</returns>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,16 @@ atom returns[Entity value]
641641
| 'conj(' args = function_arguments ')' { Assert("conj", 1, $args.list.Count); $value = MathS.Conjugate($args.list[0]); }
642642
/* The principal argument, in (-pi, pi]. https://github.com/asc-community/AngouriMath/issues/2145 */
643643
| 'arg(' args = function_arguments ')' { Assert("arg", 1, $args.list.Count); $value = MathS.Arg($args.list[0]); }
644+
/* The real and the imaginary part, written Re and Im as Mathematica, Maple and ISO 80000-2
645+
write them, and accepted as SymPy's re and im and as MATLAB's real and imag. re and im were
646+
read as products, re(z) being re * z, while there was nothing for them to mean.
647+
https://github.com/asc-community/AngouriMath/issues/2145 */
648+
| 'Re(' args = function_arguments ')' { Assert("Re", 1, $args.list.Count); $value = MathS.Re($args.list[0]); }
649+
| 'Im(' args = function_arguments ')' { Assert("Im", 1, $args.list.Count); $value = MathS.Im($args.list[0]); }
650+
| 're(' args = function_arguments ')' { Assert("re", 1, $args.list.Count); $value = MathS.Re($args.list[0]); }
651+
| 'real(' args = function_arguments ')' { Assert("real", 1, $args.list.Count); $value = MathS.Re($args.list[0]); }
652+
| 'im(' args = function_arguments ')' { Assert("im", 1, $args.list.Count); $value = MathS.Im($args.list[0]); }
653+
| 'imag(' args = function_arguments ')' { Assert("imag", 1, $args.list.Count); $value = MathS.Im($args.list[0]); }
644654

645655
/* Names the library does not have. Each is a function every other CAS spells this way, so
646656
a caller reaches for it, and without these rules each is silently read as a product --

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

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

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,20 @@ T__188=189
190190
T__189=190
191191
T__190=191
192192
T__191=192
193-
NEWLINE=193
194-
NUMBER=194
195-
SPECIALSET=195
196-
BOOLEAN=196
197-
NAN=197
198-
VARIABLE=198
199-
COMMENT=199
200-
WS=200
193+
T__192=193
194+
T__193=194
195+
T__194=195
196+
T__195=196
197+
T__196=197
198+
T__197=198
199+
NEWLINE=199
200+
NUMBER=200
201+
SPECIALSET=201
202+
BOOLEAN=202
203+
NAN=203
204+
VARIABLE=204
205+
COMMENT=205
206+
WS=206
201207
'!'=1
202208
'^'=2
203209
'-'=3
@@ -385,9 +391,15 @@ WS=200
385391
'conjugate('=185
386392
'conj('=186
387393
'arg('=187
388-
'trunc('=188
389-
'domain('=189
390-
'piecewise('=190
391-
'apply('=191
392-
'lambda('=192
393-
'NaN'=197
394+
'Re('=188
395+
'Im('=189
396+
're('=190
397+
'real('=191
398+
'im('=192
399+
'imag('=193
400+
'trunc('=194
401+
'domain('=195
402+
'piecewise('=196
403+
'apply('=197
404+
'lambda('=198
405+
'NaN'=203

0 commit comments

Comments
 (0)