File: shortest_cusp_basis.c

package info (click to toggle)
snappea 3.0d3-20.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,896 kB
  • ctags: 3,582
  • sloc: ansic: 33,469; sh: 8,293; python: 7,623; makefile: 240
file content (346 lines) | stat: -rw-r--r-- 8,478 bytes parent folder | download | duplicates (11)
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
/*
 *	shortest_cusp_basis.c
 *
 *	This file provides the functions
 *
 *		Complex	cusp_modulus(			Complex				cusp_shape);
 *
 *		void	shortest_cusp_basis(	Complex				cusp_shape,
 *										MatrixInt22			basis_change);
 *
 *		Complex	transformed_cusp_shape(	Complex				cusp_shape,
 *										CONST MatrixInt22	basis_change);
 *
 *		void	install_shortest_bases(	Triangulation		*manifold);
 *
 *	cusp_modulus() accepts a cusp_shape (longitude/meridian) and returns
 *		the cusp modulus.  Loosely speaking, the cusp modulus is defined as
 *		(second shortest translation)/(shortest translation);  it is a
 *		complex number z lying in the region |Re(z)| <= 1/2  &&  |z| >= 1.
 *		If z lies on the boundary of this region, we choose it so that
 *		Re(z) >= 0.
 *
 *	shortest_cusp_basis() accepts a cusp_shape (longitude/meridian) and
 *		computes the 2 x 2 integer matrix which transforms the old basis
 *		(u,  v ) = (meridian, longitude) to the new basis
 *		(u', v') = (shortest, second shortest).  That is,
 *
 *				| u' |   |              | | u |
 *				|    | = | basis_change | |   |
 *				| v' |   |              | | v |
 *
 *				 2 x 1        2 x 2       2 x 1
 *				complex      integer     complex
 *				vector       matrix      vector
 *
 *		(u', v') is such that v'/u' is the cusp modulus defined above.
 *
 *		Occasionally the shortest or second shortest curve won't be
 *		unique.  In most cases the conventions for the cusp modulus stated
 *		above (in particular the convention that Re(z) >= 0 when z is on
 *		the boundary of the fundamental domain) serve to uniquely specify
 *		(u', v') in spite of the nonuniqueness of lengths.  The only
 *		exceptions are the hexagonal lattice, where three different curves
 *		all have minimal length, and the square lattice, where two different
 *		curves have minimal length.  In these cases the ambiguity is not
 *		resolved, and the choice of (u', v') may be machine dependent.
 *
 *	transformed_cusp_shape() accepts a cusp_shape and a basis_change,
 *		and computes the shape of the cusp relative to the basis (u', v')
 *		defined by
 *
 *				| u' |   |              | | u |
 *				|    | = | basis_change | |   |
 *				| v' |   |              | | v |
 *
 *		(u', v') need not be the (shortest, second shortest) basis.
 *
 *	install_shortest_bases() installs the (shortest, second shortest)
 *		basis on each torus Cusp of manifold.  It does not change the bases
 *		on Klein bottle cusps.  As explained for shortest_cusp_basis()
 *		above, the (shortest, second shortest) is not well defined for a
 *		hexagonal lattice, and the results may be machine dependent.
 *
 *
 *	shortest_cusp_basis() uses the following algorithm.  In principle
 *	u and v could be any two translations which generate the fundamental
 *	group of a torus, although in shortest_cusp_basis(), u is initially 1
 *	and v is initially the cusp_shape.
 *
 *		do
 *		{
 *
 *			if (|u + v| < |u|)
 *				u += v;
 *
 *			if (|u - v| < |u|)
 *				u -= v;
 *
 *			if (|v + u| < |v|)
 *				v += u;
 *
 *			if (|v - u| < |v|)
 *				v -= u;
 *
 *		} while (still making progress);
 *
 *		if (|u| > |v|)
 *			replace (u, v) with (v, -u)
 *
 *		if (Im(v/u) < 0)
 *			flag an error -- the original orientation was wrong
 *
 *		if (v/u is on the boundary of the fundamental domain described above)
 *			make sure Re(v/u) >= 0
 *
 *	Theorem.  The above algorithm computes the
 *	(shortest, second shortest) basis.
 *
 *	Proof.  The angle between u and v must be between pi/3 and 2 pi/3;
 *	otherwise the length of the projection of v onto u would be
 *	|v| cos(theta) > |u| cos(pi/3) = |u|/2, and we would have added
 *	+-u to v, thereby shortening v.  This shows that |Re(v/u)| <= 1/2.
 *	Because we chose |u| <= |v|, |v/u| >= 1.  Therefore v/u lies within
 *	the fundamental domain described above.  It is also easy to see that
 *	v is the shortest translation which is linearly independent of u.
 *	The reason is that the row of lattice points 2v + nu is a distance
 *	at least 2 |v| sin(theta) >= 2 |u| sin(pi/3) = sqrt(3) |u| away from
 *	the row 0v + nu.
 */

#include "kernel.h"

#define EPSILON	(1e5 * DBL_EPSILON)


Complex cusp_modulus(
	Complex 	cusp_shape)
{
	MatrixInt22	basis_change;

	shortest_cusp_basis(cusp_shape, basis_change);
	return transformed_cusp_shape(cusp_shape, basis_change);
}


