Skip to content

Commit 7633d18

Browse files
Euler's constant, eulergamma (#2204)
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) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent cefe104 commit 7633d18

17 files changed

Lines changed: 285 additions & 29 deletions

File tree

‎BREAKING-CHANGES.md‎

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

553554
| Input | Was (2.6.0) | Now |
554555
|---|---|---|
555556
| `"harmonicnumber(4)".ToEntity().Simplify()` | `4 * harmonicnumber` | `25/12` |
557+
| `"harmonicnumber(1/2)".ToEntity().Simplify()` | `harmonicnumber / 2` | `2 - 2 * ln(2)` |
556558
| `"harmonicnumber(n, 2)".ToEntity()` | throws `UnhandledParseException` | `harmonicnumber(n, 2)` |
557559

558560
### 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
585587
| `"productlog(e)".ToEntity().Simplify()` | `e * productlog` | `1` |
586588
| `"lambertw(-1, x)".ToEntity()` | throws `UnhandledParseException` | `lambertw(-1, x)` |
587589

590+
### `eulergamma` is Euler's constant
591+
592+
**Silent: `eulergamma` is the Euler–Mascheroni constant `γ = 0.5772156649…`, where it was a variable.** So is
593+
`EulerGamma`, Mathematica's and SymPy's name for it; `γ` is still a variable and `gamma` the Γ function. It is evaluated to
594+
the working precision, drawn `\mathrm{\gamma}` and exported as `sympy.EulerGamma`, and a name an operator declares is still
595+
the name, so `sum(eulergamma, eulergamma, 1, 3)` is `6`
596+
([#2182](https://github.com/asc-community/AngouriMath/issues/2182)).
597+
598+
| Input | Was (2.6.0) | Now |
599+
|---|---|---|
600+
| `"eulergamma".ToEntity().EvalNumerical()` | `eulergamma` | `0.5772156649015328…` |
601+
| `"EulerGamma".ToEntity().EvalNumerical()` | `EulerGamma` | `0.5772156649015328…` |
602+
| `"eulergamma > 1/2".ToEntity().Simplify()` | `eulergamma > 1/2` | `True` |
603+
| `"eulergamma".ToEntity().Latexize()` | `\mathrm{eulergamma}` | `\mathrm{\gamma}` |
604+
588605
### The Catalan numbers
589606

590607
**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
742759
- **Evaluation.** Each is evaluated to the working precision anywhere in the complex plane but at `0, -1, -2, ...`, its poles, where it is `NaN`.
743760
- **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)).
744761
- **Integrals.** Each order integrates to the one before, and `digamma(x)` to `ln((x - 1)!)`.
745-
- **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`.
762+
- **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`.
746763

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

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

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,3 +850,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
850850

851851
[Tests/UnitTests/Algebra/SolveTest/LambertWSolveTest.cs]
852852
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
853+
854+
[Tests/UnitTests/Convenience/EulerGammaTest.cs]
855+
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

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,6 +4126,15 @@ public static (Variable, Variable, Variable, Variable, Variable) Var(string name
41264126
/// </example>
41274127
[ConstantField] public static readonly Variable pi = Variable.pi;
41284128

4129+
/// <summary>
4130+
/// Euler's constant, the Euler–Mascheroni constant <c>γ = lim (1 + 1/2 + ... + 1/n - ln n)</c>,
4131+
/// <c>0.5772156649…</c>. It is written <c>eulergamma</c>, since <c>gamma</c> is the Γ function, and
4132+
/// <c>EulerGamma</c> is read as it too. <c>digamma(1)</c> is <c>-eulergamma</c>.
4133+
/// <a href="https://en.wikipedia.org/wiki/Euler%27s_constant"/>
4134+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2182">#2182</a>
4135+
/// </summary>
4136+
[ConstantField] public static readonly Variable eulergamma = Variable.eulergamma;
4137+
41294138
// Undefined
41304139
/// <summary>
41314140
/// NaN represents both "undefined" and "indeterminate".
@@ -7816,6 +7825,11 @@ public static class DecimalConst
78167825
/// </example>
78177826
public static EDecimal e =>
78187827
InternalAMExtensions.ConstantCache.Lookup(Settings.DecimalPrecisionContext).E;
7828+
7829+
/// <summary>Euler's constant, <see cref="MathS.eulergamma"/>, to the current precision.
7830+
/// <a href="https://en.wikipedia.org/wiki/Euler%27s_constant"/></summary>
7831+
public static EDecimal eulergamma =>
7832+
Entity.Number.EulerGamma(Settings.DecimalPrecisionContext);
78197833
}
78207834

78217835
/// <summary>

