1 /*
2 * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 #ifndef AOM_AOM_DSP_MATHUTILS_H_
13 #define AOM_AOM_DSP_MATHUTILS_H_
14
15 #include <assert.h>
16 #include <math.h>
17 #include <string.h>
18
19 #include "aom_dsp/aom_dsp_common.h"
20 #include "aom_mem/aom_mem.h"
21
22 static const double TINY_NEAR_ZERO = 1.0E-16;
23
24 // Solves Ax = b, where x and b are column vectors of size nx1 and A is nxn
linsolve(int n,double * A,int stride,double * b,double * x)25 static INLINE int linsolve(int n, double *A, int stride, double *b, double *x) {
26 int i, j, k;
27 double c;
28 // Forward elimination
29 for (k = 0; k < n - 1; k++) {
30 // Bring the largest magnitude to the diagonal position
31 for (i = n - 1; i > k; i--) {
32 if (fabs(A[(i - 1) * stride + k]) < fabs(A[i * stride + k])) {
33 for (j = 0; j < n; j++) {
34 c = A[i * stride + j];
35 A[i * stride + j] = A[(i - 1) * stride + j];
36 A[(i - 1) * stride + j] = c;
37 }
38 c = b[i];
39 b[i] = b[i - 1];
40 b[i - 1] = c;
41 }
42 }
43 for (i = k; i < n - 1; i++) {
44 if (fabs(A[k * stride + k]) < TINY_NEAR_ZERO) return 0;
45 c = A[(i + 1) * stride + k] / A[k * stride + k];
46 for (j = 0; j < n; j++) A[(i + 1) * stride + j] -= c * A[k * stride + j];
47 b[i + 1] -= c * b[k];
48 }
49 }
50 // Backward substitution
51 for (i = n - 1; i >= 0; i--) {
52 if (fabs(A[i * stride + i]) < TINY_NEAR_ZERO) return 0;
53 c = 0;
54 for (j = i + 1; j <= n - 1; j++) c += A[i * stride + j] * x[j];
55 x[i] = (b[i] - c) / A[i * stride + i];
56 }
57
58 return 1;
59 }
60
61 ////////////////////////////////////////////////////////////////////////////////
62 // Least-squares
63 // Solves for n-dim x in a least squares sense to minimize |Ax - b|^2
64 // The solution is simply x = (A'A)^-1 A'b or simply the solution for
65 // the system: A'A x = A'b
least_squares(int n,double * A,int rows,int stride,double * b,double * scratch,double * x)66 static INLINE int least_squares(int n, double *A, int rows, int stride,
67 double *b, double *scratch, double *x) {
68 int i, j, k;
69 double *scratch_ = NULL;
70 double *AtA, *Atb;
71 if (!scratch) {
72 scratch_ = (double *)aom_malloc(sizeof(*scratch) * n * (n + 1));
73 scratch = scratch_;
74 }
75 AtA = scratch;
76 Atb = scratch + n * n;
77
78 for (i = 0; i < n; ++i) {
79 for (j = i; j < n; ++j) {
80 AtA[i * n + j] = 0.0;
81 for (k = 0; k < rows; ++k)
82 AtA[i * n + j] += A[k * stride + i] * A[k * stride + j];
83 AtA[j * n + i] = AtA[i * n + j];
84 }
85 Atb[i] = 0;
86 for (k = 0; k < rows; ++k) Atb[i] += A[k * stride + i] * b[k];
87 }
88 int ret = linsolve(n, AtA, n, Atb, x);
89 aom_free(scratch_);
90 return ret;
91 }
92
93 // Matrix multiply
multiply_mat(const double * m1,const double * m2,double * res,const int m1_rows,const int inner_dim,const int m2_cols)94 static INLINE void multiply_mat(const double *m1, const double *m2, double *res,
95 const int m1_rows, const int inner_dim,
96 const int m2_cols) {
97 double sum;
98
99 int row, col, inner;
100 for (row = 0; row < m1_rows; ++row) {
101 for (col = 0; col < m2_cols; ++col) {
102 sum = 0;
103 for (inner = 0; inner < inner_dim; ++inner)
104 sum += m1[row * inner_dim + inner] * m2[inner * m2_cols + col];
105 *(res++) = sum;
106 }
107 }
108 }
109
110 //
111 // The functions below are needed only for homography computation
112 // Remove if the homography models are not used.
113 //
114 ///////////////////////////////////////////////////////////////////////////////
115 // svdcmp
116 // Adopted from Numerical Recipes in C
117
apply_sign(double a,double b)118 static INLINE double apply_sign(double a, double b) {
119 return ((b) >= 0 ? fabs(a) : -fabs(a));
120 }
121
pythag(double a,double b)122 static INLINE double pythag(double a, double b) {
123 double ct;
124 const double absa = fabs(a);
125 const double absb = fabs(b);
126
127 if (absa > absb) {
128 ct = absb / absa;
129 return absa * sqrt(1.0 + ct * ct);
130 } else {
131 ct = absa / absb;
132 return (absb == 0) ? 0 : absb * sqrt(1.0 + ct * ct);
133 }
134 }
135
svdcmp(double ** u,int m,int n,double w[],double ** v)136 static INLINE int svdcmp(double **u, int m, int n, double w[], double **v) {
137 const int max_its = 30;
138 int flag, i, its, j, jj, k, l, nm;
139 double anorm, c, f, g, h, s, scale, x, y, z;
140 double *rv1 = (double *)aom_malloc(sizeof(*rv1) * (n + 1));
141 g = scale = anorm = 0.0;
142 for (i = 0; i < n; i++) {
143 l = i + 1;
144 rv1[i] = scale * g;
145 g = s = scale = 0.0;
146 if (i < m) {
147 for (k = i; k < m; k++) scale += fabs(u[k][i]);
148 if (scale != 0.) {
149 for (k = i; k < m; k++) {
150 u[k][i] /= scale;
151 s += u[k][i] * u[k][i];
152 }
153 f = u[i][i];
154 g = -apply_sign(sqrt(s), f);
155 h = f * g - s;
156 u[i][i] = f - g;
157 for (j = l; j < n; j++) {
158 for (s = 0.0, k = i; k < m; k++) s += u[k][i] * u[k][j];
159 f = s / h;
160 for (k = i; k < m; k++) u[k][j] += f * u[k][i];
161 }
162 for (k = i; k < m; k++) u[k][i] *= scale;
163 }
164 }
165 w[i] = scale * g;
166 g = s = scale = 0.0;
167 if (i < m && i != n - 1) {
168 for (k = l; k < n; k++) scale += fabs(u[i][k]);
169 if (scale != 0.) {
170 for (k = l; k < n; k++) {
171 u[i][k] /= scale;
172 s += u[i][k] * u[i][k];
173 }
174 f = u[i][l];
175 g = -apply_sign(sqrt(s), f);
176 h = f * g - s;
177 u[i][l] = f - g;
178 for (k = l; k < n; k++) rv1[k] = u[i][k] / h;
179 for (j = l; j < m; j++) {
180 for (s = 0.0, k = l; k < n; k++) s += u[j][k] * u[i][k];
181 for (k = l; k < n; k++) u[j][k] += s * rv1[k];
182 }
183 for (k = l; k < n; k++) u[i][k] *= scale;
184 }
185 }
186 anorm = fmax(anorm, (fabs(w[i]) + fabs(rv1[i])));
187 }
188
189 for (i = n - 1; i >= 0; i--) {
190 if (i < n - 1) {
191 if (g != 0.) {
192 for (j = l; j < n; j++) v[j][i] = (u[i][j] / u[i][l]) / g;
193 for (j = l; j < n; j++) {
194 for (s = 0.0, k = l; k < n; k++) s += u[i][k] * v[k][j];
195 for (k = l; k < n; k++) v[k][j] += s * v[k][i];
196 }
197 }
198 for (j = l; j < n; j++) v[i][j] = v[j][i] = 0.0;
199 }
200 v[i][i] = 1.0;
201 g = rv1[i];
202 l = i;
203 }
204 for (i = AOMMIN(m, n) - 1; i >= 0; i--) {
205 l = i + 1;
206 g = w[i];
207 for (j = l; j < n; j++) u[i][j] = 0.0;
208 if (g != 0.) {
209 g = 1.0 / g;
210 for (j = l; j < n; j++) {
211 for (s = 0.0, k = l; k < m; k++) s += u[k][i] * u[k][j];
212 f = (s / u[i][i]) * g;
213 for (k = i; k < m; k++) u[k][j] += f * u[k][i];
214 }
215 for (j = i; j < m; j++) u[j][i] *= g;
216 } else {
217 for (j = i; j < m; j++) u[j][i] = 0.0;
218 }
219 ++u[i][i];
220 }
221 for (k = n - 1; k >= 0; k--) {
222 for (its = 0; its < max_its; its++) {
223 flag = 1;
224 for (l = k; l >= 0; l--) {
225 nm = l - 1;
226 if ((double)(fabs(rv1[l]) + anorm) == anorm || nm < 0) {
227 flag = 0;
228 break;
229 }
230 if ((double)(fabs(w[nm]) + anorm) == anorm) break;
231 }
232 if (flag) {
233 c = 0.0;
234 s = 1.0;
235 for (i = l; i <= k; i++) {
236 f = s * rv1[i];
237 rv1[i] = c * rv1[i];
238 if ((double)(fabs(f) + anorm) == anorm) break;
239 g = w[i];
240 h = pythag(f, g);
241 w[i] = h;
242 h = 1.0 / h;
243 c = g * h;
244 s = -f * h;
245 for (j = 0; j < m; j++) {
246 y = u[j][nm];
247 z = u[j][i];
248 u[j][nm] = y * c + z * s;
249 u[j][i] = z * c - y * s;
250 }
251 }
252 }
253 z = w[k];
254 if (l == k) {
255 if (z < 0.0) {
256 w[k] = -z;
257 for (j = 0; j < n; j++) v[j][k] = -v[j][k];
258 }
259 break;
260 }
261 if (its == max_its - 1) {
262 aom_free(rv1);
263 return 1;
264 }
265 assert(k > 0);
266 x = w[l];
267 nm = k - 1;
268 y = w[nm];
269 g = rv1[nm];
270 h = rv1[k];
271 f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
272 g = pythag(f, 1.0);
273 f = ((x - z) * (x + z) + h * ((y / (f + apply_sign(g, f))) - h)) / x;
274 c = s = 1.0;
275 for (j = l; j <= nm; j++) {
276 i = j + 1;
277 g = rv1[i];
278 y = w[i];
279 h = s * g;
280 g = c * g;
281 z = pythag(f, h);
282 rv1[j] = z;
283 c = f / z;
284 s = h / z;
285 f = x * c + g * s;
286 g = g * c - x * s;
287 h = y * s;
288 y *= c;
289 for (jj = 0; jj < n; jj++) {
290 x = v[jj][j];
291 z = v[jj][i];
292 v[jj][j] = x * c + z * s;
293 v[jj][i] = z * c - x * s;
294 }
295 z = pythag(f, h);
296 w[j] = z;
297 if (z != 0.) {
298 z = 1.0 / z;
299 c = f * z;
300 s = h * z;
301 }
302 f = c * g + s * y;
303 x = c * y - s * g;
304 for (jj = 0; jj < m; jj++) {
305 y = u[jj][j];
306 z = u[jj][i];
307 u[jj][j] = y * c + z * s;
308 u[jj][i] = z * c - y * s;
309 }
310 }
311 rv1[l] = 0.0;
312 rv1[k] = f;
313 w[k] = x;
314 }
315 }
316 aom_free(rv1);
317 return 0;
318 }
319
SVD(double * U,double * W,double * V,double * matx,int M,int N)320 static INLINE int SVD(double *U, double *W, double *V, double *matx, int M,
321 int N) {
322 // Assumes allocation for U is MxN
323 double **nrU = (double **)aom_malloc((M) * sizeof(*nrU));
324 double **nrV = (double **)aom_malloc((N) * sizeof(*nrV));
325 int problem, i;
326
327 problem = !(nrU && nrV);
328 if (!problem) {
329 for (i = 0; i < M; i++) {
330 nrU[i] = &U[i * N];
331 }
332 for (i = 0; i < N; i++) {
333 nrV[i] = &V[i * N];
334 }
335 } else {
336 if (nrU) aom_free(nrU);
337 if (nrV) aom_free(nrV);
338 return 1;
339 }
340
341 /* copy from given matx into nrU */
342 for (i = 0; i < M; i++) {
343 memcpy(&(nrU[i][0]), matx + N * i, N * sizeof(*matx));
344 }
345
346 /* HERE IT IS: do SVD */
347 if (svdcmp(nrU, M, N, W, nrV)) {
348 aom_free(nrU);
349 aom_free(nrV);
350 return 1;
351 }
352
353 /* aom_free Numerical Recipes arrays */
354 aom_free(nrU);
355 aom_free(nrV);
356
357 return 0;
358 }
359
360 #endif // AOM_AOM_DSP_MATHUTILS_H_
361