# Finding the vertices of a hypercube

**URL:** <https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078>\
**Category:** New to Julia\
**Tags:** linearalgebra\
**Created:** [March 13, 2021, 3:10pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078 "2021-03-13T15:10:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suresh\_Jeevanandam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suresh_jeevanandam/32/22266_2.png) [@Suresh\_Jeevanandam](https://discourse.julialang.org/u/Suresh_Jeevanandam)\
**Post date:** [March 13, 2021, 3:10pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/1 "2021-03-13T15:10:55Z")

</div>

I defined a data structure to represent a hypercube. It is defined by the min and max bounds on each dimension.

```julia
struct HyperCube 
    min_bounds::Array{Float32}
    max_bounds::Array{Float32}
end

```

Now, I want to define a function that returns all the vertices.  
In Python, I would have done:  
` return list(product(*zip(self.min_bounds, self.max_bounds)))`

I am wondering what would be the most elegant way to do this in Julia.

---

<div class="post-metadata">

**Author:** ![Suresh\_Jeevanandam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suresh_jeevanandam/32/22266_2.png) [@Suresh\_Jeevanandam](https://discourse.julialang.org/u/Suresh_Jeevanandam)\
**Post date:** [March 13, 2021, 3:51pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/3 "2021-03-13T15:51:59Z")

</div>

Thanks for the response. It is missing the zip part. The Python implementation would return the below list for the example:

```julia
[(1, 2, 3),
 (1, 2, 6),
 (1, 5, 3),
 (1, 5, 6),
 (4, 2, 3),
 (4, 2, 6),
 (4, 5, 3),
 (4, 5, 6)]

```

Please note that, in `HyperCube([1, 2, 3], [4, 5, 6])` , there are three dimensions. The cube covers` [1,4]` in the first dimension, `[2,5]` in the second dimension, and so on.

---

<div class="post-metadata">

**Author:** ![Suresh\_Jeevanandam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suresh_jeevanandam/32/22266_2.png) [@Suresh\_Jeevanandam](https://discourse.julialang.org/u/Suresh_Jeevanandam)\
**Post date:** [March 13, 2021, 4:28pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/4 "2021-03-13T16:28:17Z")

</div>

I was stuck for sometime on how to do the Python’s `f(*args)` in Julia and then figured out:

`collect(Iterators.product(zip(box.min_bounds, box.max_bounds)...))`

I hope this works.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 13, 2021, 4:31pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/5 "2021-03-13T16:31:15Z")

</div>

Note that you want `Vector` not `Array` here. Otherwise it’s an abstract type since you don’t appreciate the number of dimensions.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 13, 2021, 4:32pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/6 "2021-03-13T16:32:38Z")

</div>

`Iterators.product` returns multi-dimensional object. You can add `vec` to flatten it

```julia
julia> vec(collect(Iterators.product(zip(box.min_bounds, box.max_bounds)...)))
8-element Vector{Tuple{Float32, Float32, Float32}}:
 (1.0, 2.0, 3.0)
 (4.0, 2.0, 3.0)
 (1.0, 5.0, 3.0)
 (4.0, 5.0, 3.0)
 (1.0, 2.0, 6.0)
 (4.0, 2.0, 6.0)
 (1.0, 5.0, 6.0)
 (4.0, 5.0, 6.0)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 13, 2021, 5:18pm UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/7 "2021-03-13T17:18:01Z")

</div>

> [@Oscar\_Smith](#):
>
> Note that you want `Vector` not `Array` here. Otherwise it’s an abstract type since you don’t appreciate the number of dimensions.

If you are worried about performance, I would also strongly consider using [`SVector` from StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) here, at least if you are in low (\< 20) dimensions, and anywhere you are using low-dimensional vectors in a fixed number of dimensions. (e.g. [HCubature.jl does something similar for hypercubes](https://github.com/JuliaMath/HCubature.jl/blob/c256214de91a2116268b8158d03df89e410754ec/src/HCubature.jl#L28-L34).)

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [March 14, 2021, 4:20am UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/8 "2021-03-14T04:20:01Z")

</div>

Apologies.

---

<div class="post-metadata">

**Author:** ![Suresh\_Jeevanandam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suresh_jeevanandam/32/22266_2.png) [@Suresh\_Jeevanandam](https://discourse.julialang.org/u/Suresh_Jeevanandam)\
**Post date:** [March 14, 2021, 6:19am UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/9 "2021-03-14T06:19:57Z")

</div>

Did you mean to have the _struct_ definition as below?

```julia
struct HyperCube
    min_bounds::Vector{Float32}
    max_bounds::Vector{Float32}
end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 14, 2021, 6:20am UTC](https://discourse.julialang.org/t/finding-the-vertices-of-a-hypercube/57078/10 "2021-03-14T06:20:48Z")

</div>

Yes. Exactly. `Vector{Float32}` is an alias for `Array{Float32,1}`
