File: o31_matrices.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 (586 lines) | stat: -rw-r--r-- 12,233 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
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
/*
 *	o31_matrices.c
 *
 *	This file provides the following functions for working with O31Matrices:
 *
 *		void		o31_copy(O31Matrix dest, O31Matrix source);
 *		void		o31_invert(O31Matrix m, O31Matrix m_inverse);
 *		FuncResult	gl4R_invert(GL4RMatrix m, GL4RMatrix m_inverse);
 *		double		gl4R_determinant(GL4RMatrix m);
 *		void		o31_product(O31Matrix a, O31Matrix b, O31Matrix product);
 *		Boolean		o31_equal(O31Matrix a, O31Matrix b, double epsilon);
 *		double		o31_trace(O31Matrix m);
 *		double		o31_deviation(O31Matrix m);
 *		void		o31_GramSchmidt(O31Matrix m);
 *		void		o31_conjugate(O31Matrix m, O31Matrix t, O31Matrix Tmt);
 *		double		o31_inner_product(O31Vector u, O31Vector v);
 *		void		o31_matrix_times_vector(O31Matrix m, O31Vector v, O31Vector product);
 *		void		o31_constant_times_vector(double r, O31Vector v, O31Vector product);
 *		void		o31_copy_vector(O31Vector dest, O31Vector source);
 *		void		o31_vector_sum(O31Vector a, O31Vector b, O31Vector sum);
 *		void		o31_vector_diff(O31Vector a, O31Vector b, O31Vector diff);
 */

#include "kernel.h"

/*
 *	gl4R_invert will consider a matrix to be singular iff one of the
 *	absolute value of one of the pivots is less than SINGULAR_MATRIX_EPSILON.
 */
#define SINGULAR_MATRIX_EPSILON		1e-2

#define COLUMN_PRODUCT(m, i, j)		\
	(-m[0][i]*m[0][j] + m[1][i]*m[1][j] + m[2][i]*m[2][j] + m[3][i]*m[3][j])

O31Matrix	O31_identity = {
								{1.0, 0.0, 0.0, 0.0},
								{0.0, 1.0, 0.0, 0.0},
								{0.0, 0.0, 1.0, 0.0},
								{0.0, 0.0, 0.0, 1.0}
							};


void o31_copy(
	O31Matrix	dest,
	O31Matrix	source)
{
	int	i,
		j;

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			dest[i][j] = source[i][j];
}


void o31_invert(
	O31Matrix	m,
	O31Matrix	m_inverse)
{
	/*
	 *	The inverse of an O(3,1) matrix may be computed by taking
	 *	the transpose and then negating both the zeroth row and the
	 *	zeroth column.  The proof follows easily from the fact that
	 *	multiplying an O(3,1) matrix by its transpose is almost the
	 *	same thing as computing the inner product of each pair of
	 *	columns.  (For O(4) matrices, the transpose is precisely
	 *	the inverse, because there are no minus sign in the metric
	 *	to fuss over.)
	 *
	 *	We first write the inverse into the O31Matrix temp, so that if
	 *	the parameters m and m_inverse are the same O31Matrix, we don't
	 *	overwrite m[j][i] before computing m[i][j].
	 */

	int			i,
				j;
	O31Matrix	temp;

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			temp[i][j] = ((i == 0) == (j == 0)) ? m[j][i] : -m[j][i];

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			m_inverse[i][j] = temp[i][j];
}


FuncResult gl4R_invert(
	GL4RMatrix	m,
	GL4RMatrix	m_inverse)
{
	double		row[4][8];
	double		*mm[4],
				*temp_row,
				multiple;
	int			i,
				j,
				k;

	for (i = 0; i < 4; i++)
		mm[i] = row[i];

	/*
	 *	Copy m -- don't alter the original.
	 */
	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			mm[i][j] = m[i][j];

	/*
	 *	Initialize the four right hand columns to the identity.
	 */
	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			mm[i][4 + j] = (i == j) ? 1.0 : 0.0;

	/*
	 *	Forward elimination.
	 */
	for (j = 0; j < 4; j++)
	{
		/*
		 *	Partial pivoting.
		 */
		for (i = j+1; i < 4; i++)
			if (fabs(mm[j][j]) < fabs(mm[i][j]))
			{
				temp_row	= mm[j];
				mm[j]		= mm[i];
				mm[i]		= temp_row;
			}

		/*
		 *	Is the matrix singular?
		 */
		if (fabs(mm[j][j]) < SINGULAR_MATRIX_EPSILON)
			return func_bad_input;

		/*
		 *	Divide through to get a 1.0 on the diagonal.
		 */
		multiple = 1.0 / mm[j][j];
		for (k = j; k < 8; k++)
			mm[j][k] *= multiple;

		/*
		 *	Clear out that column.
		 */
		for (i = j+1; i < 4; i++)
		{
			multiple = mm[i][j];
			for (k = j; k < 8; k++)
				mm[i][k] -= multiple * mm[j][k];
		}
	}

	/*
	 *	Back substitution.
	 */
	for (j = 4; --j >= 0; )
		for (i = j; --i >= 0; )
			for (k = 4; k < 8; k++)
				mm[i][k] -= mm[i][j] * mm[j][k];

	/*
	 *	Copy out the solution.
	 */
	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			m_inverse[i][j] = mm[i][4 + j];

	return func_OK;
}


