Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 7b0b7fd

Browse files
committed
readded tests for ComposedBoundaryPaddedArray, added CartesianIndex getindex for padded arrays
1 parent da3514e commit 7b0b7fd

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/boundary_padded_arrays.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727
Higher dimensional generalization of BoundaryPaddedVector, pads an array of dimension N along the dimension D with 2 Arrays of dimension N-1, stored in lower and upper
2828
2929
"""
30-
struct BoundaryPaddedArray{T<:Number, D, N, M, V<:AbstractArray{T, N}, B<: AbstractArray{T, M}} <: AbstractBoundaryPaddedArray{T,N}
30+
struct BoundaryPaddedArray{T, D, N, M, V<:AbstractArray{T, N}, B<: AbstractArray{T, M}} <: AbstractBoundaryPaddedArray{T,N}
3131
lower::B #an array of dimension M = N-1, used to extend the lower index boundary
3232
upper::B #Ditto for the upper index boundary
3333
u::V
@@ -71,7 +71,7 @@ end
7171

7272
# Composed BoundaryPaddedArray
7373

74-
struct ComposedBoundaryPaddedArray{T<:Number, N, M, V<:AbstractArray{T, N}, B<: AbstractArray{T, M}} <: AbstractBoundaryPaddedArray{T, N}
74+
struct ComposedBoundaryPaddedArray{T, N, M, V<:AbstractArray{T, N}, B<: AbstractArray{T, M}} <: AbstractBoundaryPaddedArray{T, N}
7575
lower::Vector{B}
7676
upper::Vector{B}
7777
u::V
@@ -107,7 +107,7 @@ Base.ndims(Q::AbstractBoundaryPaddedArray{T,N}) where {T,N} = N
107107
add_dim(A::AbstractArray, i) = reshape(A, size(A)...,i)
108108
add_dim(i) = i
109109

110-
function Base.getindex(Q::BoundaryPaddedArray{T,D,N,M,V,B}, _inds...) where {T,D,N,M,V,B} #supports range and colon indexing!
110+
function Base.getindex(Q::BoundaryPaddedArray{T,D,N,M,V,B}, _inds::NTuple{N,Int64}...) where {T,D,N,M,V,B} #supports range and colon indexing!
111111
inds = [_inds...]
112112
S = size(Q)
113113
dim = D
@@ -131,10 +131,8 @@ function Base.getindex(Q::BoundaryPaddedArray{T,D,N,M,V,B}, _inds...) where {T,D
131131
end
132132
end
133133

134-
function Base.getindex(Q::ComposedBoundaryPaddedArray, inds...) #as yet no support for range indexing or colon indexing
134+
function Base.getindex(Q::ComposedBoundaryPaddedArray{T, N, M, V, B} , inds::NTuple{N, Int64}...) where {T, N, M, V, B} #as yet no support for range indexing or colon indexing
135135
S = size(Q)
136-
T = gettype(Q)
137-
N = ndims(Q)
138136
@assert reduce((&), inds .<= S)
139137
for (dim, index) in enumerate(inds)
140138
if index == 1
@@ -155,3 +153,5 @@ function Base.getindex(Q::ComposedBoundaryPaddedArray, inds...) #as yet no suppo
155153
end
156154
return Q.u[(inds.-1)...]
157155
end
156+
157+
getindex(A::AbstractBoundaryPaddedArray{T,N}, I::CartesianIndex{N}) where {T,N} = A[Tuple(I)...]

test/boundary_padded_array.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,51 @@ for dimensionality in 2:5
1414

1515
@test A == Array(Apad) #test Concretization of BoundaryPaddedMatrix
1616

17-
for I in CartesianIndeces(A) #test getindex for all indicies of Apad
17+
for I in CartesianIndices(A) #test getindex for all indicies of Apad
1818
@test A[I] == Apad[I]
1919
end
2020
end
2121
end
22+
23+
################################################################################
24+
# Test ComposedBoundaryPaddedMatrix
25+
################################################################################
26+
27+
n = 10
28+
m = 21
29+
A = rand(n,m)
30+
A[1,1] = A[end,1] = A[1,end] = A[end,end] = 0.0
31+
32+
lower = Vector[A[1,2:(end-1)], A[2:(end-1),1]]
33+
upper = Vector[A[end,2:(end-1)], A[2:(end-1),end]]
34+
35+
Apad = ComposedBoundaryPaddedMatrix{Float64, typeof(A), typeof(lower[1])}(lower, upper, A[2:(end-1), 2:(end-1)])
36+
37+
@test A == Array(Apad) #test Concretization of BoundaryPaddedMatrix
38+
39+
for i in 1:n, j in 1:m #test getindex for all indicies of Apad
40+
@test A[i,j] == Apad[i,j]
41+
end
42+
43+
################################################################################
44+
# Test ComposedBoundaryPaddedTensor{3}
45+
################################################################################
46+
47+
n = 10
48+
m = 12
49+
o = 7
50+
A = rand(n,m,o)
51+
A[1,1,:] = A[end,1, :] = A[1,end, :] = A[end,end, :] = 0.0
52+
A[1,:,1] = A[end, :, 1] = A[1,:,end] = A[end,:,end] = 0.0
53+
A[:,1,1] = A[:,end,1] = A[:,1,end] = A[:,end,end] = 0.0
54+
55+
lower = Matrix[A[1,2:(end-1), 2:(end-1)], A[2:(end-1),1, 2:(end-1)] A[2:(end-1), 2:(end-1), 1]]
56+
upper = Matrix[A[end, 2:(end-1), 2:(end-1)], A[2:(end-1), end, 2:(end-1)] A[2:(end-1), 2:(end-1), end]]
57+
58+
Apad = BoundaryPadded3Tensor{Float64, typeof(A), typeof(lower[1])}(lower, upper, A[2:(end-1), 2:(end-1), 2:(end-1)])
59+
60+
@test A == Array(Apad) #test Concretization of BoundaryPaddedMatrix
61+
62+
for I in CartesianIndices(A) #test getindex for all indicies of Apad
63+
@test A[I] == Apad[I]
64+
end

0 commit comments

Comments
 (0)