@@ -7,6 +7,7 @@ using Compat
7
7
export integrate
8
8
export Trapezoidal, TrapezoidalEven, TrapezoidalFast, TrapezoidalEvenFast
9
9
export SimpsonEven, SimpsonEvenFast
10
+ export IntegrationMethod
10
11
11
12
@compat abstract type IntegrationMethod end
12
13
@@ -17,26 +18,28 @@ immutable TrapezoidalEvenFast <: IntegrationMethod end
17
18
immutable SimpsonEven <: IntegrationMethod end # https://en.wikipedia.org/wiki/Simpson%27s_rule#Alternative_extended_Simpson.27s_rule
18
19
immutable SimpsonEvenFast <: IntegrationMethod end
19
20
21
+ const HALF = 1 // 2
22
+
20
23
function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: Trapezoidal )
21
24
@assert length (x) == length (y) " x and y vectors must be of the same length!"
22
25
retval = zero (promote (x[1 ], y[1 ])[1 ])
23
26
for i in 1 : length (y)- 1
24
27
retval += (x[i+ 1 ] - x[i]) * (y[i] + y[i+ 1 ])
25
28
end
26
- return 0.5 * retval
29
+ return HALF * retval
27
30
end
28
31
29
32
function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: TrapezoidalEven )
30
33
@assert length (x) == length (y) " x and y vectors must be of the same length!"
31
- return (x[2 ] - x[1 ]) * (0.5 * (y[1 ] + y[end ]) + sum (y[2 : end - 1 ]))
34
+ return (x[2 ] - x[1 ]) * (HALF * (y[1 ] + y[end ]) + sum (y[2 : end - 1 ]))
32
35
end
33
36
34
37
function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: TrapezoidalFast )
35
38
retval = zero (promote (x[1 ], y[1 ])[1 ])
36
39
@fastmath @simd for i in 1 : length (y)- 1
37
40
@inbounds retval += (x[i+ 1 ] - x[i]) * (y[i] + y[i+ 1 ])
38
41
end
39
- return 0.5 * retval
42
+ return HALF * retval
40
43
end
41
44
42
45
function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: TrapezoidalEvenFast )
@@ -45,7 +48,7 @@ function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector
45
48
@fastmath @simd for i in 2 : N
46
49
@inbounds retval += y[i]
47
50
end
48
- @inbounds return (x[2 ] - x[1 ]) * (retval + 0.5 * y[1 ] + 0.5 * y[end ])
51
+ @inbounds return (x[2 ] - x[1 ]) * (retval + HALF * y[1 ] + HALF * y[end ])
49
52
end
50
53
51
54
function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: SimpsonEven )
0 commit comments