double gl4R_determinant(
	GL4RMatrix	m)
{
	/*
	 *	Two approaches come to mind for computing a 4 x 4 determinant.
	 *
	 *	(1)	Work out the 4! = 24 terms -- each a product of four
	 *		matrix entries -- and form their alternating sum.
	 *
	 *	(2)	Use Gaussian elimination.
	 *
	 *	I doubt there is much difference in efficiency between the two
	 *	methods, so I've chosen method (2) on the assumption (which I
	 *	haven't thoroughly thought out) that it will be numerically more
	 *	robust.  Numerical effects could be noticeable, because O(3,1)
	 *	matrices tend to have large entires.  On the other hand, the
	 *	determinant will always be plus or minus one, so it's not worth
	 *	getting too concerned about the precision.
	 */

	/*
	 *	gl4R_determinant() no longer calls uFatalError() when the determinant
	 *	is something other than +1 or -1.  This lets compute_approx_volume()
	 *	in Dirichlet_extras.c compute the determinant of matrices which
	 *	are in gl(2,C) but not O(3,1).  Note that matrix_io.c already calls
	 *	O31_determinants_OK() to validate matrices read from files, and
	 *	that SnapPea has had no trouble with determinants of internally
	 *	computed O(3,1) matrices.
	 *
	 *	JRW 94/11/30
	 */

	int			r,
				c,
				cc,
				pivot_row,
				row_swaps;
	double		max_abs,
				this_abs,
				temp,
				factor,
				det;
	O31Matrix	mm;

	/*
	 *	First copy the matrix, to avoid destroying it as
	 *	we compute its determinant.
	 */

	o31_copy(mm, m);

	/*
	 *	Put the matrix in upper triangular form.
	 *
	 *	Count the number of row swaps, so we can get the
	 *	correct sign at the end.
	 *
	 *	Technical comment:  We don't actually write zeros into the
	 *	lower part of the matrix;  we just pretend.
	 */

	row_swaps = 0;

	for (c = 0; c < 4; c++)
	{
		/*
		 *	Find the pivot row.
		 */

		max_abs = -1.0;

		for (r = c; r < 4; r++)
		{
			this_abs = fabs(mm[r][c]);
			if (this_abs > max_abs)
			{
				max_abs = this_abs;
				pivot_row = r;
			}
		}

		if (max_abs == 0.0)
			/*
			 *	The determinant of an O(3,1) matrix should always
			 *	be plus or minus one, never zero.
			 */
			/*	uFatalError("gl4R_determinant", "o31_matrices");	*/
			return 0.0;	/*	JRW  94/11/30  (see explanation above)	*/

		/*
		 *	Swap the pivot row into position.
		 */

		if (pivot_row != c)
		{
			for (cc = c; cc < 4; cc++)
			{
				temp				= mm[c][cc];
				mm[c][cc]			= mm[pivot_row][cc];
				mm[pivot_row][cc]	= temp;
			}
			row_swaps++;
		}

		/*
		 *	Eliminate the entries in column c which lie below the pivot.
		 */

		for (r = c + 1; r < 4; r++)
		{
			factor = - mm[r][c] / mm[c][c];

			for (cc = c + 1; cc < 4; cc++)
				mm[r][cc] += factor * mm[c][cc];
		}
	}

	/*
	 *	The determinant is now the product of the diagonal entries.
	 */

	det = 1.0;

	for (c = 0; c < 4; c++)
		det *= mm[c][c];

	if (row_swaps % 2)
		det = - det;

	/*
	 *	Do a quick error check, just to be safe.
	 *	The determinant of an O31_matrix should be +1 or -1.
	 */

/*
commented out by JRW  94/11/30 (see explanation above)

	if (fabs(fabs(det) - 1.0) > 0.01)
		uFatalError("gl4R_determinant", "o31_matrices");
*/

	return det;
}


void o31_product(
	O31Matrix	a,
	O31Matrix	b,
	O31Matrix	product)
{
	register int	i,
					j,
					k;
	register double	sum;
	O31Matrix		temp;

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
		{
			sum =  0.0;
			for (k = 0; k < 4; k++)
				sum += a[i][k] * b[k][j];
			temp[i][j] = sum;
		}

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			product[i][j] = temp[i][j];
}


Boolean	o31_equal(
	O31Matrix	a,
	O31Matrix	b,
	double		epsilon)
{
	/*
	 *	There are a number of different ways one could decide whether two
	 *	O(3,1) matrices are the same or not.  The fancier ways, such as
	 *	computing the sum of the squares of the differences of corresponding
	 *	entries, are numerically more time consuming.  For now let's just
	 *	check that all entries are equal to within epsilon.  This offers the
	 *	advantage that when scanning down lists, the vast majority of
	 *	matrices are diagnosed as different after the comparision of a
	 *	single pair of numbers.  The epsilon can be fairly large, since to
	 *	qualify as equal, two matrices must have ALL their entries equal to
	 *	within that precision.
	 */

	int	i,
		j;

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			if (fabs(a[i][j] - b[i][j]) > epsilon)
				return FALSE;

	return TRUE;
}


