# Constructing a hyper-dimensional ball for a given point

**URL:** <https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650>\
**Category:** New to Julia\
**Tags:** question, package\
**Created:** [March 7, 2023, 8:14am UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650 "2023-03-07T08:14:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 7, 2023, 8:14am UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650/1 "2023-03-07T08:14:08Z")

</div>

I was wondering if a Julia package could help me out.

Suppose I have a point in two-dimension, for instance, [0,1] \times [0,1].

What I want to do is to construct a square centred at (0.1,0.3) with a distance d.

Then I want to collect random points inside the square.

This was doable, but once I started to generalize into n-dimension with multiple points, I got lost to  
avoiding collecting overlapping points with each dimension’s box-constraint.

Would there be a package that I could look for?

Any suggestions would be greatly helpful.

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [March 7, 2023, 8:25am UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650/2 "2023-03-07T08:25:31Z")

</div>

Just to clarify: You want to draw random numbers from a n-dimensional (hyper-)square? Or do you want to make multiple squares and draw random points from their union (without giving more weight to potentially overlapping regions)?

Or something else?

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 7, 2023, 8:37am UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650/3 "2023-03-07T08:37:46Z")

</div>

The latter is something that I want to do.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 7, 2023, 3:52pm UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650/4 "2023-03-07T15:52:05Z")

</div>

Most straightforward way seems like drawing from the hyper-rectangle that contains all of them and then rejecting points that don’t belong to some rectangle in the collection. Probably inefficient in very high dimension but should work for lower dimensions. An additional optimization would be to partition into disjoint subsets of rectangles and choose a partition proportional to its volume. That would cut out a lot of empty rejection sampling space.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 7, 2023, 6:43pm UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650/5 "2023-03-07T18:43:11Z")

</div>

A variation on StefanKarpinski’s suggestion would be to choose a random hyperrectangle proportional to their volumes, draw a point `p` from that rectangle, then reject it with probability `1-1/nrect(p)` where `nrect(p)` is the number of rectangles that contain `p`. This would save the effort of partitioning into disjoint hyperrectangles at the cost of requiring many rejections if `nrect(p)` becomes large.

Each of the proposed schemes is suitable for a different problem regime. I.e, which is best will depend on factors like the number of rectangles, the fraction of space occupied by rectangles, the amount of overlap in rectangles, etc.

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [March 8, 2023, 10:30am UTC](https://discourse.julialang.org/t/constructing-a-hyper-dimensional-ball-for-a-given-point/95650/6 "2023-03-08T10:30:51Z")

</div>

Thank you very much to everyone!  
I will try several programs and compare the running time.
