Skip to content

Commit 01f0c2b

Browse files
Datserisdextorious
authored andcommitted
better function definitions (#5)
1 parent 99860b7 commit 01f0c2b

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/NumericalIntegration.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,38 @@ immutable TrapezoidalEvenFast <: IntegrationMethod end
1717
immutable SimpsonEven <: IntegrationMethod end # https://en.wikipedia.org/wiki/Simpson%27s_rule#Alternative_extended_Simpson.27s_rule
1818
immutable SimpsonEvenFast <: IntegrationMethod end
1919

20-
integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, method::IntegrationMethod=Trapezoidal()) = integrate(x, y, method)
21-
22-
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::Trapezoidal)
23-
@assert length(x) == length(y) "x and y vectors must be of the same length!"
24-
retval = zero(eltype(x))
20+
function integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}, ::Trapezoidal)
21+
@assert length(x) == length(y) "x and y vectors must be of the same length!"
22+
retval = zero(promote(x[1], y[1])[1])
2523
for i in 1 : length(y)-1
26-
retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
24+
retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
2725
end
28-
return 0.5 * retval
26+
return 0.5 * retval
2927
end
3028

31-
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::TrapezoidalEven)
29+
function integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}, ::TrapezoidalEven)
3230
@assert length(x) == length(y) "x and y vectors must be of the same length!"
3331
return 0.5 * (x[end] - x[1]) / (length(y) - 1) * (y[1] + y[end] + sum(y[2:end-1]))
3432
end
3533

36-
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::TrapezoidalFast)
37-
retval = zero(eltype(x))
34+
function integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}, ::TrapezoidalFast)
35+
retval = zero(promote(x[1], y[1])[1])
3836
@fastmath @simd for i in 1 : length(y)-1
3937
@inbounds retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
4038
end
4139
return 0.5 * retval
4240
end
4341

44-
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::TrapezoidalEvenFast)
45-
retval = zero(eltype(x))
42+
function integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}, ::TrapezoidalEvenFast)
43+
retval = zero(promote(x[1], y[1])[1])
4644
N = length(y) - 1
4745
@fastmath @simd for i in 2 : N
4846
@inbounds retval += y[i]
4947
end
5048
@inbounds return (x[end] - x[1]) / N * (retval + 0.5*y[1] + 0.5*y[end])
5149
end
5250

53-
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::SimpsonEven)
51+
function integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}, ::SimpsonEven)
5452
@assert length(x) == length(y) "x and y vectors must be of the same length!"
5553
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
5654
for i in 5 : length(y) - 1
@@ -59,7 +57,7 @@ function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::SimpsonEven)
5957
return (x[end] - x[1]) / (length(y) - 1) * retval
6058
end
6159

62-
function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::SimpsonEvenFast)
60+
function integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}, ::SimpsonEvenFast)
6361
@inbounds retval = 17*y[1] + 59*y[2] + 43*y[3] + 49*y[4]
6462
@inbounds retval += 49*y[end-3] + 43*y[end-2] + 59*y[end-1] + 17*y[end]
6563
retval /= 48
@@ -69,4 +67,6 @@ function integrate{T<:AbstractFloat}(x::Vector{T}, y::Vector{T}, ::SimpsonEvenFa
6967
@inbounds return (x[end] - x[1]) / (length(y) - 1) * retval
7068
end
7169

70+
integrate{X<:Real, Y<:Real}(x::Vector{X}, y::Vector{Y}) = integrate(x, y, TrapezoidalFast())
71+
7272
end

0 commit comments

Comments
 (0)