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
17 changes: 16 additions & 1 deletion BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|
Expand All @@ -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<double, double>("x")` | throws `KeyNotFoundException` | throws `UncompilableNodeException` |
| `"max(x > 1, 0)".ToEntity().Compile<double, double>("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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ internal static FastExpression Compile(Entity func, IEnumerable<Variable> 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<Entity>();
var cache = new Dictionary<Entity, int>();
foreach (var node in func.Nodes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal static TDelegate Compile<TDelegate>(
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
Expand Down
51 changes: 51 additions & 0 deletions Sources/AngouriMath/Functions/Compilation/StatementPositions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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 <c>sin(True)</c> as written; FastExpression, which carries a statement as its truth value, answered
/// <c>sin(1)</c>. Its truth value as a number is <c>iverson</c>'s, and a statement anywhere else is refused, by
/// both compilers alike.
/// https://github.com/asc-community/AngouriMath/issues/2227
/// </summary>
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
};
}
}
58 changes: 58 additions & 0 deletions Sources/Tests/UnitTests/Common/StatementInANumericPositionTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// A statement where a number is expected has no value, as evaluation leaves <c>sin(True)</c> as written, and both
/// compilers refuse it. FastExpression answered with the truth value taken as a number, and the LINQ compiler threw
/// <c>KeyNotFoundException</c> or <c>AngouriBugException</c>. <c>iverson</c> takes a statement as a number.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2227">#2227</a>
/// </summary>
[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<UncompilableNodeException>(() => expression.ToEntity().Compile("x"));
Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile<double, double>("x"));
Assert.Throws<UncompilableNodeException>(() => expression.ToEntity().Compile<Complex, Complex>("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<Complex, Complex>("x")(x));
}
}
}
Loading