Skip to content

Commit 2a3eef8

Browse files
Rafael-SOWNetclaude
andcommitted
Make det, transpose, trace and an entry nodes that apply to a matrix argument
det(A) of a symbol was refused at parsing, transpose(x) too, trace(A) was the product trace * A, and A[i, j] did not parse. Each of the four is now a node over what it is applied to, carried out once that is a matrix: by Substitute, Simplify, Evaled and a compiled delegate taking T[,]. Of a matrix written out each is still computed as it is parsed, as det and transpose were, so nothing #2180 answered moves but transpose(x). - Determinantf, Transposef, Tracef and MatrixEntryf, with printing that parses back, LaTeX (\det, ^{\mathrm{T}}, \operatorname{tr}, a subscript), SymPy, substitution, sorting, serialization, and MathS.Matrices.Determinant, Transpose, Trace and Entry. - An entry counts its row and column from 1, as a_ij does: [[1, 2], [3, 4]][2, 1] is 3. The brackets follow an atom directly, which did not parse before. - A non-square matrix has no determinant and no trace, and an index out of range or not whole names no entry: the node is left as it is, never a number. Written out, each is refused at parsing, as det of a non-square matrix was. A number, a statement or a set is no matrix and is refused. - det(transpose(A)) is det(A), trace(transpose(A)) is trace(A), transpose(transpose(A)) is A and transpose(A)[k, l] is A[l, k]. - The transpose, the trace and an entry are differentiated through the matrix; the determinant keeps its derivative node, Jacobi's formula needing the adjugate. - Compiled: the transpose, Bareiss' fraction-free determinant with the largest pivot where the elements have a size (exact over long and int), the trace, and the entry with its indices checked whole and in range when called. Shapes written into the expression are checked at Compile. - A product with a transpose in it is a product of matrices, and Simplify read it as commutative: transpose(A) * A - A * transpose(A) simplified to 0, and transpose(A) * B printed as B * transpose(A). Such a product is set aside as a name of its own while the rest is simplified, and put back with each factor simplified in place; the canonical sort and the print order leave it as written. - A name beside a written matrix may stand for a matrix, and simplification reads it as a number: det(A [[2, 0], [0, 3]]) was 6 A^2 at parsing, and a compiled delegate computed that. Such an argument is not carried out, at parsing, in evaluation or by Simplify, until the name is a matrix; det([[x, 1], [2, x]]), whose names are entries, is x^2 - 2 as before. Measured: the new MatrixOperationNodesTest compares every value with one computed from the entries, at real, negative and non-real matrices, 2x2 and 3x3, and every compiled value with evaluation, including a matrix argument beside a written matrix, which the base commit's SimplifiedAroundMatrixArguments leaves to these nodes. The 2.6.0 values in BREAKING-CHANGES.md are measured on the 2.6.0 package. Closes #2427. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5de0e67 commit 2a3eef8

39 files changed

