Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pluto cyclic reference
### A Pluto.jl notebook ###
# v0.16.1
using Markdown
using InteractiveUtils
# ╔═╡ d00d6ce7-6cb2-4a8e-a119-aafe2d372a77
function stencil(kernel_f!, N)
A = reshape(BandedMatrix(Zeros(N^2, N^2), (N+1, N+1)), N, N, N, N)
_A = PaddedArray(A, 0)
for I in CartesianIndices((1:N-1, 1:N-1))
ξ, η = Tuple(I) ./ (N + 1)
kernel_f!(view(_A, I .+ CartesianIndices((-1:1, -1:1)), I), ξ, η)
end
return A
end
# ╔═╡ d33ba16a-0a0b-4b8c-a0aa-12997516b14b
A = stencil(kernel_f!, 20);
# ╔═╡ 7e073004-b039-4636-947e-26b482657134
begin
struct PaddedArray{T, N, A<:AbstractArray{T, N}} <: AbstractArray{T, N}
parent::A
padding::T
end
function PaddedArray(parent::AbstractArray{T, N}, padding=zero(T)) where {T, N}
return PaddedArray{T, N, typeof(parent)}(parent, padding)
end
Base.axes(x::PaddedArray) = axes(x.parent)
Base.size(x::PaddedArray) = size(x.parent)
Base.getindex(x::PaddedArray, i::Int...) = get(x.parent, i, x.padding)
Base.setindex!(x::PaddedArray, y, i::Int...) = checkbounds(Bool, x.parent, i...) && (x.parent[i...] = y)
Base.checkbounds(::Type{Bool}, x::PaddedArray, i...) = true
end
# ╔═╡ Cell order:
# ╠═7e073004-b039-4636-947e-26b482657134
# ╠═d00d6ce7-6cb2-4a8e-a119-aafe2d372a77
# ╠═d33ba16a-0a0b-4b8c-a0aa-12997516b14b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment