Is there a convenience package for nonstandard ranges? For instance, I want a rectangle on the complex plane I could use rectangulardomain
from RootsAndPoles
but that feels pretty specific.
Efficient rectangular grids: RectiGrids.jl, some discussion:
Depending on what you need in the end, you may also use map
on top of the grid:
julia> using RectiGrids
julia> grid(1:3, 2:4)
2-dimensional KeyedArray(...) with keys:
↓ 3-element UnitRange{Int64}
→ 3-element UnitRange{Int64}
And data, 3×3 RectiGrids.RectiGridArr{...}:
(2) (3) (4)
(1) (1, 2) (1, 3) (1, 4)
(2) (2, 2) (2, 3) (2, 4)
(3) (3, 2) (3, 3) (3, 4)
# similar to rectangulardomain() from RootsAndPoles, but better:
# remains a multidimensional array, not a flat vector
julia> map(t -> complex(t...), grid(1:3, 2:4))
2-dimensional KeyedArray(...) with keys:
↓ 3-element UnitRange{Int64}
→ 3-element UnitRange{Int64}
And data, 3×3 Matrix{Complex{Int64}}:
(2) (3) (4)
(1) 1+2im 1+3im 1+4im
(2) 2+2im 2+3im 2+4im
(3) 3+2im 3+3im 3+4im
# mapview() instead of map() avoids materializing the whole array:
julia> using FlexiMaps
julia> mapview(t -> complex(t...), grid(1:3, 2:4))
3×3 FlexiMaps.MappedArray{...}:
1+2im 1+3im 1+4im
2+2im 2+3im 2+4im
3+2im 3+3im 3+4im
To some extent, a CartesianIndices
instance behaves like an nD range
julia> CartesianIndices((1:2, 3:4)) |> step
CartesianIndex(1, 1)
julia> CartesianIndices((1:2, 3:4)) |> first
CartesianIndex(1, 3)
julia> CartesianIndices((1:2, 3:4)) |> last
CartesianIndex(2, 4)
julia> using MappedArrays
julia> mappedarray(t->complex(Tuple(t)...), CartesianIndices((1:3, 2:4)))
3×3 mappedarray(var"#13#14"(), ::CartesianIndices{2, Tuple{UnitRange{Int64}, UnitRange{Int64}}}) with eltype Complex{Int64}:
1+2im 1+3im 1+4im
2+2im 2+3im 2+4im
3+2im 3+3im 3+4im
However, I’m unsure if you’re looking for this, or a 1D range of CartesianIndex
es.
1 Like
I think this question needs clarifying. The other answers assume you mean a range as in a list of discrete numbers, but I read it as a continuous domain. Which is it you’re looking for?