# Is there a package for multidimensional ranges?

**URL:** https://discourse.julialang.org/t/is-there-a-package-for-multidimensional-ranges/98355
**Category:** General Usage
**Created:** [May 5, 2023, 12:35am UTC](https://discourse.julialang.org/t/is-there-a-package-for-multidimensional-ranges/98355 "2023-05-05T00:35:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [May 5, 2023, 12:35am UTC](https://discourse.julialang.org/t/is-there-a-package-for-multidimensional-ranges/98355/1 "2023-05-05T00:35:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [May 5, 2023, 5:01am UTC](https://discourse.julialang.org/t/is-there-a-package-for-multidimensional-ranges/98355/2 "2023-05-05T05:01:47Z")

</div>

Efficient rectangular grids: [RectiGrids.jl](https://gitlab.com/aplavin/RectiGrids.jl), some discussion:

> [@Regular grid](https://discourse.julialang.org/t/regular-grid/82371/2):
>
> Depends on what functionality you need from the grid. The most bare-bones rectangular grid is Iterators.product(1:10, 1:100): it can be iterated or mapped over, but isn’t even an array. More extensive grids can be created with the [RectiGrids.jl](https://gitlab.com/aplavin/RectiGrids.jl) package. They are arrays, and even more, KeyedArrays, providing convenient lookup methods. (disclaimer: I’m the package author)

Depending on what you need in the end, you may also use `map` on top of the grid:

```julia-auto
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

```

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 5, 2023, 5:21am UTC](https://discourse.julialang.org/t/is-there-a-package-for-multidimensional-ranges/98355/3 "2023-05-05T05:21:35Z")

</div>

To some extent, a `CartesianIndices` instance behaves like an nD range

```julia
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.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [May 5, 2023, 6:48am UTC](https://discourse.julialang.org/t/is-there-a-package-for-multidimensional-ranges/98355/4 "2023-05-05T06:48:14Z")

</div>

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?
