File: torusbundle.cpp

package info (click to toggle)
regina-normal 5.1-6
  • links: PTS
  • area: main
  • in suites: buster
  • size: 54,488 kB
  • sloc: cpp: 142,029; ansic: 19,218; xml: 9,844; objc: 7,729; perl: 1,190; python: 623; sh: 614; makefile: 34
file content (344 lines) | stat: -rw-r--r-- 13,052 bytes parent folder | download | duplicates (2)
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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Computational Engine                                                  *
 *                                                                        *
 *  Copyright (c) 1999-2016, 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.                       *
 *                                                                        *
 *  As an exception, when this program is distributed through (i) the     *
 *  App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or     *
 *  (iii) Google Play by Google Inc., then that store may impose any      *
 *  digital rights management, device limits and/or redistribution        *
 *  restrictions that are required by its terms of service.               *
 *                                                                        *
 *  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.                                                   *
 *                                                                        *
 **************************************************************************/

#include "algebra/abeliangroup.h"
#include "maths/matrix.h"
#include "manifold/torusbundle.h"

namespace regina {

AbelianGroup* TorusBundle::homology() const {
    MatrixInt relns(2, 2);

    relns.entry(0, 0) = monodromy_[0][0] - 1;
    relns.entry(0, 1) = monodromy_[0][1];
    relns.entry(1, 0) = monodromy_[1][0];
    relns.entry(1, 1) = monodromy_[1][1] - 1;

    AbelianGroup* ans = new AbelianGroup();
    ans->addGroup(relns);
    ans->addRank();

    return ans;
}

std::ostream& TorusBundle::writeName(std::ostream& out) const {
    if (monodromy_.isIdentity())
        return out << "T x I";
    else
        return out << "T x I / [ " << monodromy_[0][0] << ','
            << monodromy_[0][1] << " | " << monodromy_[1][0] << ','
            << monodromy_[1][1] << " ]";
}

std::ostream& TorusBundle::writeTeXName(std::ostream& out) const {
    if (monodromy_.isIdentity())
        return out << "T^2 \\times I";
    else
        return out << "T^2 \\times I / \\homtwo{" << monodromy_[0][0] << "}{"
            << monodromy_[0][1] << "}{" << monodromy_[1][0] << "}{"
            << monodromy_[1][1] << "}";
}

void TorusBundle::reduce() {
    // Make the monodromy prettier.
    // In general we are allowed to:
    //
    // - Replace M with A M A^-1
    // - Replace M with M^-1
    //
    // Some specific tricks we can pull include:
    //
    // - Rotate the matrix 180 degrees (A = [ 0 1 | 1 0 ])
    // - Negate the off-diagonal (A = [ 1 0 | 0 -1 ])
    //
    // If det == +1, we can also:
    //
    // - Swap either diagonal individually (invert, then negate the
    //   off-diagonal, then optionally rotate by 180 degrees)
    //
    // If det == -1, we can also:
    //
    // - Simultaneously swap and negate the main diagonal (invert)

    // The determinant should be +/-1 according to our preconditions,
    // but we'd better check that anyway.
    long det = monodromy_.determinant();
    if (det != 1 && det != -1) {
        // Something is very wrong.  Don't touch it.
        std::cerr << "ERROR: TorusBundle monodromy does not have "
            "determinant +/-1.\n";
        return;
    }

    // Deal with the case where the main diagonal has strictly opposite
    // signs.
    long x;
    if (monodromy_[0][0] < 0 && monodromy_[1][1] > 0) {
        // Rotate 180 degrees to put the positive element up top.
        rotate();
    }
    while (monodromy_[0][0] > 0 && monodromy_[1][1] < 0) {
        // Set x to the greatest absolute value of any main diagonal element.
        if (monodromy_[0][0] >= - monodromy_[1][1])
            x = monodromy_[0][0];
        else
            x = - monodromy_[1][1];

        // If we catch any of the following four cases, the main diagonal
        // will either no longer have opposite signs, or it will have a
        // strictly smaller maximum absolute value.
        if (0 < monodromy_[0][1] && monodromy_[0][1] <= x) {
            addRCDown();
            continue;
        } else if (0 < - monodromy_[0][1] && - monodromy_[0][1] <= x) {
            subtractRCDown();
            continue;
        } else if (0 < monodromy_[1][0] && monodromy_[1][0] <= x) {
            subtractRCUp();
            continue;
        } else if (0 < - monodromy_[1][0] && - monodromy_[1][0] <= x) {
            addRCUp();
            continue;
        }

        // Since the determinant is +/-1 and neither element of the
        // main diagonal is zero, we cannot have both elements of the
        // off-diagonal with absolute value strictly greater than x.

        // The only remaining possibility is that some element of the
        // off-diagonal is zero (and therefore the main diagonal
        // contains +1 and -1).

        // The non-zero off-diagonal element (if any) can be reduced
        // modulo 2.  This leaves us with the following possibilities:
        //   [ 1 0 | 0 -1 ] ,   [ 1 1 | 0 -1 ],   [ 1 0 | 1 -1 ].
        // The final two possibilities are both equivalent to [ 0 1 | 1 0 ].
        if ((monodromy_[0][1] % 2) || (monodromy_[1][0] % 2)) {
            monodromy_[0][0] = monodromy_[1][1] = 0;
            monodromy_[0][1] = monodromy_[1][0] = 1;
        } else {
            monodromy_[0][1] = monodromy_[1][0] = 0;
            // The main diagonal elements stay as they are (1, -1).
        }

        // In these cases we are completely finished.
        return;
    }

    // We are now guaranteed that the main diagonal does not have
    // strictly opposite signs.

    // Time to arrange the same for the off-diagonal.

    // If the off-diagonal has strictly opposite signs, the elements
    // must be +1 and -1, and the main diagonal must contain a zero.
    // Otherwise there is no way we can get determinant +/-1.
    if (monodromy_[0][1] < 0 && monodromy_[1][0] > 0) {
        // We have [ a -1 | 1 d ].
        // Move the -1 to the bottom left corner by negating the off-diagonal.
        monodromy_[0][1] = 1;
        monodromy_[1][0] = -1;
    }
    if (monodromy_[0][1] > 0 && monodromy_[1][0] < 0) {
        // We have [ a 1 | -1 d ], where one of a or d is zero.
        // Rotate by 180 degrees to move the 0 to the bottom right
        // corner, negating the off-diagonal if necessary to preserve
        // the 1/-1 positions.
        if (monodromy_[1][1]) {
            monodromy_[0][0] = monodromy_[1][1];
            monodromy_[1][1] = 0;
        }

        // Now we have [ a 1 | -1 0 ].
        if (monodromy_[0][0] > 1) {
            addRCDown();
            // Everything becomes non-negative.
        } else if (monodromy_[0][0] < -1) {
            subtractRCUp();
            // Everything becomes non-positive.
        } else {
            // We have [ 1 1 | -1 0 ], [ 0 1 | -1 0 ] or [ -1 1 | -1 0 ].
            // All of these are canonical.
            return;
        }
    }

    // Neither diagonal has strictly opposite signs.
    // Time to give all elements of the matrix the same sign (or zero).
    bool allNegative = false;
    if (det == 1) {
        // Either all non-negative or all non-positive, as determined by
        // the main diagonal.
        // If it's going to end up negative, just switch the signs for
        // now and remember this fact for later on.
        if (monodromy_[0][0] < 0 || monodromy_[1][1] < 0) {
            allNegative = true;
            monodromy_[0][0] = - monodromy_[0][0];
            monodromy_[1][1] = - monodromy_[1][1];
        }
        if (monodromy_[0][1] < 0 || monodromy_[1][0] < 0) {
            // We're always allowed to do this.
            monodromy_[0][1] = - monodromy_[0][1];
            monodromy_[1][0] = - monodromy_[1][0];
        }
    } else {
        // The determinant is -1.
        // The entire matrix can be made non-negative.
        if (monodromy_[0][0] < 0 || monodromy_[1][1] < 0) {
            // Invert (swap and negate the main diagonal).
            x = monodromy_[0][0];
            monodromy_[0][0] = - monodromy_[1][1];
            monodromy_[1][1] = -x;
        }
        if (monodromy_[0][1] < 0 || monodromy_[1][0] < 0) {
            // Negate the off-diagonal as usual.
            monodromy_[0][1] = - monodromy_[0][1];
            monodromy_[1][0] = - monodromy_[1][0];
        }
    }

    // We now have a matrix whose entries are all non-negative.
    // Run through a cycle of equivalent matrices, and choose the nicest.
    // I'm pretty sure I can prove that this is a cycle, but the proof
    // really should be written down.
    Matrix2 start = monodromy_;
    Matrix2 best = monodromy_;
    while (1) {
        // INV: monodromy has all non-negative entries.
        // INV: best contains the best seen matrix, including the current one.

        // It can be proven (via det = +/-1) that one row must dominate
        // another, unless we have [ 1 0 | 0 1 ] or [ 0 1 | 1 0 ].
        if (monodromy_.isIdentity()) {
            if (allNegative)
                monodromy_.negate();
            return;
        }
        if (monodromy_[0][0] == 0 && monodromy_[0][1] == 1 &&
                monodromy_[1][0] == 1 && monodromy_[1][1] == 0) {
            if (allNegative)
                monodromy_.negate();
            return;
        }

        // We know at this point that one row dominates the other.
        if (monodromy_[0][0] >= monodromy_[1][0] &&
                monodromy_[0][1] >= monodromy_[1][1])
            subtractRCUp();
        else
            subtractRCDown();

        // Looking at a new matrix.
        if (monodromy_ == start)
            break;

        if (TorusBundle::simplerNonNeg(monodromy_, best))
            best = monodromy_;
    }

    // In the orientable case, run this all again for the rotated matrix.
    // This is not necessary in the non-orientable case since the
    // rotated matrix belongs to the same cycle as the original.
    if (det > 0) {
        rotate();
        if (TorusBundle::simplerNonNeg(monodromy_, best))
            best = monodromy_;

        start = monodromy_;
        while (1) {
            if (monodromy_.isIdentity()) {
                if (allNegative)
                    monodromy_.negate();
                return;
            }
            if (monodromy_[0][0] == 0 && monodromy_[0][1] == 1 &&
                    monodromy_[1][0] == 1 && monodromy_[1][1] == 0) {
                if (allNegative)
                    monodromy_.negate();
                return;
            }

            // We know at this point that one row dominates the other.
            if (monodromy_[0][0] >= monodromy_[1][0] &&
                    monodromy_[0][1] >= monodromy_[1][1])
                subtractRCUp();
            else
                subtractRCDown();

            // Looking at a new matrix.
            if (monodromy_ == start)
                break;

            if (TorusBundle::simplerNonNeg(monodromy_, best))
                best = monodromy_;
        }
    }

    monodromy_ = best;

    // Don't forget that negative case.
    if (allNegative)
        monodromy_.negate();
}

bool TorusBundle::simplerNonNeg(const Matrix2& m1, const Matrix2& m2) {
    // Value symmetric matrices above all else.
    if (m1[0][1] == m1[1][0] && m2[0][1] != m2[1][0])
        return true;
    if (m1[0][1] != m1[1][0] && m2[0][1] == m2[1][0])
        return false;

    // Go for the smallest possible bottom-right element, then so on
    // working our way up.
    if (m1[1][1] < m2[1][1])
        return true;
    if (m1[1][1] > m2[1][1])
        return false;

    if (m1[1][0] < m2[1][0])
        return true;
    if (m1[1][0] > m2[1][0])
        return false;

    if (m1[0][1] < m2[0][1])
        return true;
    if (m1[0][1] > m2[0][1])
        return false;

    if (m1[0][0] < m2[0][0])
        return true;
    return false;
}

} // namespace regina