-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnp_circles.py
45 lines (29 loc) · 914 Bytes
/
np_circles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# drawing circles and high-dimensional hyperspheres with numpy
import numpy as np
import sys
# never truncate high-d arrays
np.set_printoptions(threshold=sys.maxsize)
dimensionality = 3
dim = np.zeros(dimensionality, dtype=np.int64) + 13 # length-n array of 13s
space = np.zeros(dim)
# we start at the center of the circle, right?
center = 6 # in any dimension
radius = 4
indices = np.indices(space.shape)
# distance from center
dist = np.absolute(indices - center)
squares = dist ** 2
print(squares)
dist_euclid = np.sqrt(np.sum(squares, axis=0))
dist_taxicab = np.sum(dist, axis=0)
dist_chebyshev = np.maximum.reduce(dist)
print("euclidean:")
print(dist_euclid.astype(np.int64))
print("taxicab:")
print(dist_taxicab.astype(np.int64))
print("chebyshev:")
print(dist_chebyshev.astype(np.int64))
circle = np.where(dist_euclid < radius)
space[circle] = 1
print(f"circle r = {radius}:")
print(space)