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
27 changes: 27 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,33 @@ A square root past `10^50` that the working digits do not place as an integer is
64 of them `-oo`; this branch has none wrong, and 88 `NaN`, every one of them `Ci` or `Si` of a decimal from `10^100` off
the real line, and each wrong or `NaN` on master.

### arsinh and arcsch of a small argument keep all their digits

**Silent: wrong numbers become right ones.** `arsinh(a)` is `ln(a + sqrt(a^2 + 1))`, and for a small `a` the square root
is within `a^2/2` of 1, which the downcasting rounds onto 1 below half the working digits: from `a` below `10^(-25)` the
logarithm was `ln(1 + a)`, short of `a^2/2`, except where the digits a logarithm near 1 is evaluated again with put the
tolerance back below it, as they did for `10^(-30)` and `10^(-35)`. Above `10^(-25)` the sum near 1 kept the working
digits of 1 rather than of `a`, and the logarithm lost as many digits as `a` is below 1. The logarithm is now taken as
`ln(1 + u)` with `u = a + a^2 / (sqrt(a^2 + 1) + 1)`, which is the same number and cancels nothing, for `a` below about
`10^(-2)`; `arcsch(y)` is the same logarithm with `a = 1/y`. For an imaginary `a` the answer is imaginary. Measured on
master (0b1b7d7cc) and on this branch; the true values are mpmath's at 400 digits
([#2686](https://github.com/asc-community/AngouriMath/issues/2686)).

| `.Evaled` | Was (master) | Now |
|---|---|---|
| `arsinh(10^(-16))` | `9.99999999999999999999999999999998333…3333383333333333332882E-17`, right to 84 digits | `9.99999999999999999999999999999998333…3333333333333333332887E-17` |
| `arsinh(10^(-25))` | `9.999999999999999999999999500000…E-26`, right to 25 digits | `9.99999999999999999999999999999999999999999999999998333…E-26` |
| `arsinh(10^(-40))` | `9.999999999999999999999999999999999999999500000…E-41`, right to 40 digits | `9.999…99998333…E-41` |
| `arsinh(10^(-60))` | `9.999…9995000…E-61`, right to 60 digits | `1.000…000E-60` |
| `arcsch(10^40)` | `9.999999999999999999999999999999999999999500000…E-41`, right to 40 digits | `9.999…99998333…E-41` |
| `arcsch(10^60)` | `9.999…9995000…E-61`, right to 60 digits | `1.000…000E-60` |
| `arsinh(10^(-46) * i)` | `9.999…99966666667E-47i`, right to 92 digits | `1.000…0001666667E-46i` |

Of 1000 sampled values of `arsinh` and `arcsch`, at `±10^(-k)`, `±10^(-k) ± 10^(-k-3)/3` and `±10^(-k) i` and their
reciprocals for `k` up to 120, and at moderate and large arguments, master had 391 wrong past a relative `1e-95`; this
branch has none, and 563 changed, all of them `a` below `10^(-2)`. The last digit of some printed values moves, and an
imaginary value whose digits past the first are all 0 prints short: `arcsch(10^200 * i)` is `-1E-200i`.

### A number past the working digits is not taken for an exact one

**Not silent: wrong answers become exact ones.** At the default hundred digits, a product past about `10^50`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ protected override Entity InnerSimplify(bool isExact) =>
ExpandOnTwoArguments(Base, Antilogarithm,
(a, b) => (a, b) switch
{
(Complex n1, Complex n2) when !isExact => Number.Log(n1, WithoutARootsCancellation(n2) ?? n2) is var logarithm && MayBeNearOne(logarithm)
? NearOneWithMoreDigits(n1, n2) ?? logarithm : logarithm,
(Complex n1, Complex n2) when !isExact => OfASmallAreaArgument(n2)
?? (Number.Log(n1, WithoutARootsCancellation(n2) ?? n2) is var logarithm && MayBeNearOne(logarithm)
? NearOneWithMoreDigits(n1, n2) ?? logarithm : logarithm),
// The principal logarithm of the four points of the unit circle that are
// numbers, i times the argument in (-pi; pi]: ln(-1) is i pi and ln(i) is
// i pi / 2. ln(1) is 0 below. A trigonometric root was written ln(-1) / i
Expand Down Expand Up @@ -683,20 +684,110 @@ _ when IsExactArithmetic(a) && IsExactArithmetic(b) && b.Evaled is Complex bValu
// Where s a + t sqrt(b) cancels, s a - t sqrt(b) is about 2 s a and cancels nothing.
var conjugate = (s > 0 ? aValue : -aValue) - (t > 0 ? rootValue : -rootValue);
return conjugate is { RealPart.EDecimal.IsZero: true, ImaginaryPart.EDecimal.IsZero: true } ? null : difference / conjugate;
}

private static bool IsAHalf(Rational power) => power.ERational.Numerator.Equals(PeterO.Numbers.EInteger.One)
&& power.ERational.Denominator.Equals(PeterO.Numbers.EInteger.FromInt32(2));

/// <summary>a^2 is the square of a, and 1/y^2 the square of 1/y, which is how arcsch writes it.</summary>
private static bool IsTheSquareOf(Entity square, Entity of)
=> square is Powf(var squared, Integer(2)) && squared == of
|| of is Divf(Integer(1), var below) && square is Divf(Integer(1), Powf(var belowSquared, Integer(2))) && belowSquared == below;

/// <summary>About log10 of the larger part, from the exponent and the bits of the mantissa: within one either way.</summary>
private static long Magnitude(Complex number)
{
static long Of(PeterO.Numbers.EDecimal part)
=> part.IsZero || !part.IsFinite ? long.MinValue
: part.Exponent.ToInt64Unchecked() + (long)(part.Mantissa.GetUnsignedBitLengthAsInt64() * 0.30103);
return System.Math.Max(Of(number.RealPart.EDecimal), Of(number.ImaginaryPart.EDecimal));
}

static bool IsAHalf(Rational power) => power.ERational.Numerator.Equals(PeterO.Numbers.EInteger.One)
&& power.ERational.Denominator.Equals(PeterO.Numbers.EInteger.FromInt32(2));
// a^2 is the square of a, and 1/y^2 the square of 1/y, which is how arcsch writes it.
static bool IsTheSquareOf(Entity square, Entity of)
=> square is Powf(var squared, Integer(2)) && squared == of
|| of is Divf(Integer(1), var below) && square is Divf(Integer(1), Powf(var belowSquared, Integer(2))) && belowSquared == below;
// About log10 of the larger part, from the exponent and the bits of the mantissa: within one either way.
static long Magnitude(Complex number)
/// <summary>
/// The natural logarithm of <c>s a + sqrt(a^2 + 1)</c>, with <c>s</c> 1 or -1 -- <c>arsinh(a)</c>, and
/// <c>arcsch(y)</c> where <c>a</c> is <c>1/y</c> -- for an <c>a</c> below about <c>10^(-2)</c>, as
/// <c>ln(1 + u)</c> with <c>u = s a + a^2 / (sqrt(a^2 + 1) + 1)</c>, which is the same number and is
/// known to the working digits relative to itself; <see langword="null"/> for any other logarithm.
/// </summary>
/// <remarks>
/// <para>
/// The square root is within <c>a^2/2</c> of 1, and the downcasting takes a value within
/// <c>10^(-precision/2)</c> of an integer for the integer, so from <c>a</c> below
/// <c>10^(-precision/4)</c> it was 1 and the logarithm was <c>ln(1 + a)</c>, short of <c>a^2/2</c>:
/// <c>arsinh(10^(-25))</c> was right to 25 of a hundred digits. The logarithm near 1 is evaluated again
/// with more digits (<see cref="NearOneWithMoreDigits"/>), which kept <c>a^2/2</c> where those digits put
/// the downcasting's tolerance below it -- <c>10^(-30)</c> and <c>10^(-35)</c> were right -- and from
/// <c>10^(-40)</c> lost it again, <c>arsinh(10^(-60))</c> keeping sixty digits. Above
/// <c>10^(-precision/4)</c> the sum <c>s a + sqrt(a^2 + 1)</c> is near 1 and is rounded at the working
/// digits, so the logarithm, about <c>a</c>, is short of as many digits as <c>a</c> is below 1:
/// <c>arsinh(10^(-16))</c> had 84.
/// </para>
/// <para>
/// <c>u</c> is a sum of <c>s a</c> and a term <c>a</c> times smaller and cancels nothing, the square
/// root enters it only in that smaller term, and is made exact enough there by writing
/// <c>sqrt(a^2 + 1) - 1</c> once more as <c>a^2</c> over itself plus 2. The logarithm of <c>1 + u</c>
/// is taken with as many digits more as <c>u</c> is below 1. For an imaginary <c>a</c> below 1,
/// <c>1 + u</c> is on the unit circle and its logarithm has no real part.
/// https://github.com/asc-community/AngouriMath/issues/2686
/// </para>
/// </remarks>
private Complex? OfASmallAreaArgument(Complex antilogarithm)
{
if (Base is not Variable { Name: "e" })
return null;
var (a, root, s) = Antilogarithm switch
{
Sumf(var term, Powf(_, Rational half) radical) when IsAHalf(half) => (term, radical, 1),
Sumf(Powf(_, Rational half) radical, var term) when IsAHalf(half) => (term, radical, 1),
Minusf(Powf(_, Rational half) radical, var term) when IsAHalf(half) => (term, radical, -1),
_ => (null!, null!, 0),
};
if (s == 0 || !(root.Base is Sumf(var square, Integer(1)) && IsTheSquareOf(square, a)
|| root.Base is Sumf(Integer(1), var squareAfter) && IsTheSquareOf(squareAfter, a)))
return null;
if (!ReferenceEquals(Antilogarithm.Evaled, antilogarithm) || a.Evaled is not Complex aValue || root.Evaled is not Complex rootValue)
return null;
var size = Magnitude(aValue);
if (size == long.MinValue || size >= -2)
return null;
var context = MathS.Settings.DecimalPrecisionContext.Value;
if (context.Precision.IsZero || !context.Precision.CanFitInInt32())
return null;
var work = InternalAMExtensions.WithGuardDigits(context, 12);
Complex u;
using (MathS.Settings.DowncastingEnabled.Set(false))
using (MathS.Settings.DecimalPrecisionContext.Set(work))
{
var aSquared = aValue * aValue;
// sqrt(a^2 + 1) - 1, from the root as evaluated, and once more from itself: the root the
// downcasting took for 1 leaves it short of a^4/8, and a^6/32 after.
var excess = aSquared / (rootValue + 1);
excess = aSquared / (excess + 2);
u = (s > 0 ? aValue : -aValue) + excess;
}
var (re, im) = (u.RealPart.EDecimal, u.ImaginaryPart.EDecimal);
if (!re.IsFinite || !im.IsFinite)
return null;
if (im.IsZero)
return Real.CreateWithoutDowncasting(OneAndLn(re, context));
// ln |1 + u| is half of ln(1 + w), w = |1 + u|^2 - 1 = 2 re + re^2 + im^2.
var lnModulus = aValue.RealPart.EDecimal.IsZero ? PeterO.Numbers.EDecimal.Zero
: OneAndLn(re.Multiply(2, work).Add(re.Multiply(re, work), work).Add(im.Multiply(im, work), work), context).Divide(2, context);
var argument = im.Arctan2(PeterO.Numbers.EDecimal.One.Add(re, work), work).RoundToPrecision(context);
return Complex.CreateAtScales(lnModulus, argument, PeterO.Numbers.EDecimal.Zero, PeterO.Numbers.EDecimal.Zero);

// ln(1 + w) for a w below 1, to the digits of the context relative to itself: 1 + w taken with as
// many digits more as w is below 1, and w - w^2/2 past half the digits, where the rest is below them.
static PeterO.Numbers.EDecimal OneAndLn(PeterO.Numbers.EDecimal w, PeterO.Numbers.EContext context)
{
static long Of(PeterO.Numbers.EDecimal part)
=> part.IsZero || !part.IsFinite ? long.MinValue
: part.Exponent.ToInt64Unchecked() + (long)(part.Mantissa.GetUnsignedBitLengthAsInt64() * 0.30103);
return System.Math.Max(Of(number.RealPart.EDecimal), Of(number.ImaginaryPart.EDecimal));
if (w.IsZero)
return w;
var precision = context.Precision.ToInt32Unchecked();
var below = -(w.Exponent.ToInt64Unchecked() + w.UnsignedMantissa.GetDigitCountAsEInteger().ToInt64Unchecked());
if (below > precision / 2 + 5)
return w.Subtract(w.Multiply(w).Divide(2, context), context);
var wide = Wider(context, precision + (int)System.Math.Max(below, 0) + 10);
return PeterO.Numbers.EDecimal.One.Add(w, wide).NaturalLogarithm(wide).RoundToPrecision(context);
}
}

Expand Down
29 changes: 29 additions & 0 deletions Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@
{ 20692733745923116440.0, 985012471441596398530723840.0, 53035823659846109665657533505732608.0, 3088144109088191970827418502918634458841088.0 },
{ 985012471441596398530723840.0, 53035823659846109665657533505732608.0, 3088144109088191970827418502918634458841088.0, 190454802288659943182846234171424053458511757049856.0 }
});
var product = (Entity.Matrix)(matrix * matrix.Inverse).InnerSimplified;

Check warning on line 279 in Sources/Tests/UnitTests/Common/NumericsRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Possible null reference argument for parameter 'm2' in 'Matrix Matrix.operator *(Matrix m1, Matrix m2)'.
Assert.Equal(MathS.IdentityMatrix(4), product);
}

