Skip to content

Commit

Permalink
Add normalize option to gausshermite (#122)
Browse files Browse the repository at this point in the history
* added probabilist weights

* added tests

* removed extraneous file
  • Loading branch information
NittanyLion authored Sep 15, 2023
1 parent d3dddb7 commit c755422
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/gausshermite.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
@doc raw"""
gausshermite(n::Integer) -> x, w # nodes, weights
gausshermite(n::Integer; normalize = false) -> x, w # nodes, weights
Return nodes `x` and weights `w` of [Gauss-Hermite quadrature](https://en.wikipedia.org/wiki/Gauss%E2%80%93Hermite_quadrature).
```math
\int_{-\infty}^{+\infty} f(x) \exp(-x^2) dx \approx \sum_{i=1}^{n} w_i f(x_i)
```
The option `normalize=true` instead computes
```math
\int_{-\infty}^{+\infty} f(x) \phi(x) dx \approx \sum_{i=1}^{n} w_i f(x_i),
```
where $\phi$ is the standard normal density function.
# Examples
```jldoctest
julia> x, w = gausshermite(3);
Expand All @@ -19,10 +25,10 @@ julia> I ≈ 3(√π)/4
true
```
"""
function gausshermite(n::Integer)
function gausshermite(n::Integer; normalize = false)
x,w = unweightedgausshermite(n)
w .*= exp.(-x.^2)
x, w
normalize ? ( sqrt(2.0) * x, w / sqrt( π ) ) : ( x, w )
end

function unweightedgausshermite(n::Integer)
Expand Down
15 changes: 15 additions & 0 deletions test/test_gausshermite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@
@test FastGaussQuadrature.hermpoly_rec(0:20^2,20)[end] FastGaussQuadrature.hermpoly_rec(20^2,20)[1]
end

@testset "Normalize" begin
for t 1:6
x,w = gausshermite(t; normalize = true)
N = 2t - 1
v = 1.0
for n 2:2:N
v *= (n-1)
@test v dot( x.^n, w )
end
for n 1:2:N
@test abs( 0.0 - dot( x.^n, w ) ) < 10 * tol
end
end
end

@testset "All" begin
for n in 2:220
x,w = gausshermite(n)
Expand Down

0 comments on commit c755422

Please sign in to comment.