diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 0e88c1c08..4b50057c0 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -439,7 +439,9 @@ comparison, a connective, a Boolean, a `provided`, a `piecewise` and an Iverson A statement now compiles to its truth value, `1` where it holds, `0` where it does not, and `NaN` where it has none, as `i > 1` has none. Comparisons and connectives are those of the LINQ compiler. A `provided` is `NaN` where its predicate does not hold, and a `piecewise` is the first case whose predicate holds, or `NaN`. Every case is computed, since the -instructions have no jump ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)). +instructions have no jump ([#2195](https://github.com/asc-community/AngouriMath/issues/2195)). A statement where a +number is expected is refused, as in the LINQ compiler: `sin(x > 1)` has no value, as evaluation leaves `sin(True)`, +and `iverson` takes a statement as a number ([#2227](https://github.com/asc-community/AngouriMath/issues/2227)). | Input | Was (2.6.0) | Now | |---|---|---| @@ -449,6 +451,19 @@ instructions have no jump ([#2195](https://github.com/asc-community/AngouriMath/ | `"piecewise(x provided x > 0, -x)".ToEntity().Compile("x").Call(-3)` | `UncompilableNodeException` | `(3, 0)` | | `"iverson(x > 0)".ToEntity().Compile("x").Call(2)` | `UncompilableNodeException` | `(1, 0)` | +### A statement where a number is expected is refused with `UncompilableNodeException` + +**Not silent: an exception from outside the library is the library's own.** The LINQ compiler threw +`KeyNotFoundException` for a statement as the argument of a function, as `sin(x > 1)` or `(x > 1)^2`, and +`AngouriBugException`, which asks for a bug report, for one in `max` or `min`. Both compilers refuse a statement where +a number is expected with `UncompilableNodeException`, `iverson` being the way to take one as a number +([#2227](https://github.com/asc-community/AngouriMath/issues/2227)). + +| Input | Was (2.6.0) | Now | +|---|---|---| +| `"sin(x > 1)".ToEntity().Compile("x")` | throws `KeyNotFoundException` | throws `UncompilableNodeException` | +| `"max(x > 1, 0)".ToEntity().Compile("x")` | throws `AngouriBugException` | throws `UncompilableNodeException` | + ### The conjugate, the argument, the parts and the Iverson bracket compile **Not silent: these compile where they threw `UncompilableNodeException`.** `conj`, `arg`, `Re` and `Im` compile diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 77c1f0cc8..be1d1cfff 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -905,6 +905,12 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs] 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 +[AngouriMath/Functions/Compilation/StatementPositions.cs] +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 + +[Tests/UnitTests/Common/StatementInANumericPositionTest.cs] +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 + [Tests/UnitTests/Convenience/EulerGammaTest.cs] 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 diff --git a/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs b/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs index aebf61969..3d05e4ad3 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoFE/Compiler.cs @@ -1002,6 +1002,7 @@ internal static FastExpression Compile(Entity func, IEnumerable variab foreach (var constant in Variable.NamedConstants.Values) func = func.Substitute(constant, constant.Value); func = Compilation.CompiledMembership.Rewritten(func); + Compilation.StatementPositions.Check(func); var visited = new HashSet(); var cache = new Dictionary(); foreach (var node in func.Nodes) diff --git a/Sources/AngouriMath/Functions/Compilation/IntoLinq/IntoLinqCompiler.cs b/Sources/AngouriMath/Functions/Compilation/IntoLinq/IntoLinqCompiler.cs index 5b8b3ed96..0046f0f05 100644 --- a/Sources/AngouriMath/Functions/Compilation/IntoLinq/IntoLinqCompiler.cs +++ b/Sources/AngouriMath/Functions/Compilation/IntoLinq/IntoLinqCompiler.cs @@ -31,6 +31,7 @@ internal static TDelegate Compile( if (expr.Nodes.Any(node => node is Entity.Matrix)) expr = expr.InnerSimplified; expr = CompiledMembership.Rewritten(expr); + StatementPositions.Check(expr); var subexpressionsCache = typesAndNames.ToDictionary(c => (Entity)c.variable, c => Expression.Parameter(c.type)); var functionArguments = subexpressionsCache.Select(c => c.Value).ToArray(); // copying diff --git a/Sources/AngouriMath/Functions/Compilation/StatementPositions.cs b/Sources/AngouriMath/Functions/Compilation/StatementPositions.cs new file mode 100644 index 000000000..ab19be458 --- /dev/null +++ b/Sources/AngouriMath/Functions/Compilation/StatementPositions.cs @@ -0,0 +1,51 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Linq; +using AngouriMath.Core.Exceptions; +using static AngouriMath.Entity; + +namespace AngouriMath.Core.Compilation +{ + /// + /// Where a statement may stand in an expression that is compiled: 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 a number is expected a statement has no value, as evaluation + /// leaves sin(True) as written; FastExpression, which carries a statement as its truth value, answered + /// sin(1). Its truth value as a number is iverson's, and a statement anywhere else is refused, by + /// both compilers alike. + /// https://github.com/asc-community/AngouriMath/issues/2227 + /// + internal static class StatementPositions + { + internal static void Check(Entity expression) + { + foreach (var node in expression.Nodes) + { + if (TakesStatements(node)) + continue; + foreach (var child in node.DirectChildren) + if (IsStatementValued(child)) + throw new UncompilableNodeException( + $"`{child.Stringize()}` is a statement where `{node.Stringize()}` expects a number, and has no value there. " + + $"Its truth value as a number is iverson({child.Stringize()})."); + } + } + + private static bool TakesStatements(Entity node) + => node is Andf or Orf or Xorf or Impliesf or Notf or Iversonf or Providedf or Piecewise or Set or Quantifier + || node is Equalsf(var left, var right) && IsStatementValued(left) == IsStatementValued(right); + + private static bool IsStatementValued(Entity entity) => entity switch + { + Statement => true, + Providedf(var value, _) => IsStatementValued(value), + Piecewise piecewise => piecewise.Cases.Any(@case => IsStatementValued(@case.Expression)), + _ => false + }; + } +} diff --git a/Sources/Tests/UnitTests/Common/StatementInANumericPositionTest.cs b/Sources/Tests/UnitTests/Common/StatementInANumericPositionTest.cs new file mode 100644 index 000000000..b609c97e8 --- /dev/null +++ b/Sources/Tests/UnitTests/Common/StatementInANumericPositionTest.cs @@ -0,0 +1,58 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Numerics; +using AngouriMath; +using AngouriMath.Core.Exceptions; +using AngouriMath.Extensions; +using Xunit; + +namespace AngouriMath.Tests.Common +{ + /// + /// A statement where a number is expected has no value, as evaluation leaves sin(True) as written, and both + /// compilers refuse it. FastExpression answered with the truth value taken as a number, and the LINQ compiler threw + /// KeyNotFoundException or AngouriBugException. iverson takes a statement as a number. + /// #2227 + /// + [Trait("Area", "Common")] + public sealed class StatementInANumericPositionTest + { + [Theory] + [InlineData("sin(x > 1)")] + [InlineData("(x > 1)^2")] + [InlineData("max(x > 1, 0)")] + [InlineData("(x > 1) + 2")] + [InlineData("2 * (x in [0; 1])")] + [InlineData("(x > 1) = 2")] + [InlineData("(x > 1) < 2")] + [InlineData("sin(x > 1 provided x > 0)")] + [InlineData("x^(3 divides x)")] + [InlineData("piecewise(x > 1 provided x > 0, x) + 1")] + public void IsRefused(string expression) + { + Assert.Throws(() => expression.ToEntity().Compile("x")); + Assert.Throws(() => expression.ToEntity().Compile("x")); + Assert.Throws(() => expression.ToEntity().Compile("x")); + } + + [Theory] + [InlineData("iverson(x > 1) + 2", 2, 3)] + [InlineData("iverson(x in ZZ) * x", 2, 2)] + [InlineData("x > 1 and x < 3", 2, 1)] + [InlineData("(x > 1) = (x < 3)", 4, 0)] + [InlineData("not x > 1", 2, 0)] + [InlineData("x provided x > 0", 2, 2)] + [InlineData("piecewise(x provided x > 0, -x)", -2, 2)] + [InlineData("x in { t : t > 0 }", 2, 1)] + public void AStatementWhereOneIsTakenCompiles(string expression, double x, double expected) + { + Assert.Equal(new Complex(expected, 0), expression.ToEntity().Compile("x").Call(x)); + Assert.Equal(new Complex(expected, 0), expression.ToEntity().Compile("x")(x)); + } + } +}