comparison src/SbpOperators/stencil.jl @ 584:4aa7fe13a984

Add scale() and eltype() methods for stencils
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 02 Dec 2020 11:18:18 +0100
parents 01b851161018
children 03ef4d4740ab
comparison
equal deleted inserted replaced
560:d1929491180b 584:4aa7fe13a984
6 @assert range[2]-range[1]+1 == N 6 @assert range[2]-range[1]+1 == N
7 new{T,N}(range,weights) 7 new{T,N}(range,weights)
8 end 8 end
9 end 9 end
10 10
11 """
12 scale(s::Stencil, a)
13
14 Scale the weights of the stencil `s` with `a` and return a new stencil.
15 """
16 function scale(s::Stencil, a)
17 return Stencil(s.range, a.*s.weights)
18 end
19
20 Base.eltype(::Stencil{T}) where T = T
21
11 function flip(s::Stencil) 22 function flip(s::Stencil)
12 range = (-s.range[2], -s.range[1]) 23 range = (-s.range[2], -s.range[1])
13 return Stencil(range, reverse(s.weights)) 24 return Stencil(range, reverse(s.weights))
14 end 25 end
15 26
16 # Provides index into the Stencil based on offset for the root element 27 # Provides index into the Stencil based on offset for the root element
17 @inline function Base.getindex(s::Stencil, i::Int) 28 @inline function Base.getindex(s::Stencil, i::Int)
18 @boundscheck if i < s.range[1] || s.range[2] < i 29 @boundscheck if i < s.range[1] || s.range[2] < i
19 return eltype(s.weights)(0) 30 return zero(eltype(s))
20 end 31 end
21 return s.weights[1 + i - s.range[1]] 32 return s.weights[1 + i - s.range[1]]
22 end 33 end
23 34
24 Base.@propagate_inbounds @inline function apply_stencil(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N} 35 Base.@propagate_inbounds @inline function apply_stencil(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N}