Skip to content
Open
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
29 changes: 29 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,35 @@ master (409d68805) and on this branch
| `cos(x^(1/3)) = 1` | `{ }` | `union({ (2 * pi * n) ^ 3 provided 2 * pi * n = 0 or -1/3 * pi < arg(2 * pi * n) and arg(2 * pi * n) <= 1/3 * pi or arg(2 * pi * n) = pi }, n in ZZ)` |
| `cos(x^(2/3)) = 1` | `union({ -(2 * pi * n) ^ (3/2) provided 2 * pi * n > 0 }, n in ZZ)` | `union({ (2 * pi * n) ^ (3/2) provided 2 * pi * n = 0 or -2/3 * pi < arg(2 * pi * n) and arg(2 * pi * n) <= 2/3 * pi, -(2 * pi * n) ^ (3/2) provided 2 * pi * n > 0 }, n in ZZ)` |

### A power's domain condition: a negative base over the reals, a zero base off the real line

**Silent: wrong conditions become right ones.** Over the reals (`DomainConditionIn(Domain.Real)`, `domain(..., RR)`)
a negative base has a real power only where the exponent is a whole number or a fraction of odd denominator below 10,
whose real root the library takes. The condition restricted the base only for a literal even root, so `x^pi`, `x^x`
and `(-1)^x` were taken as defined at points where their real reading is `NaN`
([#2739](https://github.com/asc-community/AngouriMath/issues/2739)). It now says `x >= 0` for any other number, and for
a symbolic exponent `e` names the fractions: `x >= 0 or 5 e in ZZ or 7 e in ZZ or 9 e in ZZ`. A literal odd denominator
from 11 up takes the principal root, which is not real, and `x^(1/11)` now needs `x >= 0` too.

Off the real line, `0^w` is 0 where `Re(w) > 0`, and the condition of a power whose exponent is not a number said `w > 0`,
which has no value at a non-real `w`: at `0^(1 + i)`, which is 0, it was `NaN`. It now reads `Re(w) > 0`, and so does
the incomplete gamma function's at a lower limit of 0, and `0^x` simplifies to `0 provided Re(x) > 0` where it was
`0 provided x * (1 + 1 / sgn(x) ^ 2) > 0`, which had no value at 0
([#2740](https://github.com/asc-community/AngouriMath/issues/2740)). Measured on `9b75a2678` and on this branch.

| | Was (`9b75a2678`) | Now |
|---|---|---|
| `domain(x^pi, RR)` | `True` | `x >= 0` |
| `domain(x^sqrt(2), RR)` | `True` | `x >= 0` |
| `domain(x^(1/11), RR)` | `True` | `x >= 0` |
| `domain((-1)^x, RR)` | `True` | `5 * x in ZZ or 7 * x in ZZ or 9 * x in ZZ` |
| `domain(x^x, RR)` | `not x = 0 or x > 0` | `(x >= 0 or 5 * x in ZZ or 7 * x in ZZ or 9 * x in ZZ) and (not x = 0 or x > 0)` |
| `domain(x^(1/2), RR)`, `domain(x^(1/3), RR)`, `domain(e^x, RR)` | `x >= 0`, `True`, `True` | unchanged |
| `domain(0^x)` | `x > 0` | `Re(x) > 0` |
| `domain(x^y)` | `not x = 0 or y > 0` | `not x = 0 or Re(y) > 0` |
| `domain(gamma(x, 0))` | `x > 0` | `Re(x) > 0` |
| `(0^x).Simplify()` | `0 provided x * (1 + 1 / sgn(x) ^ 2) > 0` | `0 provided Re(x) > 0` |

### An odd root of a negative number is real with downcasting off as well

**Silent, with `MathS.Settings.DowncastingEnabled` off only.** An odd root of a negative number is the real one:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,39 +412,51 @@ public partial record Powf
{
// Power is undefined in two cases:
// - 0^0 is indeterminate
// - 0^(negative) is undefined (division by zero)
// - 0^w with Re(w) <= 0 is undefined (division by zero, or no limit)
//
// And over the reals there is a third, which is the whole of what `sqrt` is: an even
// root of a negative number is not real. `x ^ (1/2)` needs `x >= 0` and `x ^ (1/3)`
// needs nothing, so the exponent's *denominator* decides it and only a literal
// rational has one to read. A symbolic exponent is left with the condition above
// rather than given a guess: too strict is a wrong answer here, since this is what a
// rewrite consults before firing.
// https://github.com/asc-community/AngouriMath/issues/721
// And over the reals there is a third, which is the whole of what `sqrt` is: a negative
// base to an exponent that is not a fraction of odd denominator below 10 is not real.
// `x ^ (1/2)` needs `x >= 0` and `x ^ (1/3)` needs nothing, the library taking the real
// root there. https://github.com/asc-community/AngouriMath/issues/721
//
// A symbolic exponent asked nothing of the base, so x^pi and x^x were taken as real at a
// negative x; the condition says now which exponents take the real root.
// https://github.com/asc-community/AngouriMath/issues/2739
private protected override Entity IntrinsicCondition =>
Codomain < Domain.Complex && IsEvenRoot
? Base >= 0 & WhereZeroIsDefined
Codomain < Domain.Complex && WhereANegativeBaseIsReal is { } negativeBase
? negativeBase & WhereZeroIsDefined
: WhereZeroIsDefined;

/// <summary>
/// <c>0^w</c> is defined where <c>Re(w) &gt; 0</c>. For a non-real number <c>w</c>,
/// <c>w &gt; 0</c> has no value, the complex numbers not being ordered, and made the
/// whole condition NaN, so <c>0 b^i</c> was NaN; its real part is read instead.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1865">#1865</a>
/// A symbolic exponent is asked for its real part too: <c>w &gt; 0</c> had no value at
/// <c>0^(1 + i)</c>, which is 0. Over the reals the exponent is real, and is its own real part.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2740">#2740</a>
/// </summary>
private Entity WhereZeroIsDefined
=> (Exponent is Complex ? Exponent : Exponent.Vars.Any() ? null : Exponent.Evaled) is Complex exponent and not Real
? exponent.RealPart.IsPositive ? Boolean.True : !Base.EqualTo(0)
: !Base.EqualTo(0) | Exponent > 0;
: !Base.EqualTo(0) | (Codomain < Domain.Complex ? Exponent : new Ref(Exponent)) > 0;

/// <summary>
/// Whether the exponent is a literal rational whose denominator is even — the case
/// where a negative base leaves the real line.
/// Over the reals, where a negative base has a real power: anywhere, for a whole exponent
/// or a fraction of odd denominator below 10, whose real root is taken; nowhere for any
/// other number; and for a symbolic exponent, where it is one of those fractions, which are
/// the numbers some multiple of 5, 7 or 9 makes whole. Null where the base is not restricted,
/// or is a number that is not negative.
/// </summary>
private bool IsEvenRoot
=> Exponent is Rational and not Integer and Rational ratio
&& ratio.ERational.Denominator.Remainder(2).IsZero;

private Entity? WhereANegativeBaseIsReal
=> !Base.Vars.Any() && Base.Evaled is Real { IsNegative: false } notNegative && !notNegative.IsNaN ? null : Exponent.Evaled switch
{
Integer => null,
Rational fraction when Numerics.IntervalEvaluation.TheRealRootIsTaken(fraction) is not null => null,
Complex => Base >= 0,
_ => Base >= 0 | (5 * Exponent).In(MathS.Sets.Z) | (7 * Exponent).In(MathS.Sets.Z) | (9 * Exponent).In(MathS.Sets.Z)
};

/// <summary>
/// Whether an exact <paramref name="base"/> to a decimal <paramref name="exponent"/> would be a power too
/// large to build were the exponent the whole number just past it.
Expand Down Expand Up @@ -494,9 +506,6 @@ private static bool TryPower(Matrix m, int exp, out Entity res)
internal static bool MayHaveAnInverse(Matrix m)
=> m.IsSquare && !m.IsSingular;

// Re(x) = x/2 * (1 + 1/sgn(x)^2)
internal static Entity Re(Entity x) => x / 2 * (1 + 1 / new Signumf(x).Pow(2));

/// <summary>
/// The real odd root of a negative base, where the exponent is written as an exact fraction of odd denominator
/// below 10 and evaluates to a decimal, as it does with downcasting off: 1/3 is then a quotient of the whole
Expand Down Expand Up @@ -539,7 +548,9 @@ protected override Entity InnerSimplify(bool isExact) =>
? c.RealPart.IsPositive
? 0
: MathS.NaN
: new Providedf(0, Re(x) > 0),
// The real part, as the condition reads it: x (1 + 1/sgn(x)^2) had no value at 0.
// https://github.com/asc-community/AngouriMath/issues/2740
: new Providedf(0, new Ref(x) > 0),
// An exact power too large to build stays a power. It is finite and not zero, and as
// the decimal it was +oo or 0, which then fed exact arithmetic: 2^(10^9) - 2^(10^9)
// was NaN, 0 * 2^(10^9) was NaN and 2^(10^9) > 2^(10^9 - 1) was False. Left written,
Expand Down Expand Up @@ -2068,8 +2079,10 @@ public partial record IncompleteGammaf
{
// Defined but at z = 0 where Re a <= 0, where the integral diverges at its lower limit.
// https://github.com/asc-community/AngouriMath/issues/2075
// Re a > 0 off the real line, where a > 0 has no value.
// https://github.com/asc-community/AngouriMath/issues/2740
private protected override Entity IntrinsicCondition =>
!LowerLimit.EqualTo(0) | Parameter > 0;
!LowerLimit.EqualTo(0) | (Codomain < Domain.Complex ? Parameter : new Ref(Parameter)) > 0;

/// <inheritdoc/>
protected override Entity InnerSimplify(bool isExact)
Expand Down
10 changes: 7 additions & 3 deletions Sources/AngouriMath/Functions/PredicateEntailment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
Expand Down Expand Up @@ -225,7 +225,7 @@ private static Entity Without(Entity expression, Entity predicate)
/// (<see cref="Entity.DomainConditionIn"/>): the condition excludes the zeros of
/// <paramref name="e"/> where a conjunct <c>not d = 0</c> or <c>d &gt; 0</c> has <c>d</c>
/// vanishing with <c>e</c>, or a disjunction has every side excluding them --
/// <c>x^x</c>'s <c>not x = 0 or x &gt; 0</c>.
/// <c>x^x</c>'s <c>not x = 0 or Re(x) &gt; 0</c>.
/// </summary>
private static bool IsUndefinedAtTheZerosOf(Entity expression, Entity e)
=> ExcludesTheZerosOf(expression.DomainConditionIn(MathS.Settings.Codomain), e);
Expand All @@ -244,7 +244,7 @@ private static bool ExcludesTheZerosOf(Entity condition, Entity e)
/// <summary>
/// Whether <paramref name="d"/> is zero wherever <paramref name="e"/> is: <c>d</c> is
/// <c>e</c>, a positive power of it, a product with such a factor, a sine, tangent,
/// arcsine or arctangent of such a thing, or -- for a variable <c>e</c> -- a polynomial
/// arcsine, arctangent or real part of such a thing, or -- for a variable <c>e</c> -- a polynomial
/// in it without a constant term.
/// </summary>
private static bool VanishesWith(Entity d, Entity e)
Expand All @@ -255,6 +255,10 @@ private static bool VanishesWith(Entity d, Entity e)
// at zero.
if (d is Sinf or Tanf or Arcsinf or Arctanf)
return VanishesWith(d.DirectChildren[0], e);
// And a real part, which a power's condition reads off the real line: `x^x`'s is
// `not x = 0 or Re(x) > 0`. https://github.com/asc-community/AngouriMath/issues/2740
if (d is Ref(var argument))
return VanishesWith(argument, e);
if (d is Powf(var @base, var power) && power.Evaled is Real { IsPositive: true })
return VanishesWith(@base, e);
if (d is Mulf)
Expand Down
87 changes: 80 additions & 7 deletions Sources/Tests/UnitTests/Core/DomainConditionInAReadingTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
Expand Down Expand Up @@ -51,16 +51,89 @@ public void AReadingIsAnswered(string source, string overComplex, string overRea
}

/// <summary>
/// A symbolic exponent is left with the condition it had rather than guessed at: too
/// strict is a wrong answer here, since this is what a rewrite consults before firing.
/// Over the reals a negative base has a real power only for a whole exponent or a fraction
/// of odd denominator below 10, whose real root the library takes; a symbolic or irrational
/// exponent asked nothing of the base, so <c>x^pi</c> was taken as defined at <c>-1</c>.
/// https://github.com/asc-community/AngouriMath/issues/2739
/// </summary>
[Theory]
// (-1)^pi = -0.9027 - 0.4303i, (-2)^sqrt(2) = -0.7096 - 2.5689i: not real.
[InlineData("x ^ pi", "-1", false)]
[InlineData("x ^ pi", "2", true)]
[InlineData("x ^ pi", "0", true)]
[InlineData("x ^ sqrt(2)", "-2", false)]
[InlineData("x ^ sqrt(2)", "2", true)]
// (-1/2)^(-1/2) = -1.4142i; (-1)^(-1) = -1; (-1/3)^(-1/3) = -3^(1/3), the real root.
[InlineData("x ^ x", "-1/2", false)]
[InlineData("x ^ x", "-1", true)]
[InlineData("x ^ x", "-1/3", true)]
[InlineData("x ^ x", "1/2", true)]
[InlineData("x ^ x", "0", false)]
[InlineData("(-1) ^ x", "1/2", false)]
[InlineData("(-1) ^ x", "1/3", true)]
[InlineData("(-1) ^ x", "2/7", true)]
[InlineData("(-1) ^ x", "4/9", true)]
[InlineData("(-1) ^ x", "2", true)]
[InlineData("(-1) ^ x", "1/11", false)]
[InlineData("(-1) ^ x", "1/15", false)]
[InlineData("(-1) ^ x", "pi", false)]
[InlineData("cos(x) ^ x", "pi", false)]
[InlineData("cos(x) ^ x", "2 * pi", true)]
// A literal odd denominator from 11 up takes the principal root, which is not real.
[InlineData("x ^ (1/11)", "-1", false)]
[InlineData("x ^ (1/11)", "1", true)]
[InlineData("x ^ (3/5)", "-1", true)]
[InlineData("e ^ x", "-1", true)]
public void Issue2739_ANegativeBaseIsRealOnlyForAnOddRoot(string source, string point, bool defined)
{
var condition = source.ToEntity().DomainConditionIn(Domain.Real);
Assert.Equal(defined, (bool)condition.Substitute("x", point.ToEntity()).EvalBoolean());
// And the real reading's value agrees: a number where the condition holds, none where it does not.
var value = $"domain({source}, RR)".ToEntity().Substitute("x", point.ToEntity()).EvalNumerical();
Assert.Equal(defined, !value.IsNaN);
}

/// <summary>
/// A symbolic exponent is read in both: over the reals it restricts a negative base, and
/// over the complex numbers it does not.
/// </summary>
[Fact]
public void ASymbolicExponentIsNotGuessedAt()
public void ASymbolicExponentIsReadInEach()
{
var expr = "x ^ y".ToEntity();
Assert.Equal(
expr.DomainConditionIn(Domain.Complex).Stringize(),
expr.DomainConditionIn(Domain.Real).Stringize());
var overReal = expr.DomainConditionIn(Domain.Real);
var overComplex = expr.DomainConditionIn(Domain.Complex);
Entity At(Entity condition, string x, string y) => condition.Substitute("x", x.ToEntity()).Substitute("y", y.ToEntity()).Evaled;
Assert.Equal(Entity.Boolean.True, At(overReal, "-8", "1/3"));
Assert.Equal(Entity.Boolean.False, At(overReal, "-8", "1/2"));
Assert.Equal(Entity.Boolean.True, At(overComplex, "-8", "1/2"));
Assert.Equal(Entity.Boolean.True, At(overReal, "8", "1/2"));
}

/// <summary>
/// <c>0^w</c> is 0 where <c>Re(w) &gt; 0</c>, and the condition of a power whose exponent is
/// not a number said <c>w &gt; 0</c>, which has no value off the real line.
/// https://github.com/asc-community/AngouriMath/issues/2740
/// </summary>
[Theory]
[InlineData("0 ^ x", "1 + i", true)]
[InlineData("0 ^ x", "2", true)]
[InlineData("0 ^ x", "i", false)]
[InlineData("0 ^ x", "-1 + i", false)]
[InlineData("0 ^ x", "0", false)]
[InlineData("0 ^ x", "-1", false)]
[InlineData("(x - 1) ^ (x * i + 1)", "1", true)]
[InlineData("x ^ (x + i)", "0", false)]
[InlineData("x ^ (x + 2 + i)", "0", true)]
[InlineData("gamma(x, 0)", "1 + i", true)]
[InlineData("gamma(x, 0)", "2", true)]
public void Issue2740_AZeroBaseIsDecidedForANonRealExponent(string source, string point, bool defined)
{
var expression = source.ToEntity();
Assert.Equal(defined ? Entity.Boolean.True : Entity.Boolean.False,
expression.DomainCondition.Substitute("x", point.ToEntity()).Evaled);
var value = expression.Substitute("x", point.ToEntity()).EvalNumerical();
Assert.Equal(defined, !value.IsNaN);
}

/// <summary>
Expand Down
Loading