Mercurial > repos > public > sbplib_julia
comparison src/SbpOperators/stencil.jl @ 633:a78bda7084f6 feature/quadrature_as_outer_product
Merge w. default
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Fri, 01 Jan 2021 16:34:55 +0100 |
parents | 03ef4d4740ab |
children | e14627e79a54 |
comparison
equal
deleted
inserted
replaced
561:04d7b4eb63ef | 633:a78bda7084f6 |
---|---|
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 Stencil(weights::NTuple; center::Int) | |
13 | |
14 Create a stencil with the given weights with element `center` as the center of the stencil. | |
15 """ | |
16 function Stencil(weights::NTuple; center::Int) | |
17 N = length(weights) | |
18 range = (1, N) .- center | |
19 | |
20 return Stencil(range, weights) | |
21 end | |
22 | |
23 """ | |
24 scale(s::Stencil, a) | |
25 | |
26 Scale the weights of the stencil `s` with `a` and return a new stencil. | |
27 """ | |
28 function scale(s::Stencil, a) | |
29 return Stencil(s.range, a.*s.weights) | |
30 end | |
31 | |
32 Base.eltype(::Stencil{T}) where T = T | |
33 | |
11 function flip(s::Stencil) | 34 function flip(s::Stencil) |
12 range = (-s.range[2], -s.range[1]) | 35 range = (-s.range[2], -s.range[1]) |
13 return Stencil(range, reverse(s.weights)) | 36 return Stencil(range, reverse(s.weights)) |
14 end | 37 end |
15 | 38 |
16 # Provides index into the Stencil based on offset for the root element | 39 # Provides index into the Stencil based on offset for the root element |
17 @inline function Base.getindex(s::Stencil, i::Int) | 40 @inline function Base.getindex(s::Stencil, i::Int) |
18 @boundscheck if i < s.range[1] || s.range[2] < i | 41 @boundscheck if i < s.range[1] || s.range[2] < i |
19 return eltype(s.weights)(0) | 42 return zero(eltype(s)) |
20 end | 43 end |
21 return s.weights[1 + i - s.range[1]] | 44 return s.weights[1 + i - s.range[1]] |
22 end | 45 end |
23 | 46 |
24 Base.@propagate_inbounds @inline function apply_stencil(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N} | 47 Base.@propagate_inbounds @inline function apply_stencil(s::Stencil{T,N}, v::AbstractVector, i::Int) where {T,N} |