Skip to content

Compile dot, cross, det and transpose of a name compiled as a number - #2558

Merged
Rafael-SOWNet merged 2 commits into
masterfrom
compile-dot-of-a-vector-expression
Nov 26, 2026
Merged

Rafael-SOWNet merged 2 commits into
masterfrom
compile-dot-of-a-vector-expression

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Since #105, simplification leaves dot, cross, det, transpose, trace and an entry A[k, l] as nodes where a name stands beside a written matrix, as in dot([0, 0, 1], cos(p) * [x, y, z]), because the name may stand for a matrix. Both compilers inherited that caution, although a variable compiled as a number is one. So the third component of a rotated vector, dot([0, 0, 1], cos(p) * [x, y, z] + sin(p) / abs([a, b, c]) * cross([a, b, c], [x, y, z])), could not be compiled at all.

What was refused or wrong

2.9.0 this PR
that expression, .Compile("a", "b", "c", "p", "x", "y", "z") throws: DotProductf does not support compilation its third component, cos(p) z + sin(p) (a y - b x) / abs([a, b, c])
the same, Compile<double, …, double> throws: vectors over Int64 and Double the same; -3.9818867534572675 at (0.3, 1, 1.7, 2.4, 3.1, 3.8, 4.5)
dot(x * [1, 2], [3, 4]), Compile("x") throws 11 x
dot(x * [1, 2], [3, 4]), Compile<double, double> at x = 0.5 0 5.5
det(x * [[1, 2], [3, 4]]), Compile<double, double> at x = 0.5 0 -0.5
[1, 0]T * transpose(x * [[1, 2], [3, 4]]) * [0, 1], either compiler, at x = 0.3 0 3 x: 0.8999999999999999
[0, 0, 1]T * cross(x * [1, 0, 0], [0, y, 0]), either compiler throws x y
det(x * A), Compile<double, long[,], double> at x = 0.5, A = [[1, 2], [3, 4]] 0 -0.5
[1, 0]T * transpose(A) * [0, 1], with A compiled as a number 0 throws: the transpose is taken of a number

The full table, measured on the 2.9.0 package and on this branch, is the new BREAKING-CHANGES entry.

How

  • MatrixOperationsOverNumbers (new, in Functions/Compilation) carries the six nodes out before compiling, innermost first, wherever every name in one is compiled as a number. For Compile(params Variable[]) that is every name; for Compile<...>, every argument that is not a vector or a matrix. It goes through each node's own simplification with a new namesAreNumbers switch, so the rules for conditions, piecewise and det(transpose(X)) are not copied.
  • A node still left that is taken of a number has no value, and is refused before simplification can read it as a number and multiply it into a written matrix's entries. The FastExpression compiler refuses any node that is left, saying that Compile<..., double[]> and Compile<..., double[,]> take vectors and matrices, instead of asking for a bug report.
  • The LINQ tensor path widens rather than narrows. A matrix beside a wider number is widened to it (TensorCompilation.WidenedBeside), where the number used to be converted to the matrix's element type and truncated. dot and cross widen two vectors over different element types, as a sum of two matrices already did.
  • A name compiled as a matrix is untouched: the operation is compiled on the matrix passed in, as before.

Substitute

The 2.8.0 entry said these nodes are computed "when they are substituted". Substitute computes nothing in any node, and that entry's own table already substituted and then simplified. The sentence now says so, and a test in DotAndCrossNodesTest pins it.

Tested

  • MatrixOperationOverNumbersCompilationTest (new) and additions to DotAndCrossNodesTest check each value against the definition written out, at real, negative and non-real points. 22 of them fail on 2.9.0; the others are controls and pass on both.
  • 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 timeout inside the full parallel run that passes alone. F# wrapper tests: 135 of 135. The CrashCheck harness: 2618 cases, none crashed, none unexpected.

Not in this PR

🤖 Generated with Claude Code