Lines changed: 4217 additions & 2710 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2608,11 +2608,40 @@ the right shape, written out or once its operations are carried out, throws `Inv
26082608
|---|---|---|
26092609
| `"det([[1, 2], [3, 4]])".ToEntity()` | `det * [[1, 2], [3, 4]]` | `-2` |
26102610
| `"transpose([1, 2])".ToEntity()` | `transpose * [1, 2]` | `[[1, 2]]`, the row |
2611-
| `"transpose(x)".ToEntity()` | `transpose * x` | throws `InvalidArgumentParseException` |
2611+
| `"transpose(x)".ToEntity()` | `transpose * x` | the node `transpose(x)`, see the entry below |
26122612
| `"cross([a, b])".ToEntity()` | `cross * [a, b]` | `[-b, a]` |
26132613
| `"dot([1, 2], [3, 4])".ToEntity()` | throws `UnhandledParseException` | `1 * 3 + 2 * 4` |
26142614
| `"resultant(x^2 - 1, x - a, x)".ToEntity()` | throws `UnhandledParseException` | `a ^ 2 - 1` |
26152615

2616+
### `det`, `transpose`, `trace` and an entry `A[k, l]` of a name for a matrix are nodes
2617+
2618+
**Not silent: these were read as products, or did not parse, and are now nodes, carried out once the name is a
2619+
matrix.** `det(A)`, `transpose(A)` and `trace(A)` of a name, or of an expression that is not yet a matrix, are the
2620+
nodes `Determinantf`, `Transposef` and `Tracef`; `A[k, l]` is `MatrixEntryf`, the entry in row `k` and column `l`
2621+
counted from 1, and did not parse at all. Each is computed when the name is substituted by a matrix, by `Simplify`,
2622+
`Evaled` and a compiled delegate taking `double[,]` or `Complex[,]`. Of a matrix written out each is computed as it is
2623+
parsed, as `det` and `transpose` already were, and `trace` is new. A matrix that is not square has no determinant
2624+
and no trace, and an index out of range names no entry: the node is left, never a number. A number, a statement or
2625+
a set is refused with `InvalidArgumentParseException`. A product with a transpose in it is no longer reordered by
2626+
`Simplify`, since matrices do not commute: `transpose(A) * A - A * transpose(A)` is not `0`. A name beside a written
2627+
matrix, as in `det(x * [[1, 2], [3, 4]])`, may stand for a matrix too, and such an argument is not carried out until
2628+
it is one, where it was multiplied into the entries as a number
2629+
([#2147](https://github.com/asc-community/AngouriMath/issues/2147)).
2630+
2631+
| Input | Was (2.6.0) | Now |
2632+
|---|---|---|
2633+
| `"det(A)".ToEntity()` | `det * A` | `det(A)`, a `Determinantf` |
2634+
| `"transpose(A)".ToEntity()` | `transpose * A` | `transpose(A)`, a `Transposef` |
2635+
| `"transpose(A) * B".ToEntity()` | `transpose * A * B` | `transpose(A) * B` |
2636+
| `"trace(A)".ToEntity()` | `trace * A` | `trace(A)`, a `Tracef` |
2637+
| `"trace([[1, 2], [3, 4]])".ToEntity()` | `trace * [[1, 2], [3, 4]]` | `1 + 4` |
2638+
| `"trace(A * B)".Simplify()` | `A * B * trace` | `trace(A * B)` |
2639+
| `"A[1, 2]".ToEntity()` | throws `UnhandledParseException` | `A[1, 2]`, a `MatrixEntryf` |
2640+
| `"[[1, 2], [3, 4]][2, 1]".ToEntity()` | throws `UnhandledParseException` | `3` |
2641+
| `"det(5)".ToEntity()` | `det * 5` | throws `InvalidArgumentParseException` |
2642+
| `"det(x * [[1, 2], [3, 4]])".ToEntity()` | `det * x * [[1, 2], [3, 4]]` | `det(x * [[1, 2], [3, 4]])`: `x` may stand for a matrix |
2643+
| `"det(A)".Substitute("A", "[[1, 2], [3, 4]]").Evaled` | `[[det, det * 2], [det * 3, det * 4]]` | `-2` |
2644+
26162645
### The names of functions to come are refused by name
26172646

26182647
**Not silent: these names followed by a bracket now throw `UnrecognizedFunctionParseException` where they were read as

‎Sources/.editorconfig‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,3 +1075,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10751075

10761076
[Tests/UnitTests/Common/BranchCutCompilationTest.cs]
10771077
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
1078+
1079+
[AngouriMath/Core/Entity/Omni/Entity.MatrixOperations.Classes.cs]
1080+
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
1081+
1082+
[Tests/UnitTests/Common/MatrixOperationNodesTest.cs]
1083+
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
1084+
1085+
[AngouriMath/Functions/Simplification/ProductsOfMatrices.cs]
1086+
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

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6004,6 +6004,52 @@ public static Matrix PointwiseMultiplication(Matrix m1, Matrix m2)
60046004
public static Matrix Matrix(int rows, int columns, params Entity[] values) =>
60056005
new(GenTensor.CreateMatrix(rows, columns, (x, y) => values[x * columns + y]));
60066006

6007+
/// <summary>The determinant <c>det(A)</c> of a square matrix, as a node.</summary>
6008+
/// <param name="matrix">The matrix, written out or a name that stands for one.</param>
6009+
/// <returns>The <see cref="Entity.Determinantf"/> node, computed once its argument is a matrix</returns>
6010+
/// <remarks>
6011+
/// A matrix that is not square has no determinant, and the node is left as it is.
6012+
/// https://github.com/asc-community/AngouriMath/issues/2147
6013+
/// </remarks>
6014+
/// <example>
6015+
/// <code>
6016+
/// var det = MathS.Matrices.Determinant("A");
6017+
/// Console.WriteLine(det);
6018+
/// Console.WriteLine(det.Substitute("A", "[[1, 2], [3, 4]]").Evaled);
6019+
/// </code>
6020+
/// Prints
6021+
/// <code>
6022+
/// det(A)
6023+
/// -2
6024+
/// </code>
6025+
/// </example>
6026+
public static Entity Determinant(Entity matrix) => new Determinantf(matrix);
6027+
6028+
/// <summary>The transpose <c>transpose(A)</c>, as a node. A vector is a column, and its transpose a row.</summary>
6029+
/// <param name="matrix">The matrix, written out or a name that stands for one.</param>
6030+
/// <returns>The <see cref="Entity.Transposef"/> node, computed once its argument is a matrix</returns>
6031+
/// <remarks>https://github.com/asc-community/AngouriMath/issues/2147</remarks>
6032+
public static Entity Transpose(Entity matrix) => new Transposef(matrix);
6033+
6034+
/// <summary>The trace <c>trace(A)</c> of a square matrix, the sum of its diagonal entries, as a node.</summary>
6035+
/// <param name="matrix">The matrix, written out or a name that stands for one.</param>
6036+
/// <returns>The <see cref="Entity.Tracef"/> node, computed once its argument is a matrix</returns>
6037+
/// <remarks>
6038+
/// A matrix that is not square has no trace, and the node is left as it is.
6039+
/// https://github.com/asc-community/AngouriMath/issues/2147
6040+
/// </remarks>
6041+
public static Entity Trace(Entity matrix) => new Tracef(matrix);
6042+
6043+
/// <summary>The entry <c>A[i, j]</c> of a matrix, its rows and columns counted from 1, as a node.</summary>
6044+
/// <param name="matrix">The matrix, written out or a name that stands for one.</param>
6045+
/// <param name="row">The row, from 1.</param>
6046+
/// <param name="column">The column, from 1.</param>
6047+
/// <returns>The <see cref="Entity.MatrixEntryf"/> node, computed once its argument is a matrix and its indices are whole numbers in range</returns>
6048+
/// <remarks>
6049+
/// An index out of range names no entry, and the node is left as it is.
6050+
/// https://github.com/asc-community/AngouriMath/issues/2147
6051+
/// </remarks>
6052+
public static Entity Entry(Entity matrix, Entity row, Entity column) => new MatrixEntryf(matrix, row, column);
60076053
}
60086054

60096055
/// <summary>

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ options
8080
// So we put UPPER_CASE lexer rules after lower_case parser rules.
8181
8282
// atom is defined later
83+
/* An entry of a matrix, `A[i, j]`, its row and its column counted from 1 as a_ij is. The brackets
84+
follow what they index directly, and a bracket after an atom was not a parse before, so
85+
nothing that parsed reads differently. https://github.com/asc-community/AngouriMath/issues/2147 */
86+
indexed_atom returns[Entity value]
87+
: p = atom { $value = $p.value; }
88+
('[' args = function_arguments ']' { $value = ParsingHelpers.Entry($value, $args.list); })*
89+
;
90+
8391
factorial_expression returns[Entity value]
84-
: p = atom '!' { $value = MathS.Factorial($p.value); }
92+
: p = indexed_atom '!' { $value = MathS.Factorial($p.value); }
8593
/* The double factorial, n (n - 2) (n - 4) ..., which is not (n!)!: that keeps its parentheses.
8694
Three marks are the triple factorial in the literature, which the library does not have, so
8795
they are refused rather than read as (n!!)!. https://github.com/asc-community/AngouriMath/issues/2182 */
88-
| p = atom '!!' { $value = MathS.DoubleFactorial($p.value); }
89-
| p = atom '!!!' { throw new UnrecognizedFunctionParseException("n!!! is the triple factorial, which AngouriMath does not have; write (n!!)! for the factorial of a double factorial"); }
90-
| p = atom { $value = $p.value; }
96+
| p = indexed_atom '!!' { $value = MathS.DoubleFactorial($p.value); }
97+
| p = indexed_atom '!!!' { throw new UnrecognizedFunctionParseException("n!!! is the triple factorial, which AngouriMath does not have; write (n!!)! for the factorial of a double factorial"); }
98+
| p = indexed_atom { $value = $p.value; }
9199
;
92100
93101
power_list returns[List<Entity> value]
@@ -653,12 +661,14 @@ atom returns[Entity value]
653661
| 'Ci(' args = function_arguments ')' { Assert("Ci", 1, $args.list.Count); $value = MathS.Ci($args.list[0]); }
654662
| 'Shi(' args = function_arguments ')' { Assert("Shi", 1, $args.list.Count); $value = MathS.Shi($args.list[0]); }
655663
| 'Chi(' args = function_arguments ')' { Assert("Chi", 1, $args.list.Count); $value = MathS.Chi($args.list[0]); }
656-
/* The determinant, the transpose, the scalar and the vector product, and the resultant, each
657-
computed from what it is applied to: there is no node for any of them, so a matrix or a
658-
vector has to be written out, or be one once its operations are carried out.
659-
https://github.com/asc-community/AngouriMath/issues/2147 */
664+
/* The determinant, the transpose, the trace, the scalar and the vector product, and the
665+
resultant, each computed from what it is applied to where that is a matrix or a vector
666+
written out, or one once its operations are carried out. det, transpose and trace of
667+
anything else that may stand for a matrix, as a name does, are nodes, computed once it is
668+
one. https://github.com/asc-community/AngouriMath/issues/2147 */
660669
| 'det(' args = function_arguments ')' { Assert("det", 1, $args.list.Count); $value = ParsingHelpers.Determinant($args.list[0]); }
661670
| 'transpose(' args = function_arguments ')' { Assert("transpose", 1, $args.list.Count); $value = ParsingHelpers.Transposed($args.list[0]); }
671+
| 'trace(' args = function_arguments ')' { Assert("trace", 1, $args.list.Count); $value = ParsingHelpers.Trace($args.list[0]); }
662672
| 'dot(' args = function_arguments ')' { Assert("dot", 2, $args.list.Count); $value = ParsingHelpers.Dot($args.list[0], $args.list[1]); }
663673
| 'cross(' args = function_arguments ')' { Assert("cross", (1, 2), $args.list.Count); $value = ParsingHelpers.Cross($args.list); }
664674
| 'laplacian(' args = function_arguments ')' { Assert("laplacian", 2, $args.list.Count); $value = ParsingHelpers.Laplacian($args.list[0], $args.list[1]); }

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

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

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

Lines changed: 102 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -227,73 +227,74 @@ T__225=226
227227
T__226=227
228228
T__227=228
229229
T__228=229
230-
NEWLINE=230
231-
NUMBER=231
232-
SPECIALSET=232
233-
BOOLEAN=233
234-
NAN=234
235-
VARIABLE=235
236-
COMMENT=236
237-
WS=237
238-
'!'=1
239-
'!!'=2
240-
'!!!'=3
241-
'^'=4
242-
'-'=5
243-
'+'=6
244-
'*'=7
245-
'...'=8
246-
'…'=9
247-
'/'=10
248-
'mod'=11
249-
'intersect'=12
250-
'/\\'=13
251-
'unite'=14
252-
'\\/'=15
253-
'setsubtract'=16
254-
'\\'=17
255-
'in'=18
256-
'subset'=19
257-
'⊆'=20
258-
'superset'=21
259-
'⊇'=22
260-
'divides'=23
261-
'|'=24
262-
'>='=25
263-
'<='=26
264-
'>'=27
265-
'<'=28
266-
'='=29
267-
'<>'=30
268-
'≡'=31
269-
'('=32
270-
')'=33
271-
'not'=34
272-
'and'=35
273-
'&'=36
274-
'xor'=37
275-
'or'=38
276-
'implies'=39
277-
'->'=40
278-
'provided'=41
279-
'=>'=42
280-
':'=43
281-
'forall'=44
282-
'∀'=45
283-
'exists!'=46
284-
'∃!'=47
285-
'exists'=48
286-
'∃'=49
287-
','=50
288-
';'=51
289-
'+oo'=52
290-
'-oo'=53
291-
'(|'=54
292-
'|)'=55
293-
'#'=56
294-
'['=57
295-
']T'=58
296-
']'=59
230+
T__229=230
231+
NEWLINE=231
232+
NUMBER=232
233+
SPECIALSET=233
234+
BOOLEAN=234
235+
NAN=235
236+
VARIABLE=236
237+
COMMENT=237
238+
WS=238
239+
'['=1
240+
']'=2
241+
'!'=3
242+
'!!'=4
243+
'!!!'=5
244+
'^'=6
245+
'-'=7
246+
'+'=8
247+
'*'=9
248+
'...'=10
249+
'…'=11
250+
'/'=12
251+
'mod'=13
252+
'intersect'=14
253+
'/\\'=15
254+
'unite'=16
255+
'\\/'=17
256+
'setsubtract'=18
257+
'\\'=19
258+
'in'=20
259+
'subset'=21
260+
'⊆'=22
261+
'superset'=23
262+
'⊇'=24
263+
'divides'=25
264+
'|'=26
265+
'>='=27
266+
'<='=28
267+
'>'=29
268+
'<'=30
269+
'='=31
270+
'<>'=32
271+
'≡'=33
272+
'('=34
273+
')'=35
274+
'not'=36
275+
'and'=37
276+
'&'=38
277+
'xor'=39
278+
'or'=40
279+
'implies'=41
280+
'->'=42
281+
'provided'=43
282+
'=>'=44
283+
':'=45
284+
'forall'=46
285+
'∀'=47
286+
'exists!'=48
287+
'∃!'=49
288+
'exists'=50
289+
'∃'=51
290+
','=52
291+
';'=53
292+
'+oo'=54
293+
'-oo'=55
294+
'(|'=56
295+
'|)'=57
296+
'#'=58
297+
']T'=59
297298
'{'=60
298299
'}'=61
299300
'log('=62
@@ -432,36 +433,37 @@ WS=237
432433
'Chi('=195
433434
'det('=196
434435
'transpose('=197
435-
'dot('=198
436-
'cross('=199
437-
'laplacian('=200
438-
'forwarddifference('=201
439-
'jacobian('=202
440-
'hessian('=203
441-
'resultant('=204
442-
'conjugate('=205
443-
'conj('=206
444-
'arg('=207
445-
'Re('=208
446-
'Im('=209
447-
're('=210
448-
'real('=211
449-
'im('=212
450-
'imag('=213
451-
'trunc('=214
452-
'zeta('=215
453-
'riemannzeta('=216
454-
'hurwitzzeta('=217
455-
'heaviside('=218
456-
'unitstep('=219
457-
'isprime('=220
458-
'pochhammer('=221
459-
'doublefactorial('=222
460-
'factorial2('=223
461-
'fractionalpart('=224
462-
'quotient('=225
463-
'domain('=226
464-
'piecewise('=227
465-
'apply('=228
466-
'lambda('=229
467-
'NaN'=234
436+
'trace('=198
437+
'dot('=199
438+
'cross('=200
439+
'laplacian('=201
440+
'forwarddifference('=202
441+
'jacobian('=203
442+
'hessian('=204
443+
'resultant('=205
444+
'conjugate('=206
445+
'conj('=207
446+
'arg('=208
447+
'Re('=209
448+
'Im('=210
449+
're('=211
450+
'real('=212
451+
'im('=213
452+
'imag('=214
453+
'trunc('=215
454+
'zeta('=216
455+
'riemannzeta('=217
456+
'hurwitzzeta('=218
457+
'heaviside('=219
458+
'unitstep('=220
459+
'isprime('=221
460+
'pochhammer('=222
461+
'doublefactorial('=223
462+
'factorial2('=224
463+
'fractionalpart('=225
464+
'quotient('=226
465+
'domain('=227
466+
'piecewise('=228
467+
'apply('=229
468+
'lambda('=230
469+
'NaN'=235

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ namespace AngouriMath.Core.Antlr {
3636
[System.Diagnostics.DebuggerNonUserCode]
3737
[System.CLSCompliant(false)]
3838
internal partial class AngouriMathBaseListener : IAngouriMathListener {
39+
/// <summary>
40+
/// Enter a parse tree produced by <see cref="AngouriMathParser.indexed_atom"/>.
41+
/// <para>The default implementation does nothing.</para>
42+
/// </summary>
43+
/// <param name="context">The parse tree.</param>
44+
public virtual void EnterIndexed_atom([NotNull] AngouriMathParser.Indexed_atomContext context) { }
45+
/// <summary>
46+
/// Exit a parse tree produced by <see cref="AngouriMathParser.indexed_atom"/>.
47+
/// <para>The default implementation does nothing.</para>
48+
/// </summary>
49+
/// <param name="context">The parse tree.</param>
50+
public virtual void ExitIndexed_atom([NotNull] AngouriMathParser.Indexed_atomContext context) { }
3951
/// <summary>
4052
/// Enter a parse tree produced by <see cref="AngouriMathParser.factorial_expression"/>.
4153
/// <para>The default implementation does nothing.</para>

0 commit comments

Comments
 (0)