Skip to content

Commit 0f3df21

Browse files
A scalar over a matrix argument is not compiled (#2289)
Compiled, x / A with a matrix argument A divided x by every element: [[2, 1], [0.667, 0.5]] for 2 / [[1, 2], [3, 4]], where evaluation takes a scalar over a matrix as the scalar times the inverse, [[-4, 2], [3, -1]]. The inverse is not compiled, as a negative matrix power is not, so the quotient is now refused with UncompilableNodeException. A matrix written into the expression is inverted before it is compiled, and that is unchanged. DivideFromScalar stays public, for what it says it is, and the compiler no longer uses it. AScalarOverAMatrixArgumentTest takes the refusal, which fails on master, a matrix argument over a scalar and a written matrix inverted, which pass on both. The suite, the trio, F#, AOT and the eight harnesses pass. Fixes #2287. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent e238512 commit 0f3df21

4 files changed

Lines changed: 75 additions & 6 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,18 @@ are now also found wherever the `1` and the square sit in a sum, under the condi
464464
| `"1 + tan(x)^2 - sec(x)^2".Simplify()` | `1 + tan(x) ^ 2 - sec(x) ^ 2` | `0 provided not cos(x) = 0` |
465465
| `"a + 1 + tan(y)^2 - sec(y)^2".Simplify()` | `a + 1 + tan(y) ^ 2 - sec(y) ^ 2` | `a provided not cos(y) = 0` |
466466

467+
### A scalar over a matrix argument is not compiled
468+
469+
**Not silent: what compiled to a wrong value now throws `UncompilableNodeException`.** A scalar over a matrix that is an
470+
argument of the compiled function was compiled as the scalar over every element, where evaluation takes it as the scalar
471+
times the inverse. The inverse is not compiled, as a negative matrix power is not, so the quotient is refused. A matrix
472+
written into the expression is inverted before it is compiled, as before
473+
([#2287](https://github.com/asc-community/AngouriMath/issues/2287)).
474+
475+
| Input | Was (2.6.0) | Now |
476+
|---|---|---|
477+
| `"x / A"`, compiled over `GenTensor<double>` and called at `A = [[1, 2], [3, 4]]`, `x = 2` | `[[2, 1], [0.667, 0.5]]` | throws `UncompilableNodeException` |
478+
467479
### A square system is not answered with rows that are no solution
468480

469481
**Silent: a row that is no solution is gone, and a system with a row that fails for some values is `null`.**

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
188188
[Tests/UnitTests/Common/ListArgumentOverloadTest.cs]
189189
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
190190

191+
[Tests/UnitTests/Common/AScalarOverAMatrixArgumentTest.cs]
192+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
193+
191194
[Tests/UnitTests/Core/Multithreading/SettingsAcrossAsync.cs]
192195
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
193196

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/TensorCompilation.cs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,13 @@ private sealed class Kit
249249
internal MethodInfo SubtractFromScalar { get; }
250250
internal MethodInfo MultiplyScalar { get; }
251251
internal MethodInfo DivideScalar { get; }
252-
internal MethodInfo DivideFromScalar { get; }
253252
internal MethodInfo Power { get; }
254253

255254
private Kit(Type tensor, MethodInfo empty, MethodInfo set, MethodInfo add, MethodInfo subtract, MethodInfo multiply,
256255
MethodInfo addScalar, MethodInfo subtractScalar, MethodInfo subtractFromScalar, MethodInfo multiplyScalar,
257-
MethodInfo divideScalar, MethodInfo divideFromScalar, MethodInfo power)
258-
=> (Tensor, Empty, Set, Add, Subtract, Multiply, AddScalar, SubtractScalar, SubtractFromScalar, MultiplyScalar, DivideScalar, DivideFromScalar, Power)
259-
= (tensor, empty, set, add, subtract, multiply, addScalar, subtractScalar, subtractFromScalar, multiplyScalar, divideScalar, divideFromScalar, power);
256+
MethodInfo divideScalar, MethodInfo power)
257+
=> (Tensor, Empty, Set, Add, Subtract, Multiply, AddScalar, SubtractScalar, SubtractFromScalar, MultiplyScalar, DivideScalar, Power)
258+
= (tensor, empty, set, add, subtract, multiply, addScalar, subtractScalar, subtractFromScalar, multiplyScalar, divideScalar, power);
260259

261260
// An expression tree is how C# names a method without a string: the call inside it
262261
// is resolved at compile time, closed over T and TWrapper, and rooted for the trimmer.
@@ -274,7 +273,6 @@ internal static Kit For<T, TWrapper>() where TWrapper : struct, IOperations<T>
274273
Named<Func<T, GenTensor<T, TWrapper>, GenTensor<T, TWrapper>>>((s, a) => TensorCompilation.SubtractFromScalar(s, a)),
275274
Named<Func<GenTensor<T, TWrapper>, T, GenTensor<T, TWrapper>>>((a, s) => TensorCompilation.MultiplyScalar(a, s)),
276275
Named<Func<GenTensor<T, TWrapper>, T, GenTensor<T, TWrapper>>>((a, s) => TensorCompilation.DivideScalar(a, s)),
277-
Named<Func<T, GenTensor<T, TWrapper>, GenTensor<T, TWrapper>>>((s, a) => TensorCompilation.DivideFromScalar(s, a)),
278276
Named<Func<GenTensor<T, TWrapper>, int, GenTensor<T, TWrapper>>>((a, n) => TensorCompilation.Power(a, n)));
279277
}
280278

