Skip to content

Commit 1c053bb

Browse files
m-wellsdextorious
authored andcommitted
n-dimension integration (#17)
* n-dimensional integration * added Project and Manifest * removed Manifest and Project * fixing versioninfo not defined appveyor issue * added HCubature to REQUIRE (for testing n-dimensional integration) * moved HCubature to test/REQUIRE, and making range 0.7 friendly * added using Pkg to work with nightly
1 parent 87a130e commit 1c053bb

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ install:
2828
build_script:
2929
# Need to convert from shallow to complete for Pkg.clone to work
3030
- IF EXIST .git\shallow (git fetch --unshallow)
31-
- C:\projects\julia\bin\julia -e "versioninfo();
31+
- C:\projects\julia\bin\julia -e "using InteractiveUtils; versioninfo(); using Pkg;
3232
Pkg.clone(pwd(), \"NumericalIntegration\"); Pkg.build(\"NumericalIntegration\")"
3333

3434
test_script:
35-
- C:\projects\julia\bin\julia -e "Pkg.test(\"NumericalIntegration\")"
35+
- C:\projects\julia\bin\julia -e "using Pkg; Pkg.test(\"NumericalIntegration\")"

src/NumericalIntegration.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ function integrate(x::AbstractVector, y::AbstractVector, m::RombergEven)
176176
@inbounds return rombaux[maxsteps, prevrow]
177177
end
178178

179+
"""
180+
integrate(Y::AbstractArray{T,N}, Varargs{AbstractVector,N}, method)
181+
182+
Given an n-dimensional grid of values, compute the total integral along each dim
183+
"""
184+
function integrate(X::NTuple{N,AbstractVector}, Y::AbstractArray{T,N}, M::IntegrationMethod) :: T where {T,N}
185+
dims = size(Y)
186+
d,n = length(dims), dims[end]
187+
if d == 1
188+
return integrate(X[1], Y, M)
189+
else
190+
return integrate(X[end], [integrate(X[1:d-1], selectdim(Y,d,i), M) for i in 1:n], M)
191+
end
192+
end
193+
194+
179195

180196
# cumulative integrals
181197

@@ -242,12 +258,13 @@ function cumul_integrate(x::AbstractVector, y::AbstractMatrix, M::IntegrationMet
242258
return hcat([cumul_integrate(x,selectdim(y,dims,j),M) for j=1:size(y,dims)]...)
243259
end
244260

245-
246261
#default behaviour
247262
integrate(x::AbstractVector, y::AbstractVector) = integrate(x, y, Trapezoidal())
248263

249264
integrate(x::AbstractVector, y::AbstractMatrix; dims=2) = integrate(x, y, Trapezoidal(); dims=dims)
250265

266+
integrate(X::NTuple, Y::AbstractArray) = integrate(X, Y, Trapezoidal())
267+
251268
cumul_integrate(x::AbstractVector, y::AbstractVector) = cumul_integrate(x, y, Trapezoidal())
252269

253270
cumul_integrate(x::AbstractVector, y::AbstractMatrix; dims=2) = cumul_integrate(x, y, Trapezoidal(); dims=dims)

test/REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
StaticArrays
2+
HCubature

test/runtests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using NumericalIntegration
22
using Test
33
using InteractiveUtils # for subtypes
44
using StaticArrays
5+
using HCubature # for testing n-dimensional integration
56

67
@testset "compare with analytic result" begin
78
x = collect(-π : 2*π/2048 : π)
@@ -22,6 +23,25 @@ using StaticArrays
2223
end
2324
end
2425

26+
@testset "n-dimensional integration testing" begin
27+
X = range(0,stop=2π,length=10)
28+
Y = range(-π,stop=π,length=10)
29+
Z = range(0,stop=2,length=10)
30+
31+
A = Array{Float64}(undef,length(X),length(Y),length(Z))
32+
f(x) = sin(x[1]) + cos(x[2]) + 2x[3]
33+
34+
for (k,z) in enumerate(Z)
35+
for (j,y) in enumerate(Y)
36+
for (i,x) in enumerate(X)
37+
A[i,j,k] = f([x,y,z])
38+
end
39+
end
40+
end
41+
42+
@test isapprox(integrate((X,Y,Z),A), hcubature(f,[0,-π,0],[2π,π,2])[1], atol=1e-4)
43+
end
44+
2545
@testset "SVector" begin
2646
xs = range(0, stop=1, length=9)
2747
ys1 = randn(9)

0 commit comments

Comments
 (0)