File: frinv.c

package info (click to toggle)
modemp3d 0.1-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,688 kB
  • ctags: 1,472
  • sloc: ansic: 12,007; sh: 2,838; makefile: 341; yacc: 285; sed: 93; lex: 33; perl: 31; xml: 10
file content (127 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download | duplicates (14)
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
/*
 * Matrix operations library
 *
 * Copyright (C) 1999-2000
 *   Thomas Sailer, <sailer@ife.ee.ethz.ch>
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* AIX requires this to be the first thing in the file.  */
#ifndef __GNUC__
# if HAVE_ALLOCA_H
#  include <alloca.h>
# else
#  ifdef _AIX
#pragma alloca
#  else
#   ifndef alloca /* predefined by HP cc +Olibcalls */
char *alloca ();
#   endif
#  endif
# endif
#endif

#include <math.h>
#include <string.h>
#include "mat.h"


/*
 * Golub/van Loan, 3.1.3, p 112; PA=LU factorization with partial pivoting
 */

#define exch(x,y)  do { float z; z = (x); (x) = (y); (y) = z; } while (0)


void frlufact(float *u, unsigned int *p, const float *a, unsigned int d)
{
	unsigned int i, j, k, mu;
	float f1, f2;

	if (u != a)
		memcpy(u, a, d*d*sizeof(u[0]));
	for (k = 0; k < d-1; k++) {
		/* search pivot index */
		for (f1 = 0, i = mu = k; i < d; i++) {
			f2 = fabs(u[i*d+k]);
			if (f2 > f1) {
				f1 = f2;
				mu = i;
			}
		}
		/* exchange rows */
		p[k] = mu;
		for (i = k; i < d; i++)
			exch(u[k*d+i], u[mu*d+i]);
		f1 = u[k*d+k];
		if (f1 != 0) {
			f1 = 1 / f1;
			for (i = k+1; i < d; i++)
				u[i*d+k] *= f1;
			for (i = k+1; i < d; i++)
				for (j = k+1; j < d; j++)
					u[i*d+j] -= u[i*d+k] * u[k*d+j];
		}
	}
}

void frlusolve(float *x, const float *b, const float *u, const unsigned int *p, unsigned int d)
{
	float *y, s;
	unsigned int k, i;

	y = alloca(d * sizeof(y[0]));
	memcpy(y, b, d * sizeof(y[0]));
	for (k = 0; k < d-1; k++) {
		i = p[k];
		if (i != k)
			exch(y[k], y[i]);
		if (y[k] == 0)
			continue;
		for (i = k+1; i < d; i++)
			y[i] -= y[k] * u[i*d+k];
	}
	/* solve Ux=y */
	for (k = d; k > 0; k--) {
		s = y[k-1];
		for (i = k; i < d; i++)
			s -= u[(k-1)*d+i] * x[i];
		x[k-1] = s / u[(k-1)*d+(k-1)];
	}
}

void frinv(float *ainv, const float *a, unsigned d)
{
	float *u, *y;
	unsigned int *p;
	unsigned int k;

	u = alloca(d * d * sizeof(u[0]));
	p = alloca((d-1) * sizeof(p[0]));
	frlufact(u, p, a, d);
	for (k = 0; k < d; k++) {
		y = &ainv[k*d];
		memset(y, 0, d * sizeof(y[0]));
		y[k] = 1;
		frlusolve(y, y, u, p, d);
	}
	frtranspose(ainv, ainv, d, d);
}