Skip to content

Commit b4eaa00

Browse files
jw3126dextorious
authored andcommitted
Svector (#11)
* replace 0.5 by 1//2 * support static arrays * fix appveyor
1 parent 43245c3 commit b4eaa00

File tree

6 files changed

+60
-41
lines changed

6 files changed

+60
-41
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.5
7+
- 0.6
88
- nightly
99
notifications:
1010
email: false

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
julia 0.5
2-
Compat 0.17.0
1+
julia 0.6

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
environment:
22
matrix:
3-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
4-
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
3+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
4+
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
55
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
66
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
77

src/NumericalIntegration.jl

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,59 @@ __precompile__()
22

33
module NumericalIntegration
44

5-
using Compat
6-
75
export integrate
86
export Trapezoidal, TrapezoidalEven, TrapezoidalFast, TrapezoidalEvenFast
97
export SimpsonEven, SimpsonEvenFast
108
export IntegrationMethod
119

12-
@compat abstract type IntegrationMethod end
10+
abstract type IntegrationMethod end
1311

14-
immutable Trapezoidal <: IntegrationMethod end
15-
immutable TrapezoidalEven <: IntegrationMethod end
16-
immutable TrapezoidalFast <: IntegrationMethod end
17-
immutable TrapezoidalEvenFast <: IntegrationMethod end
18-
immutable SimpsonEven <: IntegrationMethod end # https://en.wikipedia.org/wiki/Simpson%27s_rule#Alternative_extended_Simpson.27s_rule
19-
immutable SimpsonEvenFast <: IntegrationMethod end
12+
struct Trapezoidal <: IntegrationMethod end
13+
struct TrapezoidalEven <: IntegrationMethod end
14+
struct TrapezoidalFast <: IntegrationMethod end
15+
struct TrapezoidalEvenFast <: IntegrationMethod end
16+
struct SimpsonEven <: IntegrationMethod end # https://en.wikipedia.org/wiki/Simpson%27s_rule#Alternative_extended_Simpson.27s_rule
17+
struct SimpsonEvenFast <: IntegrationMethod end
2018

2119
const HALF = 1//2
2220

23-
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::Trapezoidal)
24-
@assert length(x) == length(y) "x and y vectors must be of the same length!"
25-
retval = zero(promote(x[1], y[1])[1])
21+
function _zero(x,y)
22+
ret = zero(eltype(x)) + zero(eltype(y))
23+
ret / 2
24+
end
25+
26+
function integrate(x::AbstractVector, y::AbstractVector, ::Trapezoidal)
27+
@assert length(x) == length(y) "x and y vectors must be of the same length!"
28+
retval = _zero(x,y)
2629
for i in 1 : length(y)-1
27-
retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
30+
retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
2831
end
29-
return HALF * retval
32+
return HALF * retval
3033
end
3134

32-
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEven)
35+
function integrate(x::AbstractVector, y::AbstractVector, ::TrapezoidalEven)
3336
@assert length(x) == length(y) "x and y vectors must be of the same length!"
3437
return (x[2] - x[1]) * (HALF * (y[1] + y[end]) + sum(y[2:end-1]))
3538
end
3639

37-
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalFast)
38-
retval = zero(promote(x[1], y[1])[1])
40+
function integrate(x::AbstractVector, y::AbstractVector, ::TrapezoidalFast)
41+
retval = _zero(x,y)
3942
@fastmath @simd for i in 1 : length(y)-1
4043
@inbounds retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
4144
end
4245
return HALF * retval
4346
end
4447

45-
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEvenFast)
46-
retval = zero(promote(x[1], y[1])[1])
48+
function integrate(x::AbstractVector, y::AbstractVector, ::TrapezoidalEvenFast)
49+
retval = _zero(x,y)
4750
N = length(y) - 1
4851
@fastmath @simd for i in 2 : N
4952
@inbounds retval += y[i]
5053
end
5154
@inbounds return (x[2] - x[1]) * (retval + HALF*y[1] + HALF*y[end])
5255
end
5356

54-
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEven)
57+
function integrate(x::AbstractVector, y::AbstractVector, ::SimpsonEven)
5558
@assert length(x) == length(y) "x and y vectors must be of the same length!"
5659
retval = (17*y[1] + 59*y[2] + 43*y[3] + 49*y[4] + 49*y[end-3] + 43*y[end-2] + 59*y[end-1] + 17*y[end]) / 48
5760
for i in 5 : length(y) - 1
@@ -60,7 +63,7 @@ function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector
6063
return (x[2] - x[1]) * retval
6164
end
6265

63-
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEvenFast)
66+
function integrate(x::AbstractVector, y::AbstractVector, ::SimpsonEvenFast)
6467
@inbounds retval = 17*y[1] + 59*y[2] + 43*y[3] + 49*y[4]
6568
@inbounds retval += 49*y[end-3] + 43*y[end-2] + 59*y[end-1] + 17*y[end]
6669
retval /= 48
@@ -70,6 +73,6 @@ function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector
7073
@inbounds return (x[2] - x[1]) * retval
7174
end
7275

73-
integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}) = integrate(x, y, TrapezoidalFast())
76+
integrate(x::AbstractVector, y::AbstractVector) = integrate(x, y, TrapezoidalFast())
7477

7578
end

test/REQUIRE

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

test/runtests.jl

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
using NumericalIntegration
22
using Base.Test
3+
using StaticArrays
34

4-
x = collect(-π : π/1000 : π)
5-
y = sin.(x)
6-
p = collect(0 : π/1000 : π)
7-
q = sin.(p)
8-
for M in subtypes(IntegrationMethod)
9-
println("Testing method: ", M)
10-
for T in [Float32, Float64, BigFloat]
11-
for (xs,ys,val,atol) in [
12-
(x,y,0,1e-4),
13-
(p,q,2,1e-4),
14-
]
15-
result = @inferred integrate(T.(xs), T.(ys),M())
16-
@test isapprox(result, val, atol=atol)
17-
@test typeof(result) == T
5+
@testset "compare with analytic result" begin
6+
x = collect(-π : π/1000 : π)
7+
y = sin.(x)
8+
p = collect(0 : π/1000 : π)
9+
q = sin.(p)
10+
for M in subtypes(IntegrationMethod)
11+
for T in [Float32, Float64, BigFloat]
12+
for (xs,ys,val,atol) in [
13+
(x,y,0,1e-4),
14+
(p,q,2,1e-4),
15+
]
16+
result = @inferred integrate(T.(xs), T.(ys),M())
17+
@test isapprox(result, val, atol=atol)
18+
@test typeof(result) == T
19+
end
1820
end
1921
end
2022
end
23+
24+
@testset "SVector" begin
25+
xs = linspace(0,1,10)
26+
ys1 = randn(10)
27+
ys2 = randn(10)
28+
ys = map(SVector, ys1, ys2)
29+
for M in subtypes(IntegrationMethod)
30+
m = M()
31+
res1 = integrate(xs,ys1,m)
32+
res2 = integrate(xs,ys2,m)
33+
res = @inferred integrate(xs,ys,m)
34+
@test res @SVector [res1, res2]
35+
end
36+
end

0 commit comments

Comments
 (0)