1 #include <iostream>
2 #include "precomp.hpp"
3 #include "epnp.h"
4
5 namespace cv
6 {
7
epnp(const Mat & cameraMatrix,const Mat & opoints,const Mat & ipoints)8 epnp::epnp(const Mat& cameraMatrix, const Mat& opoints, const Mat& ipoints)
9 {
10 if (cameraMatrix.depth() == CV_32F)
11 init_camera_parameters<float>(cameraMatrix);
12 else
13 init_camera_parameters<double>(cameraMatrix);
14
15 number_of_correspondences = std::max(opoints.checkVector(3, CV_32F), opoints.checkVector(3, CV_64F));
16
17 pws.resize(3 * number_of_correspondences);
18 us.resize(2 * number_of_correspondences);
19
20 if (opoints.depth() == ipoints.depth())
21 {
22 if (opoints.depth() == CV_32F)
23 init_points<Point3f,Point2f>(opoints, ipoints);
24 else
25 init_points<Point3d,Point2d>(opoints, ipoints);
26 }
27 else if (opoints.depth() == CV_32F)
28 init_points<Point3f,Point2d>(opoints, ipoints);
29 else
30 init_points<Point3d,Point2f>(opoints, ipoints);
31
32 alphas.resize(4 * number_of_correspondences);
33 pcs.resize(3 * number_of_correspondences);
34
35 max_nr = 0;
36 A1 = NULL;
37 A2 = NULL;
38 }
39
~epnp()40 epnp::~epnp()
41 {
42 if (A1)
43 delete[] A1;
44 if (A2)
45 delete[] A2;
46 }
47
choose_control_points(void)48 void epnp::choose_control_points(void)
49 {
50 // Take C0 as the reference points centroid:
51 cws[0][0] = cws[0][1] = cws[0][2] = 0;
52 for(int i = 0; i < number_of_correspondences; i++)
53 for(int j = 0; j < 3; j++)
54 cws[0][j] += pws[3 * i + j];
55
56 for(int j = 0; j < 3; j++)
57 cws[0][j] /= number_of_correspondences;
58
59
60 // Take C1, C2, and C3 from PCA on the reference points:
61 CvMat * PW0 = cvCreateMat(number_of_correspondences, 3, CV_64F);
62
63 double pw0tpw0[3 * 3], dc[3], uct[3 * 3];
64 CvMat PW0tPW0 = cvMat(3, 3, CV_64F, pw0tpw0);
65 CvMat DC = cvMat(3, 1, CV_64F, dc);
66 CvMat UCt = cvMat(3, 3, CV_64F, uct);
67
68 for(int i = 0; i < number_of_correspondences; i++)
69 for(int j = 0; j < 3; j++)
70 PW0->data.db[3 * i + j] = pws[3 * i + j] - cws[0][j];
71
72 cvMulTransposed(PW0, &PW0tPW0, 1);
73 cvSVD(&PW0tPW0, &DC, &UCt, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
74
75 cvReleaseMat(&PW0);
76
77 for(int i = 1; i < 4; i++) {
78 double k = sqrt(dc[i - 1] / number_of_correspondences);
79 for(int j = 0; j < 3; j++)
80 cws[i][j] = cws[0][j] + k * uct[3 * (i - 1) + j];
81 }
82 }
83
compute_barycentric_coordinates(void)84 void epnp::compute_barycentric_coordinates(void)
85 {
86 double cc[3 * 3], cc_inv[3 * 3];
87 CvMat CC = cvMat(3, 3, CV_64F, cc);
88 CvMat CC_inv = cvMat(3, 3, CV_64F, cc_inv);
89
90 for(int i = 0; i < 3; i++)
91 for(int j = 1; j < 4; j++)
92 cc[3 * i + j - 1] = cws[j][i] - cws[0][i];
93
94 cvInvert(&CC, &CC_inv, CV_SVD);
95 double * ci = cc_inv;
96 for(int i = 0; i < number_of_correspondences; i++) {
97 double * pi = &pws[0] + 3 * i;
98 double * a = &alphas[0] + 4 * i;
99
100 for(int j = 0; j < 3; j++)
101 a[1 + j] =
102 ci[3 * j ] * (pi[0] - cws[0][0]) +
103 ci[3 * j + 1] * (pi[1] - cws[0][1]) +
104 ci[3 * j + 2] * (pi[2] - cws[0][2]);
105 a[0] = 1.0f - a[1] - a[2] - a[3];
106 }
107 }
108
fill_M(CvMat * M,const int row,const double * as,const double u,const double v)109 void epnp::fill_M(CvMat * M,
110 const int row, const double * as, const double u, const double v)
111 {
112 double * M1 = M->data.db + row * 12;
113 double * M2 = M1 + 12;
114
115 for(int i = 0; i < 4; i++) {
116 M1[3 * i ] = as[i] * fu;
117 M1[3 * i + 1] = 0.0;
118 M1[3 * i + 2] = as[i] * (uc - u);
119
120 M2[3 * i ] = 0.0;
121 M2[3 * i + 1] = as[i] * fv;
122 M2[3 * i + 2] = as[i] * (vc - v);
123 }
124 }
125
compute_ccs(const double * betas,const double * ut)126 void epnp::compute_ccs(const double * betas, const double * ut)
127 {
128 for(int i = 0; i < 4; i++)
129 ccs[i][0] = ccs[i][1] = ccs[i][2] = 0.0f;
130
131 for(int i = 0; i < 4; i++) {
132 const double * v = ut + 12 * (11 - i);
133 for(int j = 0; j < 4; j++)
134 for(int k = 0; k < 3; k++)
135 ccs[j][k] += betas[i] * v[3 * j + k];
136 }
137 }
138
compute_pcs(void)139 void epnp::compute_pcs(void)
140 {
141 for(int i = 0; i < number_of_correspondences; i++) {
142 double * a = &alphas[0] + 4 * i;
143 double * pc = &pcs[0] + 3 * i;
144
145 for(int j = 0; j < 3; j++)
146 pc[j] = a[0] * ccs[0][j] + a[1] * ccs[1][j] + a[2] * ccs[2][j] + a[3] * ccs[3][j];
147 }
148 }
149
compute_pose(Mat & R,Mat & t)150 void epnp::compute_pose(Mat& R, Mat& t)
151 {
152 choose_control_points();
153 compute_barycentric_coordinates();
154
155 CvMat * M = cvCreateMat(2 * number_of_correspondences, 12, CV_64F);
156
157 for(int i = 0; i < number_of_correspondences; i++)
158 fill_M(M, 2 * i, &alphas[0] + 4 * i, us[2 * i], us[2 * i + 1]);
159
160 double mtm[12 * 12], d[12], ut[12 * 12];
161 CvMat MtM = cvMat(12, 12, CV_64F, mtm);
162 CvMat D = cvMat(12, 1, CV_64F, d);
163 CvMat Ut = cvMat(12, 12, CV_64F, ut);
164
165 cvMulTransposed(M, &MtM, 1);
166 cvSVD(&MtM, &D, &Ut, 0, CV_SVD_MODIFY_A | CV_SVD_U_T);
167 cvReleaseMat(&M);
168
169 double l_6x10[6 * 10], rho[6];
170 CvMat L_6x10 = cvMat(6, 10, CV_64F, l_6x10);
171 CvMat Rho = cvMat(6, 1, CV_64F, rho);
172
173 compute_L_6x10(ut, l_6x10);
174 compute_rho(rho);
175
176 double Betas[4][4], rep_errors[4];
177 double Rs[4][3][3], ts[4][3];
178
179 find_betas_approx_1(&L_6x10, &Rho, Betas[1]);
180 gauss_newton(&L_6x10, &Rho, Betas[1]);
181 rep_errors[1] = compute_R_and_t(ut, Betas[1], Rs[1], ts[1]);
182
183 find_betas_approx_2(&L_6x10, &Rho, Betas[2]);
184 gauss_newton(&L_6x10, &Rho, Betas[2]);
185 rep_errors[2] = compute_R_and_t(ut, Betas[2], Rs[2], ts[2]);
186
187 find_betas_approx_3(&L_6x10, &Rho, Betas[3]);
188 gauss_newton(&L_6x10, &Rho, Betas[3]);
189 rep_errors[3] = compute_R_and_t(ut, Betas[3], Rs[3], ts[3]);
190
191 int N = 1;
192 if (rep_errors[2] < rep_errors[1]) N = 2;
193 if (rep_errors[3] < rep_errors[N]) N = 3;
194
195 Mat(3, 1, CV_64F, ts[N]).copyTo(t);
196 Mat(3, 3, CV_64F, Rs[N]).copyTo(R);
197 }
198
copy_R_and_t(const double R_src[3][3],const double t_src[3],double R_dst[3][3],double t_dst[3])199 void epnp::copy_R_and_t(const double R_src[3][3], const double t_src[3],
200 double R_dst[3][3], double t_dst[3])
201 {
202 for(int i = 0; i < 3; i++) {
203 for(int j = 0; j < 3; j++)
204 R_dst[i][j] = R_src[i][j];
205 t_dst[i] = t_src[i];
206 }
207 }
208
dist2(const double * p1,const double * p2)209 double epnp::dist2(const double * p1, const double * p2)
210 {
211 return
212 (p1[0] - p2[0]) * (p1[0] - p2[0]) +
213 (p1[1] - p2[1]) * (p1[1] - p2[1]) +
214 (p1[2] - p2[2]) * (p1[2] - p2[2]);
215 }
216
dot(const double * v1,const double * v2)217 double epnp::dot(const double * v1, const double * v2)
218 {
219 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
220 }
221
estimate_R_and_t(double R[3][3],double t[3])222 void epnp::estimate_R_and_t(double R[3][3], double t[3])
223 {
224 double pc0[3], pw0[3];
225
226 pc0[0] = pc0[1] = pc0[2] = 0.0;
227 pw0[0] = pw0[1] = pw0[2] = 0.0;
228
229 for(int i = 0; i < number_of_correspondences; i++) {
230 const double * pc = &pcs[3 * i];
231 const double * pw = &pws[3 * i];
232
233 for(int j = 0; j < 3; j++) {
234 pc0[j] += pc[j];
235 pw0[j] += pw[j];
236 }
237 }
238 for(int j = 0; j < 3; j++) {
239 pc0[j] /= number_of_correspondences;
240 pw0[j] /= number_of_correspondences;
241 }
242
243 double abt[3 * 3], abt_d[3], abt_u[3 * 3], abt_v[3 * 3];
244 CvMat ABt = cvMat(3, 3, CV_64F, abt);
245 CvMat ABt_D = cvMat(3, 1, CV_64F, abt_d);
246 CvMat ABt_U = cvMat(3, 3, CV_64F, abt_u);
247 CvMat ABt_V = cvMat(3, 3, CV_64F, abt_v);
248
249 cvSetZero(&ABt);
250 for(int i = 0; i < number_of_correspondences; i++) {
251 double * pc = &pcs[3 * i];
252 double * pw = &pws[3 * i];
253
254 for(int j = 0; j < 3; j++) {
255 abt[3 * j ] += (pc[j] - pc0[j]) * (pw[0] - pw0[0]);
256 abt[3 * j + 1] += (pc[j] - pc0[j]) * (pw[1] - pw0[1]);
257 abt[3 * j + 2] += (pc[j] - pc0[j]) * (pw[2] - pw0[2]);
258 }
259 }
260
261 cvSVD(&ABt, &ABt_D, &ABt_U, &ABt_V, CV_SVD_MODIFY_A);
262
263 for(int i = 0; i < 3; i++)
264 for(int j = 0; j < 3; j++)
265 R[i][j] = dot(abt_u + 3 * i, abt_v + 3 * j);
266
267 const double det =
268 R[0][0] * R[1][1] * R[2][2] + R[0][1] * R[1][2] * R[2][0] + R[0][2] * R[1][0] * R[2][1] -
269 R[0][2] * R[1][1] * R[2][0] - R[0][1] * R[1][0] * R[2][2] - R[0][0] * R[1][2] * R[2][1];
270
271 if (det < 0) {
272 R[2][0] = -R[2][0];
273 R[2][1] = -R[2][1];
274 R[2][2] = -R[2][2];
275 }
276
277 t[0] = pc0[0] - dot(R[0], pw0);
278 t[1] = pc0[1] - dot(R[1], pw0);
279 t[2] = pc0[2] - dot(R[2], pw0);
280 }
281
solve_for_sign(void)282 void epnp::solve_for_sign(void)
283 {
284 if (pcs[2] < 0.0) {
285 for(int i = 0; i < 4; i++)
286 for(int j = 0; j < 3; j++)
287 ccs[i][j] = -ccs[i][j];
288
289 for(int i = 0; i < number_of_correspondences; i++) {
290 pcs[3 * i ] = -pcs[3 * i];
291 pcs[3 * i + 1] = -pcs[3 * i + 1];
292 pcs[3 * i + 2] = -pcs[3 * i + 2];
293 }
294 }
295 }
296
compute_R_and_t(const double * ut,const double * betas,double R[3][3],double t[3])297 double epnp::compute_R_and_t(const double * ut, const double * betas,
298 double R[3][3], double t[3])
299 {
300 compute_ccs(betas, ut);
301 compute_pcs();
302
303 solve_for_sign();
304
305 estimate_R_and_t(R, t);
306
307 return reprojection_error(R, t);
308 }
309
reprojection_error(const double R[3][3],const double t[3])310 double epnp::reprojection_error(const double R[3][3], const double t[3])
311 {
312 double sum2 = 0.0;
313
314 for(int i = 0; i < number_of_correspondences; i++) {
315 double * pw = &pws[3 * i];
316 double Xc = dot(R[0], pw) + t[0];
317 double Yc = dot(R[1], pw) + t[1];
318 double inv_Zc = 1.0 / (dot(R[2], pw) + t[2]);
319 double ue = uc + fu * Xc * inv_Zc;
320 double ve = vc + fv * Yc * inv_Zc;
321 double u = us[2 * i], v = us[2 * i + 1];
322
323 sum2 += sqrt( (u - ue) * (u - ue) + (v - ve) * (v - ve) );
324 }
325
326 return sum2 / number_of_correspondences;
327 }
328
329 // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44]
330 // betas_approx_1 = [B11 B12 B13 B14]
331
find_betas_approx_1(const CvMat * L_6x10,const CvMat * Rho,double * betas)332 void epnp::find_betas_approx_1(const CvMat * L_6x10, const CvMat * Rho,
333 double * betas)
334 {
335 double l_6x4[6 * 4], b4[4];
336 CvMat L_6x4 = cvMat(6, 4, CV_64F, l_6x4);
337 CvMat B4 = cvMat(4, 1, CV_64F, b4);
338
339 for(int i = 0; i < 6; i++) {
340 cvmSet(&L_6x4, i, 0, cvmGet(L_6x10, i, 0));
341 cvmSet(&L_6x4, i, 1, cvmGet(L_6x10, i, 1));
342 cvmSet(&L_6x4, i, 2, cvmGet(L_6x10, i, 3));
343 cvmSet(&L_6x4, i, 3, cvmGet(L_6x10, i, 6));
344 }
345
346 cvSolve(&L_6x4, Rho, &B4, CV_SVD);
347
348 if (b4[0] < 0) {
349 betas[0] = sqrt(-b4[0]);
350 betas[1] = -b4[1] / betas[0];
351 betas[2] = -b4[2] / betas[0];
352 betas[3] = -b4[3] / betas[0];
353 } else {
354 betas[0] = sqrt(b4[0]);
355 betas[1] = b4[1] / betas[0];
356 betas[2] = b4[2] / betas[0];
357 betas[3] = b4[3] / betas[0];
358 }
359 }
360
361 // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44]
362 // betas_approx_2 = [B11 B12 B22 ]
363
find_betas_approx_2(const CvMat * L_6x10,const CvMat * Rho,double * betas)364 void epnp::find_betas_approx_2(const CvMat * L_6x10, const CvMat * Rho,
365 double * betas)
366 {
367 double l_6x3[6 * 3], b3[3];
368 CvMat L_6x3 = cvMat(6, 3, CV_64F, l_6x3);
369 CvMat B3 = cvMat(3, 1, CV_64F, b3);
370
371 for(int i = 0; i < 6; i++) {
372 cvmSet(&L_6x3, i, 0, cvmGet(L_6x10, i, 0));
373 cvmSet(&L_6x3, i, 1, cvmGet(L_6x10, i, 1));
374 cvmSet(&L_6x3, i, 2, cvmGet(L_6x10, i, 2));
375 }
376
377 cvSolve(&L_6x3, Rho, &B3, CV_SVD);
378
379 if (b3[0] < 0) {
380 betas[0] = sqrt(-b3[0]);
381 betas[1] = (b3[2] < 0) ? sqrt(-b3[2]) : 0.0;
382 } else {
383 betas[0] = sqrt(b3[0]);
384 betas[1] = (b3[2] > 0) ? sqrt(b3[2]) : 0.0;
385 }
386
387 if (b3[1] < 0) betas[0] = -betas[0];
388
389 betas[2] = 0.0;
390 betas[3] = 0.0;
391 }
392
393 // betas10 = [B11 B12 B22 B13 B23 B33 B14 B24 B34 B44]
394 // betas_approx_3 = [B11 B12 B22 B13 B23 ]
395
find_betas_approx_3(const CvMat * L_6x10,const CvMat * Rho,double * betas)396 void epnp::find_betas_approx_3(const CvMat * L_6x10, const CvMat * Rho,
397 double * betas)
398 {
399 double l_6x5[6 * 5], b5[5];
400 CvMat L_6x5 = cvMat(6, 5, CV_64F, l_6x5);
401 CvMat B5 = cvMat(5, 1, CV_64F, b5);
402
403 for(int i = 0; i < 6; i++) {
404 cvmSet(&L_6x5, i, 0, cvmGet(L_6x10, i, 0));
405 cvmSet(&L_6x5, i, 1, cvmGet(L_6x10, i, 1));
406 cvmSet(&L_6x5, i, 2, cvmGet(L_6x10, i, 2));
407 cvmSet(&L_6x5, i, 3, cvmGet(L_6x10, i, 3));
408 cvmSet(&L_6x5, i, 4, cvmGet(L_6x10, i, 4));
409 }
410
411 cvSolve(&L_6x5, Rho, &B5, CV_SVD);
412
413 if (b5[0] < 0) {
414 betas[0] = sqrt(-b5[0]);
415 betas[1] = (b5[2] < 0) ? sqrt(-b5[2]) : 0.0;
416 } else {
417 betas[0] = sqrt(b5[0]);
418 betas[1] = (b5[2] > 0) ? sqrt(b5[2]) : 0.0;
419 }
420 if (b5[1] < 0) betas[0] = -betas[0];
421 betas[2] = b5[3] / betas[0];
422 betas[3] = 0.0;
423 }
424
compute_L_6x10(const double * ut,double * l_6x10)425 void epnp::compute_L_6x10(const double * ut, double * l_6x10)
426 {
427 const double * v[4];
428
429 v[0] = ut + 12 * 11;
430 v[1] = ut + 12 * 10;
431 v[2] = ut + 12 * 9;
432 v[3] = ut + 12 * 8;
433
434 double dv[4][6][3];
435
436 for(int i = 0; i < 4; i++) {
437 int a = 0, b = 1;
438 for(int j = 0; j < 6; j++) {
439 dv[i][j][0] = v[i][3 * a ] - v[i][3 * b];
440 dv[i][j][1] = v[i][3 * a + 1] - v[i][3 * b + 1];
441 dv[i][j][2] = v[i][3 * a + 2] - v[i][3 * b + 2];
442
443 b++;
444 if (b > 3) {
445 a++;
446 b = a + 1;
447 }
448 }
449 }
450
451 for(int i = 0; i < 6; i++) {
452 double * row = l_6x10 + 10 * i;
453
454 row[0] = dot(dv[0][i], dv[0][i]);
455 row[1] = 2.0f * dot(dv[0][i], dv[1][i]);
456 row[2] = dot(dv[1][i], dv[1][i]);
457 row[3] = 2.0f * dot(dv[0][i], dv[2][i]);
458 row[4] = 2.0f * dot(dv[1][i], dv[2][i]);
459 row[5] = dot(dv[2][i], dv[2][i]);
460 row[6] = 2.0f * dot(dv[0][i], dv[3][i]);
461 row[7] = 2.0f * dot(dv[1][i], dv[3][i]);
462 row[8] = 2.0f * dot(dv[2][i], dv[3][i]);
463 row[9] = dot(dv[3][i], dv[3][i]);
464 }
465 }
466
compute_rho(double * rho)467 void epnp::compute_rho(double * rho)
468 {
469 rho[0] = dist2(cws[0], cws[1]);
470 rho[1] = dist2(cws[0], cws[2]);
471 rho[2] = dist2(cws[0], cws[3]);
472 rho[3] = dist2(cws[1], cws[2]);
473 rho[4] = dist2(cws[1], cws[3]);
474 rho[5] = dist2(cws[2], cws[3]);
475 }
476
compute_A_and_b_gauss_newton(const double * l_6x10,const double * rho,const double betas[4],CvMat * A,CvMat * b)477 void epnp::compute_A_and_b_gauss_newton(const double * l_6x10, const double * rho,
478 const double betas[4], CvMat * A, CvMat * b)
479 {
480 for(int i = 0; i < 6; i++) {
481 const double * rowL = l_6x10 + i * 10;
482 double * rowA = A->data.db + i * 4;
483
484 rowA[0] = 2 * rowL[0] * betas[0] + rowL[1] * betas[1] + rowL[3] * betas[2] + rowL[6] * betas[3];
485 rowA[1] = rowL[1] * betas[0] + 2 * rowL[2] * betas[1] + rowL[4] * betas[2] + rowL[7] * betas[3];
486 rowA[2] = rowL[3] * betas[0] + rowL[4] * betas[1] + 2 * rowL[5] * betas[2] + rowL[8] * betas[3];
487 rowA[3] = rowL[6] * betas[0] + rowL[7] * betas[1] + rowL[8] * betas[2] + 2 * rowL[9] * betas[3];
488
489 cvmSet(b, i, 0, rho[i] -
490 (
491 rowL[0] * betas[0] * betas[0] +
492 rowL[1] * betas[0] * betas[1] +
493 rowL[2] * betas[1] * betas[1] +
494 rowL[3] * betas[0] * betas[2] +
495 rowL[4] * betas[1] * betas[2] +
496 rowL[5] * betas[2] * betas[2] +
497 rowL[6] * betas[0] * betas[3] +
498 rowL[7] * betas[1] * betas[3] +
499 rowL[8] * betas[2] * betas[3] +
500 rowL[9] * betas[3] * betas[3]
501 ));
502 }
503 }
504
gauss_newton(const CvMat * L_6x10,const CvMat * Rho,double betas[4])505 void epnp::gauss_newton(const CvMat * L_6x10, const CvMat * Rho, double betas[4])
506 {
507 const int iterations_number = 5;
508
509 double a[6*4], b[6], x[4];
510 CvMat A = cvMat(6, 4, CV_64F, a);
511 CvMat B = cvMat(6, 1, CV_64F, b);
512 CvMat X = cvMat(4, 1, CV_64F, x);
513
514 for(int k = 0; k < iterations_number; k++)
515 {
516 compute_A_and_b_gauss_newton(L_6x10->data.db, Rho->data.db,
517 betas, &A, &B);
518 qr_solve(&A, &B, &X);
519 for(int i = 0; i < 4; i++)
520 betas[i] += x[i];
521 }
522 }
523
qr_solve(CvMat * A,CvMat * b,CvMat * X)524 void epnp::qr_solve(CvMat * A, CvMat * b, CvMat * X)
525 {
526 const int nr = A->rows;
527 const int nc = A->cols;
528
529 if (max_nr != 0 && max_nr < nr)
530 {
531 delete [] A1;
532 delete [] A2;
533 }
534 if (max_nr < nr)
535 {
536 max_nr = nr;
537 A1 = new double[nr];
538 A2 = new double[nr];
539 }
540
541 double * pA = A->data.db, * ppAkk = pA;
542 for(int k = 0; k < nc; k++)
543 {
544 double * ppAik1 = ppAkk, eta = fabs(*ppAik1);
545 for(int i = k + 1; i < nr; i++)
546 {
547 double elt = fabs(*ppAik1);
548 if (eta < elt) eta = elt;
549 ppAik1 += nc;
550 }
551 if (eta == 0)
552 {
553 A1[k] = A2[k] = 0.0;
554 //cerr << "God damnit, A is singular, this shouldn't happen." << endl;
555 return;
556 }
557 else
558 {
559 double * ppAik2 = ppAkk, sum2 = 0.0, inv_eta = 1. / eta;
560 for(int i = k; i < nr; i++)
561 {
562 *ppAik2 *= inv_eta;
563 sum2 += *ppAik2 * *ppAik2;
564 ppAik2 += nc;
565 }
566 double sigma = sqrt(sum2);
567 if (*ppAkk < 0)
568 sigma = -sigma;
569 *ppAkk += sigma;
570 A1[k] = sigma * *ppAkk;
571 A2[k] = -eta * sigma;
572 for(int j = k + 1; j < nc; j++)
573 {
574 double * ppAik = ppAkk, sum = 0;
575 for(int i = k; i < nr; i++)
576 {
577 sum += *ppAik * ppAik[j - k];
578 ppAik += nc;
579 }
580 double tau = sum / A1[k];
581 ppAik = ppAkk;
582 for(int i = k; i < nr; i++)
583 {
584 ppAik[j - k] -= tau * *ppAik;
585 ppAik += nc;
586 }
587 }
588 }
589 ppAkk += nc + 1;
590 }
591
592 // b <- Qt b
593 double * ppAjj = pA, * pb = b->data.db;
594 for(int j = 0; j < nc; j++)
595 {
596 double * ppAij = ppAjj, tau = 0;
597 for(int i = j; i < nr; i++)
598 {
599 tau += *ppAij * pb[i];
600 ppAij += nc;
601 }
602 tau /= A1[j];
603 ppAij = ppAjj;
604 for(int i = j; i < nr; i++)
605 {
606 pb[i] -= tau * *ppAij;
607 ppAij += nc;
608 }
609 ppAjj += nc + 1;
610 }
611
612 // X = R-1 b
613 double * pX = X->data.db;
614 pX[nc - 1] = pb[nc - 1] / A2[nc - 1];
615 for(int i = nc - 2; i >= 0; i--)
616 {
617 double * ppAij = pA + i * nc + (i + 1), sum = 0;
618
619 for(int j = i + 1; j < nc; j++)
620 {
621 sum += *ppAij * pX[j];
622 ppAij++;
623 }
624 pX[i] = (pb[i] - sum) / A2[i];
625 }
626 }
627
628 }
629