1 #include <petsc.h> 2 #include <petscblaslapack.h> 3 #include <taolapack.h> 4 5 #undef __FUNCT__ 6 #define __FUNCT__ "estsv" 7 static PetscErrorCode estsv(PetscInt n, PetscReal *r, PetscInt ldr, PetscReal *svmin, PetscReal *z) 8 { 9 PetscBLASInt blas1=1, blasn=n, blasnmi, blasj, blasldr = ldr; 10 PetscInt i,j; 11 PetscReal e,temp,w,wm,ynorm,znorm,s,sm; 12 13 PetscFunctionBegin; 14 for (i=0;i<n;i++) { 15 z[i]=0.0; 16 } 17 e = PetscAbs(r[0]); 18 if (e == 0.0) { 19 *svmin = 0.0; 20 z[0] = 1.0; 21 } else { 22 /* Solve R'*y = e */ 23 for (i=0;i<n;i++) { 24 /* Scale y. The scaling factor (0.01) reduces the number of scalings */ 25 if (z[i] >= 0.0) e =-PetscAbs(e); 26 else e = PetscAbs(e); 27 28 if (PetscAbs(e - z[i]) > PetscAbs(r[i + ldr*i])) { 29 temp = PetscMin(0.01,PetscAbs(r[i + ldr*i]))/PetscAbs(e-z[i]); 30 BLASscal_(&blasn, &temp, z, &blas1); 31 e = temp*e; 32 } 33 34 /* Determine the two possible choices of y[i] */ 35 if (r[i + ldr*i] == 0.0) { 36 w = wm = 1.0; 37 } else { 38 w = (e - z[i]) / r[i + ldr*i]; 39 wm = - (e + z[i]) / r[i + ldr*i]; 40 } 41 42 /* Chose y[i] based on the predicted value of y[j] for j>i */ 43 s = PetscAbs(e - z[i]); 44 sm = PetscAbs(e + z[i]); 45 for (j=i+1;j<n;j++) { 46 sm += PetscAbs(z[j] + wm * r[i + ldr*j]); 47 } 48 if (i < n-1) { 49 blasnmi = n-i-1; 50 BLASaxpy_(&blasnmi, &w, &r[i + ldr*(i+1)], &blasldr, &z[i+1], &blas1); 51 s += BLASasum_(&blasnmi, &z[i+1], &blas1); 52 } 53 if (s < sm) { 54 temp = wm - w; 55 w = wm; 56 if (i < n-1) { 57 BLASaxpy_(&blasnmi, &temp, &r[i + ldr*(i+1)], &blasldr, &z[i+1], &blas1); 58 } 59 } 60 z[i] = w; 61 } 62 63 ynorm = BLASnrm2_(&blasn, z, &blas1); 64 65 /* Solve R*z = y */ 66 for (j=n-1; j>=0; j--) { 67 /* Scale z */ 68 if (PetscAbs(z[j]) > PetscAbs(r[j + ldr*j])) { 69 temp = PetscMin(0.01, PetscAbs(r[j + ldr*j] / z[j])); 70 BLASscal_(&blasn, &temp, z, &blas1); 71 ynorm *=temp; 72 } 73 if (r[j + ldr*j] == 0) { 74 z[j] = 1.0; 75 } else { 76 z[j] = z[j] / r[j + ldr*j]; 77 } 78 temp = -z[j]; 79 blasj=j; 80 BLASaxpy_(&blasj,&temp,&r[0+ldr*j],&blas1,z,&blas1); 81 } 82 83 /* Compute svmin and normalize z */ 84 znorm = 1.0 / BLASnrm2_(&blasn, z, &blas1); 85 *svmin = ynorm*znorm; 86 BLASscal_(&blasn, &znorm, z, &blas1); 87 } 88 PetscFunctionReturn(0); 89 } 90 91 /* 92 c *********** 93 c 94 c Subroutine dgqt 95 c 96 c Given an n by n symmetric matrix A, an n-vector b, and a 97 c positive number delta, this subroutine determines a vector 98 c x which approximately minimizes the quadratic function 99 c 100 c f(x) = (1/2)*x'*A*x + b'*x 101 c 102 c subject to the Euclidean norm constraint 103 c 104 c norm(x) <= delta. 105 c 106 c This subroutine computes an approximation x and a Lagrange 107 c multiplier par such that either par is zero and 108 c 109 c norm(x) <= (1+rtol)*delta, 110 c 111 c or par is positive and 112 c 113 c abs(norm(x) - delta) <= rtol*delta. 114 c 115 c If xsol is the solution to the problem, the approximation x 116 c satisfies 117 c 118 c f(x) <= ((1 - rtol)**2)*f(xsol) 119 c 120 c The subroutine statement is 121 c 122 c subroutine dgqt(n,a,lda,b,delta,rtol,atol,itmax, 123 c par,f,x,info,z,wa1,wa2) 124 c 125 c where 126 c 127 c n is an integer variable. 128 c On entry n is the order of A. 129 c On exit n is unchanged. 130 c 131 c a is a double precision array of dimension (lda,n). 132 c On entry the full upper triangle of a must contain the 133 c full upper triangle of the symmetric matrix A. 134 c On exit the array contains the matrix A. 135 c 136 c lda is an integer variable. 137 c On entry lda is the leading dimension of the array a. 138 c On exit lda is unchanged. 139 c 140 c b is an double precision array of dimension n. 141 c On entry b specifies the linear term in the quadratic. 142 c On exit b is unchanged. 143 c 144 c delta is a double precision variable. 145 c On entry delta is a bound on the Euclidean norm of x. 146 c On exit delta is unchanged. 147 c 148 c rtol is a double precision variable. 149 c On entry rtol is the relative accuracy desired in the 150 c solution. Convergence occurs if 151 c 152 c f(x) <= ((1 - rtol)**2)*f(xsol) 153 c 154 c On exit rtol is unchanged. 155 c 156 c atol is a double precision variable. 157 c On entry atol is the absolute accuracy desired in the 158 c solution. Convergence occurs when 159 c 160 c norm(x) <= (1 + rtol)*delta 161 c 162 c max(-f(x),-f(xsol)) <= atol 163 c 164 c On exit atol is unchanged. 165 c 166 c itmax is an integer variable. 167 c On entry itmax specifies the maximum number of iterations. 168 c On exit itmax is unchanged. 169 c 170 c par is a double precision variable. 171 c On entry par is an initial estimate of the Lagrange 172 c multiplier for the constraint norm(x) <= delta. 173 c On exit par contains the final estimate of the multiplier. 174 c 175 c f is a double precision variable. 176 c On entry f need not be specified. 177 c On exit f is set to f(x) at the output x. 178 c 179 c x is a double precision array of dimension n. 180 c On entry x need not be specified. 181 c On exit x is set to the final estimate of the solution. 182 c 183 c info is an integer variable. 184 c On entry info need not be specified. 185 c On exit info is set as follows: 186 c 187 c info = 1 The function value f(x) has the relative 188 c accuracy specified by rtol. 189 c 190 c info = 2 The function value f(x) has the absolute 191 c accuracy specified by atol. 192 c 193 c info = 3 Rounding errors prevent further progress. 194 c On exit x is the best available approximation. 195 c 196 c info = 4 Failure to converge after itmax iterations. 197 c On exit x is the best available approximation. 198 c 199 c z is a double precision work array of dimension n. 200 c 201 c wa1 is a double precision work array of dimension n. 202 c 203 c wa2 is a double precision work array of dimension n. 204 c 205 c Subprograms called 206 c 207 c MINPACK-2 ...... destsv 208 c 209 c LAPACK ......... dpotrf 210 c 211 c Level 1 BLAS ... daxpy, dcopy, ddot, dnrm2, dscal 212 c 213 c Level 2 BLAS ... dtrmv, dtrsv 214 c 215 c MINPACK-2 Project. October 1993. 216 c Argonne National Laboratory and University of Minnesota. 217 c Brett M. Averick, Richard Carter, and Jorge J. More' 218 c 219 c *********** 220 */ 221 #undef __FUNCT__ 222 #define __FUNCT__ "gqt" 223 PetscErrorCode gqt(PetscInt n, PetscReal *a, PetscInt lda, PetscReal *b, 224 PetscReal delta, PetscReal rtol, PetscReal atol, 225 PetscInt itmax, PetscReal *retpar, PetscReal *retf, 226 PetscReal *x, PetscInt *retinfo, PetscInt *retits, 227 PetscReal *z, PetscReal *wa1, PetscReal *wa2) 228 { 229 PetscErrorCode ierr; 230 PetscReal f=0.0,p001=0.001,p5=0.5,minusone=-1,delta2=delta*delta; 231 PetscInt iter, j, rednc,info; 232 PetscBLASInt indef; 233 PetscBLASInt blas1=1, blasn=n, iblas, blaslda = lda,blasldap1=lda+1,blasinfo; 234 PetscReal alpha, anorm, bnorm, parc, parf, parl, pars, par=*retpar,paru, prod, rxnorm, rznorm=0.0, temp, xnorm; 235 236 PetscFunctionBegin; 237 iter = 0; 238 parf = 0.0; 239 xnorm = 0.0; 240 rxnorm = 0.0; 241 rednc = 0; 242 for (j=0; j<n; j++) { 243 x[j] = 0.0; 244 z[j] = 0.0; 245 } 246 247 /* Copy the diagonal and save A in its lower triangle */ 248 BLAScopy_(&blasn,a,&blasldap1, wa1, &blas1); 249 CHKMEMQ; 250 for (j=0;j<n-1;j++) { 251 iblas = n - j - 1; 252 BLAScopy_(&iblas,&a[j + lda*(j+1)], &blaslda, &a[j+1 + lda*j], &blas1); 253 CHKMEMQ; 254 } 255 256 /* Calculate the l1-norm of A, the Gershgorin row sums, and the 257 l2-norm of b */ 258 anorm = 0.0; 259 for (j=0;j<n;j++) { 260 wa2[j] = BLASasum_(&blasn, &a[0 + lda*j], &blas1); 261 CHKMEMQ; 262 anorm = PetscMax(anorm,wa2[j]); 263 } 264 for (j=0;j<n;j++) { 265 wa2[j] = wa2[j] - PetscAbs(wa1[j]); 266 } 267 bnorm = BLASnrm2_(&blasn,b,&blas1); 268 CHKMEMQ; 269 /* Calculate a lower bound, pars, for the domain of the problem. 270 Also calculate an upper bound, paru, and a lower bound, parl, 271 for the Lagrange multiplier. */ 272 pars = parl = paru = -anorm; 273 for (j=0;j<n;j++) { 274 pars = PetscMax(pars, -wa1[j]); 275 parl = PetscMax(parl, wa1[j] + wa2[j]); 276 paru = PetscMax(paru, -wa1[j] + wa2[j]); 277 } 278 parl = PetscMax(bnorm/delta - parl,pars); 279 parl = PetscMax(0.0,parl); 280 paru = PetscMax(0.0, bnorm/delta + paru); 281 282 /* If the input par lies outside of the interval (parl, paru), 283 set par to the closer endpoint. */ 284 285 par = PetscMax(par,parl); 286 par = PetscMin(par,paru); 287 288 /* Special case: parl == paru */ 289 paru = PetscMax(paru, (1.0 + rtol)*parl); 290 291 /* Beginning of an iteration */ 292 293 info = 0; 294 for (iter=1;iter<=itmax;iter++) { 295 /* Safeguard par */ 296 if (par <= pars && paru > 0) { 297 par = PetscMax(p001, PetscSqrtScalar(parl/paru)) * paru; 298 } 299 300 /* Copy the lower triangle of A into its upper triangle and 301 compute A + par*I */ 302 303 for (j=0;j<n-1;j++) { 304 iblas = n - j - 1; 305 BLAScopy_(&iblas,&a[j+1 + j*lda], &blas1,&a[j + (j+1)*lda], &blaslda); 306 CHKMEMQ; 307 } 308 for (j=0;j<n;j++) { 309 a[j + j*lda] = wa1[j] + par; 310 } 311 312 /* Attempt the Cholesky factorization of A without referencing 313 the lower triangular part. */ 314 LAPACKpotrf_("U",&blasn,a,&blaslda,&indef); 315 CHKMEMQ; 316 317 /* Case 1: A + par*I is pos. def. */ 318 if (indef == 0) { 319 320 /* Compute an approximate solution x and save the 321 last value of par with A + par*I pos. def. */ 322 323 parf = par; 324 BLAScopy_(&blasn, b, &blas1, wa2, &blas1); 325 CHKMEMQ; 326 LAPACKtrtrs_("U","T","N",&blasn,&blas1,a,&blaslda,wa2,&blasn,&blasinfo); 327 CHKMEMQ; 328 rxnorm = BLASnrm2_(&blasn, wa2, &blas1); 329 LAPACKtrtrs_("U","N","N",&blasn,&blas1,a,&blaslda,wa2,&blasn,&blasinfo); 330 CHKMEMQ; 331 BLAScopy_(&blasn, wa2, &blas1, x, &blas1); 332 CHKMEMQ; 333 BLASscal_(&blasn, &minusone, x, &blas1); 334 CHKMEMQ; 335 xnorm = BLASnrm2_(&blasn, x, &blas1); 336 CHKMEMQ; 337 338 /* Test for convergence */ 339 if (PetscAbs(xnorm - delta) <= rtol*delta || 340 (par == 0 && xnorm <= (1.0+rtol)*delta)) { 341 info = 1; 342 } 343 344 /* Compute a direction of negative curvature and use this 345 information to improve pars. */ 346 347 iblas=blasn*blasn; 348 349 ierr = estsv(n,a,lda,&rznorm,z);CHKERRQ(ierr); 350 CHKMEMQ; 351 pars = PetscMax(pars, par-rznorm*rznorm); 352 353 /* Compute a negative curvature solution of the form 354 x + alpha*z, where norm(x+alpha*z)==delta */ 355 356 rednc = 0; 357 if (xnorm < delta) { 358 /* Compute alpha */ 359 prod = BLASdot_(&blasn, z, &blas1, x, &blas1) / delta; 360 temp = (delta - xnorm)*((delta + xnorm)/delta); 361 alpha = temp/(PetscAbs(prod) + PetscSqrtScalar(prod*prod + temp/delta)); 362 if (prod >= 0) alpha = PetscAbs(alpha); 363 else alpha =-PetscAbs(alpha); 364 365 /* Test to decide if the negative curvature step 366 produces a larger reduction than with z=0 */ 367 rznorm = PetscAbs(alpha) * rznorm; 368 if ((rznorm*rznorm + par*xnorm*xnorm)/(delta2) <= par) { 369 rednc = 1; 370 } 371 /* Test for convergence */ 372 if (p5 * rznorm*rznorm / delta2 <= rtol*(1.0-p5*rtol)*(par + rxnorm*rxnorm/delta2)) { 373 info = 1; 374 } else if (info == 0 && (p5*(par + rxnorm*rxnorm/delta2) <= atol/delta2)) { 375 info = 2; 376 } 377 } 378 379 /* Compute the Newton correction parc to par. */ 380 if (xnorm == 0) { 381 parc = -par; 382 } else { 383 BLAScopy_(&blasn, x, &blas1, wa2, &blas1); 384 CHKMEMQ; 385 temp = 1.0/xnorm; 386 BLASscal_(&blasn, &temp, wa2, &blas1); 387 CHKMEMQ; 388 LAPACKtrtrs_("U","T","N",&blasn, &blas1, a, &blaslda, wa2, &blasn, &blasinfo); 389 CHKMEMQ; 390 temp = BLASnrm2_(&blasn, wa2, &blas1); 391 parc = (xnorm - delta)/(delta*temp*temp); 392 } 393 394 /* update parl or paru */ 395 if (xnorm > delta) { 396 parl = PetscMax(parl, par); 397 } else if (xnorm < delta) { 398 paru = PetscMin(paru, par); 399 } 400 } else { 401 /* Case 2: A + par*I is not pos. def. */ 402 403 /* Use the rank information from the Cholesky 404 decomposition to update par. */ 405 406 if (indef > 1) { 407 /* Restore column indef to A + par*I. */ 408 iblas = indef - 1; 409 BLAScopy_(&iblas,&a[indef-1 + 0*lda],&blaslda,&a[0 + (indef-1)*lda],&blas1); 410 CHKMEMQ; 411 a[indef-1 + (indef-1)*lda] = wa1[indef-1] + par; 412 413 /* compute parc. */ 414 BLAScopy_(&iblas,&a[0 + (indef-1)*lda], &blas1, wa2, &blas1); 415 CHKMEMQ; 416 LAPACKtrtrs_("U","T","N",&iblas,&blas1,a,&blaslda,wa2,&blasn,&blasinfo); 417 CHKMEMQ; 418 BLAScopy_(&iblas,wa2,&blas1,&a[0 + (indef-1)*lda],&blas1); 419 CHKMEMQ; 420 temp = BLASnrm2_(&iblas,&a[0 + (indef-1)*lda],&blas1); 421 CHKMEMQ; 422 a[indef-1 + (indef-1)*lda] -= temp*temp; 423 LAPACKtrtrs_("U","N","N",&iblas,&blas1,a,&blaslda,wa2,&blasn,&blasinfo); 424 CHKMEMQ; 425 } 426 427 wa2[indef-1] = -1.0; 428 iblas = indef; 429 temp = BLASnrm2_(&iblas,wa2,&blas1); 430 parc = - a[indef-1 + (indef-1)*lda]/(temp*temp); 431 pars = PetscMax(pars,par+parc); 432 433 /* If necessary, increase paru slightly. 434 This is needed because in some exceptional situations 435 paru is the optimal value of par. */ 436 437 paru = PetscMax(paru, (1.0+rtol)*pars); 438 } 439 440 /* Use pars to update parl */ 441 parl = PetscMax(parl,pars); 442 443 /* Test for termination. */ 444 if (info == 0) { 445 if (iter == itmax) info=4; 446 if (paru <= (1.0+p5*rtol)*pars) info=3; 447 if (paru == 0.0) info = 2; 448 } 449 450 /* If exiting, store the best approximation and restore 451 the upper triangle of A. */ 452 453 if (info != 0) { 454 /* Compute the best current estimates for x and f. */ 455 par = parf; 456 f = -p5 * (rxnorm*rxnorm + par*xnorm*xnorm); 457 if (rednc) { 458 f = -p5 * (rxnorm*rxnorm + par*delta*delta - rznorm*rznorm); 459 BLASaxpy_(&blasn, &alpha, z, &blas1, x, &blas1); 460 CHKMEMQ; 461 } 462 /* Restore the upper triangle of A */ 463 for (j = 0; j<n; j++) { 464 iblas = n - j - 1; 465 BLAScopy_(&iblas,&a[j+1 + j*lda],&blas1, &a[j + (j+1)*lda],&blaslda); 466 CHKMEMQ; 467 } 468 iblas = lda+1; 469 BLAScopy_(&blasn,wa1,&blas1,a,&iblas); 470 CHKMEMQ; 471 break; 472 } 473 par = PetscMax(parl,par+parc); 474 } 475 *retpar = par; 476 *retf = f; 477 *retinfo = info; 478 *retits = iter; 479 CHKMEMQ; 480 PetscFunctionReturn(0); 481 } 482