Skip to content

Commit 5a87936

Browse files
committed
add utility function to increment/decrement indices
1 parent de9fd7e commit 5a87936

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/Utils/Utils.jl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export
3535
cart2pol,
3636
sph2cart,
3737
pol2cart,
38-
getnodes
38+
getnodes,
39+
increment_index,
40+
decrement_index
3941

4042
"""
4143
svector(f,n)
@@ -432,4 +434,27 @@ function weight(x::T) where {T}
432434
end
433435
end
434436

437+
"""
438+
increment_index(I::CartesianIndex,k[,n=1])
439+
440+
Increment `I` by `n` along the dimension `k`. This is equivalent to `I +=
441+
n*eₖ`, where `eₖ` is a vector with with `1` at the `k`-th coordinate and zeros elsewhere.
442+
"""
443+
function increment_index(I::CartesianIndex,dim::Integer,nb::Integer=1)
444+
N = length(I)
445+
@assert 1 dim length(I)
446+
return I + CartesianIndex(ntuple(i -> i==dim ? nb : 0,N))
447+
end
448+
449+
"""
450+
decrement_index(I::CartesianIndex,k[,n=1])
451+
452+
Equivalent to [`increment_index`](@ref)(I,k,-n)
453+
"""
454+
function decrement_index(I::CartesianIndex,dim::Integer,nb::Integer=1)
455+
N = length(I)
456+
@assert 1 dim length(I)
457+
return I + CartesianIndex(ntuple(i -> i==dim ? -nb : 0,N))
458+
end
459+
435460
end # module

0 commit comments

Comments
 (0)