view stencil.jl @ 390:074862bcd626 patch_based_test

Close branch
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 01 Oct 2020 08:28:03 +0200
parents c0729ade65da
children
line wrap: on
line source

struct Stencil{T<:Real,N}
    range::Tuple{Int,Int}
    weights::NTuple{N,T}
end

function flip(s::Stencil)
    range = (-s.range[2], -s.range[1])
    return Stencil(range, reverse(s.weights))
end

# Provides index into the Stencil based on offset for the root element
function Base.getindex(s::Stencil, i::Int)
    @boundscheck if i < s.range[1] || s.range[2] < i
        return eltype(s.weights)(0)
    end
    return s.weights[1 + i - s.range[1]]
end

Base.@propagate_inbounds function apply(s::Stencil, v::AbstractVector, i::Int)
    w = zero(eltype(v))
    for j ∈ s.range[1]:s.range[2]
        @inbounds weight = s[j]
        w += weight*v[i+j]
    end
    return w
end

Base.@propagate_inbounds function applybackwards(s::Stencil, v::AbstractVector, i::Int)
    w = zero(eltype(v))
    for j ∈ s.range[1]:s.range[2]
        @inbounds weight = s[j]
        w += weight*v[i-j]
    end
    return w
end