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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009 Ralph Schreyer
Copyright (C) 2009 Klaus Spanderen
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file bicgstab.cpp
\brief bi-conjugated gradient stableized algorithm
*/
#include <ql/math/matrixutilities/bicgstab.hpp>
#include <utility>
namespace QuantLib {
BiCGstab::BiCGstab(BiCGstab::MatrixMult A,
Size maxIter,
Real relTol,
BiCGstab::MatrixMult preConditioner)
: A_(std::move(A)), M_(std::move(preConditioner)), maxIter_(maxIter), relTol_(relTol) {}
BiCGStabResult BiCGstab::solve(const Array& b, const Array& x0) const {
Real bnorm2 = Norm2(b);
if (bnorm2 == 0.0) {
BiCGStabResult result = { 0, 0.0, b};
return result;
}
Array x = ((!x0.empty()) ? x0 : Array(b.size(), 0.0));
Array r = b - A_(x);
Array rTld = r;
Array p, pTld, v, s, sTld, t;
Real omega = 1.0;
Real rho, rhoTld=1.0;
Real alpha = 0.0, beta;
Real error = Norm2(r)/bnorm2;
Size i;
for (i=0; i < maxIter_ && error >= relTol_; ++i) {
rho = DotProduct(rTld, r);
if (rho == 0.0 || omega == 0.0)
break;
if (i != 0U) {
beta = (rho / rhoTld) * (alpha / omega);
p = r + beta * (p - omega * v);
} else {
p = r;
}
pTld = (!M_ ? p : M_(p));
v = A_(pTld);
alpha = rho/DotProduct(rTld, v);
s = r-alpha*v;
if (Norm2(s) < relTol_*bnorm2) {
x += alpha*pTld;
error = Norm2(s)/bnorm2;
break;
}
sTld = (!M_ ? s : M_(s));
t = A_(sTld);
omega = DotProduct(t,s)/DotProduct(t,t);
x += alpha*pTld + omega*sTld;
r = s - omega*t;
error = Norm2(r)/bnorm2;
rhoTld = rho;
}
QL_REQUIRE(i < maxIter_, "max number of iterations exceeded");
QL_REQUIRE(error < relTol_, "could not converge");
BiCGStabResult result = { i, error, x};
return result;
}
}
|