Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Sources/AngouriMath/Docs/Usage/Shaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,25 @@ internal static GeneratedSource Emit(SourceDialect dialect, IReadOnlyList<KeyVal

// The rewrites Compile makes before compiling, so that the shader computes what it
// computes. A membership in an interval, a finite set or a union of them is the comparisons
// it stands for. A matrix operation on matrices written out is reduced to the number it gives,
// as Compile reduces it, since a shader output is a number. A quotient of one exponential
// it stands for. A matrix operation is carried out wherever its names are numbers, which every
// variable of a shader is, as Compile carries it out: dot(x [1, 2], [3, 4]) is 11 x. A product of
// matrices written out that is left then is reduced to the number it gives, as Compile reduces it
// (#425), since a shader output is a number. A quotient of one exponential
// plus a constant by the same exponential plus another, which is how tanh is spelled, is
// taken as 1 + (c1 - c2) / (b^v + c2), which does not divide an overflow by an overflow:
// in single precision e^(2x) overflows from x = 45 on.
// https://github.com/asc-community/AngouriMath/issues/2254
private static Entity Prepared(Entity expression)
{
// https://github.com/asc-community/AngouriMath/issues/2561
if (expression.Nodes.Any(node => 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));
}

Expand Down
13 changes: 13 additions & 0 deletions Sources/Tests/UnitTests/Convenience/ShadersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 }")]
Expand Down
Loading