Skip to content

Commit 00d857b

Browse files
det, transpose, dot, cross and resultant are functions in an expression (#2180)
Modenov's Problems in Geometry computes areas, collinearity, concurrency and elimination with determinants, vector products and resultants, and none of them could be written in an expression: det(M) was refused by name, transpose(x) and cross(a) were read as products, and dot, a two-vector cross and resultant did not parse. Each is computed from its arguments when it is parsed, since there is no node for any of them: det through Matrix.Determinant, transpose through Matrix.T, resultant through MathS.Polynomials.Resultant, and dot and cross from the components. dot conjugates neither side, as SymPy's dot and Mathematica's Dot do not. cross of two vectors is the vector product in three dimensions, and cross of one vector in the plane is that vector turned through a right angle anticlockwise, as Mathematica's Cross of one vector is, so dot(cross(a), b) is the book's pseudo-scalar product. An argument has to be a matrix or a vector written out, or one once its operations are carried out; anything else throws InvalidArgumentParseException naming the function. Where the fraction-free elimination cannot read the entries as polynomials, Matrix.Determinant took GenericTensor's Laplace expansion, which compiles an expression tree: ILC reports IL3050 on it, and det(M) in the parser made it reachable from every native build, so the C++ wrapper's and the Trimming & NativeAOT jobs failed to publish. The expansion along the first row is now written over the entries. It is the same expansion, needs no lock on GenericTensor's shared scratch matrix, and agrees with GenericTensor's on 60 generated matrices of roots, sines, logarithms and symbols, compared by value. The native smoke test parses a det that takes it, and a dot of a cross. McpUnitTests checks that angourimath://syntax names every function the grammar accepts. det(x), transpose(x) and cross(x) are now refused with InvalidArgumentParseException, since x is not a matrix, which that test reads as a function of something else, as it reads a count refusal. The refusal's example is the function's own: cross([1, 2, 3], [4, 5, 6]) rather than a matrix. Closes #2147. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent 766e34b commit 00d857b

20 files changed

Lines changed: 1431 additions & 992 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,20 +263,18 @@ the expression's own domain as its condition ([#2142](https://github.com/asc-com
263263
| `"(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` |
264264
| `"(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` |
265265

266-
### `conj`, `arg` and `det` are refused by name
266+
### `conj` and `arg` are refused by name
267267

268268
**Not silent: these now throw `UnrecognizedFunctionParseException` where they parsed.** A name the parser does not
269269
know, followed by a bracket, is read as a product, so `arg(z)` was the variable `arg` times `z`, and `arg(z) = 1`
270270
solved to `{ 1 / arg }`. `conjugate` was already refused by name; `conj` and `arg` are the conjugate and the argument
271-
as other systems spell them, and `det` is the determinant, which a `Matrix` has as the property `Determinant` but an
272-
expression cannot call. `re` and `im` are still read as products, as before: two letters is short enough to be a
271+
as other systems spell them. `re` and `im` are still read as products, as before: two letters is short enough to be a
273272
variable's name ([#2136](https://github.com/asc-community/AngouriMath/issues/2136)).
274273

275274
| Input | Was (2.6.0) | Now |
276275
|---|---|---|
277276
| `"arg(z)".ToEntity()` | `arg * z` | throws `UnrecognizedFunctionParseException` |
278277
| `"conj(z)".ToEntity()` | `conj * z` | throws `UnrecognizedFunctionParseException` |
279-
| `"det([[1, 2], [3, 4]])".ToEntity()` | `det * [[1, 2], [3, 4]]` | throws `UnrecognizedFunctionParseException` |
280278

281279
### Over the reals, a modulus of a polynomial is split at the polynomial's real roots
282280

@@ -357,6 +355,26 @@ is rated no shorter ([#2135](https://github.com/asc-community/AngouriMath/issues
357355
| `"cos(a + b)".Expand()` | `cos(a + b)` | `cos(a) * cos(b) + -sin(a) * sin(b)` |
358356
| `"cos(a - b)".Expand()` | `cos(a - b)` | `cos(a) * cos(b) + sin(a) * sin(b)` |
359357

358+
### `det`, `transpose`, `dot`, `cross` and `resultant` are functions in an expression
359+
360+
**Not silent: these names followed by a bracket are now computed, or refused, where they were read as products or
361+
did not parse.** `det(M)` is the determinant, `transpose(M)` the transpose, `dot(a, b)` the sum of the products of the
362+
components, `cross(a, b)` the vector product in three dimensions and `cross(a)` a vector of the plane turned through a
363+
right angle anticlockwise, as Mathematica's `Cross` of one vector is; `resultant(p, q, x)` eliminates `x`. There is
364+
no node for any of them, so each is computed when it is parsed, and left as unsimplified as what it stands for would
365+
be written out: `dot([1, 2], [3, 4])` is `1 * 3 + 2 * 4`. An argument that is not a matrix or a vector of
366+
the right shape, written out or once its operations are carried out, throws `InvalidArgumentParseException`
367+
([#2147](https://github.com/asc-community/AngouriMath/issues/2147)).
368+
369+
| Input | Was (2.6.0) | Now |
370+
|---|---|---|
371+
| `"det([[1, 2], [3, 4]])".ToEntity()` | `det * [[1, 2], [3, 4]]` | `-2` |
372+
| `"transpose([1, 2])".ToEntity()` | `transpose * [1, 2]` | `[[1, 2]]`, the row |
373+
| `"transpose(x)".ToEntity()` | `transpose * x` | throws `InvalidArgumentParseException` |
374+
| `"cross([a, b])".ToEntity()` | `cross * [a, b]` | `[-b, a]` |
375+
| `"dot([1, 2], [3, 4])".ToEntity()` | throws `UnhandledParseException` | `1 * 3 + 2 * 4` |
376+
| `"resultant(x^2 - 1, x - a, x)".ToEntity()` | throws `UnhandledParseException` | `a ^ 2 - 1` |
377+
360378
### A subscript may have a subscript
361379

362380
**Not silent: what was a parse error is a name.** A name took at most one `_`, so `x_1_2` and `X_a_beta` were lexer
@@ -370,6 +388,19 @@ in LaTeX as `X_{a_{\beta}}`. `_` still neither begins nor ends a name, and two d
370388
| `MathS.Var("X_a_beta").Latexize()` | an `UnhandledParseException` | `X_{a_{\beta}}` |
371389
| `"x__1".ToEntity()` | an `UnhandledParseException` | an `UnhandledParseException` |
372390

391+
### A determinant of entries that are not polynomials is expanded by the library itself
392+
393+
**Silent: the same value, printed differently.** Where the fraction-free elimination cannot read the entries as
394+
polynomials, `Matrix.Determinant` expanded along the first row with GenericTensor's Laplace expansion. That expansion
395+
compiles an expression tree, which a native build cannot publish, and `det(M)` in an expression made it reachable from
396+
every one. It is now written over the entries, with the same expansion along the first row and the cofactors'
397+
alternating signs as subtractions ([#2147](https://github.com/asc-community/AngouriMath/issues/2147)).
398+
399+
| Input | Was (2.6.0) | Now |
400+
|---|---|---|
401+
| `"[[sin(x), cos(x)], [-cos(x), sin(x)]]".ToEntity()` as a `Matrix`, `.Determinant` | `sin(x) ^ 2 + -cos(x) * -cos(x)` | `sin(x) ^ 2 - cos(x) * -cos(x)` |
402+
| `.Determinant.Simplify()` of `[[sqrt(a), b, 1], [c, sin(d), 2], [3, e_1, ln(f)]]` | `e_1 * ((-2) * sqrt(a) + c) + sin(d) * (sqrt(a) * ln(f) - 3) + 6 * b - b * c * ln(f)` | `sqrt(a) * (sin(d) * ln(f) - 2 * e_1) - b * (c * ln(f) - 6) + c * e_1 - 3 * sin(d)` |
403+
373404
### A numerical definite integral is Gauss–Legendre, with the digits it knows
374405

375406
**Silent: wrong answers, and values where there was NaN.** `Entity.DefiniteIntegral(x, a, b, steps)` was the mean of

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
240240
[Tests/UnitTests/Convenience/IversonTest.cs]
241241
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
242242

243+
[Tests/UnitTests/Convenience/MatrixFunctionsInAnExpressionTest.cs]
244+
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
245+
243246
[Tests/UnitTests/Common/ExponentialOfALogarithmTest.cs]
244247
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
245248

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,15 @@ atom returns[Entity value]
626626
| 'Ci(' args = function_arguments ')' { Assert("Ci", 1, $args.list.Count); $value = MathS.Ci($args.list[0]); }
627627
| 'Shi(' args = function_arguments ')' { Assert("Shi", 1, $args.list.Count); $value = MathS.Shi($args.list[0]); }
628628
| 'Chi(' args = function_arguments ')' { Assert("Chi", 1, $args.list.Count); $value = MathS.Chi($args.list[0]); }
629+
/* The determinant, the transpose, the scalar and the vector product, and the resultant, each
630+
computed from what it is applied to: there is no node for any of them, so a matrix or a
631+
vector has to be written out, or be one once its operations are carried out.
632+
https://github.com/asc-community/AngouriMath/issues/2147 */
633+
| 'det(' args = function_arguments ')' { Assert("det", 1, $args.list.Count); $value = ParsingHelpers.Determinant($args.list[0]); }
634+
| 'transpose(' args = function_arguments ')' { Assert("transpose", 1, $args.list.Count); $value = ParsingHelpers.Transposed($args.list[0]); }
635+
| 'dot(' args = function_arguments ')' { Assert("dot", 2, $args.list.Count); $value = ParsingHelpers.Dot($args.list[0], $args.list[1]); }
636+
| 'cross(' args = function_arguments ')' { Assert("cross", (1, 2), $args.list.Count); $value = ParsingHelpers.Cross($args.list); }
637+
| 'resultant(' args = function_arguments ')' { Assert("resultant", 3, $args.list.Count); $value = ParsingHelpers.Resultant($args.list[0], $args.list[1], $args.list[2]); }
629638

630639
/* Names the library does not have. Each is a function every other CAS spells this way, so
631640
a caller reaches for it, and without these rules each is silently read as a product --
@@ -634,13 +643,11 @@ atom returns[Entity value]
634643

635644
| 'trunc(' args = function_arguments ')' { $value = NotImplementedFunction("trunc", "rounding functions"); }
636645
| 'conjugate(' args = function_arguments ')' { $value = NotImplementedFunction("conjugate", "complex conjugate as a symbolic function"); }
637-
/* conj and arg are the conjugate and the argument as other systems spell them, and det the
638-
determinant, which exists on a matrix but not in an expression. Each was read as a product:
639-
arg(z) was arg * z. re and im are left as products, as MissingFunctionNamesRefusedTest
640-
records. https://github.com/asc-community/AngouriMath/issues/2136 */
646+
/* conj and arg are the conjugate and the argument as other systems spell them. Each was read
647+
as a product: arg(z) was arg * z. re and im are left as products, as
648+
MissingFunctionNamesRefusedTest records. https://github.com/asc-community/AngouriMath/issues/2136 */
641649
| 'conj(' args = function_arguments ')' { $value = NotImplementedFunction("conj", "complex conjugate as a symbolic function"); }
642650
| 'arg(' args = function_arguments ')' { $value = NotImplementedFunction("arg", "complex argument as a symbolic function"); }
643-
| 'det(' args = function_arguments ')' { $value = NotImplementedFunction("det", "determinant in an expression; a Matrix has the property Determinant"); }
644651
| 'domain(' args = function_arguments ')'
645652
{
646653
Assert("domain", 2, $args.list.Count);

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

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

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,18 @@ T__184=185
186186
T__185=186
187187
T__186=187
188188
T__187=188
189-
NEWLINE=189
190-
NUMBER=190
191-
SPECIALSET=191
192-
BOOLEAN=192
193-
NAN=193
194-
VARIABLE=194
195-
COMMENT=195
196-
WS=196
189+
T__188=189
190+
T__189=190
191+
T__190=191
192+
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
197201
'!'=1
198202
'^'=2
199203
'-'=3
@@ -373,13 +377,17 @@ WS=196
373377
'Ci('=177
374378
'Shi('=178
375379
'Chi('=179
376-
'trunc('=180
377-
'conjugate('=181
378-
'conj('=182
379-
'arg('=183
380-
'det('=184
381-
'domain('=185
382-
'piecewise('=186
383-
'apply('=187
384-
'lambda('=188
385-
'NaN'=193
380+
'det('=180
381+
'transpose('=181
382+
'dot('=182
383+
'cross('=183
384+
'resultant('=184
385+
'trunc('=185
386+
'conjugate('=186
387+
'conj('=187
388+
'arg('=188
389+
'domain('=189
390+
'piecewise('=190
391+
'apply('=191
392+
'lambda('=192
393+
'NaN'=197

0 commit comments

Comments
 (0)