-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplex.go
262 lines (205 loc) · 5 KB
/
simplex.go
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
package comptop
import (
"fmt"
"sort"
)
type simplex struct {
base Base
sorted bool
}
func (s *simplex) String() string {
s.sort()
return fmt.Sprintf("%+v", s.base)
}
func (s *simplex) sort() {
if s.sorted {
return
}
sort.Sort(s.base)
s.sorted = true
}
func (s *simplex) equals(f *simplex) bool {
sortsimplices(s, f)
if len(s.base) != len(f.base) {
return false
}
for idx := range s.base {
if s.base[idx] != f.base[idx] {
return false
}
}
return true
}
func (s *simplex) dim() Dim {
return Dim(len(s.base)) - 1
}
func (s *simplex) d() []*simplex {
sortsimplices(s)
boundary := []*simplex{}
n := len(s.base)
for j := 0; j < n; j++ {
f := make([]Index, n-1)
copy(f[0:j], s.base[0:j])
copy(f[j:n-1], s.base[j+1:n])
ss := &simplex{
base: f,
sorted: false,
}
boundary = append(boundary, ss)
}
return boundary
}
// Simplex is a p-dimensional polytope which is the convex hull of its p+1 0-dimensional simplices (points/vertices).
// Every Simplex should be part of a Complex; every Simplex in a Complex is considered to live in the same topological space.
// Simplex is uniquely identified in its Complex by its dimension ((*Simplex).Dim) and Index ((*Simplex).Index).
// Simplex can encapsulate user-defined data in its Data field.
//
// More info: https://encyclopediaofmath.org/wiki/Simplex_(abstract)
type Simplex struct {
simplex
complex *Complex
index Index
faces map[Dim]*SimplicialSet
Data interface{}
}
func (s *Simplex) String() string {
if s.Data == nil {
return fmt.Sprintf(
`Simplex{"dim": %d, "index": %d, "base": %v}`,
s.Dim(),
s.index,
s.base,
)
}
return fmt.Sprintf(
`Simplex{"dim": %d, "index": %d, "base": %v, "data": %+v}`,
s.Dim(),
s.index,
s.base,
s.Data,
)
}
// Complex returns the Complex that s belongs to.
func (s *Simplex) Complex() *Complex {
return s.complex
}
// Dim returns the dimension of s, which is defined to be 1 + (# of points/0-simplices in s).
func (s *Simplex) Dim() Dim {
return Dim(len(s.base)) - 1
}
// Index returns the Index of s, which uniquely identifies it in the basis of its corresponding ChainGroup.
func (s *Simplex) Index() Index {
return s.index
}
// Equal returns true if s and f are equal; returns false otherwise.
func (s *Simplex) Equals(f *Simplex) bool {
if s == nil || f == nil {
return false
}
if s.complex != f.complex {
return false
}
return s.simplex.equals(&f.simplex)
}
// Base returns a copy of the base set of s.
func (s *Simplex) Base() Base {
b := make(Base, len(s.base))
copy(b, s.base)
return b
}
// HasFace returns true if s has f as a face.
func (s *Simplex) HasFace(f *Simplex) bool {
if s.Intersection(f).Equals(f) {
return true
}
return false
}
// Intersection returns the intersection of simplices s and g.
func (s *Simplex) Intersection(g *Simplex) *Simplex {
sortSimplices(s, g)
intersection := make([]Index, 0)
n := g.base.Len()
for _, el := range s.base {
idx := sort.Search(n, func(j int) bool {
return g.base[j] >= el
})
if idx < n && g.base[idx] == el {
intersection = append(intersection, el)
}
}
return s.complex.GetSimplex(intersection...)
}
// Boundary computes the boundary of s as a Chain in a ChainGroup of the Complex of s.
func (s *Simplex) Boundary() *Chain {
dim := s.dim()
if dim == 0 {
return nil
}
chain := &Chain{chain: chain{simplices: []*Simplex{}}, dim: dim - 1}
chain.complex = s.complex
chain.chaingroup = s.complex.ChainGroup(chain.dim)
chain.simplices = s.Faces(dim - 1).Slice()
return chain
}
// Faces returns the set of d dimensional faces of s.
func (s *Simplex) Faces(d Dim) *SimplicialSet {
if s.dim() <= d {
return nil
}
if s.faces == nil {
s.faces = map[Dim]*SimplicialSet{}
}
if faces, exists := s.faces[d]; exists {
return faces
}
faces := []*Simplex{}
complex := s.complex
group := complex.chainGroups[d]
for _, smplx := range group.simplices {
if s.HasFace(smplx) {
faces = append(faces, smplx)
}
}
s.faces[d] = NewSimplicialSet(faces...)
return s.faces[d]
}
// Cofaces returns the set of simplices of dimension d that have s as a face.
func (s *Simplex) Cofaces(d Dim) *SimplicialSet {
if d <= s.Dim() || d > s.complex.dim {
return nil
}
cf := map[*Simplex]struct{}{}
simplices := s.complex.chaingroup(d).Simplices()
for _, smplx := range simplices {
if smplx.HasFace(s) {
cf[smplx] = struct{}{}
}
}
return &SimplicialSet{set: cf}
}
// AllCofaces returns the set of simplices of any dimension that have s as a face.
func (s *Simplex) AllCofaces() *SimplicialSet {
if s.Dim() == s.complex.dim {
return nil
}
cf := map[*Simplex]struct{}{}
for d := s.Dim() + 1; d <= s.complex.dim; d++ {
simplices := s.complex.chaingroup(d).Simplices()
for _, smplx := range simplices {
if smplx.HasFace(s) {
cf[smplx] = struct{}{}
}
}
}
return &SimplicialSet{set: cf}
}
func sortsimplices(simplices ...*simplex) {
for _, s := range simplices {
s.sort()
}
}
func sortSimplices(simplices ...*Simplex) {
for _, s := range simplices {
s.sort()
}
}