Skip to content

Commit d999d21

Browse files
i2000sdextorious
authored andcommitted
Make integrations work for complex numbers as well. (#7)
1 parent 2083c71 commit d999d21

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/NumericalIntegration.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ 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-
function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}, ::Trapezoidal)
20+
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::Trapezoidal)
2121
@assert length(x) == length(y) "x and y vectors must be of the same length!"
2222
retval = zero(promote(x[1], y[1])[1])
2323
for i in 1 : length(y)-1
@@ -26,20 +26,20 @@ function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y},
2626
return 0.5 * retval
2727
end
2828

29-
function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEven)
29+
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEven)
3030
@assert length(x) == length(y) "x and y vectors must be of the same length!"
3131
return 0.5 * (x[end] - x[1]) / (length(y) - 1) * (y[1] + y[end] + sum(y[2:end-1]))
3232
end
3333

34-
function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalFast)
34+
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalFast)
3535
retval = zero(promote(x[1], y[1])[1])
3636
@fastmath @simd for i in 1 : length(y)-1
3737
@inbounds retval += (x[i+1] - x[i]) * (y[i] + y[i+1])
3838
end
3939
return 0.5 * retval
4040
end
4141

42-
function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEvenFast)
42+
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::TrapezoidalEvenFast)
4343
retval = zero(promote(x[1], y[1])[1])
4444
N = length(y) - 1
4545
@fastmath @simd for i in 2 : N
@@ -48,7 +48,7 @@ function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y},
4848
@inbounds return (x[end] - x[1]) / N * (retval + 0.5*y[1] + 0.5*y[end])
4949
end
5050

51-
function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEven)
51+
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEven)
5252
@assert length(x) == length(y) "x and y vectors must be of the same length!"
5353
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
5454
for i in 5 : length(y) - 1
@@ -57,7 +57,7 @@ function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y},
5757
return (x[end] - x[1]) / (length(y) - 1) * retval
5858
end
5959

60-
function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEvenFast)
60+
function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}, ::SimpsonEvenFast)
6161
@inbounds retval = 17*y[1] + 59*y[2] + 43*y[3] + 49*y[4]
6262
@inbounds retval += 49*y[end-3] + 43*y[end-2] + 59*y[end-1] + 17*y[end]
6363
retval /= 48
@@ -67,6 +67,6 @@ function integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y},
6767
@inbounds return (x[end] - x[1]) / (length(y) - 1) * retval
6868
end
6969

70-
integrate{X<:Real, Y<:Real}(x::AbstractVector{X}, y::AbstractVector{Y}) = integrate(x, y, TrapezoidalFast())
70+
integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector{Y}) = integrate(x, y, TrapezoidalFast())
7171

7272
end

0 commit comments

Comments
 (0)