Skip to content

Commit cab1bf1

Browse files
committed
Added the SimpsonEvenFast method.
1 parent 8f91881 commit cab1bf1

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ language: julia
33
os:
44
- linux
55
- osx
6+
- windows
67
julia:
78
- release
8-
- nightly
99
notifications:
1010
email: false
1111
# uncomment the following lines to override the default test script

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ The currently available methods are:
3131
- TrapezoidalFast
3232
- TrapezoidalEvenFast
3333
- SimpsonEven
34+
- SimpsonEvenFast
3435

35-
All methods containing "Even" in the name assume evenly spaced data. All methods containing "Fast" omit basic correctness checks and focus on performance. The fast methods will crash horribly if you supply invalid data.
36+
All methods containing "Even" in the name assume evenly spaced data. All methods containing "Fast" omit basic correctness checks and focus on performance. Consequently, the fast methods will segfault or produce incorrect results if you supply incorrect data (vectors of different lengths, etc.).

appveyor.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ environment:
22
matrix:
33
- JULIAVERSION: "julialang/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
44
- JULIAVERSION: "julialang/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
5-
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
6-
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
75

86
branches:
97
only:

src/NumericalIntegration.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ immutable TrapezoidalEven <: IntegrationMethod end
66
immutable TrapezoidalFast <: IntegrationMethod end
77
immutable TrapezoidalEvenFast <: IntegrationMethod end
88
immutable SimpsonEven <: IntegrationMethod end # https://en.wikipedia.org/wiki/Simpson%27s_rule#Alternative_extended_Simpson.27s_rule
9+
immutable SimpsonEvenFast <: IntegrationMethod end
910

1011
integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, method::IntegrationMethod=Trapezoidal()) = integrate(x, y, method)
1112

@@ -42,16 +43,25 @@ end
4243

4344
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::SimpsonEven)
4445
@assert length(x) == length(y) "x and y vectors must be of the same length!"
45-
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]
46-
retval /= 48
46+
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
4747
for i in 5 : length(y) - 1
4848
retval += y[i]
4949
end
5050
return (x[end] - x[1]) / (length(y) - 1) * retval
5151
end
5252

53+
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::SimpsonEvenFast)
54+
@inbounds retval = 17*y[1] + 59*y[2] + 43*y[3] + 49*y[4]
55+
@inbounds retval += 49*y[end-3] + 43*y[end-2] + 59*y[end-1] + 17*y[end]
56+
retval /= 48
57+
@fastmath @inbounds for i in 5 : length(y)-1
58+
retval += y[i]
59+
end
60+
@inbounds return (x[end] - x[1]) / (length(y) - 1) * retval
61+
end
62+
5363
export integrate
5464
export Trapezoidal, TrapezoidalEven, TrapezoidalFast, TrapezoidalEvenFast
55-
export SimpsonEven
65+
export SimpsonEven, SimpsonEvenFast
5666

5767
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using NumericalIntegration
22
using Base.Test
33

4-
methods = [Trapezoidal(), TrapezoidalEven(), TrapezoidalFast(), TrapezoidalEvenFast(), SimpsonEven()]
4+
methods = [Trapezoidal(), TrapezoidalEven(), TrapezoidalFast(), TrapezoidalEvenFast(), SimpsonEven(), SimpsonEvenFast()]
55
x = collect(-π : π/1000 : π)
66
y = sin(x)
77
for method in methods

0 commit comments

Comments
 (0)