Convenient creation of an OffsetArray?

ryofurue
Wed, 17 Apr 2024 05:53:25 +0000
General Usage

I’ve been using OffsetArrays and is a bit frustrated with its cumbersome creation:

# xax[0:imax], zax[0:kmax] are OffsetArrays.
# I want to create an OffsetArray [0:imax, 0:kmax]
oa = OffsetArray(Array{Float64,2}(undef, length(xax), length(zax)), axes(xax,1), axes(zax,1))
# calculate values and assign them to oa . . .

Is there a standard, easy solution?

If the eltype of the new array is the same as those of xax and zax and the new array is always 2D, then I could use a hack like

oa = xax * zax'

But, for other cases, it’s getting more and more cumbersome:

oa = Float64.(xax * reshape(yax, 1, axes(yax)) * reshape(zax, 1, 1, axes(zax)))

I do this a lot and so I wish I would be able to do something like

oa = OffsetArray{Float64,2}(undef, axes(xax,1), axes(zax,1))
# calculate values and assign them to oa . . .

It’s entirely possible (from my past experience with Julia) that I haven’t read the right documentation. If so, I apologize. I’m trying but I have found only this:

https://juliaarrays.github.io/OffsetArrays.jl/stable/

which seems to me to suggest that OffsetArray is always a wrapper to an existing “real” array.

6 posts - 2 participants

Read full topic