@@ -380,6 +378,10 @@ public static GenTensor<T, TWrapper> MultiplyScalar<T, TWrapper>(GenTensor<T, TW
380378
public static GenTensor<T, TWrapper> DivideScalar<T, TWrapper>(GenTensor<T, TWrapper> a, T scalar) where TWrapper : struct, IOperations<T>
381379
=> Elementwise(a, x => default(TWrapper).Divide(x, scalar));
382380
/// <summary>The scalar over every element.</summary>
381+
/// <remarks>
382+
/// Not what a scalar over a matrix means, which is the scalar times the inverse, and so not what the compiler
383+
/// makes of one. https://github.com/asc-community/AngouriMath/issues/2287
384+
/// </remarks>
383385
public static GenTensor<T, TWrapper> DivideFromScalar<T, TWrapper>(T scalar, GenTensor<T, TWrapper> a) where TWrapper : struct, IOperations<T>
384386
=> Elementwise(a, x => default(TWrapper).Divide(scalar, x));
385387
/// <summary>The matrix power, a non-negative whole exponent, by repeated squaring.</summary>
@@ -452,7 +454,12 @@ internal static Expression Build(Entity.Matrix matrix, IReadOnlyList<Expression>
452454
Entity.Sumf => Expression.Call(rightKit!.AddScalar, right, value),
453455
Entity.Minusf => Expression.Call(rightKit!.SubtractFromScalar, value, right),
454456
Entity.Mulf => Expression.Call(rightKit!.MultiplyScalar, right, value),
455-
Entity.Divf => Expression.Call(rightKit!.DivideFromScalar, value, right),
457+
// A scalar over a matrix is the scalar times its inverse, as evaluation has it, and the
458+
// inverse is not compiled, as a negative power is not: dividing by every element
459+
// instead gave 2 / [[1, 2], [3, 4]] as [[2, 1], [2/3, 1/2]].
460+
// https://github.com/asc-community/AngouriMath/issues/2287
461+
Entity.Divf => throw new UncompilableNodeException(
462+
"A scalar over a matrix is the scalar times its inverse, which is not compiled: invert the matrix first"),
456463
_ => null,
457464
};
458465
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using AngouriMath.Core.Compilation.IntoLinq;
10+
using AngouriMath.Core.Exceptions;
11+
using AngouriMath.Extensions;
12+
using GenericTensor.Core;
13+
using Xunit;
14+
15+
namespace AngouriMath.Tests.Common
16+
{
17+
/// <summary>
18+
/// A scalar over a matrix argument is not compiled, where it was compiled as the scalar over every element: evaluation
19+
/// takes it as the scalar times the inverse, and the inverse is not compiled, as a negative matrix power is not. A matrix
20+
/// written into the expression is inverted before it is compiled, and that is unchanged.
21+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2287">#2287</a>
22+
/// </summary>
23+
[Trait("Area", "Common")]
24+
public sealed class AScalarOverAMatrixArgumentTest
25+
{
26+
[Fact]
27+
public void ItIsRefused()
28+
=> Assert.Throws<UncompilableNodeException>(() => "x / A".ToEntity()
29+
.Compile<GenTensor<double, DoubleOperations>, double, GenTensor<double, DoubleOperations>>("A", "x"));
30+
31+
[Fact]
32+
public void AMatrixArgumentOverAScalarIsStillCompiled()
33+
{
34+
var matrix = GenTensor<double, DoubleOperations>.CreateMatrix(new double[,] { { 1, 2 }, { 3, 4 } });
35+
var divided = "A / x".ToEntity()
36+
.Compile<GenTensor<double, DoubleOperations>, double, GenTensor<double, DoubleOperations>>("A", "x")(matrix, 2);
37+
Assert.Equal(new[] { 0.5, 1, 1.5, 2 }, new[] { divided[0, 0], divided[0, 1], divided[1, 0], divided[1, 1] });
38+
}
39+
40+
[Fact]
41+
public void AWrittenMatrixIsInvertedAsBefore()
42+
{
43+
var inverse = "x / [[1, 0], [2, 4]]".ToEntity().Compile<double, GenTensor<double, DoubleOperations>>("x")(1);
44+
Assert.Equal(new[] { 1, 0, -0.5, 0.25 }, new[] { inverse[0, 0], inverse[0, 1], inverse[1, 0], inverse[1, 1] });
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)