-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flat_Torus_Klein.py
285 lines (204 loc) · 8.52 KB
/
Flat_Torus_Klein.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
This module contains models for the flat Torus and Klein bottle using a quotient metric on the
square fundamental domain of each space, as well as examples of their
persistence computations.
"""
import time
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from ripser import ripser
from persim import plot_diagrams
import scipy as sp
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
import math
from scipy.spatial.distance import pdist
def minmax_subsample_distance_matrix(X, num_landmarks, seed=[]):
"""
This function computes minmax subsampling using a square distance matrix.
:type X: numpy array
:param X: Square distance matrix
:type num_landmarks: int
:param num_landmarks: Number of landmarks
:type seed: list
:param list: Default []. List of indices to seed the sampling algorith.
"""
num_points = len(X)
if not(seed):
ind_L = [np.random.randint(0,num_points)]
else:
ind_L = seed
num_landmarks += 1
distance_to_L = np.min(X[ind_L, :], axis=0)
for i in range(num_landmarks-1):
ind_max = np.argmax(distance_to_L)
ind_L.append(ind_max)
dist_temp = X[ind_max, :]
distance_to_L = np.minimum(distance_to_L, dist_temp)
return {'indices':ind_L, 'distance_to_L':distance_to_L}
######################################################
# Torus Flat Planar Model
######################################################
# This one is simple and runs particularly fast.
def cls_Torus(x,y):
'''
Outputs the equivalence class of the points represented by (x,y) in the fundamental domain of T
'''
arr = np.array([[x,y],[x,y+1],[x,y-1],[x+1,y],[x+1,y+1],[x+1,y-1],[x-1,y],[x-1,y+1],[x-1,y-1]])
return arr
def minDist_Torus(X,Y):
'''
Returns the geodesic distance on the fundamental domain between X and Y.
'''
md = np.min(sp.spatial.distance_matrix(cls_Torus(X[0],X[1]),cls_Torus(Y[0],Y[1])))
return md
def minDist_TorusLinf(X,Y):
'''
Returns the geodesic distance on the fundamental domain between X and Y.
'''
md = np.min(sp.spatial.distance_matrix(cls_Torus(X[0],X[1]),cls_Torus(Y[0],Y[1]),p=np.inf))
return md
# Build sampling of the flat Torus
numx = 20
numy = 20
n_landmarks = 100
xvals = np.linspace(0,1,numx)
yvals = np.linspace(0,1,numy)
x,y = np.meshgrid(xvals,yvals)
xx = x.ravel()
yy = y.ravel()
L = np.column_stack((xx,yy))
plt.scatter(L[:,0],L[:,1])
plt.show()
t0 = time.time()
dm_RR = pdist(L, minDist_Torus)
dm_RR = sp.spatial.distance.squareform(dm_RR)
print(dm_RR.shape)
t1 = time.time()-t0
print("Flat distance: %s sec" % t1)
#Compute persistence on landmarks
t0 = time.time()
sub_ind = minmax_subsample_distance_matrix(dm_RR, n_landmarks)['indices']
dm_X = dm_RR[sub_ind, :]
dm_L = dm_X[:,sub_ind]
result2 = ripser(dm_L, coeff=2, do_cocycles=True, maxdim=2, distance_matrix=True)
dgmsz2 = result2['dgms']
#print(dgmsz2) #####Sort this in reverse
result3 = ripser(dm_L, coeff=3, do_cocycles=True, maxdim=2, distance_matrix=True)
dgmsz3 = result3['dgms']
plt.figure(figsize=(8, 4))
plt.subplot(121)
plot_diagrams(dgmsz2)
plt.title("$\mathbb{Z}/2$")
plt.subplot(122)
plot_diagrams(dgmsz3)
plt.title("$\mathbb{Z}/3$")
plt.show()
t1 = time.time()-t0
print("Persistence: %s seconds" % t1)
cocycles = result2['cocycles']
H_1 = cocycles[1]
H_1_diagram = dgmsz2[1] # dgm(PH^1(R(L); Z_q))
H_1_persistence = H_1_diagram[:,1] - H_1_diagram[:,0]
H_1_persistence_sort_ind = H_1_persistence.argsort() # index of the largest bar in PH^1(R(L); Z_q)
a = H_1_diagram[H_1_persistence_sort_ind[-1], 0] # Birth of the largest class in PH^1(R(L); Z_q)
b = H_1_diagram[H_1_persistence_sort_ind[-1], 1] # Death of the largest class in PH^1(R(L); Z_q)
a2 = H_1_diagram[H_1_persistence_sort_ind[-2], 0] # Birth of the 2nd largest class in PH^1(R(L); Z_q)
b2 = H_1_diagram[H_1_persistence_sort_ind[-2], 1] # Death of the 2nd largest class in PH^1(R(L); Z_q)
my_eta = H_1[H_1_persistence_sort_ind[-1]] # Cochain representtive of the largest class in PH^1(R(L); Z_q)
my_eta2 = H_1[H_1_persistence_sort_ind[-2]]
cocycles3 = result3['cocycles']
H_13 = cocycles3[1]
H_1_diagram3 = dgmsz3[1] # dgm(PH^1(R(L); Z_q))
H_1_persistence3 = H_1_diagram3[:,1] - H_1_diagram3[:,0]
H_1_persistence_sort_ind3 = H_1_persistence3.argsort() # index of the largest bar in PH^1(R(L); Z_q)
a3 = H_1_diagram3[H_1_persistence_sort_ind3[-1], 0] # Birth of the largest class in PH^1(R(L); Z_q)
b3 = H_1_diagram3[H_1_persistence_sort_ind3[-1], 1] # Death of the largest class in PH^1(R(L); Z_q)
a32 = H_1_diagram3[H_1_persistence_sort_ind3[-2], 0] # Birth of the 2nd largest class in PH^1(R(L); Z_q)
b32 = H_1_diagram3[H_1_persistence_sort_ind3[-2], 1] # Death of the 2nd largest class in PH^1(R(L); Z_q)
epsilon = a * 1.01 # Epsilon is the radius e usefor the balls with centers in the landmarks.
print("Birth = %s, Death = %s" % (a, b))
print("Birth2 = %s, Death2 = %s" % (a2, b2))
print("Birth3 = %s, Death3 = %s" % (a3, b3))
print("Birth32 = %s, Death32 = %s" % (a32, b32))
######################################################
# Klein Bottle Flat Planar Model
######################################################
# This one is simple and runs particularly fast.
def cls_Klein(x,y):
'''
Outputs the equivalence class of the points represented by (x,y) in the fundamental domain of K
'''
arr = np.array([[x,y],[x-1,2-y],[x,y+1],[x+1,2-y],[x-1,1-y],[x+1,1-y],[x-1,-y],[x,y-1],[x+1,-y]])
return arr
def minDist_Klein(X,Y):
'''
Returns the geodesic distance on the fundamental domain between X and Y.
'''
md = np.min(sp.spatial.distance_matrix(cls_Klein(X[0],X[1]),cls_Klein(Y[0],Y[1])))
return md
# Build sampling of the flat Klein bottle
numx = 20
numy = 20
n_landmarks = 100
xvals = np.linspace(0,1,numx)
yvals = np.linspace(0,1,numy)
x,y = np.meshgrid(xvals,yvals)
xx = x.ravel()
yy = y.ravel()
L = np.column_stack((xx,yy))
plt.scatter(L[:,0],L[:,1])
plt.show()
t0 = time.time()
dm_RR = pdist(L, minDist_Klein)
dm_RR = sp.spatial.distance.squareform(dm_RR)
print(dm_RR.shape)
t1 = time.time()-t0
print("Flat distance: %s sec" % t1)
#Compute persistence on landmarks
t0 = time.time()
sub_ind = minmax_subsample_distance_matrix(dm_RR, n_landmarks)['indices']
dm_X = dm_RR[sub_ind, :]
dm_L = dm_X[:,sub_ind]
result2 = ripser(dm_L, coeff=2, do_cocycles=True, maxdim=2, distance_matrix=True)
dgmsz2 = result2['dgms']
#print(dgmsz2) #####Sort this in reverse
result3 = ripser(dm_L, coeff=3, do_cocycles=True, maxdim=2, distance_matrix=True)
dgmsz3 = result3['dgms']
plt.figure(figsize=(8, 4))
plt.subplot(121)
plot_diagrams(dgmsz2)
plt.title("$\mathbb{Z}/2$")
plt.subplot(122)
plot_diagrams(dgmsz3)
plt.title("$\mathbb{Z}/3$")
plt.show()
t1 = time.time()-t0
print("Persistence: %s seconds" % t1)
cocycles = result2['cocycles']
H_1 = cocycles[1]
H_1_diagram = dgmsz2[1] # dgm(PH^1(R(L); Z_q))
H_1_persistence = H_1_diagram[:,1] - H_1_diagram[:,0]
H_1_persistence_sort_ind = H_1_persistence.argsort() # index of the largest bar in PH^1(R(L); Z_q)
a = H_1_diagram[H_1_persistence_sort_ind[-1], 0] # Birth of the largest class in PH^1(R(L); Z_q)
b = H_1_diagram[H_1_persistence_sort_ind[-1], 1] # Death of the largest class in PH^1(R(L); Z_q)
a2 = H_1_diagram[H_1_persistence_sort_ind[-2], 0] # Birth of the 2nd largest class in PH^1(R(L); Z_q)
b2 = H_1_diagram[H_1_persistence_sort_ind[-2], 1] # Death of the 2nd largest class in PH^1(R(L); Z_q)
my_eta = H_1[H_1_persistence_sort_ind[-1]] # Cochain representtive of the largest class in PH^1(R(L); Z_q)
my_eta2 = H_1[H_1_persistence_sort_ind[-2]]
cocycles3 = result3['cocycles']
H_13 = cocycles3[1]
H_1_diagram3 = dgmsz3[1] # dgm(PH^1(R(L); Z_q))
H_1_persistence3 = H_1_diagram3[:,1] - H_1_diagram3[:,0]
H_1_persistence_sort_ind3 = H_1_persistence3.argsort() # index of the largest bar in PH^1(R(L); Z_q)
a3 = H_1_diagram3[H_1_persistence_sort_ind3[-1], 0] # Birth of the largest class in PH^1(R(L); Z_q)
b3 = H_1_diagram3[H_1_persistence_sort_ind3[-1], 1] # Death of the largest class in PH^1(R(L); Z_q)
a32 = H_1_diagram3[H_1_persistence_sort_ind3[-2], 0] # Birth of the 2nd largest class in PH^1(R(L); Z_q)
b32 = H_1_diagram3[H_1_persistence_sort_ind3[-2], 1] # Death of the 2nd largest class in PH^1(R(L); Z_q)
epsilon = a * 1.01 # Epsilon is the radius e usefor the balls with centers in the landmarks.
print("Birth = %s, Death = %s" % (a, b))
print("Birth2 = %s, Death2 = %s" % (a2, b2))
print("Birth3 = %s, Death3 = %s" % (a3, b3))
print("Birth32 = %s, Death32 = %s" % (a32, b32))