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
22 changes: 20 additions & 2 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,14 @@ It is the forward difference with a step of 1, as Mathematica's `DifferenceDelta
**Not silent: `harmonicnumber(n)` and `harmonicnumber(n, m)` are the harmonic numbers, where the one was a product and the
other a parse error.** They are `1 + 1/2^m + ... + 1/n^m` for a whole `n >= 0`, exact for a rational `m`, and
`ζ(m) - ζ(m, n + 1)` elsewhere, as Mathematica's `HarmonicNumber` is: `ψ(n + 1) - ψ(1)` for `m = 1`, evaluated numerically
through `digamma` and `polygamma` for a whole `m >= 1`. A negative integer `n` has no value but for a whole `m <= 0`
through `digamma` and `polygamma` for a whole `m >= 1`, and exactly at a half-whole `n` for `m = 1` or an even `m`. A
negative integer `n` has no value but for a whole `m <= 0`
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).

| Input | Was (2.6.0) | Now |
|---|---|---|
| `"harmonicnumber(4)".ToEntity().Simplify()` | `4 * harmonicnumber` | `25/12` |
| `"harmonicnumber(1/2)".ToEntity().Simplify()` | `harmonicnumber / 2` | `2 - 2 * ln(2)` |
| `"harmonicnumber(n, 2)".ToEntity()` | throws `UnhandledParseException` | `harmonicnumber(n, 2)` |

### A linear function times an exponential is solved by the Lambert W function
Expand Down Expand Up @@ -585,6 +587,21 @@ and `lambertw(-1/e)` is `-1`, and it is evaluated numerically elsewhere. Its der
| `"productlog(e)".ToEntity().Simplify()` | `e * productlog` | `1` |
| `"lambertw(-1, x)".ToEntity()` | throws `UnhandledParseException` | `lambertw(-1, x)` |

### `eulergamma` is Euler's constant

**Silent: `eulergamma` is the Euler–Mascheroni constant `γ = 0.5772156649…`, where it was a variable.** So is
`EulerGamma`, Mathematica's and SymPy's name for it; `γ` is still a variable and `gamma` the Γ function. It is evaluated to
the working precision, drawn `\mathrm{\gamma}` and exported as `sympy.EulerGamma`, and a name an operator declares is still
the name, so `sum(eulergamma, eulergamma, 1, 3)` is `6`
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).

| Input | Was (2.6.0) | Now |
|---|---|---|
| `"eulergamma".ToEntity().EvalNumerical()` | `eulergamma` | `0.5772156649015328…` |
| `"EulerGamma".ToEntity().EvalNumerical()` | `EulerGamma` | `0.5772156649015328…` |
| `"eulergamma > 1/2".ToEntity().Simplify()` | `eulergamma > 1/2` | `True` |
| `"eulergamma".ToEntity().Latexize()` | `\mathrm{eulergamma}` | `\mathrm{\gamma}` |

### The Catalan numbers

