File: solve_equations.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 (399 lines) | stat: -rw-r--r-- 9,515 bytes parent folder | download | duplicates (12)
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
/*
 *	solve_equations.c
 *
 *	This file provides the functions
 *
 *		FuncResult solve_complex_equations(Complex **complex_equations,
 *						int num_rows, int num_columns, Complex *solution);
 *		FuncResult solve_real_equations(double **real_equations,
 *						int num_rows, int num_columns, double *solution);
 *
 *	which do_Dehn_filling() in hyperbolic_structure.c calls to
 *	solve num_rows linear equations in num_columns variables.  Gaussian
 *	elimination with partial pivoting is used.
 *
 *	The equations are stored as an array of num_rows pointers, each
 *	of which points to an array of (num_columns + 1) entries.  The
 *	last entry in each row is the constant on the right hand side of
 *	the equation.
 *
 *	num_rows is assumed to be greater than or equal to num_columns.
 *	The equations are assumed to have rank exactly equal to num_columns.
 *	Thus, even though there may be more equations than variables, the
 *	equations are assumed to be consistent.
 *
 *	Even though these routines make no assumption about the origin or
 *	purpose of the equations, their main use is, of course, to find
 *	hyperbolic structures.  My hope in including all the equations
 *	(rather than just a linearly independent subset) is that we will
 *	get more accurate solutions, particularly in degenerate or
 *	nearly degenerate situations.
 *
 *	These functions assume an array of num_columns elements has already
 *	been allocated for the solution.
 *
 *	Technical detail:  While doing the Gaussian elimination, these functions
 *	do not actually compute or write values which are guaranteed to be one
 *	or zero, but they knows where they are.  So don't worry that in the end
 *	the matrix does not contain all ones and zeros;  the matrix will be a
 *	mess, but the solution will be correct.
 */

#include "kernel.h"


FuncResult solve_complex_equations(
	Complex	**complex_equations,
	int		num_rows,
	int		num_columns,
	Complex	*solution)
{
	/*
	 *	The following register variables are used in the n^3 bottleneck.
	 *	(See below.)
	 */

	register double 	factor_real,
						factor_imag;
	register Complex	*row_r,
						*row_c;
	register int		count;

	/*
	 *	The remaining variables are used in less critical places.
	 */

	int		r,
			c,
			cc,
			pivot_row = -1;
	double	max_modulus,
			this_modulus,
			max_error,
			error;
	Complex	*temp,
			factor;

	/*
	 *	Forward elimination.
	 */

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

		max_modulus = 0.0;

		for (r = c; r < num_rows; r++)
		{
			this_modulus = complex_modulus(complex_equations[r][c]);
			if (this_modulus > max_modulus)
			{
				max_modulus = this_modulus;
				pivot_row = r;
			}
		}

		if (max_modulus == 0.0)		/* In the old snappea, max_modulus	*/
			return func_failed;		/* was was never below 1e-100, even	*/
									/* in degenerate cases.				*/

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

		temp						 = complex_equations[c];
		complex_equations[c]		 = complex_equations[pivot_row];
		complex_equations[pivot_row] = temp;

		/*
		 *	Multiply the pivot row through by 1.0/(pivot value).
		 */

		factor = complex_div(One, complex_equations[c][c]);

		for (cc = c + 1; cc <= num_columns; cc++)
			complex_equations[c][cc] = complex_mult(
				factor,
				complex_equations[c][cc]
			);

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

		for (r = c + 1; r < num_rows; r++)
		{
			/*
			 *	The following loop is the bottleneck for computing
			 *	hyperbolic structures.  It is executed n^3 times to solve
			 *	an n x n system of equations, and no other n^3 algorithms
			 *	are used.  For this reason, I've written the loop to
			 *	maximize speed at the expense of readability.
			 *
			 *	Here's the loop in pseudocode:
			 *

			for (cc = c + 1; cc <= num_columns; cc++)
				complex_equations[r][cc]
					-= complex_equations[r][c] * complex_equations[c][cc]

			 *
			 *	Here's a version that will actually run:
			 *

			for (cc = c + 1; cc <= num_columns; cc++)
				complex_equations[r][cc] = complex_minus(
					complex_equations[r][cc],
					complex_mult(
						complex_equations[r][c],
						complex_equations[c][cc]
					)
				);

			 *
			 *	And here's the fancy, built-for-speed version:
			 */

			factor_real = - complex_equations[r][c].real;
			factor_imag = - complex_equations[r][c].imag;

			if (factor_real || factor_imag)
			{
				row_r = complex_equations[r] + c + 1;
				row_c = complex_equations[c] + c + 1;

				for (count = num_columns - c; --count >= 0; )
				{
					if (row_c->real || row_c->imag)
					{
						row_r->real +=
							factor_real * row_c->real
						  - factor_imag * row_c->imag;
						row_r->imag +=
							factor_real * row_c->imag
						  + factor_imag * row_c->real;
					}
					row_r++;
					row_c++;
				}
			}

			/*
			 *	With all the THINK C compiler's optimization options on,
			 *	the fancy version runs 8 times faster than the plain
			 *	version.  With all THINK C optimization options off,
			 *	it runs 9 times faster.  The THINK C optimizer increases
			 *	the speed of the fancy code by only 6%.
			 */

			/*
			 *	Yield some time to the window system, and check
			 *	whether the user has cancelled this computation.
			 */

			if (uLongComputationContinues() == func_cancelled)
				return func_cancelled;
		}
	}


	/*
	 *	Back substitution.
	 */

	for (c = num_columns; --c > 0; )	/*	Do columns (num_columns - 1) to 1,	*/
										/*		but skip column 0.				*/
		for (r = c; --r >= 0; )			/*	Do rows (c - 1) to 0.				*/

			complex_equations[r][num_columns] = complex_minus(
				complex_equations[r][num_columns],
				complex_mult(
					complex_equations[r][c],
					complex_equations[c][num_columns]
				)
			);

	/*
	 *	Check "extra" rows for consistency.
	 *	That is, in each of the last (num_rows - num_columns) rows,
	 *	check that the constant on the right hand side is zero.
	 *	This will give us a measure of the accuracy of the solution.
	 *	I still haven't decided what to do with this number.
	 */

	max_error = 0.0;

	for (r = num_columns; r < num_rows; r++)
	{
		error = complex_modulus(complex_equations[r][num_columns]);
		if (error > max_error)
			max_error = error;
	}

	/*
	 *	Record the solution.
	 */

	for (r = 0; r < num_columns; r++)
		solution[r] = complex_equations[r][num_columns];

	return func_OK;
}


