Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
gaussian_elimination.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2017 Heiko Bauke.
4 Copyright (C) 2018-2020, 2025-2026 Aurélien PIERRE.
5 Copyright (C) 2019 luzpaz.
6 Copyright (C) 2020 Pascal Obry.
7 Copyright (C) 2022 Martin Bařinka.
8 Copyright (C) 2022 Miloš Komarčević.
9 Copyright (C) 2023 Luca Zulberti.
10 Copyright (C) 2024 Alynx Zhou.
11
12 darktable is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 darktable is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with darktable. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26/*
27 These routines can be used to solve full-rank linear systems of
28 equations by Gaussian elimination with partial pivoting. The
29 functions gauss_make_triangular and gauss_solve have been adopted
30 from Fortran routines as presented in the book "Numerik" by
31 Helmuth Späth, Vieweg Verlag, 1994, see also
32 http://dx.doi.org/10.1007/978-3-322-89220-1
33
34*/
35
36
37#ifndef DT_MATH_GAUSSIAN_ELIMINATION_H
38#define DT_MATH_GAUSSIAN_ELIMINATION_H
39
40#include <math.h>
42#include <stdlib.h>
43
44// Gaussian elimination with partial vivoting
45// after function call the square matrix A is triangular
46// vector p keeps track of row swaps
47// returns 0 if matrix A is singular
48// matrix elements are stored in row-major order
49static int gauss_make_triangular(double *A, int *p, int n)
50{
51 p[n - 1] = n - 1; // we never swap from the last row
52 for(int k = 0; k < n; ++k)
53 {
54 // find pivot element for row swap
55 int m = k;
56 for(int i = k + 1; i < n; ++i)
57 if(fabs(A[k + n * i]) > fabs(A[k + n * m])) m = i;
58 p[k] = m; // rows k and m are swapped
59 // eliminate elements and swap rows
60 double t1 = A[k + n * m];
61 A[k + n * m] = A[k + n * k];
62 A[k + n * k] = t1; // new diagonal elements are (implicitly) one, store scaling factors on diagonal
63 if(t1 != 0)
64 {
65 for(int i = k + 1; i < n; ++i) A[k + n * i] /= -t1;
66 // swap rows
67 if(k != m)
68 for(int i = k + 1; i < n; ++i)
69 {
70 double t2 = A[i + n * m];
71 A[i + n * m] = A[i + n * k];
72 A[i + n * k] = t2;
73 }
74 for(int j = k + 1; j < n; ++j)
75 for(int i = k + 1; i < n; ++i) A[i + n * j] += A[k + j * n] * A[i + k * n];
76 }
77 else
78 // the matrix is singular
79 return 0;
80 }
81 return 1;
82}
83
84// backward substritution after Gaussian elimination
85static void gauss_solve_triangular(const double *A, const int *p, double *b, int n)
86{
87 // permute and rescale elements of right-hand-side
88 for(int k = 0; k < n - 1; ++k)
89 {
90 int m = p[k];
91 double t = b[m];
92 b[m] = b[k];
93 b[k] = t;
94 for(int i = k + 1; i < n; ++i) b[i] += A[k + n * i] * t;
95 }
96 // perform backward substritution
97 for(int k = n - 1; k > 0; --k)
98 {
99 b[k] /= A[k + n * k];
100 double t = b[k];
101 for(int i = 0; i < k; ++i) b[i] -= A[k + n * i] * t;
102 }
103 b[0] /= A[0 + 0 * n];
104}
105
106static int gauss_solve(double *A, double *b, int n)
107{
108 int *p = malloc(n * sizeof(*p));
109 int err_code = 1;
110 if((err_code = gauss_make_triangular(A, p, n))) gauss_solve_triangular(A, p, b, n);
111 dt_free(p);
112 return err_code;
113}
114
115
116static inline int transpose_dot_matrix(double *const restrict A, // input
117 double *const restrict A_square, // output
118 const size_t m, const size_t n)
119{
120 // Construct the square symmetrical definite positive matrix A' A,
121
122 for(size_t i = 0; i < n; ++i)
123 for(size_t j = 0; j < n; ++j)
124 {
125 double sum = 0.0;
126 for(size_t k = 0; k < m; ++k)
127 sum += A[k * n + i] * A[k * n + j];
128
129 A_square[i * n + j] = sum;
130 }
131
132 return 0;
133}
134
135
136static inline int transpose_dot_vector(double *const restrict A, // input
137 double *const restrict y, // input
138 double *const restrict y_square, // output
139 const size_t m, const size_t n)
140{
141 // Construct the vector A' y
142 for(size_t i = 0; i < n; ++i)
143 {
144 double sum = 0.0;
145 for(size_t k = 0; k < m; ++k)
146 sum += A[k * n + i] * y[k];
147
148 y_square[i] = sum;
149 }
150
151 return 0;
152}
153
154
155static inline int pseudo_solve_gaussian(double *const restrict A,
156 double *const restrict y,
157 const size_t m, const size_t n, const int checks)
158{
159 // Solve the weighted linear problem w A'A x = w A' y with the over-constrained rectanguler matrix A
160 // of dimension m x n (m >= n) and w a vector of weights, by the least squares method
161 int err = 0;
162
163 if(m < n)
164 {
165 fprintf(stderr, "pseudo solve: cannot cast %" G_GSIZE_FORMAT " \303\227 %" G_GSIZE_FORMAT " matrix\n", m, n);
166 return 1;
167 }
168
169 double *const restrict A_square = dt_pixelpipe_cache_alloc_align_cache(n * n * sizeof(double), 0);
170 double *const restrict y_square = dt_pixelpipe_cache_alloc_align_cache(n * sizeof(double), 0);
171
172 if(IS_NULL_PTR(y_square) || IS_NULL_PTR(A_square))
173 {
174 err = 1;
175 goto error;
176 }
177
178 #ifdef _OPENMP
179 #pragma omp parallel sections
180 #endif
181 {
182 #ifdef _OPENMP
183 #pragma omp section
184 #endif
185 {
186 // Prepare the least squares matrix = A' A
187 transpose_dot_matrix(A, A_square, m, n);
188 }
189
190 #ifdef _OPENMP
191 #pragma omp section
192 #endif
193 {
194 // Prepare the y square vector = A' y
195 transpose_dot_vector(A, y, y_square, m, n);
196 }
197 }
198
199 // Solve A' A x = A' y for x
200 if(gauss_solve(A_square, y_square, n) == 0)
201 {
202 err = 1;
203 goto error;
204 }
205 for(size_t k = 0; k < n; k++) y[k] = y_square[k];
206
207error:;
210
211 return err;
212}
213
214#endif // DT_MATH_GAUSSIAN_ELIMINATION_H
215
216// clang-format off
217// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
218// vim: shiftwidth=2 expandtab tabstop=2 cindent
219// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
220// clang-format on
static void error(char *msg)
Definition ashift_lsd.c:202
#define m
Definition basecurve.c:283
const int t
#define A(y, x)
static int pseudo_solve_gaussian(double *const restrict A, double *const restrict y, const size_t m, const size_t n, const int checks)
static int transpose_dot_matrix(double *const restrict A, double *const restrict A_square, const size_t m, const size_t n)
static int gauss_make_triangular(double *A, int *p, int n)
static void gauss_solve_triangular(const double *A, const int *p, double *b, int n)
static int gauss_solve(double *A, double *b, int n)
static int transpose_dot_vector(double *const restrict A, double *const restrict y, double *const restrict y_square, const size_t m, const size_t n)
float *const restrict const size_t k
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
#define dt_pixelpipe_cache_alloc_align_cache(size, id)
#define dt_pixelpipe_cache_free_align(mem)