Skip to content

Commit f8bcd75

Browse files
authored
SubOperator index with colon (#130)
1 parent ead327d commit f8bcd75

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/Operators/Operator.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export ldirichlet,rdirichlet,lneumann,rneumann
55
export ldiffbc,rdiffbc,diffbcs
66
export domainspace,rangespace
77

8+
const VectorIndices = Union{AbstractRange, Colon}
9+
const IntOrVectorIndices = Union{Integer, VectorIndices}
810

911
abstract type Operator{T} end #T is the entry type, Float64 or Complex{Float64}
1012

src/Operators/SubOperator.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ end
272272
size(V::SubOperator) = V.dims
273273
size(V::SubOperator,k::Int) = V.dims[k]
274274

275+
axes(V::SubOperator) = map(Base.OneTo, size(V))
276+
axes(V::SubOperator, k::Integer) = k <= 2 ? axes(V)[k] : Base.OneTo(1)
277+
275278
unsafe_getindex(V::SubOperator,k::Integer,j::Integer) = V.parent[reindex(V,parentindices(V),(k,j))...]
276-
getindex(V::SubOperator,k::Integer,j::Integer) = V.parent[reindex(V,parentindices(V),(k,j))...]
277-
getindex(V::SubOperator,k::Integer,j::AbstractRange) = V.parent[reindex(V,parentindices(V),(k,j))...]
278-
getindex(V::SubOperator,k::AbstractRange,j::Integer) = V.parent[reindex(V,parentindices(V),(k,j))...]
279-
getindex(V::SubOperator,k::AbstractRange,j::AbstractRange) = V.parent[reindex(V,parentindices(V),(k,j))...]
279+
function getindex(V::SubOperator,k::IntOrVectorIndices,j::IntOrVectorIndices)
280+
V.parent[reindex(V,parentindices(V),(k,j))...]
281+
end
280282
Base.parent(S::SubOperator) = S.parent
281283
Base.parentindices(S::SubOperator) = S.indexes
282284

test/runtests.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,35 @@ end
164164
end
165165
end
166166

167+
@testset "operator indexing" begin
168+
@testset "SubOperator" begin
169+
D = Dirichlet(ConstantSpace(0..1))
170+
S = D[:, :]
171+
@test S[1,1] == 1
172+
ax1 = axes(S, 1)
173+
ax2 = axes(S, 2)
174+
inds1 = Any[ax1, StepRange(ax1), :]
175+
inds2 = Any[ax2, StepRange(ax2), :]
176+
@testset for r2 in inds2, r1 in inds1
177+
M = S[r1, r2]
178+
@test M isa AbstractMatrix
179+
@test size(M) == (2,1)
180+
@test all(==(1), M)
181+
end
182+
@testset for r1 in inds1
183+
V = S[r1, 1]
184+
@test V isa AbstractVector
185+
@test size(V) == (2,)
186+
@test all(==(1), V)
187+
end
188+
@testset for r2 in inds2
189+
V = S[1, r2]
190+
@test V isa AbstractVector
191+
@test size(V) == (1,)
192+
@test all(==(1), V)
193+
end
194+
end
195+
end
196+
167197
@time include("ETDRK4Test.jl")
168198
include("show.jl")

0 commit comments

Comments
 (0)