double o31_trace(
	O31Matrix	m)
{
	int		i;
	double	trace;

	trace = 0.0;

	for (i = 0; i < 4; i++)
		trace += m[i][i];

	return trace;
}


double o31_deviation(
	O31Matrix	m)
{
	/*
	 *	The matrix m is, in theory, an element of SO(3,1),
	 *	so the inner product of column i with column j should be
	 *
	 *					-1		if i = j = 0,
	 *					+1		if i = j != 0, or
	 *					 0		if i != j.
	 *
	 *	Return the greatest deviation from these values, so the
	 *	calling function has some idea how precise the matrix is.
	 *
	 *	The simplest way to code this is to multiply the matrix times its
	 *	inverse.  Note that this approach relies on the fact that
	 *	o31_inverse() transposes the matrix and negates the appropriate
	 *	entries.  If o31_inverse() did Gaussian elimination to numerically
	 *	invert the matrix, we'd have to rewrite the following code.
	 */

	O31Matrix	the_inverse,
				the_product;
	double		error,
				max_error;
	int			i,
				j;

	o31_invert(m, the_inverse);
	o31_product(m, the_inverse, the_product);

	max_error = 0.0;

	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
		{
			error = fabs(the_product[i][j] - (i == j ? 1.0 : 0.0));
			if (error > max_error)
				max_error = error;
		}

	return max_error;
}


void o31_GramSchmidt(
	O31Matrix	m)
{
	/*
	 *	Given a matrix m whose columns are almost orthonormal (in the sense
	 *	of O(3,1), not O(4)), use the Gram-Schmidt process to make small
	 *	changes to the matrix entries so that the columns become orthonormal
	 *	to the highest precision possible.
	 */

	int		r,
			c,
			cc;
	double	length,
			length_of_projection;

	for (c = 0; c < 4; c++)
	{
		/*
		 *	Adjust column c to have length -1 (if c == 0) or +1 (if c > 0).
		 *	We are assuming m is already close to being in O(3,1), so
		 *	it suffices to divide column c by sqrt(fabs(length)).
		 */
		length = sqrt(fabs(COLUMN_PRODUCT(m, c, c)));	/* no need for safe_sqrt() */
		for (r = 0; r < 4; r++)
			m[r][c] /= length;

		/*
		 *	We want to make all subsequent columns be orthogonal to column c,
		 *	so subtract off their components in the direction of column c.
		 *	Because column c is now a unit vector, the inner product
		 *	<column c, column cc> gives plus or minus the length of the
		 *	projection of column cc onto column c, according to whether or
		 *	not c == 0.
		 */
		for (cc = c + 1; cc < 4; cc++)
		{
			length_of_projection = COLUMN_PRODUCT(m, c, cc);
			if (c == 0)
				length_of_projection = - length_of_projection;
			for (r = 0; r < 4; r++)
				m[r][cc] -= length_of_projection * m[r][c];
		}
	}
}


void o31_conjugate(
	O31Matrix	m,
	O31Matrix	t,
	O31Matrix	Tmt)
{
	/*
	 *	Replace m with (t^-1) m t.
	 */

	O31Matrix	t_inverse,
				temp;

	o31_invert(t, t_inverse);
	o31_product(t_inverse, m, temp);
	o31_product(temp, t, Tmt);
}


double o31_inner_product(
	O31Vector	u,
	O31Vector	v)
{
	int		i;
	double	sum;

	sum = - u[0]*v[0];

	for (i = 1; i < 4; i++)
		sum += u[i]*v[i];

	return sum;
}


void o31_matrix_times_vector(
	O31Matrix	m,
	O31Vector	v,
	O31Vector	product)
{
	register int	i,
					j;
	register double	sum;
	O31Vector		temp;

	for (i = 0; i < 4; i++)
	{
		sum =  0.0;
		for (j = 0; j < 4; j++)
			sum += m[i][j] * v[j];
		temp[i] = sum;
	}

	for (i = 0; i < 4; i++)
		product[i] = temp[i];
}


void o31_constant_times_vector(
	double		r,
	O31Vector	v,
	O31Vector	product)
{
	int		i;

	for (i = 0; i < 4; i++)
		product[i] = r * v[i];
}


void o31_copy_vector(
	O31Vector	dest,
	O31Vector	source)
{
	int	i;

	for (i = 0; i < 4; i++)
		dest[i] = source[i];
}


void o31_vector_sum(
	O31Vector	a,
	O31Vector	b,
	O31Vector	sum)
{
	int	i;

	for (i = 0; i < 4; i++)
		sum[i] = a[i] + b[i];
}


void o31_vector_diff(
	O31Vector	a,
	O31Vector	b,
	O31Vector	diff)
{
	int	i;

	for (i = 0; i < 4; i++)
		diff[i] = a[i] - b[i];
}