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
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ only in decimals, is answered as before ([#2607](https://github.com/asc-communit
| `2^x + 2^y - 8`, `x - y` | `null` | `[[2 + 2 * pi * i * n_1 / ln(2), …]]` |
| `x - y`, `ln(x) + ln(y) - 1` | `[[sqrt(e), sqrt(e)]]` | unchanged |

### 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:
`(-8)^(1/3)` is `-2`. With downcasting off, the parser keeps `1/3` as a quotient of the decimals 1 and 3, which
evaluates to `0.333…`. The power then took the principal root, `1 + 1.732i`. A power whose exponent is written as an
exact fraction with an odd denominator below 10 now takes the real root under either setting, as the precise evaluator
already did. With downcasting on, nothing changes ([#2622](https://github.com/asc-community/AngouriMath/issues/2622)).

| `.Evaled`, downcasting off | Was (2.10.0) | Now |
|---|---|---|
| `(-8)^(1/3)` | `1.000… + 1.732…i` | `-2.000…` |
| `(-8)^(2/3)` | `-2.000… + 3.464…i` | `4.000…` |
| `(-32)^(1/5)` | `1.618… + 1.175…i` | `-2.000…` |
| `(-30.5)^(1/3)` | `1.562… + 2.705…i` | `-3.124…` |
| `(-30.5)^(1/2)` | `5.522…i` | unchanged |

### A singular matrix is not inverted because its determinant is not written as 0

**Not silent: a wrong answer is now no answer.** `Matrix.Inverse`, `Matrix.Pow` with a negative exponent, `M^(-1)` and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,28 @@ internal static bool MayHaveAnInverse(Matrix m)
// 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
/// decimals 1 and 3, and is 0.333..., and (-8)^(1/3) was the principal 1 + 1.732i, where it is -2 with the
/// downcasting on. https://github.com/asc-community/AngouriMath/issues/2622
/// </summary>
private static Entity? AnOddRootOfANegative(Entity @base, Entity exponent)
=> exponent switch
{
Rational r => r,
Divf(Real { EDecimal: var above }, Real { EDecimal: var below }) when above.IsInteger() && below.IsInteger() && !below.IsZero
=> Rational.Create(PeterO.Numbers.ERational.Create(above.ToEInteger(), below.ToEInteger())),
_ => null
}
is { } fraction && Numerics.IntervalEvaluation.TheRealRootIsTaken(fraction) is { } numerator
&& exponent.Evaled is not Rational && @base.Evaled is Real { EDecimal.IsNegative: true } negative
&& Number.Pow(-negative, fraction) is var modulus
? (numerator.IsEven ? modulus : -modulus) : null;

/// <inheritdoc/>
protected override Entity InnerSimplify(bool isExact) =>
!isExact && AnOddRootOfANegative(Base, Exponent) is { } oddRoot ? oddRoot :
ExpandOnTwoArguments(Base, Exponent,
(a, b) => (a, b) switch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// 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 AngouriMath.Extensions;
using Xunit;
using static AngouriMath.Entity;

namespace AngouriMath.Tests.Core
{
/// <summary>
/// An odd root of a negative number is the real one, <c>(-8)^(1/3) = -2</c>. With downcasting off the parser keeps
/// <c>1/3</c> as a quotient, which evaluates to the decimal <c>0.333…</c>, and the power took the principal root:
/// <c>1 + 1.732i</c>. The value no longer depends on the setting.
/// https://github.com/asc-community/AngouriMath/issues/2622
/// </summary>
[Trait("Area", "Core")]
public sealed class AnOddRootIsRealWhateverTheDowncastingTest
{
[Theory]
[InlineData("(-8)^(1/3)", -2.0, 0.0)]
[InlineData("(-8)^(2/3)", 4.0, 0.0)]
[InlineData("(-8)^(-1/3)", -0.5, 0.0)]
[InlineData("(-32)^(1/5)", -2.0, 0.0)]
[InlineData("(-32)^(3/5)", -8.0, 0.0)]
[InlineData("(-30.5)^(1/3)", -3.1243998847631218, 0.0)]
[InlineData("(-2)^(1/7)", -1.1040895136738123, 0.0)]
// Halves are principal either way, a control.
[InlineData("(-30.5)^(1/2)", 0.0, 5.522680508593630)]
[InlineData("(-4)^(3/2)", 0.0, -8.0)]
// A positive base, and an even denominator, are unchanged.
[InlineData("8^(1/3)", 2.0, 0.0)]
[InlineData("(-16)^(1/4)", 1.4142135623730951, 1.4142135623730951)]
public void IsTheSameWithTheDowncastingOnAndOff(string power, double re, double im)
{
foreach (var downcasting in new[] { true, false })
{
using var _ = MathS.Settings.DowncastingEnabled.Set(downcasting);
var entity = power.ToEntity();
foreach (var value in new[] { entity.Evaled, entity.EvalNumerical() })
{
var number = Assert.IsAssignableFrom<Number.Complex>(value);
Assert.True(System.Math.Abs(number.RealPart.EDecimal.ToDouble() - re) < 1e-12
&& System.Math.Abs(number.ImaginaryPart.EDecimal.ToDouble() - im) < 1e-12,
$"{power} with downcasting {(downcasting ? "on" : "off")} is {value}");
}
}
}
}
}
Loading