00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <math.h>
00025 #include <float.h>
00026
00027 #include "compiler.h"
00028 #include "sba.h"
00029 #include "sba_chkjac.h"
00030
00031 #define SBA_EPSILON 1E-12
00032 #define SBA_EPSILON_SQ ( (SBA_EPSILON)*(SBA_EPSILON) )
00033
00034 #define SBA_ONE_THIRD 0.3333333334
00035
00036
00037 #define emalloc(sz) emalloc_(__FILE__, __LINE__, sz)
00038
00039 #define FABS(x) (((x)>=0)? (x) : -(x))
00040
00041 #define ROW_MAJOR 0
00042 #define COLUMN_MAJOR 1
00043 #define MAT_STORAGE COLUMN_MAJOR
00044
00045
00046
00047
00048
00049 struct fdj_data_x_ {
00050 void (*func)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata);
00051 int cnp, pnp, mnp;
00052 int *func_rcidxs,
00053 *func_rcsubs;
00054
00055
00056
00057
00058 double *hx, *hxx;
00059 void *adata;
00060 };
00061
00062
00063 inline static void *emalloc_(char *file, int line, size_t sz)
00064 {
00065 void *ptr;
00066
00067 ptr=(void *)malloc(sz);
00068 if(ptr==NULL){
00069 fprintf(stderr, "SBA: memory allocation request for %u bytes failed in file %s, line %d, exiting", sz, file, line);
00070 exit(1);
00071 }
00072
00073 return ptr;
00074 }
00075
00076
00077 inline static void _dblzero(register double *arr, register int count)
00078 {
00079 while(--count>=0)
00080 *arr++=0.0;
00081 }
00082
00083
00084 static double sba_mean_repr_error(int n, int mnp, double *x, double *hx, struct sba_crsm *idxij, int *rcidxs, int *rcsubs)
00085 {
00086 register int i, j;
00087 int nnz, nprojs;
00088 double *ptr1, *ptr2;
00089 double err;
00090
00091 for(i=nprojs=0, err=0.0; i<n; ++i){
00092 nnz=sba_crsm_row_elmidxs(idxij, i, rcidxs, rcsubs);
00093 nprojs+=nnz;
00094 for(j=0; j<nnz; ++j){
00095 ptr1=x + idxij->val[rcidxs[j]]*mnp;
00096 ptr2=hx + idxij->val[rcidxs[j]]*mnp;
00097
00098 err+=sqrt((ptr1[0]-ptr2[0])*(ptr1[0]-ptr2[0]) + (ptr1[1]-ptr2[1])*(ptr1[1]-ptr2[1]));
00099 }
00100 }
00101
00102 return err/((double)(nprojs));
00103 }
00104
00105
00106 static void sba_print_sol(int n, int m, double *p, int cnp, int pnp, double *x, int mnp, struct sba_crsm *idxij, int *rcidxs, int *rcsubs)
00107 {
00108 register int i, j, ii;
00109 int nnz;
00110 double *ptr;
00111
00112 if(cnp){
00113
00114 for(j=0; j<m; ++j){
00115 ptr=p+cnp*j;
00116 for(ii=0; ii<cnp; ++ii)
00117 printf("%g ", ptr[ii]);
00118 printf("\n");
00119 }
00120 }
00121
00122 if(pnp){
00123
00124 printf("\n\n\n# X Y Z nframes frame0 x0 y0 frame1 x1 y1 ...\n");
00125 for(i=0; i<n; ++i){
00126 ptr=p+cnp*m+i*pnp;
00127 for(ii=0; ii<pnp; ++ii)
00128 printf("%g ", ptr[ii]);
00129
00130 nnz=sba_crsm_row_elmidxs(idxij, i, rcidxs, rcsubs);
00131 printf("%d ", nnz);
00132
00133 for(j=0; j<nnz; ++j){
00134 ptr=x + idxij->val[rcidxs[j]]*mnp;
00135
00136 printf("%d ", rcsubs[j]);
00137 for(ii=0; ii<mnp; ++ii)
00138 printf("%g ", ptr[ii]);
00139 }
00140 printf("\n");
00141 }
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150 static double nrmL2xmy(double *const e, const double *const x, const double *const y, const int n)
00151 {
00152 const int blocksize=8, bpwr=3;
00153 register int i;
00154 int j1, j2, j3, j4, j5, j6, j7;
00155 int blockn;
00156 register double sum0=0.0, sum1=0.0, sum2=0.0, sum3=0.0;
00157
00158
00159
00160
00161 blockn = (n>>bpwr)<<bpwr;
00162
00163
00164 for(i=blockn-1; i>0; i-=blocksize){
00165 e[i ]=x[i ]-y[i ]; sum0+=e[i ]*e[i ];
00166 j1=i-1; e[j1]=x[j1]-y[j1]; sum1+=e[j1]*e[j1];
00167 j2=i-2; e[j2]=x[j2]-y[j2]; sum2+=e[j2]*e[j2];
00168 j3=i-3; e[j3]=x[j3]-y[j3]; sum3+=e[j3]*e[j3];
00169 j4=i-4; e[j4]=x[j4]-y[j4]; sum0+=e[j4]*e[j4];
00170 j5=i-5; e[j5]=x[j5]-y[j5]; sum1+=e[j5]*e[j5];
00171 j6=i-6; e[j6]=x[j6]-y[j6]; sum2+=e[j6]*e[j6];
00172 j7=i-7; e[j7]=x[j7]-y[j7]; sum3+=e[j7]*e[j7];
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 i=blockn;
00182 if(i<n){
00183
00184
00185
00186 switch(n - i){
00187 case 7 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00188 case 6 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00189 case 5 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00190 case 4 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00191 case 3 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00192 case 2 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00193 case 1 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
00194 }
00195 }
00196
00197 return sum0+sum1+sum2+sum3;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207 static double nrmCxmy(double *const e, const double *const x, const double *const y,
00208 const double *const W, const int mnp, const int nvis)
00209 {
00210 const int n=nvis*mnp;
00211 const int blocksize=8, bpwr=3;
00212 register int i, ii, k;
00213 int j1, j2, j3, j4, j5, j6, j7;
00214 int blockn, mnpsq;
00215 register double norm, sum;
00216 register const double *Wptr, *auxptr;
00217 register double *eptr;
00218
00219
00220
00221
00222 blockn = (n>>bpwr)<<bpwr;
00223
00224
00225 for(i=blockn-1; i>0; i-=blocksize){
00226 e[i ]=x[i ]-y[i ];
00227 j1=i-1; e[j1]=x[j1]-y[j1];
00228 j2=i-2; e[j2]=x[j2]-y[j2];
00229 j3=i-3; e[j3]=x[j3]-y[j3];
00230 j4=i-4; e[j4]=x[j4]-y[j4];
00231 j5=i-5; e[j5]=x[j5]-y[j5];
00232 j6=i-6; e[j6]=x[j6]-y[j6];
00233 j7=i-7; e[j7]=x[j7]-y[j7];
00234 }
00235
00236
00237
00238
00239
00240
00241
00242 i=blockn;
00243 if(i<n){
00244
00245
00246
00247 switch(n - i){
00248 case 7 : e[i]=x[i]-y[i]; ++i;
00249 case 6 : e[i]=x[i]-y[i]; ++i;
00250 case 5 : e[i]=x[i]-y[i]; ++i;
00251 case 4 : e[i]=x[i]-y[i]; ++i;
00252 case 3 : e[i]=x[i]-y[i]; ++i;
00253 case 2 : e[i]=x[i]-y[i]; ++i;
00254 case 1 : e[i]=x[i]-y[i]; ++i;
00255 }
00256 }
00257
00258
00259
00260
00261
00262 mnpsq=mnp*mnp;
00263
00264 for(i=0, Wptr=W, eptr=e, norm=0.0; i<nvis; ++i, Wptr+=mnpsq, eptr+=mnp){
00265 for(ii=0, auxptr=Wptr; ii<mnp; ++ii, auxptr+=mnp){
00266 for(k=ii, sum=0.0; k<mnp; ++k)
00267 sum+=auxptr[k]*eptr[k];
00268 eptr[ii]=sum;
00269 norm+=sum*sum;
00270 }
00271 }
00272
00273 return norm;
00274 }
00275
00276
00277 static void sba_print_inf(double *hx, int nimgs, int mnp, struct sba_crsm *idxij, int *rcidxs, int *rcsubs)
00278 {
00279 register int i, j, k;
00280 int nnz;
00281 double *phxij;
00282
00283 for(j=0; j<nimgs; ++j){
00284 nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs);
00285 for(i=0; i<nnz; ++i){
00286 phxij=hx + idxij->val[rcidxs[i]]*mnp;
00287 for(k=0; k<mnp; ++k)
00288 if(!SBA_FINITE(phxij[k]))
00289 printf("SBA: component %d of the estimated projection of point %d on camera %d is invalid!\n", k, rcsubs[i], j);
00290 }
00291 }
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 static void sba_fdjac_x(
00328 double *p,
00329 struct sba_crsm *idxij,
00330 int *rcidxs,
00331 int *rcsubs,
00332 double *jac,
00333 void *dat)
00334 {
00335 register int i, j, ii, jj;
00336 double *pa, *pb, *pqr, *ppt;
00337 register double *pAB, *phx, *phxx;
00338 int n, m, nm, nnz, Asz, Bsz, ABsz, idx;
00339
00340 double *tmpd;
00341 register double d;
00342
00343 struct fdj_data_x_ *fdjd;
00344 void (*func)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata);
00345 double *hx, *hxx;
00346 int cnp, pnp, mnp;
00347 void *adata;
00348
00349
00350
00351 fdjd=(struct fdj_data_x_ *)dat;
00352 func=fdjd->func;
00353 cnp=fdjd->cnp; pnp=fdjd->pnp; mnp=fdjd->mnp;
00354 hx=fdjd->hx;
00355 hxx=fdjd->hxx;
00356 adata=fdjd->adata;
00357
00358 n=idxij->nr; m=idxij->nc;
00359 pa=p; pb=p+m*cnp;
00360 Asz=mnp*cnp; Bsz=mnp*pnp; ABsz=Asz+Bsz;
00361
00362 nm=(n>=m)? n : m;
00363 tmpd=(double *)emalloc(nm*sizeof(double));
00364
00365 (*func)(p, idxij, fdjd->func_rcidxs, fdjd->func_rcsubs, hx, adata);
00366
00367 if(cnp){
00368
00369 for(jj=0; jj<cnp; ++jj){
00370 for(j=0; j<m; ++j){
00371 pqr=pa+j*cnp;
00372
00373 d=(double)(SBA_DELTA_SCALE)*pqr[jj];
00374 d=FABS(d);
00375 if(d<SBA_MIN_DELTA) d=SBA_MIN_DELTA;
00376
00377 tmpd[j]=d;
00378 pqr[jj]+=d;
00379 }
00380
00381 (*func)(p, idxij, fdjd->func_rcidxs, fdjd->func_rcsubs, hxx, adata);
00382
00383 for(j=0; j<m; ++j){
00384 pqr=pa+j*cnp;
00385 pqr[jj]-=tmpd[j];
00386 d=1.0/tmpd[j];
00387
00388 nnz=sba_crsm_col_elmidxs(idxij, j, rcidxs, rcsubs);
00389 for(i=0; i<nnz; ++i){
00390 idx=idxij->val[rcidxs[i]];
00391 phx=hx + idx*mnp;
00392 phxx=hxx + idx*mnp;
00393 pAB=jac + idx*ABsz;
00394
00395 for(ii=0; ii<mnp; ++ii)
00396 pAB[ii*cnp+jj]=(phxx[ii]-phx[ii])*d;
00397 }
00398 }
00399 }
00400 }
00401
00402 if(pnp){
00403
00404 for(jj=0; jj<pnp; ++jj){
00405 for(i=0; i<n; ++i){
00406 ppt=pb+i*pnp;
00407
00408 d=(double)(SBA_DELTA_SCALE)*ppt[jj];
00409 d=FABS(d);
00410 if(d<SBA_MIN_DELTA) d=SBA_MIN_DELTA;
00411
00412 tmpd[i]=d;
00413 ppt[jj]+=d;
00414 }
00415
00416 (*func)(p, idxij, fdjd->func_rcidxs, fdjd->func_rcsubs, hxx, adata);
00417
00418 for(i=0; i<n; ++i){
00419 ppt=pb+i*pnp;
00420 ppt[jj]-=tmpd[i];
00421 d=1.0/tmpd[i];
00422
00423 nnz=sba_crsm_row_elmidxs(idxij, i, rcidxs, rcsubs);
00424 for(j=0; j<nnz; ++j){
00425 idx=idxij->val[rcidxs[j]];
00426 phx=hx + idx*mnp;
00427 phxx=hxx + idx*mnp;
00428 pAB=jac + idx*ABsz + Asz;
00429
00430 for(ii=0; ii<mnp; ++ii)
00431 pAB[ii*pnp+jj]=(phxx[ii]-phx[ii])*d;
00432 }
00433 }
00434 }
00435 }
00436
00437 free(tmpd);
00438 }
00439
00440 typedef int (*PLS)(double *A, double *B, double *x, int m, int iscolmaj);
00441
00442
00443
00444
00445
00446
00447
00448 #ifdef SAVE_REDUCED_MATRIX
00449 double *copyS = NULL;
00450 int dimS = 0;
00451 #endif
00452
00453 #include <stdio.h>
00454 #include <time.h>
00455
00456 double last_mu_used;
00457 int sba_damping_iters;
00458 double sba_time_total, sba_time_solve, sba_time_system;
00459
00460 int sba_motstr_levmar_x(
00461 const int n,
00462 const int ncon,
00463
00464
00465 const int m,
00466 const int mcon,
00467
00468
00469 char *vmask,
00470 double *p,
00471
00472
00473
00474 const int cnp,
00475 const int pnp,
00476 double *x,
00477
00478
00479
00480
00481 double *covx,
00482
00483
00484
00485
00486
00487 const int mnp,
00488 void (*func)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata),
00489
00490
00491
00492
00493
00494
00495 void (*fjac)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata),
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 void *adata,
00511
00512 const int itmax,
00513 const int verbose,
00514 const double opts[SBA_OPTSSZ],
00515
00516
00517
00518 double info[SBA_INFOSZ]
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 )
00535
00536 {
00537
00538
00539 int time_total = 0, time_solve = 0;
00540
00541 clock_t t_init = clock();
00542
00543
00544
00545
00546
00547 register int i, j, ii, jj, k, l;
00548 int nvis, nnz, retval;
00549
00550
00551 double *jac;
00552 double *U;
00553 double *V;
00554
00555
00556
00557
00558 double *e;
00559
00560 double *eab;
00561
00562 double *E;
00563
00564
00565
00566
00567
00568 double *W;
00569
00570 double *Yj;
00571
00572 double *YWt;
00573 double *S;
00574 double *dp;
00575 double *Wtda;
00576 double *wght=
00577 NULL;
00578
00579
00580
00581
00582
00583
00584
00585 double *pa, *pb, *ea, *eb, *dpa, *dpb;
00586
00587
00588 int Asz, Bsz, ABsz, Usz, Vsz,
00589 Wsz, Ysz, esz, easz, ebsz,
00590 YWtsz, Wtdasz, Sblsz, covsz;
00591
00592 int Sdim;
00593
00594 register double *ptr1, *ptr2, *ptr3, *ptr4, sum;
00595 struct sba_crsm idxij;
00596
00597
00598
00599
00600
00601 int maxCvis,
00602 maxPvis,
00603 maxCPvis,
00604 *rcidxs,
00605
00606 *rcsubs;
00607
00608
00609
00610 register int itno;
00611 int issolved;
00612
00613 double *hx,
00614 *diagUV,
00615 *pdp;
00616
00617 double *diagU, *diagV;
00618
00619 register double mu,
00620 tmp;
00621 double p_eL2, eab_inf, pdp_eL2;
00622 double p_L2, dp_L2=DBL_MAX, dF, dL;
00623 double tau=FABS(opts[0]), eps1=FABS(opts[1]), eps2=FABS(opts[2]), eps2_sq=opts[2]*opts[2],
00624 eps3_sq=opts[3]*opts[3], eps4_sq=opts[4]*opts[4];
00625 double init_p_eL2;
00626 int nu=2, nu2, stop=0, nfev, njev=0, nlss=0;
00627 int nobs, nvars;
00628 const int mmcon=m-mcon;
00629 PLS linsolver=NULL;
00630 int (*matinv)(double *A, int m)=NULL;
00631
00632 struct fdj_data_x_ fdj_data;
00633 void *jac_adata;
00634
00635
00636 mu=eab_inf=0.0;
00637
00638
00639 Asz=mnp * cnp; Bsz=mnp * pnp; ABsz=Asz + Bsz;
00640 Usz=cnp * cnp; Vsz=pnp * pnp;
00641 Wsz=cnp * pnp; Ysz=cnp * pnp;
00642 esz=mnp;
00643 easz=cnp; ebsz=pnp;
00644 YWtsz=cnp * cnp;
00645 Wtdasz=pnp;
00646 Sblsz=cnp * cnp;
00647 Sdim=mmcon * cnp;
00648 covsz=mnp * mnp;
00649
00650
00651 for(i=nvis=0, jj=n*m; i<jj; ++i)
00652 nvis+=(vmask[i]!=0);
00653
00654 nobs=nvis*mnp;
00655 nvars=m*cnp + n*pnp;
00656 if(nobs<nvars){
00657 fprintf(stderr, "SBA: sba_motstr_levmar_x() cannot solve a problem with fewer measurements [%d] than unknowns [%d]\n", nobs, nvars);
00658 return SBA_ERROR;
00659 }
00660
00661
00662 sba_crsm_alloc(&idxij, n, m, nvis);
00663 for(i=k=0; i<n; ++i){
00664 idxij.rowptr[i]=k;
00665 ii=i*m;
00666 for(j=0; j<m; ++j)
00667 if(vmask[ii+j]){
00668 idxij.val[k]=k;
00669 idxij.colidx[k++]=j;
00670 }
00671 }
00672 idxij.rowptr[n]=nvis;
00673
00674
00675 for(i=maxCvis=0; i<n; ++i)
00676 if((k=idxij.rowptr[i+1]-idxij.rowptr[i])>maxCvis) maxCvis=k;
00677
00678
00679 for(j=maxPvis=0; j<m; ++j){
00680 for(i=ii=0; i<n; ++i)
00681 if(vmask[i*m+j]) ++ii;
00682 if(ii>maxPvis) maxPvis=ii;
00683 }
00684 maxCPvis=(maxCvis>=maxPvis)? maxCvis : maxPvis;
00685
00686 #if 0
00687
00688 for(j=mcon, ii=0; j<m; ++j){
00689 ++ii;
00690 for(k=j+1; k<m; ++k)
00691 if(sba_crsm_common_row(&idxij, j, k)) ii+=2;
00692 }
00693 printf("\nS block density: %.5g\n", ((double)ii)/(mmcon*mmcon)); fflush(stdout);
00694 #endif
00695
00696
00697
00698 W=(double *)emalloc((nvis*((Wsz>=ABsz)? Wsz : ABsz) + Wsz)*sizeof(double));
00699 U=(double *)emalloc(m*Usz*sizeof(double));
00700 V=(double *)emalloc(n*Vsz*sizeof(double));
00701 e=(double *)emalloc(nobs*sizeof(double));
00702 eab=(double *)emalloc(nvars*sizeof(double));
00703 E=(double *)emalloc(m*cnp*sizeof(double));
00704 Yj=(double *)emalloc(maxPvis*Ysz*sizeof(double));
00705 YWt=(double *)emalloc(YWtsz*sizeof(double));
00706 S=(double *)emalloc(m*m*Sblsz*sizeof(double));
00707 dp=(double *)emalloc(nvars*sizeof(double));
00708 Wtda=(double *)emalloc(pnp*sizeof(double));
00709 rcidxs=(int *)emalloc(maxCPvis*sizeof(int));
00710 rcsubs=(int *)emalloc(maxCPvis*sizeof(int));
00711 #ifndef SBA_DESTROY_COVS
00712 if(covx!=NULL) wght=(double *)emalloc(nvis*covsz*sizeof(double));
00713 #else
00714 if(covx!=NULL) wght=covx;
00715 #endif
00716
00717
00718 hx=(double *)emalloc(nobs*sizeof(double));
00719 diagUV=(double *)emalloc(nvars*sizeof(double));
00720 pdp=(double *)emalloc(nvars*sizeof(double));
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 jac=W + Wsz + ((Wsz>ABsz)? nvis*(Wsz-ABsz) : 0);
00743
00744
00745 pa=p; pb=p+m*cnp;
00746 ea=eab; eb=eab+m*cnp;
00747 dpa=dp; dpb=dp+m*cnp;
00748
00749 diagU=diagUV; diagV=diagUV + m*cnp;
00750
00751
00752 if(!fjac){
00753 fdj_data.func=func;
00754 fdj_data.cnp=cnp;
00755 fdj_data.pnp=pnp;
00756 fdj_data.mnp=mnp;
00757 fdj_data.hx=hx;
00758 fdj_data.hxx=(double *)emalloc(nobs*sizeof(double));
00759 fdj_data.func_rcidxs=(int *)emalloc(2*maxCPvis*sizeof(int));
00760 fdj_data.func_rcsubs=fdj_data.func_rcidxs+maxCPvis;
00761 fdj_data.adata=adata;
00762
00763 fjac=sba_fdjac_x;
00764 jac_adata=(void *)&fdj_data;
00765 }
00766 else{
00767 fdj_data.hxx=NULL;
00768 jac_adata=adata;
00769 }
00770
00771 if(itmax==0){
00772 sba_motstr_chkjac_x(func, fjac, p, &idxij, rcidxs, rcsubs, ncon, mcon, cnp, pnp, mnp, adata, jac_adata);
00773 retval=0;
00774 goto freemem_and_return;
00775 }
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785 if(covx!=NULL){
00786 for(i=0; i<n; ++i){
00787 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
00788 for(j=0; j<nnz; ++j){
00789
00790 ptr1=covx + (k=idxij.val[rcidxs[j]]*covsz);
00791 ptr2=wght + k;
00792 if(!sba_mat_cholinv(ptr1, ptr2, mnp)){
00793 fprintf(stderr, "SBA: invalid covariance matrix for x_ij (i=%d, j=%d) in sba_motstr_levmar_x()\n", i, rcsubs[j]);
00794 retval=SBA_ERROR;
00795 goto freemem_and_return;
00796 }
00797 }
00798 }
00799 sba_mat_cholinv(NULL, NULL, 0);
00800 }
00801
00802
00803 (*func)(p, &idxij, rcidxs, rcsubs, hx, adata); nfev=1;
00804
00805 if(covx==NULL)
00806 p_eL2=nrmL2xmy(e, x, hx, nobs);
00807 else
00808 p_eL2=nrmCxmy(e, x, hx, wght, mnp, nvis);
00809 if(verbose) printf("initial motstr-SBA error %g [%g]\n", p_eL2, p_eL2/nvis);
00810 init_p_eL2=p_eL2;
00811 if(!SBA_FINITE(p_eL2)) stop=7;
00812
00813 for(itno=0; itno<itmax && !stop; ++itno){
00814
00815
00816
00817 (*fjac)(p, &idxij, rcidxs, rcsubs, jac, jac_adata); ++njev;
00818
00819 if(covx!=NULL){
00820
00821
00822
00823
00824 for(i=0; i<nvis; ++i){
00825
00826 ptr1=wght + i*covsz;
00827 ptr2=jac + i*ABsz;
00828 ptr3=ptr2 + Asz;
00829
00830
00831
00832
00833
00834 for(ii=0; ii<mnp; ++ii)
00835 for(jj=0; jj<cnp; ++jj){
00836 for(k=ii, sum=0.0; k<mnp; ++k)
00837 sum+=ptr1[ii*mnp+k]*ptr2[k*cnp+jj];
00838 ptr2[ii*cnp+jj]=sum;
00839 }
00840
00841
00842 for(ii=0; ii<mnp; ++ii)
00843 for(jj=0; jj<pnp; ++jj){
00844 for(k=ii, sum=0.0; k<mnp; ++k)
00845 sum+=ptr1[ii*mnp+k]*ptr3[k*pnp+jj];
00846 ptr3[ii*pnp+jj]=sum;
00847 }
00848 }
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 _dblzero(U, m*Usz);
00860 _dblzero(ea, m*easz);
00861 for(j=mcon; j<m; ++j){
00862 ptr1=U + j*Usz;
00863 ptr2=ea + j*easz;
00864
00865 nnz=sba_crsm_col_elmidxs(&idxij, j, rcidxs, rcsubs);
00866 for(i=0; i<nnz; ++i){
00867
00868 ptr3=jac + idxij.val[rcidxs[i]]*ABsz;
00869
00870
00871 for(ii=0; ii<cnp; ++ii){
00872 for(jj=ii; jj<cnp; ++jj){
00873 for(k=0, sum=0.0; k<mnp; ++k)
00874 sum+=ptr3[k*cnp+ii]*ptr3[k*cnp+jj];
00875 ptr1[ii*cnp+jj]+=sum;
00876 }
00877
00878
00879 for(jj=0; jj<ii; ++jj)
00880 ptr1[ii*cnp+jj]=ptr1[jj*cnp+ii];
00881 }
00882
00883 ptr4=e + idxij.val[rcidxs[i]]*esz;
00884
00885 for(ii=0; ii<cnp; ++ii){
00886 for(jj=0, sum=0.0; jj<mnp; ++jj)
00887 sum+=ptr3[jj*cnp+ii]*ptr4[jj];
00888 ptr2[ii]+=sum;
00889 }
00890 }
00891 }
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901 _dblzero(V, n*Vsz);
00902 _dblzero(eb, n*ebsz);
00903 for(i=ncon; i<n; ++i){
00904 ptr1=V + i*Vsz;
00905 ptr2=eb + i*ebsz;
00906
00907 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
00908 for(j=0; j<nnz; ++j){
00909
00910 ptr3=jac + idxij.val[rcidxs[j]]*ABsz + Asz;
00911
00912
00913 for(ii=0; ii<pnp; ++ii){
00914 for(jj=ii; jj<pnp; ++jj){
00915 for(k=0, sum=0.0; k<mnp; ++k)
00916 sum+=ptr3[k*pnp+ii]*ptr3[k*pnp+jj];
00917 ptr1[ii*pnp+jj]+=sum;
00918 }
00919 }
00920
00921 ptr4=e + idxij.val[rcidxs[j]]*esz;
00922
00923 for(ii=0; ii<pnp; ++ii){
00924 for(jj=0, sum=0.0; jj<mnp; ++jj)
00925 sum+=ptr3[jj*pnp+ii]*ptr4[jj];
00926 ptr2[ii]+=sum;
00927 }
00928 }
00929 }
00930
00931
00932
00933
00934 for(i=ncon; i<n; ++i){
00935 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
00936 for(j=0; j<nnz; ++j){
00937
00938 ptr1=W + idxij.val[rcidxs[j]]*Wsz;
00939
00940 if(rcsubs[j]<mcon){
00941
00942 continue;
00943 }
00944
00945
00946 ptr2=jac + idxij.val[rcidxs[j]]*ABsz;
00947 ptr3=ptr2 + Asz;
00948
00949
00950
00951
00952
00953 for(ii=0; ii<cnp; ++ii)
00954 for(jj=0; jj<pnp; ++jj){
00955 for(k=0, sum=0.0; k<mnp; ++k)
00956 sum+=ptr2[k*cnp+ii]*ptr3[k*pnp+jj];
00957 ptr1[ii*pnp+jj]=sum;
00958 }
00959 }
00960 }
00961
00962
00963 for(i=0, p_L2=eab_inf=0.0; i<nvars; ++i){
00964 if(eab_inf < (tmp=FABS(eab[i]))) eab_inf=tmp;
00965 p_L2+=p[i]*p[i];
00966 }
00967
00968
00969
00970
00971
00972 for(j=mcon; j<m; ++j){
00973 ptr1=U + j*Usz;
00974 ptr2=diagU + j*cnp;
00975 for(i=0; i<cnp; ++i)
00976 ptr2[i]=ptr1[i*cnp+i];
00977 }
00978 for(i=ncon; i<n; ++i){
00979 ptr1=V + i*Vsz;
00980 ptr2=diagV + i*pnp;
00981 for(j=0; j<pnp; ++j)
00982 ptr2[j]=ptr1[j*pnp+j];
00983 }
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995 if((eab_inf <= eps1)){
00996 dp_L2=0.0;
00997 stop=1;
00998 break;
00999 }
01000
01001
01002 if(itno==0){
01003
01004 for(i=mcon*cnp, tmp=DBL_MIN; i<m*cnp; ++i)
01005 if(diagUV[i]>tmp) tmp=diagUV[i];
01006 for(i=m*cnp + ncon*pnp; i<nvars; ++i)
01007 if(diagUV[i]>tmp) tmp=diagUV[i];
01008 mu=tau*tmp;
01009 }
01010
01011
01012
01013
01014
01015
01016
01017 while(1){
01018
01019 for(j=mcon; j<m; ++j){
01020 ptr1=U + j*Usz;
01021 for(i=0; i<cnp; ++i)
01022 ptr1[i*cnp+i]+=mu;
01023 }
01024 for(i=ncon; i<n; ++i){
01025 ptr1=V + i*Vsz;
01026 for(j=0; j<pnp; ++j)
01027 ptr1[j*pnp+j]+=mu;
01028
01029
01030
01031
01032
01033
01034
01035
01036 j=sba_symat_invert_BK(ptr1, pnp); matinv=sba_symat_invert_BK;
01037 if(!j){
01038 fprintf(stderr, "SBA: singular matrix V*_i (i=%d) in sba_motstr_levmar_x(), increasing damping\n", i);
01039 goto moredamping;
01040
01041
01042 }
01043 }
01044
01045 _dblzero(E, m*easz);
01046
01047
01048
01049
01050
01051
01052 for(j=mcon; j<m; ++j){
01053 int mmconxUsz=mmcon*Usz;
01054
01055 nnz=sba_crsm_col_elmidxs(&idxij, j, rcidxs, rcsubs);
01056
01057
01058
01059
01060 if(ncon){
01061 for(i=ii=0; i<nnz; ++i){
01062 if(rcsubs[i]>=ncon){
01063 rcidxs[ii]=rcidxs[i];
01064 rcsubs[ii++]=rcsubs[i];
01065 }
01066 }
01067 nnz=ii;
01068 }
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078 for(i=0; i<nnz; ++i){
01079
01080 ptr3=V + rcsubs[i]*Vsz;
01081
01082
01083 ptr1=Yj + i*Ysz;
01084
01085 ptr2=W + idxij.val[rcidxs[i]]*Wsz;
01086
01087
01088
01089 for(ii=0; ii<cnp; ++ii){
01090 ptr4=ptr2+ii*pnp;
01091 for(jj=0; jj<pnp; ++jj){
01092 for(k=0, sum=0.0; k<=jj; ++k)
01093 sum+=ptr4[k]*ptr3[jj*pnp+k];
01094 for( ; k<pnp; ++k)
01095 sum+=ptr4[k]*ptr3[k*pnp+jj];
01096 ptr1[ii*pnp+jj]=sum;
01097 }
01098 }
01099 }
01100
01101
01102
01103 for(k=j; k<m; ++k){
01104
01105
01106
01107
01108
01109
01110 _dblzero(YWt, YWtsz);
01111
01112 for(i=0; i<nnz; ++i){
01113 register double *pYWt;
01114
01115
01116
01117
01118
01119 ii=idxij.colidx[idxij.rowptr[rcsubs[i]]];
01120 jj=idxij.colidx[idxij.rowptr[rcsubs[i]+1]-1];
01121 if(k<ii || k>jj) continue;
01122
01123
01124 l=sba_crsm_elmidxp(&idxij, rcsubs[i], k, j, rcidxs[i]);
01125
01126 if(l==-1) continue;
01127
01128 ptr2=W + idxij.val[l]*Wsz;
01129
01130 ptr1=Yj + i*Ysz;
01131 for(ii=0; ii<cnp; ++ii){
01132 ptr3=ptr1+ii*pnp;
01133 pYWt=YWt+ii*cnp;
01134
01135 for(jj=0; jj<cnp; ++jj){
01136 ptr4=ptr2+jj*pnp;
01137 for(l=0, sum=0.0; l<pnp; ++l)
01138 sum+=ptr3[l]*ptr4[l];
01139 pYWt[jj]+=sum;
01140 }
01141 }
01142 }
01143
01144
01145
01146
01147
01148
01149 #if MAT_STORAGE==COLUMN_MAJOR
01150 ptr2=S + (k-mcon)*mmconxUsz + (j-mcon)*cnp;
01151 #else
01152 ptr2=S + (j-mcon)*mmconxUsz + (k-mcon)*cnp;
01153 #endif
01154
01155 if(j!=k){
01156 for(ii=0; ii<cnp; ++ii, ptr2+=Sdim)
01157 for(jj=0; jj<cnp; ++jj)
01158 ptr2[jj]=
01159 #if MAT_STORAGE==COLUMN_MAJOR
01160 -YWt[jj*cnp+ii];
01161 #else
01162 -YWt[ii*cnp+jj];
01163 #endif
01164 }
01165 else{
01166 ptr1=U + j*Usz;
01167
01168 for(ii=0; ii<cnp; ++ii, ptr2+=Sdim)
01169 for(jj=0; jj<cnp; ++jj)
01170 ptr2[jj]=
01171 #if MAT_STORAGE==COLUMN_MAJOR
01172 ptr1[jj*cnp+ii] - YWt[jj*cnp+ii];
01173 #else
01174 ptr1[ii*cnp+jj] - YWt[ii*cnp+jj];
01175 #endif
01176 }
01177 }
01178
01179
01180 for(k=mcon; k<j; ++k){
01181 #if MAT_STORAGE==COLUMN_MAJOR
01182 ptr1=S + (k-mcon)*mmconxUsz + (j-mcon)*cnp;
01183 ptr2=S + (j-mcon)*mmconxUsz + (k-mcon)*cnp;
01184 #else
01185 ptr1=S + (j-mcon)*mmconxUsz + (k-mcon)*cnp;
01186 ptr2=S + (k-mcon)*mmconxUsz + (j-mcon)*cnp;
01187 #endif
01188 for(ii=0; ii<cnp; ++ii, ptr1+=Sdim)
01189 for(jj=0, ptr3=ptr2+ii; jj<cnp; ++jj, ptr3+=Sdim)
01190 ptr1[jj]=*ptr3;
01191 }
01192
01193
01194
01195 ptr1=E + j*easz;
01196
01197 for(i=0; i<nnz; ++i){
01198
01199 ptr2=Yj + i*Ysz;
01200
01201
01202 ptr3=eb + rcsubs[i]*ebsz;
01203 for(ii=0; ii<cnp; ++ii){
01204 ptr4=ptr2+ii*pnp;
01205 for(jj=0, sum=0.0; jj<pnp; ++jj)
01206 sum+=ptr4[jj]*ptr3[jj];
01207 ptr1[ii]+=sum;
01208 }
01209 }
01210
01211 ptr2=ea + j*easz;
01212 for(i=0; i<easz; ++i)
01213 ptr1[i]=ptr2[i] - ptr1[i];
01214 }
01215
01216 #if 0
01217 if(verbose>1){
01218 for(i=ii=0; i<Sdim*Sdim; ++i)
01219 if(S[i]!=0.0) ++ii;
01220 printf("\nS density: %.5g\n", ((double)ii)/(Sdim*Sdim)); fflush(stdout);
01221 }
01222 #endif
01223
01224
01225 #ifdef SAVE_REDUCED_MATRIX
01226 {
01227 dimS = m*cnp;
01228 copyS = (double*) malloc(sizeof(double)*dimS*dimS);
01229 int i;
01230 for(i = 0; i < dimS*dimS; i++)
01231 copyS[i] = S[i];
01232 }
01233 #endif
01234
01235
01236
01237 clock_t t0_solve = clock();
01238
01239
01240
01241
01242
01243
01244 issolved=sba_Axb_Chol(S, E+mcon*cnp, dpa+mcon*cnp, Sdim, MAT_STORAGE); linsolver=sba_Axb_Chol;
01245 clock_t t1_solve = clock();
01246 time_solve += t1_solve - t0_solve;
01247
01248
01249
01250
01251
01252
01253 ++nlss;
01254
01255 _dblzero(dpa, mcon*cnp);
01256
01257 if(issolved){
01258
01259
01260 for(i=ncon; i<n; ++i){
01261 ptr1=dpb + i*ebsz;
01262
01263
01264
01265 _dblzero(Wtda, Wtdasz);
01266 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
01267 for(j=0; j<nnz; ++j){
01268
01269 if(rcsubs[j]<mcon) continue;
01270
01271 ptr2=W + idxij.val[rcidxs[j]]*Wsz;
01272
01273
01274 ptr3=dpa + rcsubs[j]*cnp;
01275
01276 for(ii=0; ii<pnp; ++ii){
01277 ptr4=ptr2+ii;
01278 for(jj=0, sum=0.0; jj<cnp; ++jj)
01279 sum+=ptr4[jj*pnp]*ptr3[jj];
01280 Wtda[ii]+=sum;
01281 }
01282 }
01283
01284
01285 ptr2=eb + i*ebsz;
01286 for(ii=0; ii<pnp; ++ii)
01287 Wtda[ii]=ptr2[ii] - Wtda[ii];
01288
01289
01290
01291
01292 ptr2=V + i*Vsz;
01293 for(ii=0; ii<pnp; ++ii){
01294 for(jj=0, sum=0.0; jj<=ii; ++jj)
01295 sum+=ptr2[ii*pnp+jj]*Wtda[jj];
01296 for( ; jj<pnp; ++jj)
01297 sum+=ptr2[jj*pnp+ii]*Wtda[jj];
01298 ptr1[ii]=sum;
01299 }
01300 }
01301 _dblzero(dpb, ncon*pnp);
01302
01303
01304
01305
01306 for(i=0, dp_L2=0.0; i<nvars; ++i){
01307 pdp[i]=p[i] + (tmp=dp[i]);
01308 dp_L2+=tmp*tmp;
01309 }
01310
01311
01312 if(dp_L2<=eps2_sq*p_L2){
01313
01314 stop=2;
01315 break;
01316 }
01317
01318 if(dp_L2>=(p_L2+eps2)/SBA_EPSILON_SQ){
01319
01320 fprintf(stderr, "SBA: the matrix of the augmented normal equations is almost singular in sba_motstr_levmar_x(),\n"
01321 " minimization should be restarted from the current solution with an increased damping term\n");
01322 retval=SBA_ERROR;
01323 goto freemem_and_return;
01324 }
01325
01326 (*func)(pdp, &idxij, rcidxs, rcsubs, hx, adata); ++nfev;
01327 if(verbose>1)
01328 printf("mean reprojection error %g\n", sba_mean_repr_error(n, mnp, x, hx, &idxij, rcidxs, rcsubs));
01329
01330 if(covx==NULL)
01331 pdp_eL2=nrmL2xmy(hx, x, hx, nobs);
01332 else
01333 pdp_eL2=nrmCxmy(hx, x, hx, wght, mnp, nvis);
01334 if(!SBA_FINITE(pdp_eL2)){
01335 if(verbose)
01336 sba_print_inf(hx, m, mnp, &idxij, rcidxs, rcsubs);
01337
01338 stop=7;
01339 break;
01340 }
01341
01342 for(i=0, dL=0.0; i<nvars; ++i)
01343 dL+=dp[i]*(mu*dp[i]+eab[i]);
01344
01345 dF=p_eL2-pdp_eL2;
01346
01347 if(verbose>1)
01348 printf("\ndamping term %8g, gain ratio %8g, errors %8g / %8g = %g\n", mu, dL!=0.0? dF/dL : dF/DBL_EPSILON, p_eL2/nvis, pdp_eL2/nvis, p_eL2/pdp_eL2);
01349
01350 if(dL>0.0 && dF>0.0){
01351 tmp=(2.0*dF/dL-1.0);
01352 tmp=1.0-tmp*tmp*tmp;
01353 mu=mu*( (tmp>=SBA_ONE_THIRD)? tmp : SBA_ONE_THIRD );
01354 nu=2;
01355
01356
01357 if(pdp_eL2-2.0*sqrt(p_eL2*pdp_eL2)<(eps4_sq-1.0)*p_eL2) stop=4;
01358
01359 for(i=0; i<nvars; ++i)
01360 p[i]=pdp[i];
01361
01362 for(i=0; i<nobs; ++i)
01363 e[i]=hx[i];
01364 p_eL2=pdp_eL2;
01365 break;
01366 }
01367 }
01368
01369 moredamping:
01370
01371
01372
01373
01374 mu*=nu;
01375 nu2=nu<<1;
01376 if(nu2<=nu){
01377 fprintf(stderr, "SBA: too many failed attempts to increase the damping factor in sba_motstr_levmar_x()! Singular Hessian matrix?\n");
01378
01379 stop=6;
01380 break;
01381 }
01382 nu=nu2;
01383
01384 #if 0
01385
01386 for(j=mcon; j<m; ++j){
01387 ptr1=U + j*Usz;
01388 ptr2=diagU + j*cnp;
01389 for(i=0; i<cnp; ++i)
01390 ptr1[i*cnp+i]=ptr2[i];
01391 }
01392 for(i=ncon; i<n; ++i){
01393 ptr1=V + i*Vsz;
01394 ptr2=diagV + i*pnp;
01395 for(j=0; j<pnp; ++j)
01396 ptr1[j*pnp+j]=ptr2[j];
01397 }
01398 #endif
01399 }
01400
01401
01402 if(p_eL2<=eps3_sq) stop=5;
01403 }
01404
01405 if(itno>=itmax) stop=3;
01406
01407
01408 for(j=mcon; j<m; ++j){
01409 ptr1=U + j*Usz;
01410 ptr2=diagU + j*cnp;
01411 for(i=0; i<cnp; ++i)
01412 ptr1[i*cnp+i]=ptr2[i];
01413 }
01414 for(i=ncon; i<n; ++i){
01415 ptr1=V + i*Vsz;
01416 ptr2=diagV + i*pnp;
01417 for(j=0; j<pnp; ++j)
01418 ptr1[j*pnp+j]=ptr2[j];
01419 }
01420
01421 if(info){
01422 info[0]=init_p_eL2;
01423 info[1]=p_eL2;
01424 info[2]=eab_inf;
01425 info[3]=dp_L2;
01426 for(j=mcon, tmp=DBL_MIN; j<m; ++j){
01427 ptr1=U + j*Usz;
01428 for(i=0; i<cnp; ++i)
01429 if(tmp<ptr1[i*cnp+i]) tmp=ptr1[i*cnp+i];
01430 }
01431 for(i=ncon; i<n; ++i){
01432 ptr1=V + i*Vsz;
01433 for(j=0; j<pnp; ++j)
01434 if(tmp<ptr1[j*pnp+j]) tmp=ptr1[j*pnp+j];
01435 }
01436 info[4]=mu/tmp;
01437 info[5]=itno;
01438 info[6]=stop;
01439 info[7]=nfev;
01440 info[8]=njev;
01441 info[9]=nlss;
01442 }
01443
01444
01445 retval=(stop!=7)? itno : SBA_ERROR;
01446
01447 freemem_and_return:
01448
01449
01450 free(W); free(U); free(V);
01451 free(e); free(eab);
01452 free(E); free(Yj); free(YWt);
01453 free(S); free(dp); free(Wtda);
01454
01455 free(rcidxs); free(rcsubs);
01456 #ifndef SBA_DESTROY_COVS
01457 if(wght) free(wght);
01458 #else
01459
01460 #endif
01461
01462 free(hx); free(diagUV); free(pdp);
01463 if(fdj_data.hxx){
01464 free(fdj_data.hxx);
01465 free(fdj_data.func_rcidxs);
01466 }
01467
01468 sba_crsm_free(&idxij);
01469
01470
01471 if(matinv) (*matinv)(NULL, 0);
01472 if(linsolver) (*linsolver)(NULL, NULL, NULL, 0, 0);
01473
01474 clock_t t_end = clock();
01475
01476 time_total = t_end - t_init;
01477
01478 sba_time_total = (double)time_total / CLOCKS_PER_SEC,
01479 sba_time_solve = (double)time_solve / CLOCKS_PER_SEC;
01480 sba_time_system = sba_time_total - sba_time_solve;
01481
01482 printf("**** time total = %f\n", sba_time_total);
01483 printf("**** time solve = %f\n", sba_time_solve);
01484 printf("**** time not solve = %f\n", sba_time_system);
01485 printf("**** mu = %.12f\n", mu);
01486 last_mu_used = mu;
01487 return retval;
01488 }
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498 int sba_mot_levmar_x(
01499 const int n,
01500 const int m,
01501 const int mcon,
01502
01503
01504 char *vmask,
01505 double *p,
01506
01507 const int cnp,
01508 double *x,
01509
01510
01511
01512
01513 double *covx,
01514
01515
01516
01517
01518
01519 const int mnp,
01520 void (*func)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata),
01521
01522
01523
01524
01525
01526
01527 void (*fjac)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata),
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541 void *adata,
01542
01543 const int itmax,
01544 const int verbose,
01545 const double opts[SBA_OPTSSZ],
01546
01547
01548
01549 double info[SBA_INFOSZ]
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565 )
01566 {
01567 register int i, j, ii, jj, k;
01568 int nvis, nnz, retval;
01569
01570
01571 double *jac;
01572 double *U;
01573
01574 double *e;
01575
01576 double *ea;
01577
01578 double *dp;
01579
01580 double *wght=
01581 NULL;
01582
01583
01584
01585
01586
01587
01588
01589
01590 int Asz, Usz,
01591 esz, easz, covsz;
01592
01593 register double *ptr1, *ptr2, *ptr3, *ptr4, sum;
01594 struct sba_crsm idxij;
01595
01596
01597
01598
01599
01600 int maxCPvis,
01601 *rcidxs,
01602
01603 *rcsubs;
01604
01605
01606
01607 register int itno;
01608 int nsolved;
01609
01610 double *hx,
01611 *diagU,
01612 *pdp;
01613
01614 register double mu,
01615 tmp;
01616 double p_eL2, ea_inf, pdp_eL2;
01617 double p_L2, dp_L2=DBL_MAX, dF, dL;
01618 double tau=FABS(opts[0]), eps1=FABS(opts[1]), eps2=FABS(opts[2]), eps2_sq=opts[2]*opts[2],
01619 eps3_sq=opts[3]*opts[3], eps4_sq=opts[4]*opts[4];
01620 double init_p_eL2;
01621 int nu=2, nu2, stop=0, nfev, njev=0, nlss=0;
01622 int nobs, nvars;
01623 PLS linsolver=NULL;
01624
01625 struct fdj_data_x_ fdj_data;
01626 void *jac_adata;
01627
01628
01629 mu=ea_inf=0.0;
01630
01631
01632 Asz=mnp * cnp; Usz=cnp * cnp;
01633 esz=mnp; easz=cnp;
01634 covsz=mnp * mnp;
01635
01636
01637 for(i=nvis=0, jj=n*m; i<jj; ++i)
01638 nvis+=(vmask[i]!=0);
01639
01640 nobs=nvis*mnp;
01641 nvars=m*cnp;
01642 if(nobs<nvars){
01643 fprintf(stderr, "SBA: sba_mot_levmar_x() cannot solve a problem with fewer measurements [%d] than unknowns [%d]\n", nobs, nvars);
01644 return SBA_ERROR;
01645 }
01646
01647
01648 sba_crsm_alloc(&idxij, n, m, nvis);
01649 for(i=k=0; i<n; ++i){
01650 idxij.rowptr[i]=k;
01651 ii=i*m;
01652 for(j=0; j<m; ++j)
01653 if(vmask[ii+j]){
01654 idxij.val[k]=k;
01655 idxij.colidx[k++]=j;
01656 }
01657 }
01658 idxij.rowptr[n]=nvis;
01659
01660
01661
01662 for(i=maxCPvis=0; i<n; ++i)
01663 if((k=idxij.rowptr[i+1]-idxij.rowptr[i])>maxCPvis) maxCPvis=k;
01664
01665
01666 for(j=0; j<m; ++j){
01667 for(i=ii=0; i<n; ++i)
01668 if(vmask[i*m+j]) ++ii;
01669 if(ii>maxCPvis) maxCPvis=ii;
01670 }
01671
01672
01673 jac=(double *)emalloc(nvis*Asz*sizeof(double));
01674 U=(double *)emalloc(m*Usz*sizeof(double));
01675 e=(double *)emalloc(nobs*sizeof(double));
01676 ea=(double *)emalloc(nvars*sizeof(double));
01677 dp=(double *)emalloc(nvars*sizeof(double));
01678 rcidxs=(int *)emalloc(maxCPvis*sizeof(int));
01679 rcsubs=(int *)emalloc(maxCPvis*sizeof(int));
01680 #ifndef SBA_DESTROY_COVS
01681 if(covx!=NULL) wght=(double *)emalloc(nvis*covsz*sizeof(double));
01682 #else
01683 if(covx!=NULL) wght=covx;
01684 #endif
01685
01686
01687 hx=(double *)emalloc(nobs*sizeof(double));
01688 diagU=(double *)emalloc(nvars*sizeof(double));
01689 pdp=(double *)emalloc(nvars*sizeof(double));
01690
01691
01692 if(!fjac){
01693 fdj_data.func=func;
01694 fdj_data.cnp=cnp;
01695 fdj_data.pnp=0;
01696 fdj_data.mnp=mnp;
01697 fdj_data.hx=hx;
01698 fdj_data.hxx=(double *)emalloc(nobs*sizeof(double));
01699 fdj_data.func_rcidxs=(int *)emalloc(2*maxCPvis*sizeof(int));
01700 fdj_data.func_rcsubs=fdj_data.func_rcidxs+maxCPvis;
01701 fdj_data.adata=adata;
01702
01703 fjac=sba_fdjac_x;
01704 jac_adata=(void *)&fdj_data;
01705 }
01706 else{
01707 fdj_data.hxx=NULL;
01708 jac_adata=adata;
01709 }
01710
01711 if(itmax==0){
01712 sba_mot_chkjac_x(func, fjac, p, &idxij, rcidxs, rcsubs, mcon, cnp, mnp, adata, jac_adata);
01713 retval=0;
01714 goto freemem_and_return;
01715 }
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725 if(covx!=NULL){
01726 for(i=0; i<n; ++i){
01727 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
01728 for(j=0; j<nnz; ++j){
01729
01730 ptr1=covx + (k=idxij.val[rcidxs[j]]*covsz);
01731 ptr2=wght + k;
01732 if(!sba_mat_cholinv(ptr1, ptr2, mnp)){
01733 fprintf(stderr, "SBA: invalid covariance matrix for x_ij (i=%d, j=%d) in sba_motstr_levmar_x()\n", i, rcsubs[j]);
01734 retval=SBA_ERROR;
01735 goto freemem_and_return;
01736 }
01737 }
01738 }
01739 sba_mat_cholinv(NULL, NULL, 0);
01740 }
01741
01742
01743 (*func)(p, &idxij, rcidxs, rcsubs, hx, adata); nfev=1;
01744
01745 if(covx==NULL)
01746 p_eL2=nrmL2xmy(e, x, hx, nobs);
01747 else
01748 p_eL2=nrmCxmy(e, x, hx, wght, mnp, nvis);
01749 if(verbose) printf("initial mot-SBA error %g [%g]\n", p_eL2, p_eL2/nvis);
01750 init_p_eL2=p_eL2;
01751 if(!SBA_FINITE(p_eL2)) stop=7;
01752
01753 for(itno=0; itno<itmax && !stop; ++itno){
01754
01755
01756
01757 (*fjac)(p, &idxij, rcidxs, rcsubs, jac, jac_adata); ++njev;
01758
01759 if(covx!=NULL){
01760
01761
01762
01763
01764 for(i=0; i<nvis; ++i){
01765
01766 ptr1=wght + i*covsz;
01767 ptr2=jac + i*Asz;
01768
01769
01770 for(ii=0; ii<mnp; ++ii)
01771 for(jj=0; jj<cnp; ++jj){
01772 for(k=ii, sum=0.0; k<mnp; ++k)
01773 sum+=ptr1[ii*mnp+k]*ptr2[k*cnp+jj];
01774 ptr2[ii*cnp+jj]=sum;
01775 }
01776 }
01777 }
01778
01779
01780
01781
01782
01783
01784
01785
01786
01787 _dblzero(U, m*Usz);
01788 _dblzero(ea, m*easz);
01789 for(j=mcon; j<m; ++j){
01790 ptr1=U + j*Usz;
01791 ptr2=ea + j*easz;
01792
01793 nnz=sba_crsm_col_elmidxs(&idxij, j, rcidxs, rcsubs);
01794 for(i=0; i<nnz; ++i){
01795
01796 ptr3=jac + idxij.val[rcidxs[i]]*Asz;
01797
01798
01799 for(ii=0; ii<cnp; ++ii){
01800 for(jj=ii; jj<cnp; ++jj){
01801 for(k=0, sum=0.0; k<mnp; ++k)
01802 sum+=ptr3[k*cnp+ii]*ptr3[k*cnp+jj];
01803 ptr1[ii*cnp+jj]+=sum;
01804 }
01805
01806
01807 for(jj=0; jj<ii; ++jj)
01808 ptr1[ii*cnp+jj]=ptr1[jj*cnp+ii];
01809 }
01810
01811 ptr4=e + idxij.val[rcidxs[i]]*esz;
01812
01813 for(ii=0; ii<cnp; ++ii){
01814 for(jj=0, sum=0.0; jj<mnp; ++jj)
01815 sum+=ptr3[jj*cnp+ii]*ptr4[jj];
01816 ptr2[ii]+=sum;
01817 }
01818 }
01819 }
01820
01821
01822 for(i=0, p_L2=ea_inf=0.0; i<nvars; ++i){
01823 if(ea_inf < (tmp=FABS(ea[i]))) ea_inf=tmp;
01824 p_L2+=p[i]*p[i];
01825 }
01826
01827
01828
01829
01830
01831 for(j=mcon; j<m; ++j){
01832 ptr1=U + j*Usz;
01833 ptr2=diagU + j*cnp;
01834 for(i=0; i<cnp; ++i)
01835 ptr2[i]=ptr1[i*cnp+i];
01836 }
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848 if((ea_inf <= eps1)){
01849 dp_L2=0.0;
01850 stop=1;
01851 break;
01852 }
01853
01854
01855 if(itno==0){
01856 for(i=mcon*cnp, tmp=DBL_MIN; i<nvars; ++i)
01857 if(diagU[i]>tmp) tmp=diagU[i];
01858 mu=tau*tmp;
01859 }
01860
01861
01862 while(1){
01863
01864 for(j=mcon; j<m; ++j){
01865 ptr1=U + j*Usz;
01866 for(i=0; i<cnp; ++i)
01867 ptr1[i*cnp+i]+=mu;
01868 }
01869
01870
01871 _dblzero(dp, mcon*cnp);
01872 for(j=nsolved=mcon; j<m; ++j){
01873 ptr1=U + j*Usz;
01874 ptr2=ea + j*easz;
01875 ptr3=dp + j*cnp;
01876
01877
01878 nsolved+=sba_Axb_Chol(ptr1, ptr2, ptr3, cnp, 0); linsolver=sba_Axb_Chol;
01879
01880
01881
01882
01883
01884
01885 ++nlss;
01886 }
01887
01888 if(nsolved==m){
01889
01890
01891
01892
01893 for(i=0, dp_L2=0.0; i<nvars; ++i){
01894 pdp[i]=p[i] + (tmp=dp[i]);
01895 dp_L2+=tmp*tmp;
01896 }
01897
01898
01899 if(dp_L2<=eps2_sq*p_L2){
01900
01901 stop=2;
01902 break;
01903 }
01904
01905 if(dp_L2>=(p_L2+eps2)/SBA_EPSILON_SQ){
01906
01907 fprintf(stderr, "SBA: the matrix of the augmented normal equations is almost singular in sba_mot_levmar_x(),\n"
01908 " minimization should be restarted from the current solution with an increased damping term\n");
01909 retval=SBA_ERROR;
01910 goto freemem_and_return;
01911 }
01912
01913 (*func)(pdp, &idxij, rcidxs, rcsubs, hx, adata); ++nfev;
01914 if(verbose>1)
01915 printf("mean reprojection error %g\n", sba_mean_repr_error(n, mnp, x, hx, &idxij, rcidxs, rcsubs));
01916
01917 if(covx==NULL)
01918 pdp_eL2=nrmL2xmy(hx, x, hx, nobs);
01919 else
01920 pdp_eL2=nrmCxmy(hx, x, hx, wght, mnp, nvis);
01921 if(!SBA_FINITE(pdp_eL2)){
01922 if(verbose)
01923 sba_print_inf(hx, m, mnp, &idxij, rcidxs, rcsubs);
01924
01925 stop=7;
01926 break;
01927 }
01928
01929 for(i=0, dL=0.0; i<nvars; ++i)
01930 dL+=dp[i]*(mu*dp[i]+ea[i]);
01931
01932 dF=p_eL2-pdp_eL2;
01933
01934 if(verbose>1)
01935 printf("\ndamping term %8g, gain ratio %8g, errors %8g / %8g = %g\n", mu, dL!=0.0? dF/dL : dF/DBL_EPSILON, p_eL2/nvis, pdp_eL2/nvis, p_eL2/pdp_eL2);
01936
01937 if(dL>0.0 && dF>0.0){
01938 tmp=(2.0*dF/dL-1.0);
01939 tmp=1.0-tmp*tmp*tmp;
01940 mu=mu*( (tmp>=SBA_ONE_THIRD)? tmp : SBA_ONE_THIRD );
01941 nu=2;
01942
01943
01944 if(pdp_eL2-2.0*sqrt(p_eL2*pdp_eL2)<(eps4_sq-1.0)*p_eL2) stop=4;
01945
01946 for(i=0; i<nvars; ++i)
01947 p[i]=pdp[i];
01948
01949 for(i=0; i<nobs; ++i)
01950 e[i]=hx[i];
01951 p_eL2=pdp_eL2;
01952 break;
01953 }
01954 }
01955
01956
01957
01958
01959
01960 mu*=nu;
01961 nu2=nu<<1;
01962 if(nu2<=nu){
01963 fprintf(stderr, "SBA: too many failed attempts to increase the damping factor in sba_mot_levmar_x()! Singular Hessian matrix?\n");
01964
01965 stop=6;
01966 break;
01967 }
01968 nu=nu2;
01969
01970 #if 0
01971
01972 for(j=mcon; j<m; ++j){
01973 ptr1=U + j*Usz;
01974 ptr2=diagU + j*cnp;
01975 for(i=0; i<cnp; ++i)
01976 ptr1[i*cnp+i]=ptr2[i];
01977 }
01978 #endif
01979 }
01980
01981
01982 if(p_eL2<=eps3_sq) stop=5;
01983 }
01984
01985 if(itno>=itmax) stop=3;
01986
01987
01988 for(j=mcon; j<m; ++j){
01989 ptr1=U + j*Usz;
01990 ptr2=diagU + j*cnp;
01991 for(i=0; i<cnp; ++i)
01992 ptr1[i*cnp+i]=ptr2[i];
01993 }
01994
01995 if(info){
01996 info[0]=init_p_eL2;
01997 info[1]=p_eL2;
01998 info[2]=ea_inf;
01999 info[3]=dp_L2;
02000 for(j=mcon, tmp=DBL_MIN; j<m; ++j){
02001 ptr1=U + j*Usz;
02002 for(i=0; i<cnp; ++i)
02003 if(tmp<ptr1[i*cnp+i]) tmp=ptr1[i*cnp+i];
02004 }
02005 info[4]=mu/tmp;
02006 info[5]=itno;
02007 info[6]=stop;
02008 info[7]=nfev;
02009 info[8]=njev;
02010 info[9]=nlss;
02011 }
02012
02013 retval=(stop!=7)? itno : SBA_ERROR;
02014
02015 freemem_and_return:
02016
02017
02018 free(jac); free(U);
02019 free(e); free(ea);
02020 free(dp);
02021 free(rcidxs); free(rcsubs);
02022 #ifndef SBA_DESTROY_COVS
02023 if(wght) free(wght);
02024 #else
02025
02026 #endif
02027
02028 free(hx); free(diagU); free(pdp);
02029 if(fdj_data.hxx){
02030 free(fdj_data.hxx);
02031 free(fdj_data.func_rcidxs);
02032 }
02033
02034 sba_crsm_free(&idxij);
02035
02036
02037 if(linsolver) (*linsolver)(NULL, NULL, NULL, 0, 0);
02038
02039 return retval;
02040 }
02041
02042
02043
02044
02045
02046
02047
02048
02049 int sba_str_levmar_x(
02050 const int n,
02051 const int ncon,
02052
02053
02054 const int m,
02055 char *vmask,
02056 double *p,
02057
02058 const int pnp,
02059 double *x,
02060
02061
02062
02063
02064 double *covx,
02065
02066
02067
02068
02069
02070 const int mnp,
02071 void (*func)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *hx, void *adata),
02072
02073
02074
02075
02076
02077
02078 void (*fjac)(double *p, struct sba_crsm *idxij, int *rcidxs, int *rcsubs, double *jac, void *adata),
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092 void *adata,
02093
02094 const int itmax,
02095 const int verbose,
02096 const double opts[SBA_OPTSSZ],
02097
02098
02099
02100 double info[SBA_INFOSZ]
02101
02102
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115
02116 )
02117 {
02118 register int i, j, ii, jj, k;
02119 int nvis, nnz, retval;
02120
02121
02122 double *jac;
02123 double *V;
02124
02125 double *e;
02126
02127 double *eb;
02128
02129 double *dp;
02130
02131 double *wght=
02132 NULL;
02133
02134
02135
02136
02137
02138
02139
02140
02141 int Bsz, Vsz,
02142 esz, ebsz, covsz;
02143
02144 register double *ptr1, *ptr2, *ptr3, *ptr4, sum;
02145 struct sba_crsm idxij;
02146
02147
02148
02149
02150
02151 int maxCPvis,
02152 *rcidxs,
02153
02154 *rcsubs;
02155
02156
02157
02158 register int itno;
02159 int nsolved;
02160
02161 double *hx,
02162 *diagV,
02163 *pdp;
02164
02165 register double mu,
02166 tmp;
02167 double p_eL2, eb_inf, pdp_eL2;
02168 double p_L2, dp_L2=DBL_MAX, dF, dL;
02169 double tau=FABS(opts[0]), eps1=FABS(opts[1]), eps2=FABS(opts[2]), eps2_sq=opts[2]*opts[2],
02170 eps3_sq=opts[3]*opts[3], eps4_sq=opts[4]*opts[4];
02171 double init_p_eL2;
02172 int nu=2, nu2, stop=0, nfev, njev=0, nlss=0;
02173 int nobs, nvars;
02174 PLS linsolver=NULL;
02175
02176 struct fdj_data_x_ fdj_data;
02177 void *jac_adata;
02178
02179
02180 mu=eb_inf=tmp=0.0;
02181
02182
02183 Bsz=mnp * pnp; Vsz=pnp * pnp;
02184 esz=mnp; ebsz=pnp;
02185 covsz=mnp * mnp;
02186
02187
02188 for(i=nvis=0, jj=n*m; i<jj; ++i)
02189 nvis+=(vmask[i]!=0);
02190
02191 nobs=nvis*mnp;
02192 nvars=n*pnp;
02193 if(nobs<nvars){
02194 fprintf(stderr, "SBA: sba_str_levmar_x() cannot solve a problem with fewer measurements [%d] than unknowns [%d]\n", nobs, nvars);
02195 return SBA_ERROR;
02196 }
02197
02198
02199 sba_crsm_alloc(&idxij, n, m, nvis);
02200 for(i=k=0; i<n; ++i){
02201 idxij.rowptr[i]=k;
02202 ii=i*m;
02203 for(j=0; j<m; ++j)
02204 if(vmask[ii+j]){
02205 idxij.val[k]=k;
02206 idxij.colidx[k++]=j;
02207 }
02208 }
02209 idxij.rowptr[n]=nvis;
02210
02211
02212
02213 for(i=maxCPvis=0; i<n; ++i)
02214 if((k=idxij.rowptr[i+1]-idxij.rowptr[i])>maxCPvis) maxCPvis=k;
02215
02216
02217 for(j=0; j<m; ++j){
02218 for(i=ii=0; i<n; ++i)
02219 if(vmask[i*m+j]) ++ii;
02220 if(ii>maxCPvis) maxCPvis=ii;
02221 }
02222
02223
02224 jac=(double *)emalloc(nvis*Bsz*sizeof(double));
02225 V=(double *)emalloc(n*Vsz*sizeof(double));
02226 e=(double *)emalloc(nobs*sizeof(double));
02227 eb=(double *)emalloc(nvars*sizeof(double));
02228 dp=(double *)emalloc(nvars*sizeof(double));
02229 rcidxs=(int *)emalloc(maxCPvis*sizeof(int));
02230 rcsubs=(int *)emalloc(maxCPvis*sizeof(int));
02231 #ifndef SBA_DESTROY_COVS
02232 if(covx!=NULL) wght=(double *)emalloc(nvis*covsz*sizeof(double));
02233 #else
02234 if(covx!=NULL) wght=covx;
02235 #endif
02236
02237
02238 hx=(double *)emalloc(nobs*sizeof(double));
02239 diagV=(double *)emalloc(nvars*sizeof(double));
02240 pdp=(double *)emalloc(nvars*sizeof(double));
02241
02242
02243 if(!fjac){
02244 fdj_data.func=func;
02245 fdj_data.cnp=0;
02246 fdj_data.pnp=pnp;
02247 fdj_data.mnp=mnp;
02248 fdj_data.hx=hx;
02249 fdj_data.hxx=(double *)emalloc(nobs*sizeof(double));
02250 fdj_data.func_rcidxs=(int *)emalloc(2*maxCPvis*sizeof(int));
02251 fdj_data.func_rcsubs=fdj_data.func_rcidxs+maxCPvis;
02252 fdj_data.adata=adata;
02253
02254 fjac=sba_fdjac_x;
02255 jac_adata=(void *)&fdj_data;
02256 }
02257 else{
02258 fdj_data.hxx=NULL;
02259 jac_adata=adata;
02260 }
02261
02262 if(itmax==0){
02263 sba_str_chkjac_x(func, fjac, p, &idxij, rcidxs, rcsubs, ncon, pnp, mnp, adata, jac_adata);
02264 retval=0;
02265 goto freemem_and_return;
02266 }
02267
02268
02269
02270
02271
02272
02273
02274
02275
02276 if(covx!=NULL){
02277 for(i=0; i<n; ++i){
02278 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
02279 for(j=0; j<nnz; ++j){
02280
02281 ptr1=covx + (k=idxij.val[rcidxs[j]]*covsz);
02282 ptr2=wght + k;
02283 if(!sba_mat_cholinv(ptr1, ptr2, mnp)){
02284 fprintf(stderr, "SBA: invalid covariance matrix for x_ij (i=%d, j=%d) in sba_motstr_levmar_x()\n", i, rcsubs[j]);
02285 retval=SBA_ERROR;
02286 goto freemem_and_return;
02287 }
02288 }
02289 }
02290 sba_mat_cholinv(NULL, NULL, 0);
02291 }
02292
02293
02294 (*func)(p, &idxij, rcidxs, rcsubs, hx, adata); nfev=1;
02295
02296 if(covx==NULL)
02297 p_eL2=nrmL2xmy(e, x, hx, nobs);
02298 else
02299 p_eL2=nrmCxmy(e, x, hx, wght, mnp, nvis);
02300 if(verbose) printf("initial str-SBA error %g [%g]\n", p_eL2, p_eL2/nvis);
02301 init_p_eL2=p_eL2;
02302 if(!SBA_FINITE(p_eL2)) stop=7;
02303
02304 for(itno=0; itno<itmax && !stop; ++itno){
02305
02306
02307
02308 (*fjac)(p, &idxij, rcidxs, rcsubs, jac, jac_adata); ++njev;
02309
02310 if(covx!=NULL){
02311
02312
02313
02314
02315 for(i=0; i<nvis; ++i){
02316
02317 ptr1=wght + i*covsz;
02318 ptr2=jac + i*Bsz;
02319
02320
02321 for(ii=0; ii<mnp; ++ii)
02322 for(jj=0; jj<pnp; ++jj){
02323 for(k=ii, sum=0.0; k<mnp; ++k)
02324 sum+=ptr1[ii*mnp+k]*ptr2[k*pnp+jj];
02325 ptr2[ii*pnp+jj]=sum;
02326 }
02327 }
02328 }
02329
02330
02331
02332
02333
02334
02335
02336
02337
02338 _dblzero(V, n*Vsz);
02339 _dblzero(eb, n*ebsz);
02340 for(i=ncon; i<n; ++i){
02341 ptr1=V + i*Vsz;
02342 ptr2=eb + i*ebsz;
02343
02344 nnz=sba_crsm_row_elmidxs(&idxij, i, rcidxs, rcsubs);
02345 for(j=0; j<nnz; ++j){
02346
02347 ptr3=jac + idxij.val[rcidxs[j]]*Bsz;
02348
02349
02350 for(ii=0; ii<pnp; ++ii){
02351 for(jj=ii; jj<pnp; ++jj){
02352 for(k=0, sum=0.0; k<mnp; ++k)
02353 sum+=ptr3[k*pnp+ii]*ptr3[k*pnp+jj];
02354 ptr1[ii*pnp+jj]+=sum;
02355 }
02356
02357
02358 for(jj=0; jj<ii; ++jj)
02359 ptr1[ii*pnp+jj]=ptr1[jj*pnp+ii];
02360 }
02361
02362 ptr4=e + idxij.val[rcidxs[j]]*esz;
02363
02364 for(ii=0; ii<pnp; ++ii){
02365 for(jj=0, sum=0.0; jj<mnp; ++jj)
02366 sum+=ptr3[jj*pnp+ii]*ptr4[jj];
02367 ptr2[ii]+=sum;
02368 }
02369 }
02370 }
02371
02372
02373 for(i=0, p_L2=eb_inf=0.0; i<nvars; ++i){
02374 if(eb_inf < (tmp=FABS(eb[i]))) eb_inf=tmp;
02375 p_L2+=p[i]*p[i];
02376 }
02377
02378
02379
02380
02381
02382 for(i=ncon; i<n; ++i){
02383 ptr1=V + i*Vsz;
02384 ptr2=diagV + i*pnp;
02385 for(j=0; j<pnp; ++j)
02386 ptr2[j]=ptr1[j*pnp+j];
02387 }
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398
02399 if((eb_inf <= eps1)){
02400 dp_L2=0.0;
02401 stop=1;
02402 break;
02403 }
02404
02405
02406 if(itno==0){
02407 for(i=ncon*pnp, tmp=DBL_MIN; i<nvars; ++i)
02408 if(diagV[i]>tmp) tmp=diagV[i];
02409 mu=tau*tmp;
02410 }
02411
02412
02413 while(1){
02414
02415 for(i=ncon; i<n; ++i){
02416 ptr1=V + i*Vsz;
02417 for(j=0; j<pnp; ++j)
02418 ptr1[j*pnp+j]+=mu;
02419 }
02420
02421
02422 _dblzero(dp, ncon*pnp);
02423 for(i=nsolved=ncon; i<n; ++i){
02424 ptr1=V + i*Vsz;
02425 ptr2=eb + i*ebsz;
02426 ptr3=dp + i*pnp;
02427
02428
02429 nsolved+=sba_Axb_Chol(ptr1, ptr2, ptr3, pnp, 0); linsolver=sba_Axb_Chol;
02430
02431
02432
02433
02434
02435
02436 ++nlss;
02437 }
02438
02439 if(nsolved==n){
02440
02441
02442
02443
02444 for(i=0, dp_L2=0.0; i<nvars; ++i){
02445 pdp[i]=p[i] + (tmp=dp[i]);
02446 dp_L2+=tmp*tmp;
02447 }
02448
02449
02450 if(dp_L2<=eps2_sq*p_L2){
02451
02452 stop=2;
02453 break;
02454 }
02455
02456 if(dp_L2>=(p_L2+eps2)/SBA_EPSILON_SQ){
02457
02458 fprintf(stderr, "SBA: the matrix of the augmented normal equations is almost singular in sba_motstr_levmar_x(),\n"
02459 " minimization should be restarted from the current solution with an increased damping term\n");
02460 retval=SBA_ERROR;
02461 goto freemem_and_return;
02462 }
02463
02464 (*func)(pdp, &idxij, rcidxs, rcsubs, hx, adata); ++nfev;
02465 if(verbose>1)
02466 printf("mean reprojection error %g\n", sba_mean_repr_error(n, mnp, x, hx, &idxij, rcidxs, rcsubs));
02467
02468 if(covx==NULL)
02469 pdp_eL2=nrmL2xmy(hx, x, hx, nobs);
02470 else
02471 pdp_eL2=nrmCxmy(hx, x, hx, wght, mnp, nvis);
02472 if(!SBA_FINITE(pdp_eL2)){
02473 if(verbose)
02474 sba_print_inf(hx, m, mnp, &idxij, rcidxs, rcsubs);
02475
02476 stop=7;
02477 break;
02478 }
02479
02480 for(i=0, dL=0.0; i<nvars; ++i)
02481 dL+=dp[i]*(mu*dp[i]+eb[i]);
02482
02483 dF=p_eL2-pdp_eL2;
02484
02485 if(verbose>1)
02486 printf("\ndamping term %8g, gain ratio %8g, errors %8g / %8g = %g\n", mu, dL!=0.0? dF/dL : dF/DBL_EPSILON, p_eL2/nvis, pdp_eL2/nvis, p_eL2/pdp_eL2);
02487
02488 if(dL>0.0 && dF>0.0){
02489 tmp=(2.0*dF/dL-1.0);
02490 tmp=1.0-tmp*tmp*tmp;
02491 mu=mu*( (tmp>=SBA_ONE_THIRD)? tmp : SBA_ONE_THIRD );
02492 nu=2;
02493
02494
02495 if(pdp_eL2-2.0*sqrt(p_eL2*pdp_eL2)<(eps4_sq-1.0)*p_eL2) stop=4;
02496
02497 for(i=0; i<nvars; ++i)
02498 p[i]=pdp[i];
02499
02500 for(i=0; i<nobs; ++i)
02501 e[i]=hx[i];
02502 p_eL2=pdp_eL2;
02503 break;
02504 }
02505 }
02506
02507
02508
02509
02510
02511 mu*=nu;
02512 nu2=nu<<1;
02513 if(nu2<=nu){
02514 fprintf(stderr, "SBA: too many failed attempts to increase the damping factor in sba_str_levmar_x()! Singular Hessian matrix?\n");
02515
02516 stop=6;
02517 break;
02518 }
02519 nu=nu2;
02520
02521 #if 0
02522
02523 for(i=ncon; i<n; ++i){
02524 ptr1=V + i*Vsz;
02525 ptr2=diagV + i*pnp;
02526 for(j=0; j<pnp; ++j)
02527 ptr1[j*pnp+j]=ptr2[j];
02528 }
02529 #endif
02530 }
02531
02532 if(p_eL2<=eps3_sq) stop=5;
02533 }
02534
02535 if(itno>=itmax) stop=3;
02536
02537
02538 for(i=ncon; i<n; ++i){
02539 ptr1=V + i*Vsz;
02540 ptr2=diagV + i*pnp;
02541 for(j=0; j<pnp; ++j)
02542 ptr1[j*pnp+j]=ptr2[j];
02543 }
02544
02545 if(info){
02546 info[0]=init_p_eL2;
02547 info[1]=p_eL2;
02548 info[2]=eb_inf;
02549 info[3]=dp_L2;
02550 for(i=ncon; i<n; ++i){
02551 ptr1=V + i*Vsz;
02552 for(j=0; j<pnp; ++j)
02553 if(tmp<ptr1[j*pnp+j]) tmp=ptr1[j*pnp+j];
02554 }
02555 info[4]=mu/tmp;
02556 info[5]=itno;
02557 info[6]=stop;
02558 info[7]=nfev;
02559 info[8]=njev;
02560 info[9]=nlss;
02561 }
02562
02563 retval=(stop!=7)? itno : SBA_ERROR;
02564
02565 freemem_and_return:
02566
02567
02568 free(jac); free(V);
02569 free(e); free(eb);
02570 free(dp);
02571 free(rcidxs); free(rcsubs);
02572 #ifndef SBA_DESTROY_COVS
02573 if(wght) free(wght);
02574 #else
02575
02576 #endif
02577
02578 free(hx); free(diagV); free(pdp);
02579 if(fdj_data.hxx){
02580 free(fdj_data.hxx);
02581 free(fdj_data.func_rcidxs);
02582 }
02583
02584 sba_crsm_free(&idxij);
02585
02586
02587 if(linsolver) (*linsolver)(NULL, NULL, NULL, 0, 0);
02588
02589 return retval;
02590 }