void shortest_cusp_basis(
	Complex 	cusp_shape,
	MatrixInt22	basis_change)	/* basis_change is an output variable here */
{
	Complex	u,
			v,
			u_plus_v,
			u_minus_v,
			temp,
			cusp_modulus;
	double	mod_u,				/*	These are the complex moduli	*/
			mod_v,				/*	of the preceding variables.		*/
			mod_u_plus_v,
			mod_u_minus_v;
	int		i,
			j,
			temp_int;
	Boolean	progress;

	/*
	 *	For an explanation of this algorithm, see the documentation above.
	 */

	/*
	 *	Make sure cusp_shape is nondegenerate.
	 */
	if (fabs(cusp_shape.imag) < EPSILON)
	{
		for (i = 0; i < 2; i++)
			for (j = 0; j < 2; j++)
				basis_change[i][j] = 0;
		return;
	}

	u = One;
	v = cusp_shape;

	mod_u = complex_modulus(u);
	mod_v = complex_modulus(v);

	for (i = 0; i < 2; i++)
		for (j = 0; j < 2; j++)
			basis_change[i][j] = (i == j);

	do
	{
		progress = FALSE;

		u_plus_v = complex_plus(u,v);
		mod_u_plus_v = complex_modulus(u_plus_v);
		if (mod_u - mod_u_plus_v > EPSILON)
		{
			u = u_plus_v;
			mod_u = mod_u_plus_v;
			for (j = 0; j < 2; j++)
				basis_change[0][j] += basis_change[1][j];
			progress = TRUE;
		}
		else if (mod_v - mod_u_plus_v > EPSILON)
		{
			v = u_plus_v;
			mod_v = mod_u_plus_v;
			for (j = 0; j < 2; j++)
				basis_change[1][j] += basis_change[0][j];
			progress = TRUE;
		}

		u_minus_v = complex_minus(u,v);
		mod_u_minus_v = complex_modulus(u_minus_v);
		if (mod_u - mod_u_minus_v > EPSILON)
		{
			u = u_minus_v;
			mod_u = mod_u_minus_v;
			for (j = 0; j < 2; j++)
				basis_change[0][j] -= basis_change[1][j];
			progress = TRUE;
		}
		else if (mod_v - mod_u_minus_v > EPSILON)
		{
			v = complex_negate(u_minus_v);
			mod_v = mod_u_minus_v;
			for (j = 0; j < 2; j++)
				basis_change[1][j] -= basis_change[0][j];
			progress = TRUE;
		}

	} while (progress);

	if (mod_u > mod_v + EPSILON)
	{
		temp = u;
		u    = v;
		v    = complex_negate(temp);

		for (j = 0; j < 2; j++)
		{
			temp_int			= basis_change[0][j];
			basis_change[0][j]	= basis_change[1][j];
			basis_change[1][j]	= - temp_int;
		}
	}

	cusp_modulus = complex_div(v,u);

	if (cusp_modulus.imag < 0)
		uFatalError("cusp_modulus", "cusp_modulus");

	if (cusp_modulus.real < -0.5 + EPSILON)
	{
		/*
		 *	Do an extra v += u.
		 */

		cusp_modulus.real = 0.5;

		for (j = 0; j < 2; j++)
			basis_change[1][j] += basis_change[0][j];
	}

	if (complex_modulus(cusp_modulus) < 1.0 + EPSILON)
	{
		if (cusp_modulus.real < -EPSILON)
		{
			/*
			 *	Replace (u,v) with (v,-u).
			 */
			cusp_modulus.real = - cusp_modulus.real;
			for (j = 0; j < 2; j++)
			{
				temp_int			= basis_change[0][j];
				basis_change[0][j]	= basis_change[1][j];
				basis_change[1][j]	= - temp_int;
			}
		}
	}
}


Complex transformed_cusp_shape(
	      Complex 		cusp_shape,
	CONST MatrixInt22	basis_change)	/* basis_change is an input variable here */
{
	Complex	u,
			v;

	u = complex_plus(
		complex_real_mult(
			basis_change[0][0],
			One
		),
		complex_real_mult(
			basis_change[0][1],
			cusp_shape
		)
	);

	v = complex_plus(
		complex_real_mult(
			basis_change[1][0],
			One
		),
		complex_real_mult(
			basis_change[1][1],
			cusp_shape
		)
	);

	if (complex_modulus(u) < EPSILON)
		return Infinity;
	else
		return complex_div(v,u);
}


void install_shortest_bases(
	Triangulation	*manifold)
{
	Cusp		*cusp;
	MatrixInt22	*change_matrices;
	int			i,
				j;

	/*
	 *	Allocate an array to store the change of basis matrices.
	 */

	change_matrices = NEW_ARRAY(manifold->num_cusps, MatrixInt22);

	/*
	 *	Compute the change of basis matrices.
	 */

	for (cusp = manifold->cusp_list_begin.next;
		 cusp != &manifold->cusp_list_end;
		 cusp = cusp->next)

		if (cusp->topology == torus_cusp)

			shortest_cusp_basis(	cusp->cusp_shape[initial],
									change_matrices[cusp->index]);

		else

			for (i = 0; i < 2; i++)
				for (j = 0; j < 2; j++)
					change_matrices[cusp->index][i][j] = (i == j);

	/*
	 *	Install the change of basis matrices.
	 */

	if (change_peripheral_curves(manifold, change_matrices) != func_OK)

		uFatalError("install_shortest_bases", "shortest_cusp_basis");

	/*
	 *	Free the array used to store the change of basis matrices.
	 */

	my_free(change_matrices);
}