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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
# Iterative methods using reverse-communication raw material
# These methods solve
# Ax = b for x
# where A must have A.matvec(x,*args) defined
# or be a numeric array
__all__ = ['bicg','bicgstab','cg','cgs','gmres','qmr']
import _iterative
import scipy_base as sb
_type_conv = {'f':'s', 'd':'d', 'F':'c', 'D':'z'}
class get_matvec:
methname = 'matvec'
def __init__(self, obj, *args):
self.obj = obj
self.args = args
if isinstance(obj, sb.ArrayType):
self.callfunc = self.type1
return
meth = getattr(obj,self.methname,None)
if not callable(meth):
raise ValueError, "Object must be an array "\
"or have a callable %s attribute." % (self.methname,)
self.obj = meth
self.callfunc = self.type2
def __call__(self, x):
return self.callfunc(x)
def type1(self, x):
return sb.dot(self.obj, x)
def type2(self, x):
return self.obj(x,*self.args)
class get_rmatvec(get_matvec):
methname = 'rmatvec'
def type1(self, x):
return sb.dot(x, self.obj)
class get_psolve:
methname = 'psolve'
def __init__(self, obj, *args):
self.obj = obj
self.args = args
meth = getattr(obj,self.methname,None)
if meth is None: # no preconditiong available
self.callfunc = self.type1
return
if not callable(meth):
raise ValueError, "Preconditioning method %s "\
"must be callable." % (self.methname,)
self.obj = meth
self.callfunc = self.type2
def __call__(self, x):
return self.callfunc(x)
def type1(self, x):
return x
def type2(self, x):
return self.obj(x,*self.args)
class get_rpsolve(get_psolve):
methname = 'rpsolve'
class get_psolveq(get_psolve):
def __call__(self, x, which):
return self.callfunc(x, which)
def type1(self, x, which):
return x
def type2(self, x, which):
return self.obj(x,which,*self.args)
class get_rpsolveq(get_psolveq):
methname = 'rpsolve'
def bicg(A,b,x0=None,tol=1e-5,maxiter=None):
"""Use BIConjugate Gradient iteration to solve A x = b
Inputs:
A -- An array or an object with matvec(x) and rmatvec(x) methods
to represent A * x and A^H * x respectively. May also have
psolve(b) and rpsolve(b) methods for representing solutions
to the preconditioning equations M * x = b and
M^H * x = b respectively.
b -- An n-length vector
Outputs:
x -- The converged solution
info -- output result
0 : successful exit
>0 : convergence to tolerance not achieved, number of iterations
<0 : illegal input or breakdown
Optional Inputs:
x0 -- (0) default starting guess
tol -- (1e-5) relative tolerance to achieve
maxiter -- (10*n) maximum number of iterations
"""
b = sb.asarray(b)
n = len(b)
typ = b.typecode()
if maxiter is None:
maxiter = n*10
x = x0
if x is None:
x = sb.zeros(n,typ)
matvec, psolve, rmatvec, rpsolve = (None,)*4
ltr = _type_conv[typ]
revcom = _iterative.__dict__[ltr+'bicgrevcom']
stoptest = _iterative.__dict__[ltr+'stoptest2']
resid = tol
ndx1 = 1
ndx2 = -1
work = sb.zeros(6*n,typ)
ijob = 1
info = 0
ftflag = True
bnrm2 = -1.0
iter_ = maxiter
while 1:
x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \
revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob)
slice1 = slice(ndx1-1, ndx1-1+n)
slice2 = slice(ndx2-1, ndx2-1+n)
if (ijob == -1):
break
elif (ijob == 1):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(work[slice1])
elif (ijob == 2):
if rmatvec is None:
rmatvec = get_rmatvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*rmatvec(work[slice1])
elif (ijob == 3):
if psolve is None:
psolve = get_psolve(A)
work[slice1] = psolve(work[slice2])
elif (ijob == 4):
if rpsolve is None:
rpsolve = get_rpsolve(A)
work[slice1] = rpsolve(work[slice2])
elif (ijob == 5):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(x)
elif (ijob == 6):
if ftflag:
info = -1
ftflag = False
bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info)
ijob = 2
return x, info
def bicgstab(A,b,x0=None,tol=1e-5,maxiter=None):
"""Use BIConjugate Gradient STABilized iteration to solve A x = b
Inputs:
A -- An array or an object with a matvec(x) method
to represent A * x. May also have a psolve(b) methods for
representing solution to the preconditioning equation
M * x = b.
b -- An n-length vector
Outputs:
x -- The converged solution
info -- output result
0 : successful exit
>0 : convergence to tolerance not achieved, number of iterations
<0 : illegal input or breakdown
Optional Inputs:
x0 -- (0) default starting guess
tol -- (1e-5) relative tolerance to achieve
maxiter -- (10*n) maximum number of iterations
"""
b = sb.asarray(b)
n = len(b)
typ = b.typecode()
if maxiter is None:
maxiter = n*10
x = x0
if x is None:
x = sb.zeros(n,typ)
matvec, psolve = (None,)*2
ltr = _type_conv[typ]
revcom = _iterative.__dict__[ltr+'bicgstabrevcom']
stoptest = _iterative.__dict__[ltr+'stoptest2']
resid = tol
ndx1 = 1
ndx2 = -1
work = sb.zeros(7*n,typ)
ijob = 1
info = 0
ftflag = True
bnrm2 = -1.0
iter_ = maxiter
while 1:
x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \
revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob)
slice1 = slice(ndx1-1, ndx1-1+n)
slice2 = slice(ndx2-1, ndx2-1+n)
if (ijob == -1):
break
elif (ijob == 1):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(work[slice1])
elif (ijob == 2):
if psolve is None:
psolve = get_psolve(A)
work[slice1] = psolve(work[slice2])
elif (ijob == 3):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(x)
elif (ijob == 4):
if ftflag:
info = -1
ftflag = False
bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info)
ijob = 2
return x, info
def cg(A,b,x0=None,tol=1e-5,maxiter=None):
"""Use Conjugate Gradient iteration to solve A x = b (A^H = A)
Inputs:
A -- An array or an object with a matvec(x) method
to represent A * x. May also have a psolve(b) methods for
representing solution to the preconditioning equation
M * x = b.
b -- An n-length vector
Outputs:
x -- The converged solution
info -- output result
0 : successful exit
>0 : convergence to tolerance not achieved, number of iterations
<0 : illegal input or breakdown
Optional Inputs:
x0 -- (0) default starting guess
tol -- (1e-5) relative tolerance to achieve
maxiter -- (10*n) maximum number of iterations
"""
b = sb.asarray(b)
n = len(b)
typ = b.typecode()
if maxiter is None:
maxiter = n*10
x = x0
if x is None:
x = sb.zeros(n,typ)
matvec, psolve = (None,)*2
ltr = _type_conv[typ]
revcom = _iterative.__dict__[ltr+'cgrevcom']
stoptest = _iterative.__dict__[ltr+'stoptest2']
resid = tol
ndx1 = 1
ndx2 = -1
work = sb.zeros(4*n,typ)
ijob = 1
info = 0
ftflag = True
bnrm2 = -1.0
iter_ = maxiter
while 1:
x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \
revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob)
slice1 = slice(ndx1-1, ndx1-1+n)
slice2 = slice(ndx2-1, ndx2-1+n)
if (ijob == -1):
break
elif (ijob == 1):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(work[slice1])
elif (ijob == 2):
if psolve is None:
psolve = get_psolve(A)
work[slice1] = psolve(work[slice2])
elif (ijob == 3):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(x)
elif (ijob == 4):
if ftflag:
info = -1
ftflag = False
bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info)
ijob = 2
return x, info
def cgs(A,b,x0=None,tol=1e-5,maxiter=None):
"""Use Conjugate Gradient Squared iteration to solve A x = b
Inputs:
A -- An array or an object with a matvec(x) method
to represent A * x. May also have a psolve(b) methods for
representing solution to the preconditioning equation
M * x = b.
b -- An n-length vector
Outputs:
x -- The converged solution
info -- output result
0 : successful exit
>0 : convergence to tolerance not achieved, number of iterations
<0 : illegal input or breakdown
Optional Inputs:
x0 -- (0) default starting guess
tol -- (1e-5) relative tolerance to achieve
maxiter -- (10*n) maximum number of iterations
"""
b = sb.asarray(b)
n = len(b)
typ = b.typecode()
if maxiter is None:
maxiter = n*10
x = x0
if x is None:
x = sb.zeros(n,typ)
matvec, psolve = (None,)*2
ltr = _type_conv[typ]
revcom = _iterative.__dict__[ltr+'cgsrevcom']
stoptest = _iterative.__dict__[ltr+'stoptest2']
resid = tol
ndx1 = 1
ndx2 = -1
work = sb.zeros(7*n,typ)
ijob = 1
info = 0
ftflag = True
bnrm2 = -1.0
iter_ = maxiter
while 1:
x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \
revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob)
slice1 = slice(ndx1-1, ndx1-1+n)
slice2 = slice(ndx2-1, ndx2-1+n)
if (ijob == -1):
break
elif (ijob == 1):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(work[slice1])
elif (ijob == 2):
if psolve is None:
psolve = get_psolve(A)
work[slice1] = psolve(work[slice2])
elif (ijob == 3):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(x)
elif (ijob == 4):
if ftflag:
info = -1
ftflag = False
bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info)
ijob = 2
return x, info
# not working yet.
def gmres(A,b,restrt=None,x0=None,tol=1e-5,maxiter=None):
"""Use Generalized Minimal RESidual iteration to solve A x = b
Inputs:
A -- An array or an object with a matvec(x) method
to represent A * x. May also have a psolve(b) methods for
representing solution to the preconditioning equation
M * x = b.
b -- An n-length vector
Outputs:
x -- The converged solution
info -- output result
0 : successful exit
>0 : convergence to tolerance not achieved, number of iterations
<0 : illegal input or breakdown
Optional Inputs:
restrt -- (n) When to restart (change this to get faster performance -- but
may not converge).
x0 -- (0) default starting guess
tol -- (1e-5) relative tolerance to achieve
maxiter -- (10*n) maximum number of iterations
"""
b = sb.asarray(b)
n = len(b)
typ = b.typecode()
if maxiter is None:
maxiter = n*10
x = x0
if x is None:
x = sb.zeros(n,typ)
matvec, psolve = (None,)*2
ltr = _type_conv[typ]
revcom = _iterative.__dict__[ltr+'gmresrevcom']
stoptest = _iterative.__dict__[ltr+'stoptest2']
if restrt is None:
restrt = n
resid = tol
ndx1 = 1
ndx2 = -1
work = sb.zeros((6+restrt)*n,typ)
work2 = sb.zeros((restrt+1)*(2*restrt+2),typ)
ijob = 1
info = 0
ftflag = True
bnrm2 = -1.0
iter_ = maxiter
while 1:
x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \
revcom(b, x, restrt, work, work2, iter_, resid, info, ndx1, ndx2, ijob)
slice1 = slice(ndx1-1, ndx1-1+n)
slice2 = slice(ndx2-1, ndx2-1+n)
if (ijob == -1):
break
elif (ijob == 1):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(x)
elif (ijob == 2):
if psolve is None:
psolve = get_psolve(A)
work[slice1] = psolve(work[slice2])
elif (ijob == 3):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(work[slice1])
elif (ijob == 4):
if ftflag:
info = -1
ftflag = False
bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info)
ijob = 2
return x, info
def qmr(A,b,x0=None,tol=1e-5,maxiter=None):
"""Use Quasi-Minimal Residucal iteration to solve A x = b
Inputs:
A -- An array or an object with matvec(x) and rmatvec(x) methods
to represent A * x and A^H * x respectively. May also have
psolve(b,<which>) and rpsolve(b,<which>) methods for
representing solutions to the preconditioning equations
M * x = b and M^H * x = b respectively. The <which> argument
may be given to specify 'left' or 'right' preconditioning.
b -- An n-length vector
Outputs:
x -- The converged solution
info -- output result
0 : successful exit
>0 : convergence to tolerance not achieved, number of iterations
<0 : illegal input or breakdown
Optional Inputs:
x0 -- (0) default starting guess
tol -- (1e-5) relative tolerance to achieve
maxiter -- (10*n) maximum number of iterations
"""
b = sb.asarray(b)
n = len(b)
typ = b.typecode()
if maxiter is None:
maxiter = n*10
x = x0
if x is None:
x = sb.zeros(n,typ)
matvec, psolve, rmatvec, rpsolve = (None,)*4
ltr = _type_conv[typ]
revcom = _iterative.__dict__[ltr+'qmrrevcom']
stoptest = _iterative.__dict__[ltr+'stoptest2']
resid = tol
ndx1 = 1
ndx2 = -1
work = sb.zeros(11*n,typ)
ijob = 1
info = 0
ftflag = True
bnrm2 = -1.0
iter_ = maxiter
while 1:
x, iter_, resid, info, ndx1, ndx2, sclr1, sclr2, ijob = \
revcom(b, x, work, iter_, resid, info, ndx1, ndx2, ijob)
slice1 = slice(ndx1-1, ndx1-1+n)
slice2 = slice(ndx2-1, ndx2-1+n)
if (ijob == -1):
break
elif (ijob == 1):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(work[slice1])
elif (ijob == 2):
if rmatvec is None:
rmatvec = get_rmatvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*rmatvec(work[slice1])
elif (ijob == 3):
if psolve is None:
psolve = get_psolveq(A)
work[slice1] = psolve(work[slice2],'left')
elif (ijob == 4):
if psolve is None:
psolve = get_psolveq(A)
work[slice1] = psolve(work[slice2],'right')
elif (ijob == 5):
if rpsolve is None:
rpsolve = get_rpsolveq(A)
work[slice1] = rpsolve(work[slice2],'left')
elif (ijob == 6):
if rpsolve is None:
rpsolve = get_rpsolveq(A)
work[slice1] = rpsolve(work[slice2],'right')
elif (ijob == 7):
if matvec is None:
matvec = get_matvec(A)
work[slice2] *= sclr2
work[slice2] += sclr1*matvec(x)
elif (ijob == 8):
if ftflag:
info = -1
ftflag = False
bnrm2, resid, info = stoptest(work[slice1], b, bnrm2, tol, info)
ijob = 2
return x, info
|