Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/homology/tests.py
4096 views
1
"""
2
Tests for chain complexes, simplicial complexes, etc.
3
4
These test whether CHomP gives the same answers as Sage's built-in
5
homology calculator.
6
7
TESTS::
8
9
sage: from sage.homology.tests import test_random_chain_complex
10
sage: test_random_chain_complex(trials=20) # optional - CHomP
11
sage: test_random_chain_complex(level=2, trials=20) # optional - CHomP
12
sage: test_random_chain_complex(level=3, trials=20) # long time # optional - CHomP
13
14
sage: from sage.homology.tests import test_random_simplicial_complex
15
sage: test_random_simplicial_complex(level=1, trials=20) # optional - CHomP
16
sage: test_random_simplicial_complex(level=2, trials=20) # optional - CHomP
17
sage: test_random_simplicial_complex(level=5/2, trials=10) # long time # optional - CHomP
18
"""
19
from sage.misc.random_testing import random_testing
20
21
def random_chain_complex(level=1):
22
"""
23
Return a random chain complex, defined by specifying a single
24
random matrix in a random degree, with differential of degree
25
either 1 or -1. The matrix is randomly sparse or dense.
26
27
:param level: measure of complexity: the larger this is, the
28
larger the matrix can be, and the larger its degree can be in
29
the chain complex.
30
:type level: positive integer; optional, default 1
31
32
EXAMPLES::
33
34
sage: from sage.homology.tests import random_chain_complex
35
sage: C = random_chain_complex()
36
sage: C
37
Chain complex with at most 2 nonzero terms over Integer Ring
38
sage: C._degree # random: either 1 or -1
39
1
40
"""
41
from sage.misc.prandom import randint
42
from sage.matrix.constructor import random_matrix
43
from sage.homology.chain_complex import ChainComplex
44
from sage.rings.integer_ring import ZZ
45
bound = 50*level
46
nrows = randint(0, bound)
47
ncols = randint(0, bound)
48
sparseness = bool(randint(0, 1))
49
mat = random_matrix(ZZ, nrows, ncols, sparse=sparseness)
50
dim = randint(-bound, bound)
51
deg = 2 * randint(0, 1) - 1 # -1 or 1
52
return ChainComplex({dim: mat}, degree = deg)
53
54
@random_testing
55
def test_random_chain_complex(level=1, trials=1, verbose=False):
56
"""
57
Compute the homology of a random chain complex with and without
58
CHomP, and compare the results. If they are not the same, raise
59
an error.
60
61
:param level: measure of complexity of the chain complex -- see
62
:func:`random_chain_complex`
63
:type level: positive integer; optional, default 1
64
:param trials: number of trials to conduct
65
:type trials: positive integer; optional, default 1
66
:param verbose: if ``True``, print verbose messages
67
:type verbose: boolean; optional, default ``False``
68
69
EXAMPLES::
70
71
sage: from sage.homology.tests import test_random_chain_complex
72
sage: test_random_chain_complex(trials=2) # optional - CHomP
73
"""
74
for i in range(trials):
75
C = random_chain_complex(level=level)
76
for d in C.differential():
77
chomp = C.homology(d, verbose=verbose)
78
no_chomp = C.homology(d, algorithm='no_chomp', verbose=verbose)
79
if chomp != no_chomp:
80
print "Homology in dimension %s according to CHomP: %s" % (d, chomp)
81
print "Homology in dimension %s according to Sage: %s" % (d, no_chomp)
82
print "Chain complex: %s" % C.differential()
83
raise ValueError
84
85
def random_simplicial_complex(level=1, p=0.5):
86
"""
87
Return a random simplicial complex.
88
89
:param level: measure of complexity: the larger this is, the more
90
vertices and therefore the larger the possible dimension of the
91
complex.
92
:type level: positive integer; optional, default 1
93
:param p: probability, passed on to ``simplicial_complexes.RandomComplex``
94
:type p: float between 0 and 1; optional; default 0.5
95
96
EXAMPLES::
97
98
sage: from sage.homology.tests import random_simplicial_complex
99
sage: X = random_simplicial_complex()
100
sage: X # random
101
Simplicial complex with vertex set (0, 1, 2, 3, 4, 5, 6, 7) and 31 facets
102
sage: X.dimension() < 11
103
True
104
"""
105
from sage.misc.prandom import randint
106
from sage.homology.examples import simplicial_complexes
107
n = randint(2, 4*level)
108
dim = randint(1, n)
109
return simplicial_complexes.RandomComplex(n, dim, p)
110
111
@random_testing
112
def test_random_simplicial_complex(level=1, trials=1, verbose=False):
113
"""
114
Compute the homology of a random simplicial complex with and
115
without CHomP, and compare the results. If they are not the same,
116
raise an error.
117
118
:param level: measure of complexity of the simplicial complex --
119
see :func:`random_simplicial_complex`
120
:type level: positive integer; optional, default 1
121
:param trials: number of trials to conduct
122
:type trials: positive integer; optional, default 1
123
:param verbose: if ``True``, print verbose messages
124
:type verbose: boolean; optional, default ``False``
125
126
This gets pretty slow if ``level`` is more than 3.
127
128
EXAMPLES::
129
130
sage: from sage.homology.tests import test_random_simplicial_complex
131
sage: test_random_simplicial_complex(trials=2) # optional - CHomP
132
"""
133
for i in range(trials):
134
X = random_simplicial_complex(level=level)
135
chomp = X.homology(verbose=verbose)
136
no_chomp = X.homology(algorithm='no_chomp', verbose=verbose)
137
if chomp != no_chomp:
138
print "Homology according to CHomP: %s" % chomp
139
print "Homology according to Sage: %s" % no_chomp
140
print "Simplicial complex: %s" % X
141
print "Its chain complex: %s" % X.chain_complex()
142
raise ValueError
143
144