Skip to content

Commit 1c9dbce

Browse files
A statement where a number is expected is refused by both compilers (#2229)
A statement in a numeric position, such as the argument of sin or a summand, has no value: evaluation leaves sin(True) and True + 2 as written. The compilers each disagreed with that, differently: - FastExpression, which carries a statement as its truth value, took True as 1. So sin(x > 1) at 2 was 0.8414709848078965 and max(x > 1, 0) was 1. This came with #2209 and is unreleased. - The LINQ compiler threw KeyNotFoundException for sin(x > 1) and (x > 1)^2, from widening a bool to double. For max(x > 1, 0) it threw AngouriBugException, which asks for a bug report. Both are on 2.6.0. StatementPositions.Check now runs in both compilers, after the membership rewrite and before anything is compiled. A statement-valued node is a statement, or a provided or piecewise over one. It may stand at the root, as an operand of a connective, on both sides of an equality of statements, in an Iverson bracket, as a provided's or a piecewise's value or predicate, and in a set or a quantifier. Anywhere else it is refused with UncompilableNodeException, whose message points to iverson, the way to take a statement as a number. StatementInANumericPositionTest: 10 statements in numeric positions, refused by FastExpression and by LINQ over double and over Complex. All 10 fail without the check. Also 8 places where a statement is taken, which compile as before. Fixes #2227. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 595f523 commit 1c9dbce

6 files changed

Lines changed: 133 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,9 @@ comparison, a connective, a Boolean, a `provided`, a `piecewise` and an Iverson
439439
A statement now compiles to its truth value, `1` where it holds, `0` where it does not, and `NaN` where it has none, as
440440
`i > 1` has none. Comparisons and connectives are those of the LINQ compiler. A `provided` is `NaN` where its predicate
441441
does not hold, and a `piecewise` is the first case whose predicate holds, or `NaN`. Every case is computed, since the
442-
instructions have no jump ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)).
442+
instructions have no jump ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)). A statement where a
443+
number is expected is refused, as in the LINQ compiler: `sin(x > 1)` has no value, as evaluation leaves `sin(True)`,
444+
and `iverson` takes a statement as a number ([#2227](https://github.com/asc-community/AngouriMath/issues/2227)).
443445

444446
| Input | Was (2.6.0) | Now |
445447
|---|---|---|
@@ -449,6 +451,19 @@ instructions have no jump ([#2195](https://github.com/asc-community/AngouriMath/
449451
| `"piecewise(x provided x > 0, -x)".ToEntity().Compile("x").Call(-3)` | `UncompilableNodeException` | `(3, 0)` |
450452
| `"iverson(x > 0)".ToEntity().Compile("x").Call(2)` | `UncompilableNodeException` | `(1, 0)` |
451453

454+
### A statement where a number is expected is refused with `UncompilableNodeException`
455+
456+
**Not silent: an exception from outside the library is the library's own.** The LINQ compiler threw
457+
`KeyNotFoundException` for a statement as the argument of a function, as `sin(x > 1)` or `(x > 1)^2`, and
458+
`AngouriBugException`, which asks for a bug report, for one in `max` or `min`. Both compilers refuse a statement where
459+
a number is expected with `UncompilableNodeException`, `iverson` being the way to take one as a number
460+
([#2227](https://github.com/asc-community/AngouriMath/issues/2227)).
461+
462+
| Input | Was (2.6.0) | Now |
463+
|---|---|---|
464+
| `"sin(x > 1)".ToEntity().Compile<double, double>("x")` | throws `KeyNotFoundException` | throws `UncompilableNodeException` |
465+
| `"max(x > 1, 0)".ToEntity().Compile<double, double>("x")` | throws `AngouriBugException` | throws `UncompilableNodeException` |
466+
452467
### The conjugate, the argument, the parts and the Iverson bracket compile
453468

454469
**Not silent: these compile where they threw `UncompilableNodeException`.** `conj`, `arg`, `Re` and `Im` compile

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
905905
[Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs]
906906
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
907907

908+
[AngouriMath/Functions/Compilation/StatementPositions.cs]
909+
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
910+
911+
[Tests/UnitTests/Common/StatementInANumericPositionTest.cs]
912+
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
913+
908914
[Tests/UnitTests/Convenience/EulerGammaTest.cs]
909915
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
910916

‎Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ internal static FastExpression Compile(Entity func, IEnumerable<Variable> variab
10021002
foreach (var constant in Variable.NamedConstants.Values)
10031003
func = func.Substitute(constant, constant.Value);
10041004
func = Compilation.CompiledMembership.Rewritten(func);
1005+
Compilation.StatementPositions.Check(func);
10051006
var visited = new HashSet<Entity>();
10061007
var cache = new Dictionary<Entity, int>();
10071008
foreach (var node in func.Nodes)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal static TDelegate Compile<TDelegate>(
3131
if (expr.Nodes.Any(node => node is Entity.Matrix))
3232
expr = expr.InnerSimplified;
3333
expr = CompiledMembership.Rewritten(expr);
34+
StatementPositions.Check(expr);
3435

3536
var subexpressionsCache = typesAndNames.ToDictionary(c => (Entity)c.variable, c => Expression.Parameter(c.type));
3637
var functionArguments = subexpressionsCache.Select(c => c.Value).ToArray(); // copying
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 System.Linq;
9+
using AngouriMath.Core.Exceptions;
10+
using static AngouriMath.Entity;
11+
12+
namespace AngouriMath.Core.Compilation
13+
{
14+
/// <summary>
15+
/// Where a statement may stand in an expression that is compiled: at the root, as an operand of a connective, on
16+
/// both sides of an equality of statements, in an Iverson bracket, as a provided's or a piecewise's value or
17+
/// predicate, and in a set or a quantifier. Anywhere a number is expected a statement has no value, as evaluation
18+
/// leaves <c>sin(True)</c> as written; FastExpression, which carries a statement as its truth value, answered
19+
/// <c>sin(1)</c>. Its truth value as a number is <c>iverson</c>'s, and a statement anywhere else is refused, by
20+
/// both compilers alike.
21+
/// https://github.com/asc-community/AngouriMath/issues/2227
22+
/// </summary>
23+
internal static class StatementPositions
24+
{
25+
internal static void Check(Entity expression)
26+
{
27+
foreach (var node in expression.Nodes)
28+
{
29+
if (TakesStatements(node))
30+
continue;
31+
foreach (var child in node.DirectChildren)
32+
if (IsStatementValued(child))
33+
throw new UncompilableNodeException(
34+
$"`{child.Stringize()}` is a statement where `{node.Stringize()}` expects a number, and has no value there. "
35+
+ $"Its truth value as a number is iverson({child.Stringize()}).");
36+
}
37+
}
38+
39+
private static bool TakesStatements(Entity node)
40+
=> node is Andf or Orf or Xorf or Impliesf or Notf or Iversonf or Providedf or Piecewise or Set or Quantifier
41+
|| node is Equalsf(var left, var right) && IsStatementValued(left) == IsStatementValued(right);
42+
43+
private static bool IsStatementValued(Entity entity) => entity switch
44+
{
45+
Statement => true,
46+
Providedf(var value, _) => IsStatementValued(value),
47+
Piecewise piecewise => piecewise.Cases.Any(@case => IsStatementValued(@case.Expression)),
48+
_ => false
49+
};
50+
}
51+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 System.Numerics;
9+
using AngouriMath;
10+
using AngouriMath.Core.Exceptions;
11+
using AngouriMath.Extensions;
12+
using Xunit;
13+
14+
namespace AngouriMath.Tests.Common
15+
{
16+
/// <summary>
17+
/// A statement where a number is expected has no value, as evaluation leaves <c>sin(True)</c> as written, and both
18+
/// compilers refuse it. FastExpression answered with the truth value taken as a number, and the LINQ compiler threw
19+
/// <c>KeyNotFoundException</c> or <c>AngouriBugException</c>. <c>iverson</c> takes a statement as a number.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2227">#2227</a>
21+
/// </summary>
22+
[Trait("Area", "Common")]
23+
public sealed class StatementInANumericPositionTest
24+
{
25+
[Theory]
26+
[InlineData("sin(x > 1)")]
27+
[InlineData("(x > 1)^2")]
28+
[InlineData("max(x > 1, 0)")]
29+
[InlineData("(x > 1) + 2")]
30+
[InlineData("2 * (x in [0; 1])")]
31+
[InlineData("(x > 1) = 2")]
32+
[InlineData("(x > 1) < 2")]
33+
[InlineData("sin(x > 1 provided x > 0)")]
34+
[InlineData("x^(3 divides x)")]
35+
[InlineData("piecewise(x > 1 provided x > 0, x) + 1")]
36+
public void IsRefused(string expression)
37+
{
38+
Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile("x"));
39+
Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile<double, double>("x"));
40+
Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile<Complex, Complex>("x"));
41+
}
42+
43+
[Theory]
44+
[InlineData("iverson(x > 1) + 2", 2, 3)]
45+
[InlineData("iverson(x in ZZ) * x", 2, 2)]
46+
[InlineData("x > 1 and x < 3", 2, 1)]
47+
[InlineData("(x > 1) = (x < 3)", 4, 0)]
48+
[InlineData("not x > 1", 2, 0)]
49+
[InlineData("x provided x > 0", 2, 2)]
50+
[InlineData("piecewise(x provided x > 0, -x)", -2, 2)]
51+
[InlineData("x in { t : t > 0 }", 2, 1)]
52+
public void AStatementWhereOneIsTakenCompiles(string expression, double x, double expected)
53+
{
54+
Assert.Equal(new Complex(expected, 0), expression.ToEntity().Compile("x").Call(x));
55+
Assert.Equal(new Complex(expected, 0), expression.ToEntity().Compile<Complex, Complex>("x")(x));
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)