@@ -2,56 +2,59 @@ __precompile__()
2
2
3
3
module NumericalIntegration
4
4
5
- using Compat
6
-
7
5
export integrate
8
6
export Trapezoidal, TrapezoidalEven, TrapezoidalFast, TrapezoidalEvenFast
9
7
export SimpsonEven, SimpsonEvenFast
10
8
export IntegrationMethod
11
9
12
- @compat abstract type IntegrationMethod end
10
+ abstract type IntegrationMethod end
13
11
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
20
18
21
19
const HALF = 1 // 2
22
20
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)
26
29
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 ])
28
31
end
29
- return HALF * retval
32
+ return HALF * retval
30
33
end
31
34
32
- function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: TrapezoidalEven )
35
+ function integrate (x:: AbstractVector , y:: AbstractVector , :: TrapezoidalEven )
33
36
@assert length (x) == length (y) " x and y vectors must be of the same length!"
34
37
return (x[2 ] - x[1 ]) * (HALF * (y[1 ] + y[end ]) + sum (y[2 : end - 1 ]))
35
38
end
36
39
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 )
39
42
@fastmath @simd for i in 1 : length (y)- 1
40
43
@inbounds retval += (x[i+ 1 ] - x[i]) * (y[i] + y[i+ 1 ])
41
44
end
42
45
return HALF * retval
43
46
end
44
47
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 )
47
50
N = length (y) - 1
48
51
@fastmath @simd for i in 2 : N
49
52
@inbounds retval += y[i]
50
53
end
51
54
@inbounds return (x[2 ] - x[1 ]) * (retval + HALF* y[1 ] + HALF* y[end ])
52
55
end
53
56
54
- function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: SimpsonEven )
57
+ function integrate (x:: AbstractVector , y:: AbstractVector , :: SimpsonEven )
55
58
@assert length (x) == length (y) " x and y vectors must be of the same length!"
56
59
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
57
60
for i in 5 : length (y) - 1
@@ -60,7 +63,7 @@ function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector
60
63
return (x[2 ] - x[1 ]) * retval
61
64
end
62
65
63
- function integrate {X<:Number, Y<:Number} (x:: AbstractVector{X} , y:: AbstractVector{Y} , :: SimpsonEvenFast )
66
+ function integrate (x:: AbstractVector , y:: AbstractVector , :: SimpsonEvenFast )
64
67
@inbounds retval = 17 * y[1 ] + 59 * y[2 ] + 43 * y[3 ] + 49 * y[4 ]
65
68
@inbounds retval += 49 * y[end - 3 ] + 43 * y[end - 2 ] + 59 * y[end - 1 ] + 17 * y[end ]
66
69
retval /= 48
@@ -70,6 +73,6 @@ function integrate{X<:Number, Y<:Number}(x::AbstractVector{X}, y::AbstractVector
70
73
@inbounds return (x[2 ] - x[1 ]) * retval
71
74
end
72
75
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 ())
74
77
75
78
end
0 commit comments