File: homology.cpp

package info (click to toggle)
regina-normal 4.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 16,824 kB
  • ctags: 7,862
  • sloc: cpp: 63,296; ansic: 12,913; sh: 10,556; perl: 3,294; makefile: 947; python: 188
file content (274 lines) | stat: -rw-r--r-- 10,485 bytes parent folder | download
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2008, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  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.                       *
 *                                                                        *
 *  This program is distributed in the hope that it will be useful, but   *
 *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public             *
 *  License along with this program; if not, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

// ntriangulationhomology.cpp
//        Implements homology as a property of a triangulation.

#include "triangulation/ntriangulation.h"
#include "maths/nmatrixint.h"

namespace regina {

const NAbelianGroup& NTriangulation::getHomologyH1() const {
    if (H1.known())
        return *H1.value();

    if (getNumberOfTetrahedra() == 0)
        return *(H1 = new NAbelianGroup());

    // Calculate the first homology.
    // Find a maximal forest in the dual 1-skeleton.
    // Note that this will ensure the skeleton has been calculated.
    stdhash::hash_set<NFace*, HashPointer> forest;
    maximalForestInDualSkeleton(forest);

    // Build a presentation matrix.
    // Each non-boundary not-in-forest face is a generator.
    // Each non-boundary edge is a relation.
    unsigned long nBdryEdges = 0;
    unsigned long nBdryFaces = 0;
    for (BoundaryComponentIterator bit = boundaryComponents.begin();
            bit != boundaryComponents.end(); bit++) {
        nBdryEdges += (*bit)->getNumberOfEdges();
        nBdryFaces += (*bit)->getNumberOfFaces();
    }
    long nGens = getNumberOfFaces() - nBdryFaces - forest.size();
    long nRels = getNumberOfEdges() - nBdryEdges;
    NMatrixInt pres(nRels, nGens);

    // Find out which face corresponds to which generator.
    long* genIndex = new long[getNumberOfFaces()];
    long i = 0;
    for (FaceIterator fit = faces.begin(); fit != faces.end(); fit++) {
        if ((*fit)->isBoundary())
            genIndex[fit - faces.begin()] = -1;
        else if (forest.count(*fit))
            genIndex[fit - faces.begin()] = -1;
        else {
            genIndex[fit - faces.begin()] = i;
            i++;
        }
    }

    // Run through each edge and put the relations in the matrix.
    std::deque<NEdgeEmbedding>::const_iterator embit;
    NTetrahedron* currTet;
    NFace* face;
    int currTetFace;
    long faceGenIndex;
    i = 0;
    for (EdgeIterator eit = edges.begin(); eit != edges.end(); eit++) {
        if (! (*eit)->isBoundary()) {
            // Put in the relation corresponding to this edge.
            for (embit = (*eit)->getEmbeddings().begin();
                    embit != (*eit)->getEmbeddings().end(); embit++) {
                currTet = (*embit).getTetrahedron();
                currTetFace = (*embit).getVertices()[2];
                face = currTet->getFace(currTetFace);
                faceGenIndex = genIndex[faceIndex(face)];
                if (faceGenIndex >= 0) {
                    if ((face->getEmbedding(0).getTetrahedron() == currTet) &&
                            (face->getEmbedding(0).getFace() == currTetFace))
                        pres.entry(i, faceGenIndex) += 1;
                    else
                        pres.entry(i, faceGenIndex) -= 1;
                }
            }
            i++;
        }
    }

    delete[] genIndex;

    // Build the group from the presentation matrix and tidy up.
    NAbelianGroup* ans = new NAbelianGroup();
    ans->addGroup(pres);
    return *(H1 = ans);
}

const NAbelianGroup& NTriangulation::getHomologyH1Rel() const {
    if (H1Rel.known())
        return *H1Rel.value();

    if (getNumberOfBoundaryComponents() == 0)
        return *(H1Rel = new NAbelianGroup(getHomologyH1()));

    // Calculate the relative first homology wrt the boundary.

    // Find a maximal forest in the 1-skeleton.
    // Note that this will ensure the skeleton has been calculated.
    stdhash::hash_set<NEdge*, HashPointer> forest;
    maximalForestInSkeleton(forest, false);

    // Build a presentation matrix.
    // Each non-boundary not-in-forest edge is a generator.
    // Each non-boundary face is a relation.
    unsigned long nBdryVertices = 0;
    unsigned long nBdryEdges = 0;
    unsigned long nBdryFaces = 0;
    unsigned long nClosedComponents = 0;
    for (BoundaryComponentIterator bit = boundaryComponents.begin();
            bit != boundaryComponents.end(); bit++) {
        nBdryVertices += (*bit)->getNumberOfVertices();
        nBdryEdges += (*bit)->getNumberOfEdges();
        nBdryFaces += (*bit)->getNumberOfFaces();
    }
    for (ComponentIterator cit = components.begin();
            cit != components.end(); cit++)
        if ((*cit)->isClosed())
            nClosedComponents++;
    long nGens = getNumberOfEdges() - nBdryEdges
        - getNumberOfVertices() + nBdryVertices
        + nClosedComponents;
    long nRels = getNumberOfFaces() - nBdryFaces;
    NMatrixInt pres(nRels, nGens);

    // Find out which edge corresponds to which generator.
    long* genIndex = new long[getNumberOfEdges()];
    long i = 0;
    for (EdgeIterator eit = edges.begin(); eit != edges.end(); eit++) {
        if ((*eit)->isBoundary())
            genIndex[eit - edges.begin()] = -1;
        else if (forest.count(*eit))
            genIndex[eit - edges.begin()] = -1;
        else {
            genIndex[eit - edges.begin()] = i;
            i++;
        }
    }

    // Run through each face and put the relations in the matrix.
    NTetrahedron* currTet;
    NPerm currTetVertices;
    long edgeGenIndex;
    i = 0;
    int faceEdge, currEdgeStart, currEdgeEnd, currEdge;
    for (FaceIterator fit = faces.begin(); fit != faces.end(); fit++) {
        if (! (*fit)->isBoundary()) {
            // Put in the relation corresponding to this face.
            currTet = (*fit)->getEmbedding(0).getTetrahedron();
            currTetVertices = (*fit)->getEmbedding(0).getVertices();
            for (faceEdge = 0; faceEdge < 3; faceEdge++) {
                currEdgeStart = currTetVertices[faceEdge];
                currEdgeEnd = currTetVertices[(faceEdge + 1) % 3];
                // Examine the edge from vertex edgeStart to edgeEnd
                // in tetrahedron currTet.
                currEdge = edgeNumber[currEdgeStart][currEdgeEnd];
                edgeGenIndex = genIndex[edgeIndex(
                    currTet->getEdge(currEdge))];
                if (edgeGenIndex >= 0) {
                    if (currTet->getEdgeMapping(currEdge)[0] == currEdgeStart)
                        pres.entry(i, edgeGenIndex) += 1;
                    else
                        pres.entry(i, edgeGenIndex) -= 1;
                }
            }
            i++;
        }
    }

    delete[] genIndex;

    // Build the group from the presentation matrix and tidy up.
    NAbelianGroup* ans = new NAbelianGroup();
    ans->addGroup(pres);
    return *(H1Rel = ans);
}

const NAbelianGroup& NTriangulation::getHomologyH1Bdry() const {
    if (H1Bdry.known())
        return *H1Bdry.value();

    // Run through the individual boundary components and add the
    // appropriate pieces to the homology group.
    unsigned long rank = 0;
    unsigned long z2rank = 0;

    // Ensure that the skeleton has been calculated.
    if (! calculatedSkeleton)
        calculateSkeleton();

    for (BoundaryComponentIterator bit = boundaryComponents.begin();
            bit != boundaryComponents.end(); bit++) {
        if ((*bit)->isOrientable()) {
            rank += (2 - (*bit)->getEulerCharacteristic());
        } else {
            rank += (1 - (*bit)->getEulerCharacteristic());
            z2rank++;
        }
    }

    // Build the group and tidy up.
    NAbelianGroup* ans = new NAbelianGroup();
    ans->addRank(rank);
    ans->addTorsionElement(2, z2rank);
    return *(H1Bdry = ans);
}

const NAbelianGroup& NTriangulation::getHomologyH2() const {
    if (H2.known())
        return *H2.value();

    if (getNumberOfTetrahedra() == 0)
        return *(H2 = new NAbelianGroup());

    // Calculations are different for orientable vs non-orientable
    // components.
    // We know the only components will be Z and Z_2.
    long rank, z2rank;
    if (isOrientable()) {
        // Same as H1Rel without the torsion elements.
        rank = getHomologyH1Rel().getRank();
        z2rank = 0;
    } else {
        // Non-orientable!
        // z2rank = # closed cmpts - # closed orientable cmpts
        z2rank = 0;
        for (ComponentIterator cit = components.begin();
                cit != components.end(); cit++)
            if ((*cit)->isClosed())
                if (! ((*cit)->isOrientable()))
                    z2rank++;

        // Find rank(Z_2) + rank(Z) and take off z2rank.
        rank = getHomologyH1Rel().getRank() +
            getHomologyH1Rel().getTorsionRank(2) -
            getHomologyH1().getTorsionRank(2) -
            z2rank;
    }

    // Build the new group and tidy up.
    NAbelianGroup* ans = new NAbelianGroup();
    ans->addRank(rank);
    if (z2rank)
        ans->addTorsionElement(2, z2rank);
    return *(H2 = ans);
}

} // namespace regina