Skip to content

Do not truncate a constant or a quotient beside a matrix of whole numbers - #2559

Merged
Rafael-SOWNet merged 4 commits into
masterfrom
constants-beside-a-matrix-of-whole-numbers
Nov 26, 2026
Merged

Rafael-SOWNet merged 4 commits into
masterfrom
constants-beside-a-matrix-of-whole-numbers

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

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 to master.

Compiling over a matrix argument of whole numbers (long[,], int[,] or BigInteger[,]) 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]]:

2.9.0 and master this PR
trace(A * 0.5) over long[,] 0 2.5
trace(A) * 0.5 over long[,] 0 2.5
A[1, 1] * 0.5 over long[,] 0 0.5
sqrt(det(A) + 4) over long[,] 1 1.4142135623730951
trace(A / 2) over long[,] 2 2.5
det(A) / 4 over long[,] -0.5 -0.5
trace(A) * 0.5 over int[,] 0 2.5
A * 0.5 as Compile<long[,], double[,]> refused [[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

  • IntoLinqCompiler picked one constant type, the element type of the matrix arguments, and converted every Number in the expression to it. Over long that turned 0.5 and 1 / 4 into 0 and pi into 3, inside the matrix arithmetic and outside it.
  • TensorCompilation.Binary divided a matrix over long entry by entry with integer division, so trace(A / 2) was 2. 1 / A, A / B and A ^ (-1) were refused, for want of an inverse over the whole numbers.

How

  • Constants: a constant is 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.
  • Division: a matrix of whole numbers is widened to double before it is divided or raised to a negative constant power, as a quotient of two whole numbers already was. So 1 / A, A / B and A ^ (-1) over a long[,] argument are now computed over double.
  • What stays whole is still computed exactly over the whole numbers, tested near 2^60. A result that is not whole, asked for as long[,], is refused rather than truncated. A ^ 0.5 over long[,] was the identity and now has no value, as over double[,]. AMatrixOfWholeNumbersIsNotInverted is renamed for that reason: the inverse is computed now, and refused only as a matrix of whole numbers.

Tested

  • 24 new and changed cases in CompileMatrixTest, over long, int and BigInteger, each checked against evaluation with A substituted. They fail without this commit and pass with it.
  • The failures in a full unit-test run are the same on 2.9.0: cases that depend on the culture (a decimal comma under a Dutch culture) and a timing test, plus a timeout inside the full parallel run that passes alone. F# wrapper tests: 135 of 135.

Not in this PR

  • An integer constant too large for int, beside an int[,] argument, still wraps around, as A * A over int does.
  • A ^ n over long[,], with a variable n that is negative when called, is refused rather than computed over double, because the result type is fixed before n is known.

🤖 Generated with Claude Code

Rafael-SOWNet and others added 3 commits November 26, 2026 13:26
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

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.73%. Comparing base (b90ac49) to head (1a28209).

Files with missing lines Patch % Lines
...unctions/Compilation/IntoLinq/TensorCompilation.cs 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.24% <90.90%> (+0.02%) ⬆️
fsharp 32.79% <ø> (ø)
interactive 40.09% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...Functions/Compilation/IntoLinq/IntoLinqCompiler.cs 97.90% <100.00%> (+0.09%) ⬆️
...unctions/Compilation/IntoLinq/TensorCompilation.cs 82.37% <75.00%> (+0.73%) ⬆️

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Rafael-SOWNet
Rafael-SOWNet changed the base branch from compile-dot-of-a-vector-expression to master November 26, 2026 13:48
@Rafael-SOWNet
Rafael-SOWNet merged commit 5a08e12 into master Nov 26, 2026
73 of 74 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Nov 26, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the constants-beside-a-matrix-of-whole-numbers branch November 26, 2026 14:40
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Compile over a long[,] or int[,] argument truncates every constant: trace(A) * 0.5 is 0

1 participant