Repository navigation
Compile a matrix argument as an array, with its inverse and whole powers - #2429
Merged
Merged
Conversation
A matrix compiles under the LINQ compiler since #1425, to a GenTensor, with the sum, difference, product, scalar operations and whole power as loops over the element wrapper. work/compilecheck's matrix section found what is wrong around that: - A matrix power's exponent was cast to int, which truncates. On master [[x, 1], [0, y]] ^ x over double at 0.7 was the identity; on 2.6.0 simplification raised each entry to the power instead, [[0.779, 1], [0, NaN]]. Evaluation leaves a matrix to a power that is not whole unevaluated. The exponent is now checked to be whole when the delegate is called, and InvalidMatrixOperationException is thrown where it is not. An exponent of type Complex compiles, where it was refused for want of a conversion to int. - The inverse was not compiled: A ^ (-1) of an argument threw NotSupportedException when called, and 1 / A and A / B were refused, where evaluation answers each with the inverse. It is Gauss-Jordan elimination with partial pivoting over double, float and Complex; a zero pivot gives NaN in every entry, as a compiled division by zero has no value. A matrix of whole numbers is not inverted, its inverse not being one. - Written matrices whose shapes do not fit, which simplification leaves as written, are refused by Compile with InvalidMatrixOperationException naming the shapes. On master such a delegate threw at every call; on 2.6.0 [[x, 1, y]] ^ 2 compiled to the entrywise square. A matrix argument's shape is checked when the delegate is called. - double[,], Complex[,] and the other element types' arrays were refused, or leaked KeyNotFoundException. They are matrices as arguments and as the result, converted to and from the tensor at the delegate's boundary, so a caller need not know GenericTensor. - FastExpression simplifies an expression with a matrix in it first, as the LINQ compiler does since #425, so a number built out of matrices compiles there too. - That simplification reads a variable as a number, so with a matrix argument beside written matrices [[1, 0]] * A * [[0], [1]] was multiplied out to 0, which the LINQ delegate returned whatever A was, on 2.6.0 as on master. Only the parts that do not mention a matrix argument are simplified now, a written matrix beside a matrix argument is over the argument's element type, and a matrix known when compiling to be 1 x 1 compiles as a number, its entry. Measured: compilecheck's matrix section on master, FastExpression 2 of 33, LINQ Complex 27, LINQ double 28, two disagreements; on the branch FastExpression 5, every matrix-argument row agrees with evaluation, and the one disagreement left is abs([x, y]) at a complex point, which is in simplification. Each new test compares a compiled matrix with Substitute then Evaled, entry by entry, at negative and non-real points, and the 1 x 1 regression against Substitute then Evaled at real and complex A, over double and Complex. Compile, matrix and tensor tests: 1264 passed. FSharpWrapperUnitTests: 134 passed. The NativeAOT smoke test, published with a quotient and a power over Complex[,] added: 74 of 74. The 2.6.0 values in BREAKING-CHANGES.md are measured on the 2.6.0 package. Closes #2422. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2429 +/- ##
==========================================
+ Coverage 86.81% 86.85% +0.03%
==========================================
Files 312 312
Lines 69551 69709 +158
Branches 20316 20378 +62
==========================================
+ Hits 60383 60546 +163
+ Misses 4932 4918 -14
- Partials 4236 4245 +9
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This was referenced Nov 13, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2422.
A matrix compiles under the LINQ compiler since #1425. This adds what was missing around it: matrices as
T[,]arguments and results, the inverse, a whole-exponent check on matrix powers, written shapes checked when compiling, and #425's simplify-first in FastExpression.Coverage, by
work/compilecheck's matrix section. 33 expressions inxandyare compared entry by entry withSubstitute→Evaled→EvalNumerical, at real and non-real points.6d5f1755aComplex/doubleCompile[[x, 1], [0, y]] ^ xoverdoubleInvalidMatrixOperationExceptionwhen calledGenTensor<double>/<Complex>(17 rows)A ^ (-1)throwsNotSupportedExceptionwhen called;1 / AandA / Bare refusedAgives NaNdouble[,]KeyNotFoundExceptionBehaviour changes (2.6.0 → now). The 2.6.0 values were measured on the 2.6.0 package; the entries are in
BREAKING-CHANGES.md.x / AatA = [[1, 2], [3, 4]],x = 2[[2, 1], [0.667, 0.5]][[-4, 2], [3, -1]]A ^ (-1)at[[1, 2], [3, 4]]NotSupportedExceptionwhen called[[-2, 1], [1.5, -0.5]]A / BUncompilableNodeExceptionAtimes the inverse ofB[[x, 1], [0, y]] ^ xoverdoubleat(0.7, -1.3)[[0.779, 1], [0, NaN]], each entry to the powerInvalidMatrixOperationExceptionComplexUncompilableNodeExceptionx[[x, 1, y]] ^ 2overComplexat(1, 2)[[1, 1, 4]]InvalidMatrixOperationExceptionwhen compilingA * Boverdouble[,]UncompilableNodeException[1, 0]T * [[a, b], [c, d]] * [0, 1]UncompilableNodeExceptionb[[1, 0]] * A * [[0], [1]]todouble, atA = [[1, 2], [3, -4]]0, for everyA2A + [[1, 2], [3, 4]]overGenTensor<double>UncompilableNodeException[[2, 4], [6, 0]][[1, 0]] * AoverGenTensor<double>KeyNotFoundException[[1, 2]][[1, 0]] * A * [[0], [1]]was a wrong answer on 2.6.0. Simplification before compiling read the argumentAas a number. Now only the parts that mention no matrix argument are simplified first. A result known to be 1×1 compiles as a number, and written constants compile over the arguments' element type.Out of scope
transpose,det,traceand indexing of a matrix argument, which need nodes: det, transpose, trace and an entry of a name for a matrix #2427, in a follow-up PR.sin(A)element by element, andT[]vectors: a follow-up PR. Matrix functions in the power-series sense are not computed by evaluation either.abs([x, y])simplifying tosqrt(x^2 + y^2), which disagrees with evaluation at a complex point. That is The norm of a vector with complex entries is NaN when evaluated and wrong when simplified #2423.Checked on this branch
Complex[,]were added to the smoke test.TensorCompilationhelpers are recorded inPublicApi.txt. The compiled expression trees call them, as they call the existingAdd,MultiplyandPower.🤖 Generated with Claude Code