Expand Down Expand Up @@ -772,5 +772,34 @@
{
Assert.Equal(MathS.NaN, expression.ToEntity().Evaled);
}

// arsinh(a) is ln(a + sqrt(a^2 + 1)), and for a small a the square root is within a^2/2 of 1: the downcasting
// took it for 1 from a below 10^-25, at the working digits and at those the logarithm near 1 is evaluated again
// with, and the logarithm was ln(1 + a), short of a^2/2 -- 25 digits of arsinh(10^-25), 60 of arsinh(10^-60).
// Above, the sum near 1 kept the working digits of 1 and not of a: 84 digits of arsinh(10^-16). Each part is held
// to a relative 1e-95 of mpmath's at 400 digits, and a part that is 0 to 0.
// https://github.com/asc-community/AngouriMath/issues/2686
[Theory]
[InlineData("arsinh(10^(-25))", "9.9999999999999999999999999999999999999999999999999833333333333333333333333333333333333333333333333334083333333e-26", "0")]
[InlineData("arsinh(10^(-60))", "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-60", "0")]
[InlineData("arsinh(-10^(-40))", "-9.9999999999999999999999999999999999999999999999999999999999999999999999999999999833333333333333333333333333333e-41", "0")]
[InlineData("arsinh(10^(-16))", "9.9999999999999999999999999999999833333333333333333333333333333334083333333333333333333333333333328869047619048e-17", "0")]
[InlineData("arsinh(10^(-80))", "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-80", "0")]
[InlineData("arsinh(10^(-20) + 10^(-23)/3)", "1.0003333333333333333333333333333333333333166499944438271604938271604938271604938279117446607716512376543209877e-20", "0")]
[InlineData("arsinh(10^(-46) * i)", "0", "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016666666666666667e-46")]
[InlineData("arsinh(-10^(-30) * i)", "0", "-1.0000000000000000000000000000000000000000000000000000000000001666666666666666666666666666666666666666666666667e-30")]
[InlineData("arcsch(10^40)", "9.9999999999999999999999999999999999999999999999999999999999999999999999999999999833333333333333333333333333333e-41", "0")]
[InlineData("arcsch(-10^60)", "-1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-60", "0")]
[InlineData("arcsch(10^46 * i)", "0", "-1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000016666666666666667e-46")]
public void Issue2686_TheAreaSineOfASmallArgumentKeepsItsDigits(string expression, string trueReal, string trueImaginary)
{
var value = Assert.IsAssignableFrom<Entity.Number.Complex>(expression.ToEntity().Evaled);
foreach (var (part, truth) in new[] { (value.RealPart.EDecimal, trueReal), (value.ImaginaryPart.EDecimal, trueImaginary) })
{
var expected = EDecimal.FromString(truth);
Assert.True(part.IsFinite && part.Subtract(expected).Abs().CompareTo(expected.Abs().Multiply(EDecimal.FromString("1E-95"))) <= 0,
$"{expression} is {value}, where it is {trueReal} + {trueImaginary}i");
}
}
}
}
Loading