Skip to content

Commit 25a839b

Browse files
The Gaussian, its even moments and the error functions are integrated (#1512)
* The Gaussian, its even moments and the error functions are integrated int F^(a x^2 + b x + c) dx is F^(c - b^2/(4a)) times the Gaussian of A = a ln F in u = x + b/(2a), and int e^(A u^2) du is sqrt(pi)/(2 sqrt(-A)) erf(sqrt(-A) u), written with erfi and the real root where A is decidably positive. Beside an even power of x, on either side of 0, parts reduce it two powers at a time to the Gaussian; an odd power ends at an elementary integral or at the exponential integral and is not taken. erf, erfc and erfi are integrated by parts against 1. Part of #1501. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * e^(x^2) is answered, so the examples of an unanswered integral are x^x and e^(x^3) The documented example of Integrate(string), the transformation tests and the exponential ansatz's declined row used e^(x^2) as the integral with no answer; it is sqrt(pi)/2 erfi(x) now. They use x^x, which has no antiderivative in any function the library will have, and e^(x^3) where the row is about the ansatz's shape. y' + y = e^(x^2) moves from what the ODE solver declines to what it solves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * The Gaussian and its moments over the half line With #1509, a definite integral takes the limit where its antiderivative is undefined at a bound, so the moments' x^k e^(-x^2) at +oo is 0 and the half line gives sqrt(pi)/2, sqrt(pi)/4 and 3 sqrt(pi)/8. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 877c449 commit 25a839b

10 files changed

Lines changed: 327 additions & 16 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,24 @@ antiderivative with no limit at the bound still gives `NaN`, as `-cos(x)` does a
16181618
| `integral(1/x^2, x, 0, 1)` | `NaN` | `+oo` |
16191619
| `integral(sin(x), x, 0, +oo)` | `NaN` | `NaN` |
16201620

1621+
### The Gaussian and the error functions are integrated
1622+
1623+
`e^(-x^2)` was left unintegrated, and its antiderivative is `sqrt(pi)/2 erf(x)`. An exponential
1624+
of a quadratic is now integrated to an error function of the square it completes, written with
1625+
`erfi` and a real root where the square's coefficient is decidably positive. Beside an even power
1626+
of `x`, on either side of 0, it is integrated by parts down to that, and `erf`, `erfc` and `erfi`
1627+
themselves by parts against 1. A symbolic exponent is answered for the generic case, as
1628+
`F^(a x)/(a ln F)` already was ([#1501](https://github.com/asc-community/AngouriMath/issues/1501)).
1629+
1630+
| Input | Was (2.5.0) | Now |
1631+
|---|---|---|
1632+
| `"e^(-x^2)".Integrate("x")` | `integral(e ^ (-x ^ 2), x)` | `sqrt(pi) / 2 * erf(x) + C` |
1633+
| `"e^(x^2)".Integrate("x")` | `integral(e ^ x ^ 2, x)` | `sqrt(pi) / 2 * erfi(x) + C` |
1634+
| `"x^2*e^(-x^2)".Integrate("x")` | `integral(x ^ 2 * e ^ (-x ^ 2), x)` | `x * e ^ (-x ^ 2) / (-2) + 1/2 * sqrt(pi) / 2 * erf(x) + C` |
1635+
| `"f^(a+b*x^2)".Integrate("x")` | `integral(f ^ (a + b * x ^ 2), x)` | `f ^ a * sqrt(pi) / (2 * sqrt(-b * ln(f))) * erf(sqrt(-b * ln(f)) * x) + C` |
1636+
| `"erf(x)".Integrate("x")` | `UnrecognizedFunctionParseException` | `x * erf(x) + e ^ (-x ^ 2) / sqrt(pi) + C` |
1637+
| `integral(e^(-x^2), x, -oo, +oo)` | `integral(e ^ (-x ^ 2), x, -oo, +oo)` | `sqrt(pi)` |
1638+
16211639
### `binomial(n, k)` is a function
16221640

16231641
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

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

625625
[Tests/UnitTests/Calculus/ImproperIntegralTest.cs]
626626
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
627+
628+
[Tests/UnitTests/Calculus/GaussianIntegralTest.cs]
629+
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/AngouriMathExtensions.cs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,17 +784,19 @@ public static Entity Differentiate(this string str, Variable x)
784784
/// An integrated expression. It might remain the same or be transformed into nodes with no integrals.
785785
/// </returns>
786786
/// <example>
787-
/// The constant of integration is carried as the variable <c>C</c>; an integral that
788-
/// has no elementary antiderivative comes back as the node itself, which is how this
789-
/// says it could not settle the question:
787+
/// The constant of integration is carried as the variable <c>C</c>; an integral with no
788+
/// antiderivative written in the library's functions comes back as the node itself,
789+
/// which is how this says it could not settle the question:
790790
/// <code>
791791
/// Console.WriteLine("1 / x".Integrate("x"));
792792
/// Console.WriteLine("e ^ (x ^ 2)".Integrate("x"));
793+
/// Console.WriteLine("x ^ x".Integrate("x"));
793794
/// </code>
794795
/// Prints
795796
/// <code>
796797
/// ln(x) + C
797-
/// integral(e ^ x ^ 2, x)
798+
/// sqrt(pi) / 2 * erfi(x) + C
799+
/// integral(x ^ x, x)
798800
/// </code>
799801
/// </example>
800802
public static Entity Integrate(this string str, Variable x)

‎Sources/AngouriMath/Functions/Continuous/Integration/IntegralPatterns.cs‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ internal static Entity AntiderivativeLog(Entity arg)
5858
Entity.Cotanf(var arg) => arg,
5959
Entity.Absf(var arg) => arg,
6060
Entity.Signumf(var arg) => arg,
61+
Entity.Erff(var arg) => arg,
62+
Entity.Erfcf(var arg) => arg,
63+
Entity.Erfif(var arg) => arg,
6164
Entity.Arcsinf(var arg) => arg,
6265
Entity.Arccosf(var arg) => arg,
6366
Entity.Arctanf(var arg) => arg,
@@ -67,6 +70,111 @@ internal static Entity AntiderivativeLog(Entity arg)
6770
_ => null
6871
};
6972

73+
/// <summary>
74+
/// <c>int F^(a x^2 + b x + c) dx</c> for a non-zero <c>a</c>: the square completed is
75+
/// <c>a (x + b/(2a))^2 + c - b^2/(4a)</c>, so the integral is <c>F^(c - b^2/(4a))</c> times
76+
/// the Gaussian of <c>A = a ln F</c> in <c>u = x + b/(2a)</c>.
77+
/// </summary>
78+
private static Entity Gaussian(Entity @base, Entity a, Entity b, Entity c, Entity.Variable x)
79+
{
80+
// Built without the terms a zero linear part would contribute: b/(2a) is 0 only
81+
// provided a is not zero, and that condition, stated twice over, was all a symbolic
82+
// F^(a x^2) got for it. The answer is for the generic case, as F^(a x)/(a ln F) is.
83+
if (TreeAnalyzer.IsZero(b))
84+
return MathS.Pow(@base, c) * UnitGaussian(a * MathS.Ln(@base), x);
85+
return MathS.Pow(@base, c - b * b / (4 * a)) * UnitGaussian(a * MathS.Ln(@base), x + b / (2 * a));
86+
}
87+
88+
/// <summary>
89+
/// <c>int e^(A u^2) du</c> for a non-zero <c>A</c>: <c>sqrt(pi)/(2 sqrt(-A)) erf(sqrt(-A) u)</c>,
90+
/// or with <c>erfi</c> and the real root where <c>A</c> is decidably positive.
91+
/// </summary>
92+
/// <remarks>
93+
/// The sign is read from <c>Evaled</c>, as the integrator's other sign tests are; they move
94+
/// together to the interval evaluation of https://github.com/asc-community/AngouriMath/pull/1497.
95+
/// Finiteness is asked as well, because <see cref="Entity.Number.Real.IsPositive"/> holds
96+
/// for <c>NaN</c> and <c>+oo</c>.
97+
/// </remarks>
98+
private static Entity UnitGaussian(Entity A, Entity u)
99+
=> A.Evaled is Entity.Number.Real { EDecimal.IsFinite: true, IsPositive: true }
100+
? MathS.Sqrt(MathS.pi) / (2 * MathS.Sqrt(A)) * MathS.Erfi(MathS.Sqrt(A) * u)
101+
: MathS.Sqrt(MathS.pi) / (2 * MathS.Sqrt(-A)) * MathS.Erf(MathS.Sqrt(-A) * u);
102+
103+
/// <summary>
104+
/// <c>int k x^m F^(a x^2 + c) dx</c> for an even whole <c>m</c>, not zero, and a non-zero
105+
/// <c>a</c>: the Gaussian moments. With <c>A = a ln F</c> and <c>I_m</c> the integral of
106+
/// <c>x^m e^(A x^2)</c>, parts give <c>I_m = x^(m - 1) e^(A x^2)/(2A) - (m - 1)/(2A) I_(m - 2)</c>,
107+
/// and read the other way <c>I_m = x^(m + 1) e^(A x^2)/(m + 1) - 2A/(m + 1) I_(m + 2)</c> for a
108+
/// negative <c>m</c>, each two powers nearer the Gaussian <c>I_0</c>. An odd <c>m</c> ends at
109+
/// <c>I_1 = e^(A x^2)/(2A)</c>, which is elementary and answered elsewhere, or at <c>I_(-1)</c>,
110+
/// which is the exponential integral, so it is not taken here.
111+
/// https://github.com/asc-community/AngouriMath/issues/1501
112+
/// </summary>
113+
private static Entity? GaussianMoment(Entity expr, Entity.Variable x)
114+
{
115+
// Asked of every integrand that reaches the table, so a type test comes first, and
116+
// nothing here captures a local: a closure is allocated on entry, before any return.
117+
if (expr is not (Entity.Mulf or Entity.Divf))
118+
return null;
119+
var (numerator, denominator) = expr is Entity.Divf(var n, var d) ? (n, d) : (expr, (Entity)Entity.Number.Integer.One);
120+
Entity coefficient = Entity.Number.Integer.One;
121+
int? power = null;
122+
Entity.Powf? gaussian = null;
123+
foreach (var factor in Entity.Mulf.LinearChildren(numerator))
124+
if (!TakeMomentFactor(factor, inverted: false, x, ref coefficient, ref power, ref gaussian))
125+
return null;
126+
foreach (var factor in Entity.Mulf.LinearChildren(denominator))
127+
if (!TakeMomentFactor(factor, inverted: true, x, ref coefficient, ref power, ref gaussian))
128+
return null;
129+
if (power is not { } m || m == 0 || m % 2 != 0 || System.Math.Abs(m) > 32 || gaussian is null
130+
|| !TreeAnalyzer.TryGetPolyQuadratic(gaussian.Exponent, x, out var a, out var b, out var c)
131+
|| TreeAnalyzer.IsZero(a) || !TreeAnalyzer.IsZero(b))
132+
return null;
133+
var bell = MathS.Pow(gaussian.Base, a * MathS.Sqr(x));
134+
return coefficient * MathS.Pow(gaussian.Base, c) * Moment(m, a * MathS.Ln(gaussian.Base), bell, x);
135+
}
136+
137+
/// <summary>
138+
/// Reads one factor of a Gaussian moment into the constant coefficient, the power of
139+
/// <paramref name="x"/> or the exponential, and says whether it was one of the three.
140+
/// </summary>
141+
private static bool TakeMomentFactor(Entity factor, bool inverted, Entity.Variable x,
142+
ref Entity coefficient, ref int? power, ref Entity.Powf? gaussian)
143+
{
144+
if (!factor.ContainsNode(x))
145+
{
146+
coefficient = inverted ? coefficient / factor : coefficient * factor;
147+
return true;
148+
}
149+
if (power is null && factor == x)
150+
{
151+
power = inverted ? -1 : 1;
152+
return true;
153+
}
154+
if (power is null && factor is Entity.Powf(var b, Entity.Number.Integer n) && b == x && n.EInteger.CanFitInInt32())
155+
{
156+
power = inverted ? -n.EInteger.ToInt32Checked() : n.EInteger.ToInt32Checked();
157+
return true;
158+
}
159+
if (!inverted && gaussian is null && factor is Entity.Powf(var @base, _) exponential && !@base.ContainsNode(x))
160+
{
161+
gaussian = exponential;
162+
return true;
163+
}
164+
return false;
165+
}
166+
167+
/// <summary>
168+
/// <c>I_k</c>, the integral of <c>x^k e^(A x^2)</c> for an even <c>k</c>, where
169+
/// <paramref name="bell"/> is that exponential as the integrand writes it.
170+
/// </summary>
171+
private static Entity Moment(int k, Entity A, Entity bell, Entity.Variable x) => k switch
172+
{
173+
0 => UnitGaussian(A, x),
174+
> 0 => MathS.Pow(x, k - 1) * bell / (2 * A) + ((1 - k) / (2 * A)).InnerSimplified * Moment(k - 2, A, bell, x),
175+
_ => MathS.Pow(x, k + 1) * bell / (k + 1) + (-2 * A / (k + 1)).InnerSimplified * Moment(k + 2, A, bell, x),
176+
};
177+
70178
internal static Entity? TryStandardIntegrals(Entity expr, Entity.Variable x) => expr switch
71179
{
72180
// Every rule below divides by the linear rate of its integrand's argument, and
@@ -126,6 +234,34 @@ _ when TryReadSineCosinePowers(expr, x, out var trigArg, out var sinePower, out
126234
!@base.ContainsNode(x) && TreeAnalyzer.TryGetPolyLinear(power, x, out var a, out _) =>
127235
MathS.Pow(@base, power) / (a * MathS.Ln(@base)),
128236

237+
// The Gaussian: an exponential of a quadratic is an error function of the square it
238+
// completes. With A u^2 the exponent's square part in u = x + b/(2a), the integral
239+
// of e^(A u^2) is sqrt(pi)/(2 sqrt(-A)) erf(sqrt(-A) u), which differentiates back for
240+
// every A that is not zero, whichever root is taken. Where A is decidably positive it
241+
// is written with erfi and the real root instead, so that e^(x^2) is sqrt(pi)/2 erfi(x)
242+
// rather than an error function of i x. https://github.com/asc-community/AngouriMath/issues/1501
243+
Entity.Powf(var @base, var power) when
244+
!@base.ContainsNode(x) && TreeAnalyzer.TryGetPolyQuadratic(power, x, out var a, out var b, out var c)
245+
&& !TreeAnalyzer.IsZero(a) =>
246+
Gaussian(@base, a, b, c, x),
247+
248+
// And the Gaussian beside an even power of x, to either side of it.
249+
_ when GaussianMoment(expr, x) is { } moment => moment,
250+
251+
// By parts against 1, the way the inverse trigonometric functions are:
252+
// int erf(u) = u erf(u) + e^(-u^2)/sqrt(pi), and the same for the other two.
253+
Entity.Erff(var arg) when
254+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
255+
(arg * MathS.Erf(arg) + MathS.Pow(MathS.e, -MathS.Sqr(arg)) / MathS.Sqrt(MathS.pi)) / a,
256+
257+
Entity.Erfcf(var arg) when
258+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
259+
(arg * MathS.Erfc(arg) - MathS.Pow(MathS.e, -MathS.Sqr(arg)) / MathS.Sqrt(MathS.pi)) / a,
260+
261+
Entity.Erfif(var arg) when
262+
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) =>
263+
(arg * MathS.Erfi(arg) - MathS.Pow(MathS.e, MathS.Sqr(arg)) / MathS.Sqrt(MathS.pi)) / a,
264+
129265
Entity.Absf(var arg) when
130266
TreeAnalyzer.TryGetPolyLinear(arg, x, out var a, out _) => // ∫ |ax + b| dx = sgn(ax + b) * (ax + b)^2 / (2a)
131267
MathS.Signum(arg) * MathS.Pow(arg, 2) / (2 * a),

‎Sources/Tests/UnitTests/Calculus/ExponentialAnsatzTest.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ namespace AngouriMath.Tests.Calculus
2828
/// </para>
2929
/// <para>
3030
/// Every answer is differentiated back and compared to the integrand numerically; the
31-
/// non-elementary neighbours — <c>e^x/x</c>, <c>e^(x^2)</c> — are pinned as declined, since
32-
/// answering them would be a wrong answer and not a missing one.
31+
/// non-elementary neighbours — <c>e^x/x</c>, <c>e^(x^3)</c> — are pinned as declined, since
32+
/// an elementary answer to them would be a wrong answer and not a missing one.
33+
/// (<c>e^(x^2)</c> is answered, with <c>erfi</c>, since
34+
/// https://github.com/asc-community/AngouriMath/issues/1501.)
3335
/// </para>
3436
/// </remarks>
3537
[Trait("Area", "Calculus")]
@@ -181,7 +183,7 @@ public void AnExponentialTimesARootOfAQuadraticIsDeclinedWhereThereIsNone()
181183
/// </summary>
182184
[Theory]
183185
[InlineData("e^x/x")]
184-
[InlineData("e^(x^2)")]
186+
[InlineData("e^(x^3)")]
185187
[InlineData("e^x/(1 + x)")]
186188
[InlineData("e^(1/x)")]
187189
public void ANonElementaryOneIsDeclined(string integrand)

‎Sources/Tests/UnitTests/Calculus/ExponentialSubstitutionIntegralTest.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ public void ASymbolicBase()
333333
/// elementary antiderivative at all, and its exponent is not linear, which is the check
334334
/// that stops it — a rewrite would leave an <c>x</c> standing beside the new variable.
335335
/// Should it later be answered by something else, this moves rather than being deleted.
336+
/// It now is, with <c>erfi</c>, by the Gaussian's own rule
337+
/// (https://github.com/asc-community/AngouriMath/issues/1501), and what this row pins, that
338+
/// the substitution did not write it, still holds.
336339
/// </summary>
337340
[Theory]
338341
[InlineData("e^(x^2)")]

0 commit comments

Comments
 (0)