FuncResult solve_real_equations(
	double	**real_equations,
	int		num_rows,
	int		num_columns,
	double	*solution)
{
	/*
	 *	The following register variables are used in the n^3 bottleneck.
	 *	(See below.)
	 */

	register double factor,
					*row_r,
					*row_c;
	register int	count;

	/*
	 *	The remaining variables are used in less critical places.
	 */

	int		r,
			c,
			cc,
			pivot_row = -1;
	double	max_abs,
			this_abs,
			max_error,
			error,
			*temp;

	/*
	 *	Forward elimination.
	 */

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

		max_abs = 0.0;

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

		if (max_abs == 0.0)
			return func_failed;

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

		temp						= real_equations[c];
		real_equations[c]			= real_equations[pivot_row];
		real_equations[pivot_row]	= temp;

		/*
		 *	Multiply the pivot row through by 1.0/(pivot value).
		 */

		factor = 1.0 / real_equations[c][c];

		for (cc = c + 1; cc <= num_columns; cc++)
			real_equations[c][cc] *= factor;

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

		for (r = c + 1; r < num_rows; r++)
		{
			factor = - real_equations[r][c];

			/*
			 *	The following loop is the bottleneck for computing
			 *	hyperbolic structures.  It is executed n^3 times to solve
			 *	an n x n system of equations, and no other n^3 algorithms
			 *	are used.  For this reason, I've written the loop to
			 *	maximize speed at the expense of readability.
			 *
			 *	Here's the loop in its humanly comprehensible form:
			 *

			if (factor)
				for (cc = c + 1; cc <= num_columns; cc++)
					real_equations[r][cc] += factor * real_equations[c][cc];

			 *
			 *	Here's the optimized version of the same thing:
			 */

			if (factor)
			{
				row_r = real_equations[r] + c + 1;
				row_c = real_equations[c] + c + 1;
				for (count = num_columns - c; --count>=0; )
					*row_r++ += factor * *row_c++;
			}

			/*
			 *	Yield some time to the window system, and check
			 *	whether the user has cancelled this computation.
			 */

			if (uLongComputationContinues() == func_cancelled)
				return func_cancelled;
		}
	}


	/*
	 *	Back substitution.
	 */

	for (c = num_columns; --c > 0; )	/*	Do columns (num_columns - 1) to 1,	*/
										/*		but skip column 0.				*/
		for (r = c; --r >= 0; )			/*	Do rows (c - 1) to 0.				*/

			real_equations[r][num_columns] -= real_equations[r][c] * real_equations[c][num_columns];

	/*
	 *	Check "extra" rows for consistency.
	 *	That is, in each of the last (num_rows - num_columns) rows,
	 *	check that the constant on the right hand side is zero.
	 *	This will give us a measure of the accuracy of the solution.
	 *	I still haven't decided what to do with this number.
	 */

	max_error = 0.0;

	for (r = num_columns; r < num_rows; r++)
	{
		error = fabs(real_equations[r][num_columns]);
		if (error > max_error)
			max_error = error;
	}

	/*
	 *	Record the solution.
	 */

	for (r = 0; r < num_columns; r++)
		solution[r] = real_equations[r][num_columns];

	return func_OK;
}