‎Sources/AngouriMath/Core/Entity/Continuous/Number/ExponentialIntegrals.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static (EDecimal Re, EDecimal Im) EiByTheAsymptoticSeries(EDecimal re, E
173173
/// digits' <c>ln 10</c>. The terms stay positive past <c>k = n</c>, where they are largest,
174174
/// and ten guard digits cover what the first ones cancel.
175175
/// </summary>
176-
private static EDecimal EulerGamma(EContext work)
176+
internal static EDecimal EulerGamma(EContext work)
177177
{
178178
var digits = work.Precision.ToInt32Checked();
179179
return eulerGamma.GetOrAdd(digits, static digits =>

‎Sources/AngouriMath/Core/Entity/Continuous/Number/Polygamma.cs‎

Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,47 @@ public static Complex Polygamma(EInteger order, Complex z)
6060
}
6161

6262
/// <summary>
63-
/// <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,
64-
/// or <see langword="null"/>: a rational times <c>pi^(n + 1)</c>, plus a rational.
63+
/// <c>psi^(n)(q)</c> exactly, for <c>n = 0</c> or an odd order <c>n</c> and a whole or half-whole
64+
/// <c>q</c> other than a pole, or <see langword="null"/>: a rational times <c>pi^(n + 1)</c>, plus a
65+
/// rational, or for the digamma function a rational less Euler's constant and, at a half-whole
66+
/// <c>q</c>, <c>2 ln 2</c>.
6567
/// </summary>
6668
/// <remarks>
69+
/// <c>psi(1) = -gamma</c> and <c>psi(1/2) = -gamma - 2 ln 2</c> (DLMF 5.4.12, 5.4.13), and
6770
/// <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
6871
/// <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
69-
/// rational times <c>pi^(2m)</c>; the recurrence <c>psi^(n)(z + 1) = psi^(n)(z) - n!/z^(n + 1)</c> moves
70-
/// the argument to either. An even order wants <c>zeta</c> at an odd number, or Euler's constant, which
71-
/// have no closed form here. https://github.com/asc-community/AngouriMath/issues/171
72+
/// rational times <c>pi^(2m)</c>; the recurrence <c>psi^(n)(z + 1) = psi^(n)(z) + (-1)^n n!/z^(n + 1)</c>
73+
/// moves the argument to either. An even order from 2 wants <c>zeta</c> at an odd number, which has no
74+
/// closed form here. https://github.com/asc-community/AngouriMath/issues/171
7275
/// </remarks>
7376
internal static Entity? ExactPolygamma(EInteger order, ERational argument)
77+
=> ExactPolygammaParts(order, argument) is var (n, coefficient, rest)
78+
? Exact(withGamma: n == 0, multipleFirst: n != 0, n, coefficient, rest)
79+
: null;
80+
81+
/// <summary>
82+
/// The harmonic number <c>H_n^(m) = (-1)^m/(m - 1)! (psi^(m - 1)(1) - psi^(m - 1)(n + 1))</c> exactly, where
83+
/// <see cref="ExactPolygamma"/> has both values: <c>m = 1</c> or an even <c>m</c>, at a half-whole <c>n</c>.
84+
/// Euler's constant, which both carry for <c>m = 1</c>, is left out rather than cancelled.
85+
/// </summary>
86+
internal static Entity? ExactHarmonicNumber(int m, ERational n)
87+
{
88+
if (m < 1 || ExactPolygammaParts(m - 1, ERational.One) is not var (order, coefficientAtOne, restAtOne)
89+
|| ExactPolygammaParts(m - 1, n.Add(ERational.One)) is not var (_, coefficient, rest))
90+
return null;
91+
var scale = ERational.FromEInteger(m % 2 == 0 ? EInteger.One : EInteger.FromInt32(-1))
92+
.Divide(ERational.FromEInteger(Factorial(m - 1)));
93+
return Exact(withGamma: false, multipleFirst: false, order,
94+
coefficientAtOne.Subtract(coefficient).Multiply(scale), restAtOne.Subtract(rest).Multiply(scale));
95+
}
96+
97+
/// <summary>
98+
/// <c>psi^(n)(q)</c> but for Euler's constant, as a rational times <c>pi^(n + 1)</c> for an odd
99+
/// <c>n</c> or times <c>ln 2</c> for <c>n = 0</c>, and a rational.
100+
/// </summary>
101+
private static (int Order, ERational Coefficient, ERational Remainder)? ExactPolygammaParts(EInteger order, ERational argument)
74102
{
75-
if (order.Sign <= 0 || order.IsEven || order.CompareTo(100) > 0)
103+
if (order.Sign < 0 || order.Sign > 0 && order.IsEven || order.CompareTo(100) > 0)
76104
return null;
77105
var half = ERational.Create(EInteger.One, EInteger.FromInt32(2));
78106
var start = argument.Denominator.Equals(EInteger.One) ? ERational.One
@@ -83,8 +111,21 @@ public static Complex Polygamma(EInteger order, Complex z)
83111
if (steps.Abs().CompareTo(1000) > 0)
84112
return null;
85113
var n = order.ToInt32Checked();
86-
var m = (n + 1) / 2;
87114
var factorial = Factorial(n);
115+
// The recurrence, up from the start or down to the argument; (-1)^n n!/z^(n + 1) a step.
116+
var rest = ERational.Zero;
117+
var power = n + 1;
118+
var step = ERational.FromEInteger(n % 2 == 0 ? factorial : factorial.Negate());
119+
if (steps.Sign > 0)
120+
for (var j = EInteger.Zero; j.CompareTo(steps) < 0; j = j.Add(EInteger.One))
121+
rest = rest.Add(step.Divide(Pow(start.Add(ERational.FromEInteger(j)), power)));
122+
else
123+
for (var j = EInteger.One; j.CompareTo(steps.Negate()) <= 0; j = j.Add(EInteger.One))
124+
rest = rest.Subtract(step.Divide(Pow(start.Subtract(ERational.FromEInteger(j)), power)));
125+
if (n == 0)
126+
// psi(1) = -gamma, and psi(1/2) = -gamma - 2 ln 2.
127+
return (n, start.Equals(half) ? ERational.FromInt32(-2) : ERational.Zero, rest);
128+
var m = (n + 1) / 2;
88129
// zeta(2m) / pi^(2m) = (-1)^(m + 1) B_(2m) 2^(2m) / (2 (2m)!)
89130
var zetaOverPiPower = Bernoulli(m).Multiply(ERational.FromEInteger(EInteger.One.ShiftLeft(2 * m)))
90131
.Divide(ERational.FromEInteger(Factorial(2 * m).Multiply(2)));
@@ -93,22 +134,42 @@ public static Complex Polygamma(EInteger order, Complex z)
93134
var piCoefficient = zetaOverPiPower.Multiply(ERational.FromEInteger(factorial));
94135
if (start.Equals(half))
95136
piCoefficient = piCoefficient.Multiply(ERational.FromEInteger(EInteger.One.ShiftLeft(n + 1).Subtract(EInteger.One)));
96-
// The recurrence, up from the start or down to the argument.
97-
var rest = ERational.Zero;
98-
var power = n + 1;
99-
if (steps.Sign > 0)
100-
for (var j = EInteger.Zero; j.CompareTo(steps) < 0; j = j.Add(EInteger.One))
101-
rest = rest.Subtract(ERational.FromEInteger(factorial).Divide(Pow(start.Add(ERational.FromEInteger(j)), power)));
102-
else
103-
for (var j = EInteger.One; j.CompareTo(steps.Negate()) <= 0; j = j.Add(EInteger.One))
104-
rest = rest.Add(ERational.FromEInteger(factorial).Divide(Pow(start.Subtract(ERational.FromEInteger(j)), power)));
105-
var piTerm = Rational.Create(piCoefficient) * MathS.Pow(MathS.pi, Integer.Create(power));
106-
return (rest.Sign < 0 ? piTerm - Rational.Create(rest.Negate()) : piTerm + Rational.Create(rest)).InnerSimplified;
137+
return (n, piCoefficient, rest);
107138

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

143+
// c ln 2 for n = 0 and c pi^(n + 1) otherwise, and r, less Euler's constant where it is asked for, without
144+
// the terms that are zero and with a negative one subtracted. An odd order leads with the multiple of
145+
// pi^(n + 1), as it always has; the digamma function and a difference lead with the rational.
146+
private static Entity Exact(bool withGamma, bool multipleFirst, int n, ERational coefficient, ERational rest)
147+
{
148+
Entity? sum = null;
149+
var unit = n == 0 ? MathS.Ln(Integer.Create(2)) : MathS.Pow(MathS.pi, Integer.Create(n + 1));
150+
if (multipleFirst)
151+
{
152+
Add(coefficient, unit);
153+
Add(rest, null);
154+
}
155+
else
156+
{
157+
Add(rest, null);
158+
Add(coefficient, unit);
159+
}
160+
if (withGamma)
161+
sum = sum is null ? -MathS.eulergamma : sum - MathS.eulergamma;
162+
return (sum ?? Integer.Zero).InnerSimplified;
163+
164+
void Add(ERational c, Entity? of)
165+
{
166+
if (c.IsZero)
167+
return;
168+
var term = of is null ? Rational.Create(c.Abs()) : Rational.Create(c.Abs()) * of;
169+
sum = sum is null ? (c.Sign < 0 ? -term : term) : c.Sign < 0 ? sum - term : sum + term;
170+
}
171+
}
172+
112173
private static (EDecimal Re, EDecimal Im) Polygamma(int n, EDecimal re, EDecimal im, EContext work)
113174
{
114175
if (re.CompareTo(EDecimal.FromString("0.5")) < 0)

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Variable.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public void Deconstruct(out string name)
6060
new Dictionary<string, Func<Complex>>
6161
{
6262
{ nameof(pi), static () => Real.Create(MathS.DecimalConst.pi) },
63-
{ nameof(e), static () => Real.Create(MathS.DecimalConst.e) }
63+
{ nameof(e), static () => Real.Create(MathS.DecimalConst.e) },
64+
{ nameof(eulergamma), static () => Real.Create(MathS.DecimalConst.eulergamma) }
6465
};
6566

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

7071
[ConstantField] internal static readonly Constant pi = NamedConstants[nameof(pi)];
7172
[ConstantField] internal static readonly Constant e = NamedConstants[nameof(e)];
73+
[ConstantField] internal static readonly Constant eulergamma = NamedConstants[nameof(eulergamma)];
7274

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

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

0 commit comments

Comments
 (0)