Skip to content

Commit b90ae8f

Browse files
max and min are defined where their arguments are real (#2261)
The complex numbers are not ordered, and max and min are read over the reals, so max(i, i) is NaN and max(i, 2) is left as written. Their condition was True, and max(x, x) simplified to x, which has a value at x = i. The condition is now that both arguments are real. The node's own fold of max(a, a) and min(a, a) to a did not consult the condition, and is now a provided a in RR, simplified in place: for a number that decides it, so max(i, i) stays NaN and max(3, 3) is 3, and for a symbol it is the condition. MaxMinOffTheRealLineTest takes the condition and the value at four points, max(i, i) and min(1 + i, 1 + i), and max(x, x) and min(x, x) simplified, at i and at 3. Four of the cases fail on master. A value where the condition fails is NaN, or the node left as written. RoundMinMaxGcdTest pinned min(x, x) as x, and now has x provided x in RR, with min(3, 3) beside it. Found by work/domaincheck. Part of #2256. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 47ea3b3 commit b90ae8f

5 files changed

Lines changed: 85 additions & 6 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,18 @@ conjugate as `0 - 1i`, in decimals, on the one instance that every `i` is, in ev
695695
|---|---|---|
696696
| `Complex.ImaginaryOne.Conjugate`, after `(1 / Complex.ImaginaryOne).Evaled` with `DowncastingEnabled` off | `0 - 1i` | `-i` |
697697

698+
### `max` and `min` are defined where their arguments are real
699+
700+
**Silent: `max(x, x)` and `min(x, x)` simplify to `x provided x in RR`, where they were `x`.** The complex numbers are
701+
not ordered, and both nodes are read over the reals, so `max(i, i)` has no value. Their condition said they were
702+
defined everywhere, and `max(x, x)` simplified to `x`, which has a value at `i`. A comparison of two real arguments is
703+
unchanged ([#2256](https://github.com/asc-community/AngouriMath/issues/2256)).
704+
705+
| Input | Was (2.6.0) | Now |
706+
|---|---|---|
707+
| `"max(x, x)".ToEntity().Simplify()` | `x` | `x provided x in RR` |
708+
| `"max(x, 2)".ToEntity().DomainCondition` | `True` | `x in RR` |
709+
698710
### `conjugate`, `conj` and `arg` are functions
699711

700712
**Not silent: these names followed by a bracket are now the complex conjugate and the principal argument, where they

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
237237
[Tests/UnitTests/Convenience/BinomialTest.cs]
238238
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
239239

240+
[Tests/UnitTests/Convenience/MaxMinOffTheRealLineTest.cs]
241+
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
242+
240243
[Tests/UnitTests/Convenience/ConjugateAndArgumentTest.cs]
241244
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
242245

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,10 @@ protected override Entity InnerSimplify(bool isExact)
13381338

13391339
public partial record Minf
13401340
{
1341-
private protected override Entity IntrinsicCondition => Boolean.True;
1341+
// Defined where both arguments are real: the complex numbers are not ordered, and the node is
1342+
// read over the reals, so min(i, i) is NaN. The condition said True, and min(x, x) simplified
1343+
// to x, which has a value at x = i. https://github.com/asc-community/AngouriMath/issues/2256
1344+
private protected override Entity IntrinsicCondition => Left.In(MathS.Sets.R) & Right.In(MathS.Sets.R);
13421345

13431346
/// <inheritdoc/>
13441347
protected override Entity InnerSimplify(bool isExact)
@@ -1348,23 +1351,25 @@ protected override Entity InnerSimplify(bool isExact)
13481351
// Only ordered arguments compare. A complex one is left alone rather
13491352
// than guessed at, which is what SymPy's Min does too.
13501353
(Real l, Real r) => l.EDecimal.CompareTo(r.EDecimal) <= 0 ? l : r,
1351-
var (l, r) when l == r => l,
1354+
// min(a, a) is a where a is real, and has no value where it is not.
1355+
var (l, r) when l == r => l.Provided(l.In(MathS.Sets.R)).InnerSimplified(isExact),
13521356
_ => null
13531357
},
13541358
(@this, a, b) => ((Minf)@this).New(a, b), isExact);
13551359
}
13561360

13571361
public partial record Maxf
13581362
{
1359-
private protected override Entity IntrinsicCondition => Boolean.True;
1363+
// As Minf's: defined where both arguments are real. https://github.com/asc-community/AngouriMath/issues/2256
1364+
private protected override Entity IntrinsicCondition => Left.In(MathS.Sets.R) & Right.In(MathS.Sets.R);
13601365

13611366
/// <inheritdoc/>
13621367
protected override Entity InnerSimplify(bool isExact)
13631368
=> ExpandOnTwoArguments(Left, Right,
13641369
(a, b) => (a, b) switch
13651370
{
13661371
(Real l, Real r) => l.EDecimal.CompareTo(r.EDecimal) >= 0 ? l : r,
1367-
var (l, r) when l == r => l,
1372+
var (l, r) when l == r => l.Provided(l.In(MathS.Sets.R)).InnerSimplified(isExact),
13681373
_ => null
13691374
},
13701375
(@this, a, b) => ((Maxf)@this).New(a, b), isExact);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.Extensions;
10+
using Xunit;
11+
12+
namespace AngouriMath.Tests.Convenience
13+
{
14+
/// <summary>
15+
/// <c>max</c> and <c>min</c> are defined where both arguments are real, as their values are: the complex numbers are not
16+
/// ordered, and <c>max(i, i)</c> is <c>NaN</c>. Their condition said <c>True</c>, so <c>max(x, x)</c> simplified to
17+
/// <c>x</c>, which has a value at <c>x = i</c>.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2256">#2256</a>
19+
/// </summary>
20+
[Trait("Area", "Convenience")]
21+
public sealed class MaxMinOffTheRealLineTest
22+
{
23+
[Theory]
24+
[InlineData("max(x, 2)", "i", false)]
25+
[InlineData("min(x, 2)", "1/2 + 6/5 * i", false)]
26+
[InlineData("max(x, 2)", "-3", true)]
27+
[InlineData("min(x, x)", "pi", true)]
28+
public void TheConditionIsWhereTheValueIs(string expression, string at, bool defined)
29+
{
30+
var condition = expression.ToEntity().DomainCondition.Substitute("x", at.ToEntity()).Evaled;
31+
Assert.Equal(defined ? Entity.Boolean.True : Entity.Boolean.False, condition);
32+
// Where it is not defined, evaluation gives NaN or leaves the node, as max(i, 2) is left: no number.
33+
var value = expression.ToEntity().Substitute("x", at.ToEntity()).Evaled;
34+
Assert.Equal(defined, value is Entity.Number && !value.IsNaN);
35+
}
36+
37+
[Theory]
38+
[InlineData("max(i, i)")]
39+
[InlineData("min(1 + i, 1 + i)")]
40+
public void OfAComplexNumberWithItselfItHasNoValue(string expression)
41+
{
42+
Assert.True(expression.ToEntity().Evaled.IsNaN, $"{expression} evaluates to {expression.ToEntity().Evaled}");
43+
Assert.True(expression.ToEntity().Simplify().IsNaN, $"{expression} simplifies to {expression.ToEntity().Simplify()}");
44+
}
45+
46+
[Theory]
47+
[InlineData("max(x, x)")]
48+
[InlineData("min(x, x)")]
49+
public void ASimplificationHasNoValueWhereTheOriginalHasNone(string expression)
50+
{
51+
var simplified = expression.ToEntity().Simplify();
52+
Assert.True(simplified.Substitute("x", "i".ToEntity()).Evaled.IsNaN, $"{simplified} at i");
53+
Assert.Equal(Entity.Number.Integer.Create(3), simplified.Substitute("x", 3).Evaled);
54+
}
55+
}
56+
}

‎Sources/Tests/UnitTests/Convenience/RoundMinMaxGcdTest.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ public void RoundIsNotFloorOfTheArgumentPlusAHalf(string at)
6868
public void MinAndMaxCompareWhereTheArgumentsAreOrdered(string input, int expected)
6969
=> Assert.Equal(Entity.Number.Integer.Create(expected), input.ToEntity().Simplify());
7070

71+
// That thing where it is real: the complex numbers are not ordered, and min(i, i) has no value.
72+
// https://github.com/asc-community/AngouriMath/issues/2256
7173
[Theory]
72-
[InlineData("min(x, x)", "x")]
73-
[InlineData("max(x, x)", "x")]
74+
[InlineData("min(x, x)", "x provided x in RR")]
75+
[InlineData("max(x, x)", "x provided x in RR")]
76+
[InlineData("min(3, 3)", "3")]
7477
public void MinAndMaxOfOneThingWithItselfIsThatThing(string input, string expected)
7578
=> Assert.Equal(expected.ToEntity(), input.ToEntity().Simplify());
7679

0 commit comments

Comments
 (0)