VIRCAM Pipeline 2.3.12
solve.c
1/*
2
3$Id: solve.c,v 1.3 2015/09/22 15:09:20 jim Exp $
4
5* This file is part of the CASU Pipeline utilities
6* Copyright (C) 2015 European Southern Observatory
7*
8* This program is free software; you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation; either version 2 of the License, or
11* (at your option) any later version.
12*
13* This program is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16* GNU General Public License for more details.
17*
18* You should have received a copy of the GNU General Public License
19* along with this program; if not, write to the Free Software
20* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22#include <stdio.h>
23#include <stdlib.h>
24
25#include "imcore.h"
26#include "floatmath.h"
27#include "util.h"
28
29/* gauss elimination to solve ax=b */
30
31extern void imcore_solve (double a[25][25], double b[25], int m) {
32 double temp, big, pivot, rmax;
33 int i, iu, j, k, l = 0, jl, ib, ir;
34
35 iu = m-1;
36 for(i = 0; i < iu; i++) {
37 big = 0.0;
38
39 /* find largest remaining term in ith column for pivot */
40 for(k = i; k < m; k++) {
41 rmax = fabs(a[i][k]);
42 if(rmax > big) {
43 big = rmax;
44 l = k;
45 }
46 }
47
48 /* check for non-zero term */
49 if(big == 0.0) {
50 for(ib = 0; ib < m; ib++) b[ib] = 0.0;
51/* fprintf(stderr, "solve: Zero determinant\n"); */
52 return;
53 }
54
55 if(i != l) {
56 /* switch rows */
57 for(j = 0; j < m; j++) {
58 temp = a[j][i];
59 a[j][i] = a[j][l];
60 a[j][l] = temp;
61 }
62 temp = b[i];
63 b[i] = b[l];
64 b[l] = temp;
65 }
66
67 /* pivotal reduction */
68 pivot = a[i][i];
69 jl = i+1;
70
71 for(j = jl; j < m; j++) {
72 temp = a[i][j]/pivot;
73 b[j] -= temp*b[i];
74 for(k = i; k < m; k++) a[k][j] -= temp*a[k][i];
75 }
76 }
77
78 /* back substitution for solution */
79 for(i = 0; i < m; i++) {
80 ir = m-1-i;
81 if(a[ir][ir] != 0.0) {
82 temp = b[ir];
83 if(ir != m-1) {
84 for(j = 1; j <= i; j++) {
85 k = m-j;
86 temp -= a[k][ir]*b[k];
87 }
88 }
89 b[ir] = temp/a[ir][ir];
90 }
91 else
92 b[ir] = 0.0;
93 }
94}
95
96/*
97
98$Log: solve.c,v $
99Revision 1.3 2015/09/22 15:09:20 jim
100Fixed guards and comments
101
102Revision 1.2 2015/08/12 11:16:55 jim
103Modified procedure names to protect namespace
104
105Revision 1.1.1.1 2015/06/12 10:44:32 jim
106Initial import
107
108Revision 1.2 2014/04/09 09:09:51 jim
109Detabbed
110
111Revision 1.1.1.1 2013/08/27 12:07:48 jim
112Imported
113
114
115*/