Repository navigation
Do not truncate a constant or a quotient beside a matrix of whole numbers - #2559
Merged
Rafael-SOWNet merged 4 commits intoNov 26, 2026
Merged
Conversation
The 2.8.0 entry in BREAKING-CHANGES said dot and cross were computed "when they are substituted, by Simplify, Evaled, or a compiled delegate", which reads as though Substitute carried the product out. It does not, and never did: dot([0, 0, 1], v) with v = [x, y, z] substituted is dot([0, 0, 1], [x, y, z]) on 2.8.0, 2.9.0 and master. The behaviour is the right half. Substitute computes nothing in any node, and the matrix nodes beside these - det, transpose, trace, an entry - rebuild the same way; the entry's own table, the det entry's table and the MathS.Matrices.DotProduct example all substitute and then call Simplify or Evaled. So the sentence is reworded, the test class's summary with it, and a test pins that substitution leaves the node and simplification carries it out. Part of #105. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Since #105 simplification leaves dot, cross, det, transpose, trace and an entry as nodes where a name stands beside a written matrix, as in dot([0, 0, 1], cos(p) * [x, y, z]): the name may stand for a matrix. Both compilers inherited that caution although a variable compiled as a number is one. - Compile(params Variable[]), whose variables are all numbers, had no form for any of the six nodes, so dot(x * [1, 2], [3, 4]) was refused with a request to report a bug. - Compile<...> converted a double to the long of a written vector of whole numbers, so dot(x * [1, 2], [3, 4]) and det(x * [[1, 2], [3, 4]]) were 0 at x = 0.5, and dot([0, 0, 1], v) over a double vector was refused for its element types. - Both multiplied a transpose beside written matrices out as a number: [1, 0]T * transpose(x * [[1, 2], [3, 4]]) * [0, 1] was 0, not 3 x. MatrixOperationsOverNumbers now carries the six out before compiling, innermost first, wherever every name in one is compiled as a number, through the nodes' own simplification with a namesAreNumbers switch, so the rules for conditions, piecewise and det(transpose(X)) are not copied. One still left that is taken of a number has no value and is refused before simplification can read it as a number; FastExpression refuses any left, saying where vectors and matrices are compiled. A name compiled as a matrix is untouched. The LINQ tensor path widens rather than narrows: a matrix beside a wider number is widened to it, which fixes det(x * A) over a long[,] A (0 at x = 0.5) and refuses no complex number beside a double matrix, and dot and cross widen two vectors over different element types as a sum of two matrices already did. Measured on v2.9.0 and on this branch; the table is in BREAKING-CHANGES. On v2.9.0, 22 of the new tests fail and the rest, the controls, pass; all pass here. Not fixed here: InnerSimplified itself still reads [1, 0]T * transpose(A) * [0, 1] as 0 for a symbolic A, and the LINQ compiler still compiles a constant beside a long[,] argument as a long, so trace(A * 0.5) is 0. Part of #105. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…bers Compile<...> with a matrix argument over long, int or BigInteger converted every constant in the expression to that element type, so 0.5 was 0, 1 / 4 was 0 and pi was 3, inside the matrix arithmetic and outside it: trace(A * 0.5) and trace(A) * 0.5 were 0 at A = [[1, 2], [3, 4]]. It also divided such a matrix entry by entry with truncation, so trace(A / 2) was 2 where it is 2.5, and refused 1 / A, A / B and A ^ (-1) for want of an inverse among the whole numbers. A constant is now converted to the element type only where that type holds it, an integer for the whole numbers; otherwise it keeps its own type and the matrix it meets is widened to it. A quotient of a matrix of whole numbers, and a power of one to a negative constant, are computed over double, as a quotient of two whole numbers already was. What stays whole is still computed over the whole numbers, exactly; a result that is not whole, asked for as long[,], is refused rather than truncated, and A ^ 0.5 has no value, as over double, where it was the identity. AMatrixOfWholeNumbersIsNotInverted is renamed for that reason: the inverse is computed now, and refused only as a matrix of whole numbers. Measured on v2.9.0 (the published package) and on this branch; the table is in BREAKING-CHANGES. The 24 new and changed cases in CompileMatrixTest fail on the base commit and pass here. Full UnitTests: 21455 passed, 5 failed. Four fail identically on v2.9.0 on this machine, three on the Dutch decimal comma and one on timing; ln(x) * erf(x) times out only inside the full run and passes alone on both. FSharpWrapperUnitTests 135/135. Part of #2556. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2559 +/- ##
==========================================
+ Coverage 86.71% 86.73% +0.02%
==========================================
Files 344 344
Lines 74810 74820 +10
Branches 21822 21827 +5
==========================================
+ Hits 64869 64896 +27
+ Misses 5372 5358 -14
+ Partials 4569 4566 -3
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rafael-SOWNet
changed the base branch from
compile-dot-of-a-vector-expression
to
master
November 26, 2026 13:48
Rafael-SOWNet
added a commit
that referenced
this pull request
Nov 26, 2026
…NGES entries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 #2556.
Stacked on #2558. It reuses that PR's widening of a matrix beside a wider number (
TensorCompilation.WidenedBeside), so its base is that PR's branch and this diff shows only its own commit. Merge #2558 first; this PR then needs retargeting tomaster.Compiling over a matrix argument of whole numbers (
long[,],int[,]orBigInteger[,]) converted every constant in the expression to that type, so a fraction was truncated and the delegate returned a wrong number with no error.What was wrong
With
A = [[1, 2], [3, 4]]:trace(A * 0.5)overlong[,]02.5trace(A) * 0.5overlong[,]02.5A[1, 1] * 0.5overlong[,]00.5sqrt(det(A) + 4)overlong[,]11.4142135623730951trace(A / 2)overlong[,]22.5det(A) / 4overlong[,]-0.5-0.5trace(A) * 0.5overint[,]02.5A * 0.5asCompile<long[,], double[,]>[[0.5, 1], [1.5, 2]]The full table, measured on the 2.9.0 package and on this branch, is the new BREAKING-CHANGES entry.
Why
IntoLinqCompilerpicked one constant type, the element type of the matrix arguments, and converted everyNumberin the expression to it. Overlongthat turned0.5and1 / 4into0andpiinto3, inside the matrix arithmetic and outside it.TensorCompilation.Binarydivided a matrix overlongentry by entry with integer division, sotrace(A / 2)was2.1 / A,A / BandA ^ (-1)were refused, for want of an inverse over the whole numbers.How
doublebefore it is divided or raised to a negative constant power, as a quotient of two whole numbers already was. So1 / A,A / BandA ^ (-1)over along[,]argument are now computed overdouble.long[,], is refused rather than truncated.A ^ 0.5overlong[,]was the identity and now has no value, as overdouble[,].AMatrixOfWholeNumbersIsNotInvertedis renamed for that reason: the inverse is computed now, and refused only as a matrix of whole numbers.Tested
CompileMatrixTest, overlong,intandBigInteger, each checked against evaluation withAsubstituted. They fail without this commit and pass with it.Not in this PR
int, beside anint[,]argument, still wraps around, asA * Aoverintdoes.A ^ noverlong[,], with a variablenthat is negative when called, is refused rather than computed overdouble, because the result type is fixed beforenis known.🤖 Generated with Claude Code