diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index a28df484c..ab4ed66b1 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -205,6 +205,27 @@ are those of [#2147](https://github.com/asc-community/AngouriMath/issues/2147) a | `"[1, 0]T * A * [0, 1]".ToEntity().InnerSimplified` | `0` | unchanged | | `"[1, 2] * det(A)".ToEntity().InnerSimplified` | `[det(A), 2 * det(A)]` | unchanged | +### 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 }")]