CR2RE Pipeline Reference Manual 1.6.10
hdrl_cat_solve.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2017 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "hdrl_cat_solve.h"
21
22
23/*---------------------------------------------------------------------------*/
31/*----------------------------------------------------------------------------*/
32
35/* ---------------------------------------------------------------------------*/
48/* ---------------------------------------------------------------------------*/
49cpl_error_code hdrl_solve(double a[25][25], double b[25], cpl_size m)
50{
51 cpl_size l = 0;
52 cpl_size iu = m - 1;
53
54 for (cpl_size i = 0; i < iu; i++) {
55
56 /* find largest remaining term in i-th column for pivot */
57 double big = 0.;
58 for (cpl_size k = i; k < m; k++) {
59
60 double rmax = fabs(a[i][k]);
61 if (rmax > big) {
62 big = rmax;
63 l = k;
64 }
65 }
66
67 /* check for non-zero term */
68 if (big == 0.) {
69 for (cpl_size ib = 0; ib < m; ib++) {
70 b[ib] = 0.;
71 }
72 return CPL_ERROR_NONE;
73 }
74
75 if (i != l) {
76
77 /* switch rows */
78 for (cpl_size j = 0; j < m; j++) {
79
80 double temp = a[j][i];
81 a[j][i] = a[j][l];
82 a[j][l] = temp;
83 }
84
85 double temp = b[i];
86 b[i] = b[l];
87 b[l] = temp;
88 }
89
90 /* pivotal reduction */
91 double pivot = a[i][i];
92
93 cpl_size jl = i + 1;
94 for (cpl_size j = jl; j < m; j++) {
95
96 double temp = a[i][j] / pivot;
97
98 b[j] -= temp * b[i];
99
100 for (cpl_size k = i; k < m; k++) {
101 a[k][j] -= temp * a[k][i];
102 }
103 }
104 }
105
106 /* back substitution for solution */
107 for (cpl_size i = 0; i < m; i++) {
108
109 cpl_size ir = m - 1 - i;
110 if (a[ir][ir] != 0.) {
111
112 double temp = b[ir];
113 if (ir != m-1) {
114
115 for (cpl_size j = 1; j <= i; j++) {
116 cpl_size k = m - j;
117 temp -= a[k][ir] * b[k];
118 }
119 }
120
121 b[ir] = temp / a[ir][ir];
122
123 } else {
124
125 b[ir] = 0.;
126 }
127 }
128
129 return CPL_ERROR_NONE;
130}
131
cpl_error_code hdrl_solve(double a[25][25], double b[25], cpl_size m)
Use Gauss-Jordan elimination to solve ax=b.