comparison stencil.jl @ 8:433008d3b7d3

Move stencil to its own file
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 17 Dec 2018 14:30:59 +0100
parents
children 64530c07165b
comparison
equal deleted inserted replaced
7:99591b5c34a6 8:433008d3b7d3
1 struct Stencil
2 range::NTuple{2,Int}
3 weights::Vector # TBD: Should this be a tuple?
4 function Stencil(range, weights)
5 width = range[2]-range[1]+1
6 if width != length(weights)
7 error("The width and the number of weights must be the same")
8 end
9 new(range, weights)
10 end
11 end
12
13 function flip(s::Stencil)
14 range = (-s.range[2], -s.range[1])
15 s = Stencil(range, s.weights[end:-1:1])
16 end
17
18 # Provides index into the Stencil based on offset for the root element
19 function Base.getindex(s::Stencil, i::Int)
20 if s.range[1] <= i <= s.range[2]
21 return s.weights[1 + i - s.range[1]]
22 else
23 return 0
24 end
25 end
26
27 function apply(s::Stencil, v::AbstractVector, i::Int)
28 w = zero(v[0])
29 for j ∈ i+(s.range[1]:s.range[2])
30 w += v[j]
31 end
32 return w
33 end