forked from GabrieleSantin/VKOGA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
132 lines (89 loc) · 2.91 KB
/
dictionary.py
File metadata and controls
132 lines (89 loc) · 2.91 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 22 08:48:46 2020
@author: gab
"""
from abc import ABC, abstractmethod
import numpy as np
class Dictionary(ABC):
@abstractmethod
def __init__(self):
super().__init__()
@abstractmethod
def sample(self):
pass
class DeterministicDictionary(Dictionary):
def __init__(self, X):
super().__init__()
self.X = X
self.d = self.X.shape[0]
def sample(self):
return self.X
class RandomDictionary(Dictionary):
def __init__(self, n, d):
super().__init__()
self.n = n
self.d = d
class RandomDictionarySquare(RandomDictionary):
def sample(self):
return np.random.rand(self.n, self.d)
class RandomDictionaryDisk(RandomDictionary):
def __init__(self, n):
super().__init__(n, 2)
def sample(self):
r = np.random.rand(self.n, 1)
theta = 2 * np.pi * np.random.rand(self.n)
return np.sqrt(r) * np.c_[np.cos(theta), np.sin(theta)]
class RandomDictionarySphere(RandomDictionary):
def __init__(self, n):
super().__init__(n, 3)
def sample(self):
u = np.random.rand(self.n, 1)
v = np.random.rand(self.n, 1)
theta = 2 * np.pi * u
phi = np.arccos(2 * v - 1)
return np.c_[np.cos(theta) * np.sin(phi), np.sin(theta) * np.sin(phi), np.cos(phi)]
class RandomDictionaryThorus(RandomDictionary):
def __init__(self, n):
super().__init__(n, 3)
def sample(self):
""" https://math.stackexchange.com/questions/2017079/uniform-random-points-on-a-torus """
R = 2
r = 1
points = np.empty((0, self.d))
while points.shape[0] < self.n:
n_left = self.n - points.shape[0]
u = np.random.rand(n_left, 1)
v = np.random.rand(n_left, 1)
w = np.random.rand(n_left, 1)
theta = 2 * np.pi * u
phi = 2 * np.pi * v
idx = np.nonzero(w <= (R + r * np.cos(theta)) / (R + r))
new_points = np.c_[(R + r * np.cos(theta[idx])) * np.cos(phi[idx]), (R + r * np.cos(theta[idx])) * np.sin(phi[idx]), r * np.sin(theta[idx])]
points = np.r_[points, new_points]
return points
def main():
import numpy as np
import matplotlib.pyplot as plt
d = 3
n_random = 1000
dictionary = RandomDictionaryThorus(n_random)
X = dictionary.sample()
if d == 2:
fig = plt.figure(2)
fig.clf()
ax = fig.gca()
ax.plot(X[:, 0], X[:, 1], '.')
ax.grid()
ax.axis('equal')
elif d == 3:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(2)
fig.clf()
ax = fig.gca(projection='3d')
ax.plot(X[:, 0], X[:, 1], X[:, 2], '.')
ax.grid()
ax.axis('equal')
if __name__ == '__main__':
main()