A package for arbitrary array indices?

Hello,

I am a looking for a package that allows me to dynamically change the indexing of an array. I’m aware of OffsetArrays but this is not what I need.

Say I have an 8 by 8 symmetric matrix A. I have an algorithm that defines the “merging” of two indices. Say that I merge indices 3 and 5 by doing A[3,:] .= (A[3,:] .+ A[5,:])./2 and the same for the columns. Then I can get rid of 5, in practice I do it with a view and an InvertedIndex to exclude 5. So this view is a 7x7 matrix now.

The new elements at index 3 are in fact that of another node from a graph (that was defined by A), node 9. What I would like is a way to change the indexing of A such that
A[9,:] gives me what’s in A[3,:], and A[3,:] should be out of bounds. In other words, indexing A maps 3 to 9.

I suppose I could implement that myself but I’d like to avoid the risk of making mistakes if this already exists.

Thanks.

I’m not aware of any packages that allow you to redefine array indices. Maybe a different data structure like a dictionary would be the answer?

D = Dict(i => A[:,a] for (i,a) in enumerate(axes(A,2)))

Which would allow for things like:

D[9] = (D[3] + D[5])/2
delete!(D,3)
delete!(D,5)

There are probably more efficient methods though. If A is based on a graph perhaps MetaGraphs.jl might be useful?

Your application seems highly specialized. Maybe you should just build your own array type. Then getindex can do whatever you would like.

https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array

2 Likes