**Not silent: `catalannumber(n)` is the Catalan number, where it was a product.** It is `binomial(2n, n) / (n + 1)` for
Expand Down Expand Up @@ -742,7 +759,7 @@ no others. Where that proof does not go through, or a bound is symbolic, the int
- **Evaluation.** Each is evaluated to the working precision anywhere in the complex plane but at `0, -1, -2, ...`, its poles, where it is `NaN`.
- **Derivatives.** Each order differentiates to the next. The derivative of `x!`, which was left unevaluated, is `x! * digamma(x + 1)` ([#171](https://github.com/asc-community/AngouriMath/issues/171)).
- **Integrals.** Each order integrates to the one before, and `digamma(x)` to `ln((x - 1)!)`.
- **Exact values.** An odd order at a whole or half-whole argument is a rational times a power of `pi` plus a rational: `polygamma(1, 1)` is `1/6 * pi ^ 2`.
- **Exact values.** An odd order at a whole or half-whole argument is a rational times a power of `pi` plus a rational: `polygamma(1, 1)` is `1/6 * pi ^ 2`. The digamma function there is a rational less Euler's constant, and at a half-whole argument less `2 ln 2` as well: `digamma(1)` is `-eulergamma`.

| Input | Was (2.6.0) | Now |
|---|---|---|
Expand All @@ -753,6 +770,7 @@ no others. Where that proof does not go through, or a bound is symbolic, the int
| `"gamma(x)".ToEntity().Differentiate("x")` | `derivative((x - 1)!, x)` | `(x - 1)! * digamma(x - 1 + 1)` |
| `"polygamma(1, 3)".ToEntity().InnerSimplified` | an `UnhandledParseException` | `1/6 * pi ^ 2 - 5/4` |
| `"digamma(x)".ToEntity().Integrate("x")` | `digamma * x ^ 2 / 2 + C`, of the product | `ln((x - 1)!) + C` |
| `"digamma(1/2)".ToEntity().Simplify()` | `digamma / 2` | `(-2) * ln(2) - eulergamma` |

### `gamma(a, z)` is the upper incomplete gamma function

Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -850,3 +850,6 @@ 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

[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
14 changes: 14 additions & 0 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4126,6 +4126,15 @@ public static (Variable, Variable, Variable, Variable, Variable) Var(string name
/// </example>
[ConstantField] public static readonly Variable pi = Variable.pi;

/// <summary>
/// Euler's constant, the Euler–Mascheroni constant <c>γ = lim (1 + 1/2 + ... + 1/n - ln n)</c>,
/// <c>0.5772156649…</c>. It is written <c>eulergamma</c>, since <c>gamma</c> is the Γ function, and
/// <c>EulerGamma</c> is read as it too. <c>digamma(1)</c> is <c>-eulergamma</c>.
/// <a href="https://en.wikipedia.org/wiki/Euler%27s_constant"/>
/// <a href="https://github.com/asc-community/AngouriMath/issues/2182">#2182</a>
/// </summary>
[ConstantField] public static readonly Variable eulergamma = Variable.eulergamma;

// Undefined
/// <summary>
/// NaN represents both "undefined" and "indeterminate".
Expand Down Expand Up @@ -7816,6 +7825,11 @@ public static class DecimalConst
/// </example>
public static EDecimal e =>
InternalAMExtensions.ConstantCache.Lookup(Settings.DecimalPrecisionContext).E;

/// <summary>Euler's constant, <see cref="MathS.eulergamma"/>, to the current precision.
/// <a href="https://en.wikipedia.org/wiki/Euler%27s_constant"/></summary>
public static EDecimal eulergamma =>
Entity.Number.EulerGamma(Settings.DecimalPrecisionContext);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static (EDecimal Re, EDecimal Im) EiByTheAsymptoticSeries(EDecimal re, E
/// digits' <c>ln 10</c>. The terms stay positive past <c>k = n</c>, where they are largest,
/// and ten guard digits cover what the first ones cancel.
/// </summary>
private static EDecimal EulerGamma(EContext work)
internal static EDecimal EulerGamma(EContext work)
{
var digits = work.Precision.ToInt32Checked();
return eulerGamma.GetOrAdd(digits, static digits =>
Expand Down
97 changes: 79 additions & 18 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,47 @@ public static Complex Polygamma(EInteger order, Complex z)
}

/// <summary>
/// <c>psi^(n)(q)</c> exactly, for an odd order <c>n</c> and a whole or half-whole <c>q</c> other than a pole,
/// or <see langword="null"/>: a rational times <c>pi^(n + 1)</c>, plus a rational.
/// <c>psi^(n)(q)</c> exactly, for <c>n = 0</c> or an odd order <c>n</c> and a whole or half-whole
/// <c>q</c> other than a pole, or <see langword="null"/>: a rational times <c>pi^(n + 1)</c>, plus a
/// rational, or for the digamma function a rational less Euler's constant and, at a half-whole
/// <c>q</c>, <c>2 ln 2</c>.
/// </summary>
/// <remarks>
/// <c>psi(1) = -gamma</c> and <c>psi(1/2) = -gamma - 2 ln 2</c> (DLMF 5.4.12, 5.4.13), and
/// <c>psi^(n)(1) = n! zeta(n + 1)</c> and <c>psi^(n)(1/2) = (2^(n + 1) - 1) n! zeta(n + 1)</c> for an odd
/// <c>n</c> (DLMF 5.15.2, 5.15.3), where <c>zeta(2m) = (-1)^(m + 1) B_(2m) (2 pi)^(2m)/(2 (2m)!)</c> is a
/// rational times <c>pi^(2m)</c>; the recurrence <c>psi^(n)(z + 1) = psi^(n)(z) - n!/z^(n + 1)</c> moves
/// the argument to either. An even order wants <c>zeta</c> at an odd number, or Euler's constant, which
/// have no closed form here. https://github.com/asc-community/AngouriMath/issues/171
/// rational times <c>pi^(2m)</c>; the recurrence <c>psi^(n)(z + 1) = psi^(n)(z) + (-1)^n n!/z^(n + 1)</c>
/// moves the argument to either. An even order from 2 wants <c>zeta</c> at an odd number, which has no
/// closed form here. https://github.com/asc-community/AngouriMath/issues/171
/// </remarks>
internal static Entity? ExactPolygamma(EInteger order, ERational argument)
=> ExactPolygammaParts(order, argument) is var (n, coefficient, rest)
? Exact(withGamma: n == 0, multipleFirst: n != 0, n, coefficient, rest)
: null;

/// <summary>
/// The harmonic number <c>H_n^(m) = (-1)^m/(m - 1)! (psi^(m - 1)(1) - psi^(m - 1)(n + 1))</c> exactly, where
/// <see cref="ExactPolygamma"/> has both values: <c>m = 1</c> or an even <c>m</c>, at a half-whole <c>n</c>.
/// Euler's constant, which both carry for <c>m = 1</c>, is left out rather than cancelled.
/// </summary>
internal static Entity? ExactHarmonicNumber(int m, ERational n)
{
if (m < 1 || ExactPolygammaParts(m - 1, ERational.One) is not var (order, coefficientAtOne, restAtOne)
|| ExactPolygammaParts(m - 1, n.Add(ERational.One)) is not var (_, coefficient, rest))
return null;
var scale = ERational.FromEInteger(m % 2 == 0 ? EInteger.One : EInteger.FromInt32(-1))
.Divide(ERational.FromEInteger(Factorial(m - 1)));
return Exact(withGamma: false, multipleFirst: false, order,
coefficientAtOne.Subtract(coefficient).Multiply(scale), restAtOne.Subtract(rest).Multiply(scale));
}

/// <summary>
/// <c>psi^(n)(q)</c> but for Euler's constant, as a rational times <c>pi^(n + 1)</c> for an odd
/// <c>n</c> or times <c>ln 2</c> for <c>n = 0</c>, and a rational.
/// </summary>
private static (int Order, ERational Coefficient, ERational Remainder)? ExactPolygammaParts(EInteger order, ERational argument)
{
if (order.Sign <= 0 || order.IsEven || order.CompareTo(100) > 0)
if (order.Sign < 0 || order.Sign > 0 && order.IsEven || order.CompareTo(100) > 0)
return null;
var half = ERational.Create(EInteger.One, EInteger.FromInt32(2));
var start = argument.Denominator.Equals(EInteger.One) ? ERational.One
Expand All @@ -83,8 +111,21 @@ public static Complex Polygamma(EInteger order, Complex z)
if (steps.Abs().CompareTo(1000) > 0)
return null;
var n = order.ToInt32Checked();
var m = (n + 1) / 2;
var factorial = Factorial(n);
// The recurrence, up from the start or down to the argument; (-1)^n n!/z^(n + 1) a step.
var rest = ERational.Zero;
var power = n + 1;
var step = ERational.FromEInteger(n % 2 == 0 ? factorial : factorial.Negate());
if (steps.Sign > 0)
for (var j = EInteger.Zero; j.CompareTo(steps) < 0; j = j.Add(EInteger.One))
rest = rest.Add(step.Divide(Pow(start.Add(ERational.FromEInteger(j)), power)));
else
for (var j = EInteger.One; j.CompareTo(steps.Negate()) <= 0; j = j.Add(EInteger.One))
rest = rest.Subtract(step.Divide(Pow(start.Subtract(ERational.FromEInteger(j)), power)));
if (n == 0)
// psi(1) = -gamma, and psi(1/2) = -gamma - 2 ln 2.
return (n, start.Equals(half) ? ERational.FromInt32(-2) : ERational.Zero, rest);
var m = (n + 1) / 2;
// zeta(2m) / pi^(2m) = (-1)^(m + 1) B_(2m) 2^(2m) / (2 (2m)!)
var zetaOverPiPower = Bernoulli(m).Multiply(ERational.FromEInteger(EInteger.One.ShiftLeft(2 * m)))
.Divide(ERational.FromEInteger(Factorial(2 * m).Multiply(2)));
Expand All @@ -93,22 +134,42 @@ public static Complex Polygamma(EInteger order, Complex z)
var piCoefficient = zetaOverPiPower.Multiply(ERational.FromEInteger(factorial));
if (start.Equals(half))
piCoefficient = piCoefficient.Multiply(ERational.FromEInteger(EInteger.One.ShiftLeft(n + 1).Subtract(EInteger.One)));
// The recurrence, up from the start or down to the argument.
var rest = ERational.Zero;
var power = n + 1;
if (steps.Sign > 0)
for (var j = EInteger.Zero; j.CompareTo(steps) < 0; j = j.Add(EInteger.One))
rest = rest.Subtract(ERational.FromEInteger(factorial).Divide(Pow(start.Add(ERational.FromEInteger(j)), power)));
else
for (var j = EInteger.One; j.CompareTo(steps.Negate()) <= 0; j = j.Add(EInteger.One))
rest = rest.Add(ERational.FromEInteger(factorial).Divide(Pow(start.Subtract(ERational.FromEInteger(j)), power)));
var piTerm = Rational.Create(piCoefficient) * MathS.Pow(MathS.pi, Integer.Create(power));
return (rest.Sign < 0 ? piTerm - Rational.Create(rest.Negate()) : piTerm + Rational.Create(rest)).InnerSimplified;
return (n, piCoefficient, rest);

static ERational Pow(ERational x, int exponent)
=> ERational.Create(x.Numerator.Pow(exponent), x.Denominator.Pow(exponent));
}

// c ln 2 for n = 0 and c pi^(n + 1) otherwise, and r, less Euler's constant where it is asked for, without
// the terms that are zero and with a negative one subtracted. An odd order leads with the multiple of
// pi^(n + 1), as it always has; the digamma function and a difference lead with the rational.
private static Entity Exact(bool withGamma, bool multipleFirst, int n, ERational coefficient, ERational rest)
{
Entity? sum = null;
var unit = n == 0 ? MathS.Ln(Integer.Create(2)) : MathS.Pow(MathS.pi, Integer.Create(n + 1));
if (multipleFirst)
{
Add(coefficient, unit);
Add(rest, null);
}
else
{
Add(rest, null);
Add(coefficient, unit);
}
if (withGamma)
sum = sum is null ? -MathS.eulergamma : sum - MathS.eulergamma;
return (sum ?? Integer.Zero).InnerSimplified;

void Add(ERational c, Entity? of)
{
if (c.IsZero)
return;
var term = of is null ? Rational.Create(c.Abs()) : Rational.Create(c.Abs()) * of;
sum = sum is null ? (c.Sign < 0 ? -term : term) : c.Sign < 0 ? sum - term : sum + term;
}
}

private static (EDecimal Re, EDecimal Im) Polygamma(int n, EDecimal re, EDecimal im, EContext work)
{
if (re.CompareTo(EDecimal.FromString("0.5")) < 0)
Expand Down
9 changes: 7 additions & 2 deletions Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public void Deconstruct(out string name)
new Dictionary<string, Func<Complex>>
{
{ nameof(pi), static () => Real.Create(MathS.DecimalConst.pi) },
{ nameof(e), static () => Real.Create(MathS.DecimalConst.e) }
{ nameof(e), static () => Real.Create(MathS.DecimalConst.e) },
{ nameof(eulergamma), static () => Real.Create(MathS.DecimalConst.eulergamma) }
};

/// <summary>Each constant as the name a writer types, which is the form a binder can take.</summary>
Expand All @@ -69,16 +70,20 @@ public void Deconstruct(out string name)

[ConstantField] internal static readonly Constant pi = NamedConstants[nameof(pi)];
[ConstantField] internal static readonly Constant e = NamedConstants[nameof(e)];
[ConstantField] internal static readonly Constant eulergamma = NamedConstants[nameof(eulergamma)];

/// <summary>
/// The other spellings of a constant's name, each read as the constant itself: <c>π</c>
/// (U+03C0) is <c>pi</c> and the script <c>ℯ</c> (U+212F) is <c>e</c>, as mathematics
/// writes them. The Cyrillic <c>е</c> is not among them: it is a letter of an alphabet the
/// grammar admits on purpose, and reading it as a constant would be the same surprise the
/// other way round. https://github.com/asc-community/AngouriMath/issues/1260
/// Euler's constant is also read as <c>EulerGamma</c>, Mathematica's and SymPy's name for it,
/// and not as <c>γ</c>, which is as often a variable.
/// https://github.com/asc-community/AngouriMath/issues/2182
/// </summary>
[ConstantField] private static readonly IReadOnlyDictionary<string, string> ConstantSpellings =
new Dictionary<string, string> { { "\u03C0", nameof(pi) }, { "\u212F", nameof(e) } };
new Dictionary<string, string> { { "\u03C0", nameof(pi) }, { "\u212F", nameof(e) }, { "EulerGamma", nameof(eulergamma) } };

/// <summary>The name a spelling stands for: <c>π</c> is <c>pi</c>, and any other name is itself.</summary>
private static string Spelled(string name) => ConstantSpellings.TryGetValue(name, out var constant) ? constant : name;
Expand Down
Loading
Loading