1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/*
Find the largest value for alpha<=1 such that X+alpha*dX is PD.
Return the optimal X+alpha*dX in work1. If the search fails,
return alpha=-1.0.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <csdp/declarations.h>
#define LANCZOSITS 30
double linesearch(
int n,
struct blockmatrix dX,
struct blockmatrix work1,
struct blockmatrix work2,
struct blockmatrix work3,
struct blockmatrix cholinv,
double *q,
double *z,
double *workvec,
double stepfrac,
double start,
int printlevel)
{
int i,j,jj;
double alpha;
double scale1;
double scale2;
int inc;
double lalpha[LANCZOSITS+1];
double lbeta[LANCZOSITS+1];
double lbeta2[LANCZOSITS+1];
double evalues[LANCZOSITS+2];
double maxeigs[LANCZOSITS+1];
double reorth[LANCZOSITS+1];
double maxeig;
int maxn,blk,method;
double *lanczosvectors;
/*
* Allocate space for storing the Lanczos vectors.
*/
lanczosvectors=(double *) malloc((LANCZOSITS+1)*n*sizeof(double));
if (lanczosvectors==NULL)
{
printf("Storage Allocation Failed!\n");
exit(205);
};
/*
* First, figure out which method to use. If maxn is big, then use
* lots of matrix/vector mults. If maxn is small, then multiply the
* matrices once and for all.
*/
maxn=0;
for (blk=1; blk<=work1.nblocks; blk++)
{
if ((work1.blocks[blk].blocksize > maxn) &&
(work1.blocks[blk].blockcategory==MATRIX))
maxn=work1.blocks[blk].blocksize;
};
if (maxn > 6*LANCZOSITS)
{
method=1; /* matrix vector mults. */
if (printlevel >= 4)
{
printf("linesearch method 1 \n");
};
}
else
{
method=2; /* matrix matrix mults. */
if (printlevel >= 4)
{
printf("linesearch method 2 \n");
};
};
/*
*
*/
if (method==1)
{
scale1=-1.0;
zero_mat(work1);
store_unpacked(cholinv,work3);
triu(work3);
addscaledmat(work1,scale1,work3,work2);
trans(work2);
}
else
{
/*
* method=2.
*/
/*
* First, multiply dX*cholinv. Store it in work3.
*/
scale1=1.0;
scale2=0.0;
store_unpacked(cholinv,work2);
triu(work2);
mat_mult(scale1,scale2,dX,work2,work3);
/*
* Now, find R^{-T}
*/
trans(work2);
scale1=-1.0;
scale2=0.0;
mat_mult(scale1,scale2,work2,work3,work1);
};
/*
* Initialize q.
*/
for (i=1; i<=n; i++)
q[i]=1.0/sqrt(n*1.0);
for (i=1; i<=n; i++)
lanczosvectors[ijtok(i,1,n)]=q[i];
/*
* Next, perform the Lancoz iterations.
*/
maxeig=-1.0e200;
for (j=1; j<=LANCZOSITS; j++)
{
maxeigs[j]=-1.0e100;
if (method == 1)
{
matvec(work3,q,z);
matvec(dX,z,workvec);
matvec(work2,workvec,z);
}
else
{
matvec(work1,q,z);
};
lalpha[j]=0.0;
for (i=1; i<=n; i++)
lalpha[j]=lalpha[j]+q[i]*z[i];
/*
* We'll use BLAS routines to do the reorthogonalization. First,
* Compute reorth=lanczosvectors'*z. Then compute
* z=z-lanczosvectors*reorth.
*
* lanczosvectors is of size n by j, with lda=n.
*/
scale1=1.0;
scale2=0.0;
inc=1;
#ifdef HIDDENSTRLEN
dgemv_("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc,1);
#else
#ifdef NOUNDERBLAS
#ifdef CAPSBLAS
DGEMV("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#else
dgemv("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#endif
#else
#ifdef CAPSBLAS
DGEMV_("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#else
dgemv_("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#endif
#endif
#endif
scale1=-1.0;
scale2=1.0;
inc=1;
#ifdef HIDDENSTRLEN
dgemv_("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc,1);
#else
#ifdef NOUNDERBLAS
#ifdef CAPSBLAS
DGEMV("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#else
dgemv("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#endif
#else
#ifdef CAPSBLAS
DGEMV_("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#else
dgemv_("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#endif
#endif
#endif
scale1=1.0;
scale2=0.0;
inc=1;
#ifdef HIDDENSTRLEN
dgemv_("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc,1);
#else
#ifdef NOUNDERBLAS
#ifdef CAPSBLAS
DGEMV("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#else
dgemv("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#endif
#else
#ifdef CAPSBLAS
DGEMV_("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#else
dgemv_("T",&n,&j,&scale1,lanczosvectors,&n,z+1,&inc,&scale2,reorth+1,&inc);
#endif
#endif
#endif
scale1=-1.0;
scale2=1.0;
inc=1;
#ifdef HIDDENSTRLEN
dgemv_("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc,1);
#else
#ifdef NOUNDERBLAS
#ifdef CAPSBLAS
DGEMV("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#else
dgemv("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#endif
#else
#ifdef CAPSBLAS
DGEMV_("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#else
dgemv_("N",&n,&j,&scale1,lanczosvectors,&n,reorth+1,&inc,&scale2,z+1,&inc);
#endif
#endif
#endif
/*
* Compute the norm of z.
*/
lbeta[j]=norm2(n,z+1);
if (fabs(lbeta[j])<1.0e-16)
{
if (printlevel >= 3)
printf("Small beta[j]\n");
j=j-1;
jj=j;
goto DONEEARLY;
};
for (i=1; i<=n; i++)
q[i]=z[i]/lbeta[j];
/*
* Store the Lanczos vector.
*/
for (i=1; i<=n; i++)
lanczosvectors[ijtok(i,j+1,n)]=q[i];
if (j>=5)
{
/*
* Now, get ready to call qreig to get the eigenvalues.
*/
for (i=1; i<=j-1; i++)
lbeta2[i]=lbeta[i]*lbeta[i];
for (i=1; i<=j; i++)
evalues[i]=lalpha[i];
qreig(j,evalues,lbeta2);
maxeigs[j]=-1.0e100;
for (i=1; i<=j; i++)
{
if (printlevel >= 4)
printf ("qreig evalue %e \n",evalues[i]);
if (evalues[i] > maxeigs[j])
maxeigs[j]=evalues[i];
};
if (maxeigs[j] > maxeig)
maxeig=maxeigs[j];
};
/*
* Now, decide whether or not to stop.
*/
if ((j>=7) && (maxeigs[j] <= 1/(3*start)) &&
(fabs((maxeigs[j]-maxeigs[j-2])/(0.000001+fabs(maxeigs[j]))) < 0.2))
{
if (printlevel >= 4)
printf("Stopping on <1/3s j=%d \n",j);
jj=j;
goto DONEEARLY;
};
if ((j>=8) && (fabs((maxeigs[j]-maxeigs[j-2])/(0.000001+fabs(maxeigs[j]))) < 0.02))
{
if (printlevel >= 4)
printf("Stopping here, on tightness j=%d \n",j);
maxeig=maxeig+0.01*fabs(maxeig);
jj=j;
goto DONEEARLY;
};
};
jj=LANCZOSITS;
DONEEARLY:
if (printlevel >= 4)
{
for (i=1; i<=jj; i++)
printf("maxeigs[%d]=%e \n",i,maxeigs[i]);
printf("maxeig %e \n",maxeig);
};
if (printlevel >= 4)
printf("Lancoz converged after %d iters\n",jj);
if (printlevel >= 3)
{
if (maxeig >0.0)
printf("eigsearch: alpha=%e \n",stepfrac/maxeig);
else
printf("eigsearch: alpha=+Inf\n");
};
if (((stepfrac/maxeig) < start) && (maxeig > 0))
alpha=stepfrac/maxeig;
else
alpha=start;
free(lanczosvectors);
return(alpha);
}
|