03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Vector p(resource(x)), q(resource(x)), r(resource(x)), z(resource(x));<br />

r = b − A∗x;<br />

while (! iter.finished(r)) {<br />

z = solve(L, r);<br />

rho = dot(r, z);<br />

if (iter.first())<br />

p = z;<br />

else<br />

p = z + (rho / rho 1) ∗ p;<br />

q = A ∗ p;<br />

alpha = rho / dot(p, q);<br />

x += alpha ∗ p;<br />

r −= alpha ∗ q;<br />

rho 1 = rho;<br />

++iter;<br />

}<br />

return iter;<br />

}<br />

int main (int argc, char∗ argv[])<br />

{<br />

int size;<br />

}<br />

// set size<br />

mtl::compressed2D A(size, size);<br />

mtl::dense vector x(size), b(size);<br />

// set A and b<br />

// Create preconditioner<br />

itl::pc::ic 0 L(A);<br />

// Object that controls iteration, terminate if residual is below 10ˆ−9 or decrease<br />

// by 6 orders of magnitude, abord after 30 iterations if not converged<br />

itl::basic iteration iter(b, 30, 1.e−6, 1.e−9);<br />

conjugate gradient(A, x, b, L, iter);<br />

return 0 ;<br />

Listing 1.2: High Abstraction Implementation of CG<br />

The first thing you might realize is that the CG implementation is readable without comments.<br />

As a thumb of rule, if other people’s comments look like your program sources then you are<br />

a really good programmer. If you compare the mathematical notation in Algorithm 1 with<br />

Listing 1.2 you will realize that — except <strong>for</strong> the type and variable declarations at the beginnig<br />

— they are identical. Some readers might think that it looks more like Matlab or Mathematica<br />

15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!