From 07db58b21fa25c9f6ddf06842b098f8378873955 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Thu, 29 Oct 2026 16:30:59 +0000 Subject: [PATCH] Euler's constant, eulergamma eulergamma is the Euler-Mascheroni constant, 0.5772156649..., and EulerGamma, its name in Mathematica and SymPy, is read as it. It is evaluated to the working precision by the Brent-McMillan routine the exponential and trigonometric integrals already used, and it has a value in the interval and decimal evaluators and in both compilers. It draws as \mathrm{\gamma} and exports as sympy.EulerGamma. With it the digamma function is exact at a whole or half-whole argument: psi(1) = -gamma and psi(1/2) = -gamma - 2 ln 2, and the recurrence from either. digamma(3) is 3/2 - eulergamma. The harmonic number at a half-whole n is exact for m = 1 and an even m, from the same values with Euler's constant left out, since it cancels: harmonicnumber(1/2) is 2 - 2 ln 2. Part of #2182 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 22 +++- Sources/.editorconfig | 3 + Sources/AngouriMath/Convenience/MathS.cs | 14 ++ .../Continuous/Number/ExponentialIntegrals.cs | 2 +- .../Entity/Continuous/Number/Polygamma.cs | 97 +++++++++++--- .../Core/Entity/Omni/Entity.Variable.cs | 9 +- Sources/AngouriMath/Docs/Usage/Syntax.md | 15 ++- ...aluation.Continuous.Arithmetics.Classes.cs | 3 + .../Functions/Output/Latex/Latex.Classes.cs | 7 + .../Output/ToSympy/ToSympy.Classes.cs | 1 + .../Numerics/IntervalEvaluation.cs | 2 + .../AngouriMath/Numerics/PreciseEvaluation.cs | 9 ++ Sources/MCP/AngouriMath.Mcp/Resources.cs | 1 + Sources/Tests/AotSmokeTest/Program.cs | 2 + Sources/Tests/UnitTests/Common/PublicApi.txt | 2 + .../UnitTests/Convenience/EulerGammaTest.cs | 120 ++++++++++++++++++ .../UnitTests/Convenience/PolygammaTest.cs | 5 +- 17 files changed, 285 insertions(+), 29 deletions(-) create mode 100644 Sources/Tests/UnitTests/Convenience/EulerGammaTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index b44d31176..c96fee704 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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 @@ -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 @@ -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 | |---|---|---| @@ -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 diff --git a/Sources/.editorconfig b/Sources/.editorconfig index 24582b422..8285c80fe 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -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 diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index f2dde916f..56980e290 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -4126,6 +4126,15 @@ public static (Variable, Variable, Variable, Variable, Variable) Var(string name /// [ConstantField] public static readonly Variable pi = Variable.pi; + /// + /// Euler's constant, the Euler–Mascheroni constant γ = lim (1 + 1/2 + ... + 1/n - ln n), + /// 0.5772156649…. It is written eulergamma, since gamma is the Γ function, and + /// EulerGamma is read as it too. digamma(1) is -eulergamma. + /// + /// #2182 + /// + [ConstantField] public static readonly Variable eulergamma = Variable.eulergamma; + // Undefined /// /// NaN represents both "undefined" and "indeterminate". @@ -7816,6 +7825,11 @@ public static class DecimalConst /// public static EDecimal e => InternalAMExtensions.ConstantCache.Lookup(Settings.DecimalPrecisionContext).E; + + /// Euler's constant, , to the current precision. + /// + public static EDecimal eulergamma => + Entity.Number.EulerGamma(Settings.DecimalPrecisionContext); } /// diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs index 1e63c3941..bc172e4d9 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs @@ -173,7 +173,7 @@ private static (EDecimal Re, EDecimal Im) EiByTheAsymptoticSeries(EDecimal re, E /// digits' ln 10. The terms stay positive past k = n, where they are largest, /// and ten guard digits cover what the first ones cancel. /// - private static EDecimal EulerGamma(EContext work) + internal static EDecimal EulerGamma(EContext work) { var digits = work.Precision.ToInt32Checked(); return eulerGamma.GetOrAdd(digits, static digits => diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs index e0421ef1d..5d16e30d5 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs @@ -60,19 +60,47 @@ public static Complex Polygamma(EInteger order, Complex z) } /// - /// psi^(n)(q) exactly, for an odd order n and a whole or half-whole q other than a pole, - /// or : a rational times pi^(n + 1), plus a rational. + /// psi^(n)(q) exactly, for n = 0 or an odd order n and a whole or half-whole + /// q other than a pole, or : a rational times pi^(n + 1), plus a + /// rational, or for the digamma function a rational less Euler's constant and, at a half-whole + /// q, 2 ln 2. /// /// + /// psi(1) = -gamma and psi(1/2) = -gamma - 2 ln 2 (DLMF 5.4.12, 5.4.13), and /// psi^(n)(1) = n! zeta(n + 1) and psi^(n)(1/2) = (2^(n + 1) - 1) n! zeta(n + 1) for an odd /// n (DLMF 5.15.2, 5.15.3), where zeta(2m) = (-1)^(m + 1) B_(2m) (2 pi)^(2m)/(2 (2m)!) is a - /// rational times pi^(2m); the recurrence psi^(n)(z + 1) = psi^(n)(z) - n!/z^(n + 1) moves - /// the argument to either. An even order wants zeta at an odd number, or Euler's constant, which - /// have no closed form here. https://github.com/asc-community/AngouriMath/issues/171 + /// rational times pi^(2m); the recurrence psi^(n)(z + 1) = psi^(n)(z) + (-1)^n n!/z^(n + 1) + /// moves the argument to either. An even order from 2 wants zeta at an odd number, which has no + /// closed form here. https://github.com/asc-community/AngouriMath/issues/171 /// 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; + + /// + /// The harmonic number H_n^(m) = (-1)^m/(m - 1)! (psi^(m - 1)(1) - psi^(m - 1)(n + 1)) exactly, where + /// has both values: m = 1 or an even m, at a half-whole n. + /// Euler's constant, which both carry for m = 1, is left out rather than cancelled. + /// + 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)); + } + + /// + /// psi^(n)(q) but for Euler's constant, as a rational times pi^(n + 1) for an odd + /// n or times ln 2 for n = 0, and a rational. + /// + 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 @@ -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))); @@ -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) diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs index d990a3174..9252471a4 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs @@ -60,7 +60,8 @@ public void Deconstruct(out string name) new Dictionary> { { 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) } }; /// Each constant as the name a writer types, which is the form a binder can take. @@ -69,6 +70,7 @@ 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)]; /// /// The other spellings of a constant's name, each read as the constant itself: π @@ -76,9 +78,12 @@ public void Deconstruct(out string name) /// writes them. The Cyrillic е 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 EulerGamma, Mathematica's and SymPy's name for it, + /// and not as γ, which is as often a variable. + /// https://github.com/asc-community/AngouriMath/issues/2182 /// [ConstantField] private static readonly IReadOnlyDictionary ConstantSpellings = - new Dictionary { { "\u03C0", nameof(pi) }, { "\u212F", nameof(e) } }; + new Dictionary { { "\u03C0", nameof(pi) }, { "\u212F", nameof(e) }, { "EulerGamma", nameof(eulergamma) } }; /// The name a spelling stands for: π is pi, and any other name is itself. private static string Spelled(string name) => ConstantSpellings.TryGetValue(name, out var constant) ? constant : name; diff --git a/Sources/AngouriMath/Docs/Usage/Syntax.md b/Sources/AngouriMath/Docs/Usage/Syntax.md index 6a73f625e..00e2f4a61 100644 --- a/Sources/AngouriMath/Docs/Usage/Syntax.md +++ b/Sources/AngouriMath/Docs/Usage/Syntax.md @@ -115,7 +115,7 @@ first of those was a parse error. ## Numbers and constants -`1`, `1.5`, `.5`, `1.`, `1/2`, `1e-9`, `i`, `e`, `pi`, `+oo`, `-oo`, `NaN`, `true`, `false`. +`1`, `1.5`, `.5`, `1.`, `1/2`, `1e-9`, `i`, `e`, `pi`, `eulergamma`, `+oo`, `-oo`, `NaN`, `true`, `false`. A trailing `i` makes the literal imaginary, exponent and all: `3i`, `1.5e3i`. `i` on its own is the imaginary unit, and it is a **number** rather than a name — which is why `x i` is `x ^ i` and not a @@ -142,6 +142,10 @@ is `pi` and the script `ℯ` is `e`, so `sin(π)` is `0` and `ln(ℯ)` is `1`. T they are variables that print like `e` and `x`, and `х + x` has two ([#1260](https://github.com/asc-community/AngouriMath/issues/1260)). +`eulergamma` is Euler's constant, the Euler–Mascheroni constant `γ = 0.5772156649…`, also read as +`EulerGamma` and drawn `\mathrm{\gamma}`. It is spelled out because `gamma` is the Γ function, and `γ` +is a variable ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). + A subscript may have one of its own: `X_a_beta` is one name, `X` with the subscript `a_beta`, and in LaTeX it is `X_{a_{\beta}}` ([#524](https://github.com/asc-community/AngouriMath/issues/524)). @@ -363,7 +367,8 @@ which is `6^3 - 1`. It is taken in a variable **Harmonic numbers** — `harmonicnumber(n)`, `1 + 1/2 + ... + 1/n`, and `harmonicnumber(n, m)`, `1 + 1/2^m + ... + 1/n^m`, drawn `\operatorname{H}_{n}` and `\operatorname{H}_{n}^{(m)}`: `harmonicnumber(4)` is `25/12`. Off the whole numbers they are `ζ(m) - ζ(m, n + 1)`, as Mathematica's -`HarmonicNumber` is, evaluated numerically for a whole `m >= 1`: `harmonicnumber(1/2)` is `2 - 2 ln 2`. +`HarmonicNumber` is, evaluated numerically for a whole `m >= 1`, and exactly at a half-whole `n` for +`m = 1` or an even `m`: `harmonicnumber(1/2)` is `2 - 2 * ln(2)`. A negative integer `n` has no value but for a whole `m <= 0` ([#2182](https://github.com/asc-community/AngouriMath/issues/2182)). @@ -422,8 +427,10 @@ order is the derivative of the one before: the derivative of `polygamma(n, x)` i `(x - 1)! * digamma(x)`. Integrating goes the other way: `polygamma(n, x)` integrates to `polygamma(n - 1, x)`, and `digamma(x)` to `ln((x - 1)!)`. An odd order at a whole or half-whole argument is exact, a rational times a power of `pi` plus a rational: `polygamma(1, 1)` is -`1/6 * pi ^ 2` and `polygamma(1, 3)` is `1/6 * pi ^ 2 - 5/4`. Other special values, such as -`digamma(1) = -gamma`, stay as written, since the library has no symbol for Euler's constant. +`1/6 * pi ^ 2` and `polygamma(1, 3)` is `1/6 * pi ^ 2 - 5/4`. So is the digamma function there, +through Euler's constant: `digamma(1)` is `-eulergamma`, `digamma(3)` is `3/2 - eulergamma` and +`digamma(1/2)` is `(-2) * ln(2) - eulergamma`. An even order from `2` stays as written, since it wants +`zeta` at an odd number. **Incomplete gamma function** — `gamma(a, z)`. With a second argument, `gamma` is the upper incomplete gamma function `Gamma(a, z) = int_z^oo t^(a - 1) e^(-t) dt`, as Mathematica's `Gamma[a, z]` and diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs index 02b5e7715..b168e9d6a 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs @@ -716,6 +716,9 @@ protected override Entity InnerSimplify(bool isExact) => => Sum(whole.ToInt32Checked(), order).InnerSimplified(isExact), (Integer { EInteger.Sign: >= 0 }, Number) => null, (Integer { EInteger.Sign: < 0 }, Number order) when order is not Integer { EInteger.Sign: <= 0 } => MathS.NaN, + // A half-whole n, exactly where both polygamma values are: H(1/2) is 2 - 2 ln 2. + (Rational value, Integer { EInteger: var order }) when isExact && order.Sign > 0 && order.CompareTo(PeterO.Numbers.EInteger.FromInt32(LargestOrder)) <= 0 + && Number.ExactHarmonicNumber(order.ToInt32Checked(), value.ERational) is { } exact => exact, // Elsewhere, numerically: ψ(n + 1) - ψ(1) for m = 1, and through the polygamma // function for a whole m >= 2, as ζ(m) - ζ(m, n + 1) is. (Complex value, Integer { EInteger: var order }) when !isExact && order.Sign > 0 && order.CompareTo(PeterO.Numbers.EInteger.FromInt32(LargestOrder)) <= 0 diff --git a/Sources/AngouriMath/Functions/Output/Latex/Latex.Classes.cs b/Sources/AngouriMath/Functions/Output/Latex/Latex.Classes.cs index 95fe06ac0..4535512a5 100644 --- a/Sources/AngouriMath/Functions/Output/Latex/Latex.Classes.cs +++ b/Sources/AngouriMath/Functions/Output/Latex/Latex.Classes.cs @@ -76,6 +76,13 @@ public partial record Constant { /// private protected override bool IsOwnNameUpright => true; + + /// + /// Euler's constant is drawn as its symbol, upright as every constant is, so that + /// it reads back as the constant and not as a variable called gamma. + /// #2182 + private protected override string LatexizeNode() + => Name == nameof(eulergamma) ? @"\mathrm{\gamma}" : base.LatexizeNode(); } } } diff --git a/Sources/AngouriMath/Functions/Output/ToSympy/ToSympy.Classes.cs b/Sources/AngouriMath/Functions/Output/ToSympy/ToSympy.Classes.cs index 31eaffa0d..917ec045a 100644 --- a/Sources/AngouriMath/Functions/Output/ToSympy/ToSympy.Classes.cs +++ b/Sources/AngouriMath/Functions/Output/ToSympy/ToSympy.Classes.cs @@ -22,6 +22,7 @@ internal override string ToSymPy() => Name switch { "e" => "sympy.E", + nameof(eulergamma) => "sympy.EulerGamma", _ => "sympy." + Name }; } diff --git a/Sources/AngouriMath/Numerics/IntervalEvaluation.cs b/Sources/AngouriMath/Numerics/IntervalEvaluation.cs index abd6e8ac5..e77232e6e 100644 --- a/Sources/AngouriMath/Numerics/IntervalEvaluation.cs +++ b/Sources/AngouriMath/Numerics/IntervalEvaluation.cs @@ -412,6 +412,8 @@ private static ComplexInterval EvaluateOtherThanArithmetic(Entity expr) return ComplexInterval.Real(Interval.Around(Math.PI)); case Variable variable when variable == MathS.e: return ComplexInterval.Real(Interval.Around(Math.E)); + case Variable variable when variable == MathS.eulergamma: + return ComplexInterval.Real(Interval.Around(0.5772156649015329)); case Variable variable when boundVariable is not null && variable == boundVariable: return ComplexInterval.Real(boundRange); case Powf(var @base, var exponent): diff --git a/Sources/AngouriMath/Numerics/PreciseEvaluation.cs b/Sources/AngouriMath/Numerics/PreciseEvaluation.cs index 81c1fd027..37fdcb4ec 100644 --- a/Sources/AngouriMath/Numerics/PreciseEvaluation.cs +++ b/Sources/AngouriMath/Numerics/PreciseEvaluation.cs @@ -398,6 +398,13 @@ private PreciseInterval E() return new(e.RoundToPrecision(down), e.RoundToPrecision(up)); } + private PreciseInterval EulerGamma() + { + var gamma = EDecimal.FromString( + "0.57721566490153286060651209008240243104215933593992359880576723488486772677766467093694706329174674951463144724980708248096050401448654283622417399764492353625350033374293733773767394279259525824709491"); + return new(gamma.RoundToPrecision(down), gamma.RoundToPrecision(up)); + } + private PreciseComplexInterval Evaluate(Entity expr) { // Ahead of the values remembered by node: a name bound to one root now is bound to @@ -455,6 +462,8 @@ private PreciseComplexInterval EvaluateOtherThanArithmetic(Entity expr) return Real(Pi()); case Variable variable when variable == MathS.e: return Real(E()); + case Variable variable when variable == MathS.eulergamma: + return Real(EulerGamma()); case Powf(var @base, var exponent): { if (@base == MathS.e) diff --git a/Sources/MCP/AngouriMath.Mcp/Resources.cs b/Sources/MCP/AngouriMath.Mcp/Resources.cs index 50074fd67..0ff34bb99 100644 --- a/Sources/MCP/AngouriMath.Mcp/Resources.cs +++ b/Sources/MCP/AngouriMath.Mcp/Resources.cs @@ -129,6 +129,7 @@ apply lambda` | `7 % 3` | a parse error. `%` stays free to mean percent | | `mod` as a variable name | a parse error: `mod` is a keyword. Rename it | | `log(2, 8)` | `3` — base first, then argument | + | `gamma` for Euler's constant | `gamma` is the Γ function. Euler's constant γ is `eulergamma`, also `EulerGamma` | The "silently become" cases are the dangerous ones: they parse, and they mean something else. Anything else followed by `(` gets a warning. diff --git a/Sources/Tests/AotSmokeTest/Program.cs b/Sources/Tests/AotSmokeTest/Program.cs index 7da7a29ec..8e1d03fd6 100644 --- a/Sources/Tests/AotSmokeTest/Program.cs +++ b/Sources/Tests/AotSmokeTest/Program.cs @@ -146,6 +146,8 @@ private static void Parsing() CheckClose("harmonicnumber(x, 2) at x = 4", At(harmonic, "x", 4), 205.0 / 144); var difference = MathS.FromString("forwarddifference(x^3, x)"); CheckClose("forwarddifference(x^3, x) at x = 2", At(difference, "x", 2), 27 - 8); + var eulerGamma = MathS.FromString("x + eulergamma + digamma(1)"); + CheckClose("x + eulergamma + digamma(1) at x = 2", At(eulerGamma, "x", 2), 2); } private static void Simplification() diff --git a/Sources/Tests/UnitTests/Common/PublicApi.txt b/Sources/Tests/UnitTests/Common/PublicApi.txt index 3e257f43f..935c06bcb 100644 --- a/Sources/Tests/UnitTests/Common/PublicApi.txt +++ b/Sources/Tests/UnitTests/Common/PublicApi.txt @@ -3380,6 +3380,7 @@ AngouriMath.MathS+Compute.Phi(AngouriMath.Entity+Number+Integer) : AngouriMath.E AngouriMath.MathS+Compute.SymbolicFormOfCosine(AngouriMath.Entity) : AngouriMath.Entity AngouriMath.MathS+Compute.SymbolicFormOfSine(AngouriMath.Entity) : AngouriMath.Entity AngouriMath.MathS+DecimalConst.e { } : PeterO.Numbers.EDecimal +AngouriMath.MathS+DecimalConst.eulergamma { } : PeterO.Numbers.EDecimal AngouriMath.MathS+DecimalConst.pi { } : PeterO.Numbers.EDecimal AngouriMath.MathS+Diagnostic.CatchOnSimplify { } : AngouriMath.Convenience.Setting> AngouriMath.MathS+Diagnostic.OutputExplicit { } : AngouriMath.Convenience.Setting @@ -3643,6 +3644,7 @@ AngouriMath.MathS.ZeroMatrix(System.Int32) : AngouriMath.Entity+Matrix AngouriMath.MathS.ZeroMatrix(System.Int32, System.Int32) : AngouriMath.Entity+Matrix AngouriMath.MathS.ZeroVector(System.Int32) : AngouriMath.Entity+Matrix AngouriMath.MathS.e : AngouriMath.Entity+Variable +AngouriMath.MathS.eulergamma : AngouriMath.Entity+Variable AngouriMath.MathS.i : AngouriMath.Entity+Number+Complex AngouriMath.MathS.oo : AngouriMath.Entity+Number+Real AngouriMath.MathS.pi : AngouriMath.Entity+Variable diff --git a/Sources/Tests/UnitTests/Convenience/EulerGammaTest.cs b/Sources/Tests/UnitTests/Convenience/EulerGammaTest.cs new file mode 100644 index 000000000..f2b3ba158 --- /dev/null +++ b/Sources/Tests/UnitTests/Convenience/EulerGammaTest.cs @@ -0,0 +1,120 @@ +// +// 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 System.Linq; +using AngouriMath; +using AngouriMath.Extensions; +using PeterO.Numbers; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Convenience +{ + /// + /// Euler's constant, eulergamma, and the digamma and harmonic values it makes exact. It was a + /// variable, and digamma(1) stayed as written. + /// #2182 + /// + [Trait("Area", "Convenience")] + public sealed class EulerGammaTest + { + private static void Close(Entity value, string expected, string tolerance) + { + var real = Assert.IsAssignableFrom(value.EvalNumerical()); + Assert.True(real.EDecimal.Subtract(EDecimal.FromString(expected)).Abs().CompareTo(EDecimal.FromString(tolerance)) < 0, real.Stringize()); + } + + /// Against mpmath, to the default hundred digits and to a hundred and fifty. + [Fact] + public void ItIsEulersConstant() + { + Close("eulergamma".ToEntity(), "0.5772156649015328606065120900824024310421593359399235988057672348848677267776646709369470632917467495", "1e-98"); + using var _ = MathS.Settings.DecimalPrecisionContext.Set(new EContext(150, ERounding.HalfEven, -100000, 100000, false)); + Close(MathS.eulergamma, "0.577215664901532860606512090082402431042159335939923598805767234884867726777664670936947063291746749514631447249807082480960504014486542836224173997644", "1e-148"); + } + + [Fact] + public void ItIsAConstantSpelledOutOrAsMathematicaSpellsIt() + { + Assert.IsType("eulergamma".ToEntity()); + Assert.Equal(MathS.eulergamma, "EulerGamma".ToEntity()); + Assert.Equal("eulergamma", "EulerGamma".ToEntity().Stringize()); + Assert.IsNotType("γ".ToEntity()); + Assert.Equal(Number.Integer.One, "gamma(1)".ToEntity().Simplify()); + } + + [Fact] + public void ItIsDrawnAsGammaAndExportedAsSymPysConstant() + { + Assert.Equal(@"\mathrm{\gamma}", MathS.eulergamma.Latexize()); + Assert.Equal("sympy.EulerGamma", MathS.eulergamma.ToSymPy()); + Assert.Equal(MathS.eulergamma, MathS.eulergamma.Stringize().ToEntity()); + var json = System.Text.Json.JsonSerializer.Serialize(MathS.eulergamma); + Assert.IsType(System.Text.Json.JsonSerializer.Deserialize(json)); + } + + /// A name an operator declares is the name, as sum(e, e, 1, 3) is 6. + [Fact] + public void ADeclaredNameIsTheName() + => Assert.Equal(Number.Integer.Create(6), "sum(eulergamma, eulergamma, 1, 3)".ToEntity().Simplify()); + + [Fact] + public void ItCompiles() + { + Assert.Equal(1.5772156649015329, "x + eulergamma".ToEntity().Compile("x").Call(1).Real, 15); + Assert.Equal(1.5772156649015329, "x + eulergamma".ToEntity().Compile("x")(1), 15); + } + + /// + /// Exact at a whole or half-whole argument, and equal there to mpmath's value, to 30 digits: + /// psi(1) = -gamma, psi(1/2) = -gamma - 2 ln 2, and the recurrence from either. + /// + [Theory] + [InlineData("digamma(1)", "-0.5772156649015328606065120900824024310422")] + [InlineData("digamma(2)", "0.4227843350984671393934879099175975689578")] + [InlineData("digamma(3)", "0.9227843350984671393934879099175975689578")] + [InlineData("digamma(1/2)", "-1.963510026021423479440976332998755567193")] + [InlineData("digamma(3/2)", "0.03648997397857652055902366700124443280684")] + [InlineData("digamma(-1/2)", "0.03648997397857652055902366700124443280684")] + [InlineData("digamma(-3/2)", "0.7031566406452431872256903336679110994735")] + [InlineData("digamma(5/2)", "0.7031566406452431872256903336679110994735")] + public void TheDigammaFunctionIsExactAtAWholeOrHalfWholeArgument(string written, string expected) + { + var exact = written.ToEntity().InnerSimplified; + Assert.DoesNotContain(exact.Nodes, node => node is Polygammaf); + Assert.Contains(MathS.eulergamma, exact.Nodes); + Close(exact, expected, "1e-30"); + } + + [Fact] + public void TheDigammaFunctionAtOneIsMinusEulersConstant() + { + Assert.Equal("-eulergamma", "digamma(1)".ToEntity().Simplify().Stringize()); + Assert.Equal(Number.Integer.Zero, ("digamma(1)".ToEntity() + MathS.eulergamma).Simplify()); + } + + /// Exact at a half-whole n for m = 1 and an even m, and equal to mpmath's value. + [Theory] + [InlineData("harmonicnumber(1/2)", "0.613705638880109381165535757083646863849")] + [InlineData("harmonicnumber(-1/2)", "-1.386294361119890618834464242916353136151")] + [InlineData("harmonicnumber(3/2)", "1.280372305546776047832202423750313530516")] + [InlineData("harmonicnumber(1/2, 2)", "0.7101318663035471270551696667079496215621")] + [InlineData("harmonicnumber(3/2, 2)", "1.154576310747991571499614111152394066007")] + public void TheHarmonicNumberIsExactAtAHalfWholeArgument(string written, string expected) + { + var exact = written.ToEntity().InnerSimplified; + Assert.DoesNotContain(exact.Nodes, node => node is HarmonicNumberf or Polygammaf); + Assert.DoesNotContain(MathS.eulergamma, exact.Nodes); + Close(exact, expected, "1e-30"); + } + + /// An odd order from 3 wants zeta(3), which has no closed form here. + [Fact] + public void AnOddOrderOfTheHarmonicNumberStaysAsWritten() + => Assert.IsType("harmonicnumber(1/2, 3)".ToEntity().InnerSimplified); + } +} diff --git a/Sources/Tests/UnitTests/Convenience/PolygammaTest.cs b/Sources/Tests/UnitTests/Convenience/PolygammaTest.cs index 9e3322c86..1b782a5d1 100644 --- a/Sources/Tests/UnitTests/Convenience/PolygammaTest.cs +++ b/Sources/Tests/UnitTests/Convenience/PolygammaTest.cs @@ -96,9 +96,10 @@ public void AnOddOrderAtAWholeOrHalfWholeArgumentIsExact(string expression, stri AssertClose(expression.EvalNumerical().Stringize(), Assert.IsType(simplified.EvalNumerical()).EDecimal); } - /// An even order wants zeta at an odd number, or Euler's constant: it stays as written. + /// An even order from 2 wants zeta at an odd number, and an argument neither whole nor half-whole + /// has no closed form here: it stays as written. [Theory] - [InlineData("digamma(1)")] + [InlineData("digamma(1/3)")] [InlineData("polygamma(2, 1)")] [InlineData("polygamma(1, 1/3)")] public void OtherwiseItStaysAsWritten(string expression)