From 7e0ce7ee3b5fd607c4321031fc91eede061c6325 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 26 Nov 2026 14:41:20 +0000 Subject: [PATCH] Carry out a matrix operation of a name beside a written matrix in a shader 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 --- BREAKING-CHANGES.md | 21 +++++++++++++++++++ Sources/AngouriMath/Docs/Usage/Shaders.md | 6 +++++- .../Compilation/IntoSource/ShaderSource.cs | 20 +++++++++++------- .../UnitTests/Convenience/ShadersTest.cs | 13 ++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 867459d8e..036e43fb0 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -175,6 +175,27 @@ truncated; a matrix power whose exponent is not whole has no value, as over `dou | `"A / B".Compile("A", "B")` | throws `UncompilableNodeException`: no inverse of a matrix of whole numbers | `[[16, 11], [34, 27]] / 29` to rounding | | `"1 / A".Compile("A")` | throws `UncompilableNodeException`: no inverse of a matrix of whole numbers | throws `UncompilableNodeException`: a matrix over `Double` | +### A shader carries out a scalar product, a determinant or a transpose of a name beside a written matrix + +**Not silent: what was refused is printed.** `MathS.Shaders.Hlsl` and `Glsl` refused `dot`, `cross`, `det`, +`transpose`, `trace` and an entry `A[k, l]` of an expression with a name beside a written matrix. Simplification +leaves these as written, since the name may stand for a matrix, but every variable of a shader is a number. Each is now +carried out as `Compile` carries it out, and a product of written matrices left after that is reduced to the number it +gives, as `Compile` reduces it. An operation that has no value, such as vectors of different lengths or a transpose of +a name, is still refused ([#2561](https://github.com/asc-community/AngouriMath/issues/2561)). + +| `MathS.Shaders.Hlsl` of the output `o` | Was (2.9.0) | Now | +|---|---|---| +| `dot(x * [1, 2], [3, 4])` | refused at `dot(x * [1, 2], [3, 4])` | `o = ((x * 3.0) + ((x * 2.0) * 4.0));` | +| `dot([1, 0], cos(y) * [x, y])` | refused at `dot([1, 0], cos(y) * [x, y])` | `o = (cos(y) * x);` | +| `det(x * [[1, 2], [3, 4]])` | refused at `det(x * [[1, 2], [3, 4]])` | `o = (-(2.0 * (x * x)));` | +| `trace(y * [[1, 2], [3, 4]]) + x` | refused at `trace(y * [[1, 2], [3, 4]])` | `o = ((y + (y * 4.0)) + x);` | +| `[0, 0, 1]T * cross(x * [1, 0, 0], [0, y, 0])` | refused at `[[0, 0, 1]]` | `o = (x * y);` | +| `[1, 0]T * transpose(x * [[1, 2], [3, y]]) * [0, 1]` | refused at `[[1, 0]]` | `o = (x * 3.0);` | +| `(x * [[1, 2], [3, 4]])[2, 1] + y` | refused at `(x * [[1, 2], [3, 4]])[2, 1]` | `o = ((x * 3.0) + y);` | +| `[1, 0]T * [[x, y], [1, 2]] * [0, 1]` | refused at `[[1, 0]]` | `o = y;` | +| `x + dot(x * [1, 2], [3, 4, 5])` | refused at `dot(x * [1, 2], [3, 4, 5])` | refused at `dot([x, x * 2], [3, 4, 5])` | + --- ## 2.9.0 — since 2.8.0 diff --git a/Sources/AngouriMath/Docs/Usage/Shaders.md b/Sources/AngouriMath/Docs/Usage/Shaders.md index 2a9697a25..8a486b2a1 100644 --- a/Sources/AngouriMath/Docs/Usage/Shaders.md +++ b/Sources/AngouriMath/Docs/Usage/Shaders.md @@ -222,7 +222,9 @@ exponent happens to be whole. **Nothing is simplified.** What is printed is the expression you give, so `Simplify` it first if you want a smaller one. The only rewrites are the ones `Compile` makes as well, and each keeps the -value: an operation on matrices written out is the number it gives; a membership in an interval +value: a scalar product, a determinant, a transpose, a trace or an entry is carried out with every +variable a number, so `dot(x * [1, 2], [3, 4])` is `11 x`, and a product of matrices written out +whose value is a number is that number; a membership in an interval or a finite set is the comparisons it stands for; and `(b^v + c1) / (b^v + c2)`, which is how `tanh` is spelled, is taken as `1 + (c1 - c2) / (b^v + c2)`, so that `tanh(x)` is 1 for a large `x` rather than infinity over infinity — in single precision `e^(2x)` overflows from `x = 45` on. @@ -238,6 +240,8 @@ or a finite set is the comparisons it stands for; and `(b^v + c1) / (b^v + c2)`, functions, and `gcd` and `lcm`, which need a loop; - sets and memberships in a set of numbers, and matrices: each output is one number, so assign a vector's entries one by one; +- a matrix operation with no value where every variable is a number, such as `dot` of two vectors + of different lengths or the transpose of a variable; - whatever `Compile` refuses — a derivative, an integral, a limit, a sum or a product left unevaluated, or a statement where a number is expected — with the message `Compile` gives. diff --git a/Sources/AngouriMath/Functions/Compilation/IntoSource/ShaderSource.cs b/Sources/AngouriMath/Functions/Compilation/IntoSource/ShaderSource.cs index 3b88d423a..0826fb5bc 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoSource/ShaderSource.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoSource/ShaderSource.cs @@ -91,21 +91,25 @@ internal static GeneratedSource Emit(SourceDialect dialect, IReadOnlyList node is Matrix)) - expression = expression.Replace(node => node switch - { - DotProductf(Matrix, Matrix) or CrossProductf(Matrix, Matrix) or Transposef(Matrix) - or Determinantf(Matrix) or Tracef(Matrix) or MatrixEntryf(Matrix, _, _) => node.InnerSimplified, - _ => node, - }); + { + expression = MatrixOperationsOverNumbers.Rewritten(expression, static _ => true); + // With an operation still left, simplification would read it as a number beside the written + // matrices (#2557); it is refused as it stands instead. + if (expression.Nodes.Any(node => node is Matrix) && !expression.Nodes.Any(MatrixOperationsOverNumbers.IsMatrixOperation)) + expression = expression.InnerSimplified; + } return ExponentialQuotients.Rewritten(CompiledMembership.Rewritten(expression)); } diff --git a/Sources/Tests/UnitTests/Convenience/ShadersTest.cs b/Sources/Tests/UnitTests/Convenience/ShadersTest.cs index c56b2439b..43731122d 100644 --- a/Sources/Tests/UnitTests/Convenience/ShadersTest.cs +++ b/Sources/Tests/UnitTests/Convenience/ShadersTest.cs @@ -139,6 +139,17 @@ public void TanhDoesNotDivideAnOverflowByAnOverflow() new Determinantf(MathS.Matrix(new Entity[,] { { "x", "y" }, { "1", "x" } })), new Tracef(MathS.Matrix(new Entity[,] { { "x", "y" }, { "1", "x" } })), (Entity)"sin(x)" + new DotProductf(new CrossProductf(MathS.Vector("x", "y", "1"), MathS.Vector("1", "y", "x")), MathS.Vector("1", "1", "1")), + // A name beside a written matrix, which simplification keeps in case the name is a matrix: every variable of + // a shader is a number. https://github.com/asc-community/AngouriMath/issues/2561 + "dot(x * [1, 2], [3, 4])", + "dot([1, 0], cos(y) * [x, y])", + "det(x * [[1, 2], [3, 4]])", + "trace(y * [[1, 2], [3, 4]]) + x", + "[0, 0, 1]T * cross(x * [1, 0, 0], [0, y, 0])", + "[1, 0]T * transpose(x * [[1, 2], [3, y]]) * [0, 1]", + "det(transpose(x * [[1, y], [2, 3]])) - y", + "(x * [[1, 2], [3, 4]])[2, 1] + y", + "[1, 0]T * [[x, y], [1, 2]] * [0, 1]", }; [Theory] @@ -220,6 +231,8 @@ public void VariablesAreRenamed() [InlineData("det(x)", "det(x)")] [InlineData("transpose(x)", "transpose(x)")] [InlineData("[x, y]", "[x, y]")] + [InlineData("x + dot(x * [1, 2], [3, 4, 5])", "dot([x, x * 2], [3, 4, 5])")] + [InlineData("[1, 0]T * transpose(x) * [0, 1]", "[[1, 0]]")] [InlineData("iverson(x in RR)", "x in RR")] [InlineData("beta(x, y) + 1", "beta(x, y)")] [InlineData("x + {1, 2}", "{ 1, 2 }")]