Rafael-SOWNet and others added 2 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>
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.75362% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.71%. Comparing base (b02ab06) to head (629ca62).

Files with missing lines Patch % Lines
...nctions/Compilation/MatrixOperationsOverNumbers.cs 92.10% 1 Missing and 2 partials ⚠️
...unctions/Compilation/IntoLinq/TensorCompilation.cs 77.77% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2558      +/-   ##
==========================================
+ Coverage   86.70%   86.71%   +0.01%     
==========================================
  Files         343      344       +1     
  Lines       74754    74810      +56     
  Branches    21810    21822      +12     
==========================================
+ Hits        64814    64874      +60     
+ Misses       5374     5368       -6     
- Partials     4566     4568       +2     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.22% <92.75%> (+0.01%) ⬆️
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 Δ
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 87.14% <100.00%> (-0.18%) ⬇️
...Functions/Compilation/IntoLinq/IntoLinqCompiler.cs 97.81% <100.00%> (+0.04%) ⬆️
...pilation/IntoLinq/TensorCompilation.Elementwise.cs 87.20% <100.00%> (+4.26%) ⬆️
...luation/Evaluation.Omni/Evaluation.Omni.Classes.cs 87.33% <100.00%> (+0.16%) ⬆️
...unctions/Compilation/IntoLinq/TensorCompilation.cs 81.63% <77.77%> (-0.22%) ⬇️
...nctions/Compilation/MatrixOperationsOverNumbers.cs 92.10% <92.10%> (ø)

... and 5 files with indirect coverage changes

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

@Rafael-SOWNet
Rafael-SOWNet merged commit b90ac49 into master Nov 26, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in AngouriMath Nov 26, 2026
Rafael-SOWNet added a commit that referenced this pull request Nov 26, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
@Rafael-SOWNet
Rafael-SOWNet deleted the compile-dot-of-a-vector-expression branch November 26, 2026 15:42
Rafael-SOWNet added a commit that referenced this pull request Nov 26, 2026
…hader (#2562)

MathS.Shaders.Hlsl and Glsl refused dot, cross, det, transpose, trace and an entry of an
expression with a name beside a written matrix, such as dot(x * [1, 2], [3, 4]), giving as the
reason that every variable of the shader is a number, which is why each has a value. The shader
now carries them out with MatrixOperationsOverNumbers, as Compile does since #2558, and a product
of written matrices left after that is reduced to the number it gives, as Compile reduces it
(#425). An operation still left, such as a transpose of a variable, is refused before
simplification could read it as a number beside the written matrices (#2557).

Closes #2561.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Rafael-SOWNet added a commit that referenced this pull request Nov 27, 2026
v2.9.0 and 2.10.0's last kernel change, from one interleaved run of the
key-commits workflow (45925911365). A 3067th column is added to the two
history tables at the top, from the Kernel Benchmark's run on that commit
(45924828059).

Allocation is flat except CompileEasy +2.47% and CompileHard +1.40%, one
step at the 3059th (#2558), a walk of the nodes before compiling. The
SolveHard timing rise on the runner is not seen re-timed on one desktop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rafael-SOWNet added a commit that referenced this pull request Nov 27, 2026
* Temporary: measure the 3053rd and the 3067th, interleaved, for the 2.10.0 pair

Restored before this branch is merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Revert "Temporary: measure the 3053rd and the 3067th, interleaved, for the 2.10.0 pair"

This reverts commit bae6f6e.

* Measure the 2.10.0 performance pair, the 3053rd and the 3067th

v2.9.0 and 2.10.0's last kernel change, from one interleaved run of the
key-commits workflow (45925911365). A 3067th column is added to the two
history tables at the top, from the Kernel Benchmark's run on that commit
(45924828059).

Allocation is flat except CompileEasy +2.47% and CompileHard +1.40%, one
step at the 3059th (#2558), a walk of the nodes before compiling. The
SolveHard timing rise on the runner is not seen re-timed on one desktop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

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.

1 participant