/
finite_topological_spaces.py
1958 lines (1648 loc) · 73.8 KB
/
finite_topological_spaces.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Finite topological spaces
This module implements finite topological spaces and related concepts.
A *finite topological space* is a topological space with finitely many points and
a *finite preordered set* is a finite set with a transitive and reflexive relation.
Finite spaces and finite preordered sets are basically the same objects considered
from different perspectives. Given a finite topological space `X`, for every point
`x\in X`, define the *minimal open set* `U_x` as the intersection of all the open
sets which contain `x` (it is an open set since arbitrary intersections of open
sets in finite spaces are open). The minimal open sets constitute a basis for the
topology of `X`. Indeed, any open set `U` of `X` is the union of the sets `U_x`
with `x\in U`. This basis is called the *minimal basis of* `X`. A preorder on `X`
is given by `x\leqslant y` if `x\in U_y`.
If `X` is now a finite preordered set, one can define a topology on `X` given by
the basis `\lbrace y\in X\vert y\leqslant x\rbrace_{x\in X}`. Note that if `y\leqslant x`,
then `y` is contained in every basic set containing `x`, and therefore `y\in U_x`.
Conversely, if `y\in U_x`, then `y\in\lbrace z\in X\vert z\leqslant x\rbrace`.
Therefore `y\leqslant x` if and only if `y\in U_x`. This shows that these two
applications, relating topologies and preorders on a finite set, are mutually
inverse. This simple remark, made in first place by Alexandroff [Ale1937]_, allows
us to study finite spaces by combining Algebraic Topology with the combinatorics
arising from their intrinsic preorder structures. The antisymmetry of a finite
preorder corresponds exactly to the `T_0` separation axiom. Recall that a topological
space `X` is said to be `T_0` if for any pair of points in `X` there exists an
open set containing one and only one of them. Therefore finite `T_0`-spaces are
in correspondence with finite partially ordered sets (posets) [Bar2011]_.
Now, if `X = \lbrace x_1, x_2, \ldots , x_n\rbrace` is a finite space and for
each `i` the unique minimal open set containing `x_i` is denoted by `U_i`, a
*topogenous matrix* of the space is the `n \times n` matrix `A = \left[a_{ij}\right]`
defined by `a_{ij} = 1` if `x_i \in U_j` and `a_{ij} = 0` otherwise (this is the
transposed matrix of the Definition 1 in [Shi1968]_). A finite space `X` is `T_0`
if and only if the topogenous matrix `A` defined above is similar (via a permutation
matrix) to a certain upper triangular matrix [Shi1968]_. This is the reason one
can assume that the topogenous matrix of a finite `T_0`-space is upper triangular.
AUTHOR::
- Julian Cuevas-Rozo (2020): Initial version
REFERENCES:
- [Ale1937]_
- [Bar2011]_
- [Shi1968]_
"""
# ****************************************************************************
# Copyright (C) 2020 Julian Cuevas-Rozo <jlcrozo@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# https://www.gnu.org/licenses/
# ****************************************************************************
from sage.structure.parent import Parent
from sage.matrix.constructor import matrix
from sage.matrix.matrix_integer_sparse import Matrix_integer_sparse
from sage.combinat.posets.posets import Poset
from sage.rings.integer_ring import ZZ
from sage.homology.homology_group import HomologyGroup
from sage.libs.ecl import EclObject, ecl_eval, EclListIterator
from sage.interfaces import kenzo
from sage.features.kenzo import Kenzo
###############################################################
# This section (lines 76 to 246) will be included to src/sage/interfaces/kenzo.py
kenzonames = ['2h-regularization',
'copier-matrice',
'creer-matrice',
'convertarray',
'dvfield-aux',
'edges-to-matrice',
'h-regular-dif',
'h-regular-dif-dvf-aux',
'matrice-to-lmtrx',
'mtrx-prdc',
'newsmith-equal-matrix',
'newsmith-mtrx-prdc',
'random-top-2space',
'randomtop',
'vector-to-list']
if Kenzo().is_present():
ecl_eval("(require :kenzo)")
ecl_eval("(in-package :cat)")
ecl_eval("(setf *HOMOLOGY-VERBOSE* nil)")
for s in kenzonames:
name = '__{}__'.format(s.replace('-', '_'))
exec('{} = EclObject("{}")'.format(name, s))
def quotient_group_matrices(*matrices, left_null=False, right_null=False, check=True):
r"""
Return a presentation of the homology group `\ker M1/ \im M2`.
INPUT:
- ``matrices`` -- A tuple of ECL matrices. The length `L` of this parameter
can take the value 0, 1 or 2.
- ``left_null`` -- (default ``False``) A boolean.
- ``right_null`` -- (default ``False``) A boolean.
- ``check`` -- (default ``True``) A boolean. If it is ``True`` and `L=2`, it
checks that the product of the ``matrices`` is the zero matrix.
OUTPUT:
- If `L=0`, it returns the trivial group.
- If `L=1` (``matrices`` = M), then one of the parameters ``left_null`` or
``right_null`` must be ``True``: in case ``left_null`` == ``True``, it
returns the homology group `\ker 0/ \im M` and in case ``right_null`` == ``True``,
it returns the homology group `\ker M/ \im 0`.
- If `L=2` (``matrices`` = (M1, M2)), it returns the homology group `\ker M1/ \im M2`.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import quotient_group_matrices, __convertarray__
sage: from sage.interfaces.kenzo import s2k_matrix
sage: quotient_group_matrices()
0
sage: s_M1 = matrix(2, 3, [1, 2, 3, 4, 5, 6])
sage: M1 = __convertarray__(s2k_matrix(s_M1))
sage: quotient_group_matrices(M1, left_null=True)
C3
sage: quotient_group_matrices(M1, right_null=True)
Z
sage: s_M2 = matrix(2, 2, [1, -1, 1, -1])
sage: M2 = __convertarray__(s2k_matrix(s_M2))
sage: s_M3 = matrix(2, 2, [1, 0, 1, 0])
sage: M3 = __convertarray__(s2k_matrix(s_M3))
sage: quotient_group_matrices(M2, M3)
0
sage: s_M4 = matrix(2, 2, [0, 0, 1, 0])
sage: M4 = __convertarray__(s2k_matrix(s_M4))
sage: quotient_group_matrices(M2, M4)
Traceback (most recent call last):
...
AssertionError: m1*m2 must be zero
"""
assert not (left_null and right_null), "left_null and right_null must not be both True"
if len(matrices)==0:
return HomologyGroup(0, ZZ)
elif len(matrices)==1:
if left_null==True:
m2 = matrices[0]
m1 = __creer_matrice__(0, kenzo.__nlig__(m2))
elif right_null==True:
m1 = matrices[0]
m2 = __creer_matrice__(kenzo.__ncol__(m1), 0)
else:
raise AssertionError("left_null or right_null must be True")
elif len(matrices)==2:
m1, m2 = matrices
if check==True:
rowsm1 = kenzo.__nlig__(m1)
colsm1 = kenzo.__ncol__(m1)
rowsm2 = kenzo.__nlig__(m2)
colsm2 = kenzo.__ncol__(m2)
assert colsm1==rowsm2, "Number of columns of m1 must be equal to the number of rows of m2"
assert __newsmith_equal_matrix__(__newsmith_mtrx_prdc__(m1, m2), \
__creer_matrice__(rowsm1, colsm2)).python(), \
"m1*m2 must be zero"
homology = kenzo.__homologie__(__copier_matrice__(m1), __copier_matrice__(m2))
lhomomology = [i for i in EclListIterator(homology)]
res = []
for component in lhomomology:
pair = [i for i in EclListIterator(component)]
res.append(pair[0].python())
return HomologyGroup(len(res), ZZ, res)
def k2s_binary_matrix_sparse(kmatrix):
r"""
Converts a Kenzo binary sparse matrice (type `matrice`) to a matrix in SageMath.
INPUT:
- ``kmatrix`` -- A Kenzo binary sparse matrice (type `matrice`).
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import k2s_binary_matrix_sparse, \
s2k_binary_matrix_sparse, __randomtop__
sage: KM2 = __randomtop__(6,1)
sage: k2s_binary_matrix_sparse(KM2)
[1 1 1 1 1 1]
[0 1 1 1 1 1]
[0 0 1 1 1 1]
[0 0 0 1 1 1]
[0 0 0 0 1 1]
[0 0 0 0 0 1]
sage: KM = __randomtop__(100, float(0.8))
sage: SM = k2s_binary_matrix_sparse(KM)
sage: SM == k2s_binary_matrix_sparse(s2k_binary_matrix_sparse(SM))
True
"""
data = __vector_to_list__(__matrice_to_lmtrx__(kmatrix)).python()
dim = len(data)
mat_dict = {}
for j in range(dim):
colj = data[j]
for entry in colj:
mat_dict[(entry[0], j)] = 1
return matrix(dim, mat_dict)
def s2k_binary_matrix_sparse(smatrix):
r"""
Converts a binary matrix in SageMath to a Kenzo binary sparse matrice (type `matrice`).
INPUT:
- ``smatrix`` -- A binary matrix.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import k2s_binary_matrix_sparse, \
s2k_binary_matrix_sparse
sage: SM2 = matrix.ones(5)
sage: s2k_binary_matrix_sparse(SM2)
<ECL:
========== MATRIX 5 lines + 5 columns =====
L1=[C1=1][C2=1][C3=1][C4=1][C5=1]
L2=[C1=1][C2=1][C3=1][C4=1][C5=1]
L3=[C1=1][C2=1][C3=1][C4=1][C5=1]
L4=[C1=1][C2=1][C3=1][C4=1][C5=1]
L5=[C1=1][C2=1][C3=1][C4=1][C5=1]
========== END-MATRIX>
"""
dim = smatrix.nrows()
entries = []
for entry in smatrix.dict().keys():
entries.append([entry[0]+1, entry[1]+1])
kentries = EclObject(entries)
return __edges_to_matrice__(kentries, dim)
###############################################################
def FiniteSpace(data, elements=None, is_T0=False):
r"""
Construct a finite topological space from various forms of input data.
INPUT:
- ``data`` -- different input are accepted by this constructor:
1. A dictionary representing the minimal basis of the space.
2. A list or tuple of minimal open sets (in this case the elements of the
space are assumed to be ``range(n)`` where ``n`` is the length of ``data``).
3. A topogenous matrix (assumed sparse). If ``elements=None``, the elements
of the space are assumed to be ``range(n)`` where ``n`` is the dimension
of the matrix.
4. A finite poset (by now if ``poset._is_facade = False``, the methods are
not completely tested).
- ``elements`` -- (default ``None``) it is ignored when data is of type 1, 2
or 4. When ``data`` is a topogenous matrix, this parameter gives the
underlying set of the space.
- ``is_T0`` -- (default ``False``) it is a boolean that indicates, when it is
previously known, if the finite space is `T_0.
EXAMPLES:
A dictionary as ``data``::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace({'a': {'a', 'c'}, 'b': {'b'}, 'c':{'a', 'c'}}) ; T
Finite topological space of 3 points with minimal basis
{'a': {'a', 'c'}, 'b': {'b'}, 'c': {'a', 'c'}}
sage: type(T)
<class 'sage.homology.finite_topological_spaces.FiniteTopologicalSpace'>
sage: FiniteSpace({'a': {'a', 'b'}})
Traceback (most recent call last):
...
ValueError: The data does not correspond to a valid dictionary
sage: FiniteSpace({'a': {'a', 'b'}, 'b': {'a', 'b'}, 'c': {'a', 'c'}})
Traceback (most recent call last):
...
ValueError: The introduced data does not define a topology
When ``data`` is a tuple or a list, the elements are in ``range(n)`` where
``n`` is the length of ``data``::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace([{0, 3}, {1, 3}, {2, 3}, {3}]) ; T
Finite T0 topological space of 4 points with minimal basis
{0: {3, 0}, 1: {3, 1}, 2: {3, 2}, 3: {3}}
sage: type(T)
<class 'sage.homology.finite_topological_spaces.FiniteTopologicalSpace_T0'>
sage: T.elements()
[3, 0, 1, 2]
sage: FiniteSpace(({0, 2}, {0, 2}))
Traceback (most recent call last):
...
ValueError: This kind of data assume the elements are in range(2)
If ``data`` is a topogenous matrix, the parameter ``elements``, when it is not
``None``, determines the list of elements of the space::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: mat_dict = {(0, 0): 1, (0, 3): 1, (0, 4): 1, (1, 1): 1, (1, 2): 1, (2, 1): 1, \
....: (2, 2): 1, (3, 3): 1, (3, 4): 1, (4, 3): 1, (4, 4): 1}
sage: mat = matrix(mat_dict) ; mat
[1 0 0 1 1]
[0 1 1 0 0]
[0 1 1 0 0]
[0 0 0 1 1]
[0 0 0 1 1]
sage: T = FiniteSpace(mat) ; T
Finite topological space of 5 points with minimal basis
{0: {0}, 1: {1, 2}, 2: {1, 2}, 3: {0, 3, 4}, 4: {0, 3, 4}}
sage: T.elements()
[0, 1, 2, 3, 4]
sage: M = FiniteSpace(mat, elements=(5, 'e', 'h', 0, 'c')) ; M
Finite topological space of 5 points with minimal basis
{5: {5}, 'e': {'e', 'h'}, 'h': {'e', 'h'}, 0: {5, 0, 'c'}, 'c': {5, 0, 'c'}}
sage: M.elements()
[5, 'e', 'h', 0, 'c']
sage: FiniteSpace(mat, elements=[5, 'e', 'h', 0, 0])
Traceback (most recent call last):
...
AssertionError: Not valid list of elements
Finally, when ``data`` is a finite poset, the corresponding finite T0 space
is constructed::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: P = Poset([[1, 2], [4], [3], [4], []])
sage: T = FiniteSpace(P) ; T
Finite T0 topological space of 5 points with minimal basis
{0: {0}, 1: {0, 1}, 2: {0, 2}, 3: {0, 2, 3}, 4: {0, 1, 2, 3, 4}}
sage: type(T)
<class 'sage.homology.finite_topological_spaces.FiniteTopologicalSpace_T0'>
sage: T.poset() == P
True
"""
if hasattr(data, '_hasse_diagram'): # isinstance(data, FinitePosets): # type 4
minimal_basis = {x: set(data.order_ideal([x])) for x in data.list()}
topogenous = data.lequal_matrix()
return FiniteTopologicalSpace_T0(elements=data.list(), minimal_basis=minimal_basis,
topogenous=topogenous, poset=data)
topogenous = None
if isinstance(data, dict): # type 1
n = len(data)
eltos = set()
for B in data.values():
eltos = eltos.union(B)
if not eltos==set(data):
raise ValueError("The data does not correspond to a valid dictionary")
basis = data
if isinstance(data, (list, tuple)): # type 2
n = len(data)
eltos = set()
# In this case, the elements are assumed being range(n)
for B in data:
eltos = eltos.union(B)
if not eltos==set(range(n)):
raise ValueError("This kind of data assume the elements are in range({})".format(n))
basis = dict(zip(range(n), data))
if isinstance(data, Matrix_integer_sparse): # type 3
n = data.dimensions()[0]
assert n==data.dimensions()[1], \
"Topogenous matrices are square"
assert set(data.dict().values())=={1}, \
"Topogenous matrices must have entries in {0,1}"
basis = {}
# Extracting a minimal basis from the topogenous matrix info
if elements:
if not isinstance(elements, (list, tuple)):
raise ValueError("Parameter 'elements' must be a list or a tuple")
assert len(set(elements))==n, \
"Not valid list of elements"
for j in range(n):
Uj = set([elements[i] for i in data.nonzero_positions_in_column(j)])
basis[elements[j]] = Uj
eltos = elements
else:
for j in range(n):
Uj = set(data.nonzero_positions_in_column(j))
basis[j] = Uj
eltos = range(n)
# This fixes a topological sort (it guarantees an upper triangular topogenous matrix)
eltos = list(eltos)
sorted_str_eltos = sorted([str(x) for x in eltos])
eltos.sort(key = lambda x: (len(basis[x]), sorted_str_eltos.index(str(x))))
# Now, check that 'basis' effectively defines a minimal basis for a topology
nonzero = {(eltos.index(x), j):1 for j in range(n) \
for x in basis[eltos[j]]}
topogenous = matrix(n, nonzero)
squared = topogenous*topogenous
if not topogenous.nonzero_positions() == squared.nonzero_positions():
raise ValueError("The introduced data does not define a topology")
if is_T0:
return FiniteTopologicalSpace_T0(elements=eltos, minimal_basis=basis,
topogenous=topogenous)
# Determine if the finite space is T0
partition = []
eltos2 = eltos.copy()
while eltos2:
x = eltos2.pop(0)
Ux = basis[x] - set([x])
equiv_class = set([x])
for y in Ux:
if x in basis[y]:
equiv_class = equiv_class.union(set([y]))
eltos2.remove(y)
partition.append(equiv_class)
if len(partition)==n:
return FiniteTopologicalSpace_T0(elements=eltos, minimal_basis=basis,
topogenous=topogenous)
result = FiniteTopologicalSpace(elements=eltos, minimal_basis=basis,
topogenous=topogenous)
setattr(result, '_T0', partition)
return result
def RandomFiniteT0Space(*args):
r"""
Return a random finite `T_0` space.
INPUT:
- ``args`` -- A tuple of two arguments. The first argument must be an integer
number, while the second argument must be either a number between 0 and 1, or
``True``.
OUTPUT:
- If ``args[1]``=``True``, a random finite `T_0` space of cardinality ``args[0]``
of height 3 without beat points is returned.
- If ``args[1]`` is a number, a random finite `T_0` space of cardinality ``args[0]``
and density ``args[1]`` of ones in its topogenous matrix is returned.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import RandomFiniteT0Space
sage: RandomFiniteT0Space(5, 0)
Finite T0 topological space of 5 points with minimal basis
{0: {0}, 1: {1}, 2: {2}, 3: {3}, 4: {4}}
sage: RandomFiniteT0Space(5, 2)
Finite T0 topological space of 5 points with minimal basis
{0: {0}, 1: {0, 1}, 2: {0, 1, 2}, 3: {0, 1, 2, 3}, 4: {0, 1, 2, 3, 4}}
sage: RandomFiniteT0Space(6, True)
Finite T0 topological space of 6 points with minimal basis
{0: {0}, 1: {1}, 2: {0, 1, 2}, 3: {0, 1, 3}, 4: {0, 1, 2, 3, 4}, 5: {0, 1, 2, 3, 5}}
sage: RandomFiniteT0Space(150, 0.2)
Finite T0 topological space of 150 points
sage: RandomFiniteT0Space(5, True)
Traceback (most recent call last):
...
AssertionError: The first argument must be an integer number greater than 5
"""
assert len(args)==2, "Two arguments must be given"
assert args[0].is_integer(), "The first argument must be an integer number"
if args[1]==True:
assert args[0]>5, "The first argument must be an integer number greater than 5"
kenzo_top = __random_top_2space__(args[0])
else:
kenzo_top = __randomtop__(args[0], EclObject(float(args[1])))
topogenous = k2s_binary_matrix_sparse(kenzo_top)
basis = {j:set(topogenous.nonzero_positions_in_column(j)) for j in range(args[0])}
return FiniteTopologicalSpace_T0(elements=list(range(args[0])), minimal_basis=basis,
topogenous=topogenous)
class FiniteTopologicalSpace(Parent):
r"""
Finite topological spaces.
Users should not call this directly, but instead use :func:`FiniteSpace`.
See that function for more documentation.
"""
def __init__(self, elements, minimal_basis, topogenous):
r"""
Define a finite topological space.
INPUT:
- ``elements`` -- list of the elements of the space.
- ``minimal_basis`` -- a dictionary where the values are sets representing
the minimal open sets containing the respective key.
- ``topogenous`` -- a topogenous matrix of the finite space corresponding
to the order given by ``elements``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteTopologicalSpace
sage: elements = [1, 2, 'a', 3]
sage: minimal_basis = {'a': {3, 'a'}, 3: {3, 'a'}, 2: {2, 1}, 1: {1}}
sage: mat_dict = {(0, 0): 1, (0, 1): 1, (1, 1): 1, (2, 2): 1, \
....: (2, 3): 1, (3, 2): 1, (3, 3): 1}
sage: T = FiniteTopologicalSpace(elements, minimal_basis, matrix(mat_dict)) ; T
Finite topological space of 4 points with minimal basis
{'a': {'a', 3}, 3: {'a', 3}, 2: {1, 2}, 1: {1}}
sage: T.topogenous_matrix() == matrix(mat_dict)
True
"""
# Assign attributes
self._cardinality = len(elements)
self._elements = elements
self._minimal_basis = minimal_basis
self._topogenous = topogenous
def space_sorting(self, element):
r"""
Return a pair formed by the index of `element` in `self._elements` and
the index of `str(element)` in the sorted list consisting of the strings of
elements in `self._elements`.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace({0: {3, 0}, 3: {3, 0}, 2: {2, 1}, 1: {1}})
sage: T._elements
[1, 0, 2, 3]
sage: T.space_sorting(1)
(0, 1)
sage: T.space_sorting(2)
(2, 2)
"""
eltos = self._elements
sorted_str_eltos = sorted([str(x) for x in eltos])
return (eltos.index(element), sorted_str_eltos.index(str(element)))
def _repr_(self):
r"""
Print representation.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: FiniteSpace({0: {0, 1}, 1: {0, 1}})
Finite topological space of 2 points with minimal basis
{0: {0, 1}, 1: {0, 1}}
sage: Q = Poset((divisors(120), attrcall("divides")), linear_extension=True)
sage: FiniteSpace(Q)
Finite T0 topological space of 16 points
"""
n = self._cardinality
if n < 10:
sorted_minimal_basis = {x: sorted(self._minimal_basis[x], key=self.space_sorting)
for x in self._minimal_basis}
return "Finite topological space of {} points with minimal basis \n {}" \
.format(n, sorted_minimal_basis).replace('[', '{').replace(']', '}')
else:
return "Finite topological space of {} points".format(n)
def __contains__(self, x):
r"""
Return ``True`` if ``x`` is an element of the finite space.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: P = Poset((divisors(6), attrcall("divides")), linear_extension=True)
sage: T = FiniteSpace(P)
sage: 3 in T
True
sage: 4 in T
False
"""
return x in self._elements
def elements(self):
r"""
Return the list of elements in the underlying set of the finite space.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {1}, {2, 3}, {3}))
sage: T.elements()
[0, 1, 3, 2]
"""
return self._elements
def underlying_set(self):
r"""
Return the underlying set of the finite space.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {1}, {2, 3}, {3}))
sage: T.underlying_set()
{0, 1, 2, 3}
"""
return set(self._elements)
def subspace(self, points=None, is_T0=False):
r"""
Return the subspace whose elements are in ``points``.
INPUT:
- ``points`` -- (default ``None``) A tuple, list or set contained in ``self.elements()``.
- ``is_T0`` -- (default ``False``) If it is known that the resulting subspace is `T_0`, fix ``True``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {1, 3, 4}, {0, 2, 5}, {1, 3, 4}, {1, 3, 4}, {0, 2, 5}))
sage: T.subspace((0, 3, 5))
Finite T0 topological space of 3 points with minimal basis
{0: {0}, 3: {3}, 5: {0, 5}}
sage: T.subspace([4])
Finite T0 topological space of 1 points with minimal basis
{4: {4}}
sage: T.subspace() == T
True
"""
if points is None:
return self
assert isinstance(points, (tuple, list, set)), \
"Parameter must be of type tuple, list or set"
points = set(points)
assert points <= set(self._elements), \
"There are points that are not in the space"
if points==set(self._elements):
return self
minimal_basis = {x: self._minimal_basis[x] & points for x in points}
return FiniteSpace(minimal_basis, is_T0=is_T0)
def cardinality(self):
r"""
Return the number of elements in the finite space.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: P = Poset((divisors(360), attrcall("divides")), linear_extension=True)
sage: T = FiniteSpace(P)
sage: T.cardinality() == P.cardinality()
True
"""
return self._cardinality
def minimal_basis(self):
r"""
Return the minimal basis that generates the topology of the finite space.
OUTPUT:
- A dictionary whose keys are the elements of the space and the values
are the minimal open sets containing the respective element.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {0, 1, 2}, {0, 1, 2}, {3, 4}, {3, 4}))
sage: T.minimal_basis()
{0: {0}, 1: {0, 1, 2}, 2: {0, 1, 2}, 3: {3, 4}, 4: {3, 4}}
sage: M = T.equivalent_T0()
sage: M.minimal_basis()
{0: {0}, 1: {0, 1}, 3: {3}}
"""
return self._minimal_basis
def minimal_open_set(self, x):
r"""
Return the minimal open set containing ``x``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {0, 1, 2}, {0, 1, 2}, {3, 4}, {3, 4}))
sage: T.minimal_open_set(1)
{0, 1, 2}
"""
if not x in self:
raise ValueError("The point {} is not an element of the space".format(x))
else:
return self._minimal_basis[x]
def topogenous_matrix(self):
r"""
Return the topogenous matrix of the finite space.
OUTPUT:
- A binary matrix whose `(i,j)` entry is equal to 1 if and only if ``self._elements[i]``
is in ``self._minimal_basis[self._elements[j]]``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {1, 3, 4}, {0, 2, 5}, {1, 3, 4}, {1, 3, 4}, {0, 2, 5}))
sage: T.topogenous_matrix()
[1 0 1 0 0 1]
[0 1 0 1 1 0]
[0 0 1 0 0 1]
[0 1 0 1 1 0]
[0 1 0 1 1 0]
[0 0 1 0 0 1]
sage: T0 = T.equivalent_T0()
sage: T0.topogenous_matrix()
[1 0 1]
[0 1 0]
[0 0 1]
"""
return self._topogenous
def is_T0(self):
r"""
Return ``True`` if the finite space satisfies the T0 separation axiom.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace([{0}, {1}, {2, 3}, {2, 3}])
sage: T.is_T0()
False
sage: T.equivalent_T0().is_T0()
True
"""
return isinstance(self, FiniteTopologicalSpace_T0)
def equivalent_T0(self, points=None, check=True):
r"""
Return a finite T0 space homotopy equivalent to ``self``.
INPUT:
- ``points`` -- (default ``None``) a tuple, list or set of representatives
elements of the equivalent classes induced by the partition ``self._T0``.
- ``check`` -- if ``True`` (default), it is checked that ``points`` effectively
defines a set of representatives of the partition ``self._T0``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace(({0}, {1, 3, 4}, {0, 2, 5}, {1, 3, 4}, {1, 3, 4}, {0, 2, 5}))
sage: T.is_T0()
False
sage: T._T0
[{0}, {1, 3, 4}, {2, 5}]
sage: M1 = T.equivalent_T0()
sage: M1.is_T0()
True
sage: M1.elements()
[0, 1, 2]
sage: M2 = T.equivalent_T0(points={0,4,5}, check=False)
sage: M2.elements()
[0, 4, 5]
sage: T.equivalent_T0(points={0,3,4})
Traceback (most recent call last):
...
ValueError: Parameter 'points' is not a valid set of representatives
"""
if self._T0 is True:
return self
else:
if points is None:
points = [list(A)[0] for A in self._T0]
elif check:
assert isinstance(points, (tuple, list, set)), \
"Parameter 'points' must be of type tuple, list or set"
assert len(points)==len(self._T0), \
"Parameter 'points' does not have a valid length"
points2 = set(points.copy())
partition = self._T0.copy()
while points2:
x = points2.pop()
class_x = None
for k in range(len(partition)):
if x in partition[k]:
class_x = k
partition.pop(k)
break
if class_x is None:
raise ValueError("Parameter 'points' is not a valid set of representatives")
return self.subspace(points, is_T0=True)
def Ux(self, x):
r"""
Return the list of the elements in the minimal open set containing ``x``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: minimal_basis = {5: {5}, 6: {5, 6}, 3: {3, 5}, 2: {2, 5, 6}, \
4: {2, 4, 5, 6}, 1: {1, 5}}
sage: T = FiniteSpace(minimal_basis)
sage: T.Ux(2)
[5, 6, 2]
TESTS::
sage: import random
sage: T = FiniteSpace(posets.RandomPoset(30, 0.2))
sage: x = random.choice(T._elements)
sage: T.is_contractible(T.Ux(x))
True
"""
return sorted(self._minimal_basis[x], key=self.space_sorting)
def Fx(self, x):
r"""
Return the list of the elements in the closure of `\lbrace x\rbrace`.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: minimal_basis = {5: {5}, 6: {5, 6}, 3: {3, 5}, 2: {2, 5, 6}, \
4: {2, 4, 5, 6}, 1: {1, 5}}
sage: T = FiniteSpace(minimal_basis)
sage: T.Fx(2)
[2, 4]
TESTS::
sage: import random
sage: T = FiniteSpace(posets.RandomPoset(30, 0.2))
sage: x = random.choice(T._elements)
sage: T.is_contractible(T.Fx(x))
True
"""
result = [y for y in self._elements if x in self._minimal_basis[y]]
if result==[]:
raise ValueError("The point {} is not an element of the space".format(x))
return result
def Cx(self, x):
r"""
Return the list of the elements in the star of ``x``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: minimal_basis = {5: {5}, 6: {5, 6}, 3: {3, 5}, 2: {2, 5, 6}, \
4: {2, 4, 5, 6}, 1: {1, 5}}
sage: T = FiniteSpace(minimal_basis)
sage: T.Cx(2)
[5, 6, 2, 4]
TESTS::
sage: import random
sage: T = FiniteSpace(posets.RandomPoset(30, 0.2))
sage: x = random.choice(T._elements)
sage: T.is_contractible(T.Cx(x))
True
"""
return self.Ux(x) + self.Fx(x)[1:]
def Ux_tilded(self, x):
r"""
Return the list of the elements in `\widehat{U}_x = U_x \minus \lbrace x\rbrace`.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: minimal_basis = {5: {5}, 6: {5, 6}, 3: {3, 5}, 2: {2, 5, 6}, \
4: {2, 4, 5, 6}, 1: {1, 5}}
sage: T = FiniteSpace(minimal_basis)
sage: T.Ux_tilded(2)
[5, 6]
"""
return self.Ux(x)[:-1]
def Fx_tilded(self, x):
r"""
Return the list of the elements in `\widehat{F}_x = F_x \minus \lbrace x\rbrace`.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: minimal_basis = {5: {5}, 6: {5, 6}, 3: {3, 5}, 2: {2, 5, 6}, \
4: {2, 4, 5, 6}, 1: {1, 5}}
sage: T = FiniteSpace(minimal_basis)
sage: T.Fx_tilded(2)
[4]
"""
return self.Fx(x)[1:]
def Cx_tilded(self, x):
r"""
Return the list of the elements in `\widehat{C}_x = C_x \minus \lbrace x\rbrace`.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: minimal_basis = {5: {5}, 6: {5, 6}, 3: {3, 5}, 2: {2, 5, 6}, \
4: {2, 4, 5, 6}, 1: {1, 5}}
sage: T = FiniteSpace(minimal_basis)
sage: T.Cx_tilded(2)
[5, 6, 4]
"""
return self.Ux(x)[:-1] + self.Fx(x)[1:]
def opposite(self):
r"""
Return the opposite space of ``self``.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: mat_dict = {(0, 0): 1, (0, 3): 1, (0, 4): 1, (1, 1): 1, (1, 2): 1, (2, 1): 1, \
....: (2, 2): 1, (3, 3): 1, (3, 4): 1, (4, 3): 1, (4, 4): 1}
sage: T = FiniteSpace(matrix(mat_dict))
sage: T
Finite topological space of 5 points with minimal basis
{0: {0}, 1: {1, 2}, 2: {1, 2}, 3: {0, 3, 4}, 4: {0, 3, 4}}
sage: T.opposite()
Finite topological space of 5 points with minimal basis
{0: {3, 4, 0}, 1: {1, 2}, 2: {1, 2}, 3: {3, 4}, 4: {3, 4}}
sage: T.topogenous_matrix()
[1 0 0 1 1]
[0 1 1 0 0]
[0 1 1 0 0]
[0 0 0 1 1]
[0 0 0 1 1]
sage: T.opposite().topogenous_matrix()
[1 1 0 0 0]
[1 1 0 0 0]
[0 0 1 1 1]
[0 0 1 1 1]
[0 0 0 0 1]
"""
minimal_basis_op = {x:set(self.Fx(x)) for x in self._elements}
T0 = isinstance(self, FiniteTopologicalSpace_T0)
return FiniteSpace(minimal_basis_op, is_T0=T0)
def is_interior_point(self, x, E):
r"""
Return ``True`` if ``x`` is an interior point of ``E`` in the finite space.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace([{0, 1}, {1}, {2, 3, 4}, {2, 3, 4}, {4}])
sage: T.is_interior_point(1, {1, 2, 3})
True
sage: T.is_interior_point(2, {1, 2, 3})
False
sage: T.is_interior_point(1, set())
False
sage: T.is_interior_point(3, T.underlying_set())
True
"""
assert x in self.underlying_set() , "Parameter 'x' must be an element of the space"
assert E <= self.underlying_set() , "Parameter 'E' must be a subset of the underlying set"
if not x in E:
return False
return self._minimal_basis[x] <= E
def interior(self, E):
r"""
Return the interior of a subset in the finite space.
EXAMPLES::
sage: from sage.homology.finite_topological_spaces import FiniteSpace
sage: T = FiniteSpace([{0, 1}, {1}, {2, 3, 4}, {2, 3, 4}, {4}])
sage: T.interior({1, 2, 3})
{1}
sage: T.interior({1, 2, 3, 4})
{1, 2, 3, 4}
sage: T.interior({2, 3})
set()
TESTS::
sage: import random
sage: T = FiniteSpace(posets.RandomPoset(30, 0.5))
sage: X = T.underlying_set()
sage: k = randint(0,len(X))
sage: E = set(random.sample(X, k))
sage: Int = T.interior(E)
sage: T.is_open(Int)
True
sage: T.interior(Int) == Int