C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden C++ for Scientists - Technische Universität Dresden

math.tu.dresden.de
from math.tu.dresden.de More from this publisher
03.12.2012 Views

186 CHAPTER 5. META-PROGRAMMING template scalar times vector expressions operator∗( Scalar const& s, Vector const& v ) { return scalar times vector expressions( s, v ) ; } Put all classes and functions in the namespace athens. You can also make an expression template for the addition of two vectors. Write a small program, e.g. int main() { athens::my vector v( 5 ) ; ... Fill in some values of v ... athens::my vector w( 5 ) ; w = 5.0 ∗ v ; w = 5.0 ∗ (7.0 ∗ v ) ; w = v + 7.0∗v ; // (If you have added the operator+) } Use the debugger to see what happens.

Inheritance Chapter 6 C ++ is a multi-paradigm language and the paradigm that is most strongly associated with C ++ is ‘Object-Oriented Programming’ (OOP). The authors feel nevertheless that it is not the most important paradigm for scientific programming because it is inferior to generic programming for two major reasons: • Flexibility and • Performance. However, the impact of these two disadvantages is negligible in some situations. The performance is only deteriorated when we use virtual functions (§ 6.1). OOP in combination with generic programming is a very powerful mechanism to provide a form of reusability that neither of the paradigms can provide on it own (§ 6.3–§ 6.5). 6.1 Basic Principles See section ?? from page ?? to page ??. 6.2 Dynamic Selection by Sub-typing solver base class The way solvers are selected in AMDiS. The MTL4 solvers generic functions. AMDiS is only slightly generic but many decisions are made at run-time (by means of pointers and virtual functions). So, we needed a way to call the generic functions but decide at run time which one. The dynamic solver selection can be done with classical C features like: #include #include class matrix {}; class vector {}; void cg(const matrix& A, const vector& b, vector& x) 187

186 CHAPTER 5. META-PROGRAMMING<br />

template <br />

scalar times vector expressions operator∗( Scalar const& s, Vector const& v ) {<br />

return scalar times vector expressions( s, v ) ;<br />

}<br />

Put all classes and functions in the namespace athens. You can also make an expression template<br />

<strong>for</strong> the addition of two vectors.<br />

Write a small program, e.g.<br />

int main() {<br />

athens::my vector v( 5 ) ;<br />

... Fill in some values of v ...<br />

athens::my vector w( 5 ) ;<br />

w = 5.0 ∗ v ;<br />

w = 5.0 ∗ (7.0 ∗ v ) ;<br />

w = v + 7.0∗v ; // (If you have added the operator+)<br />

}<br />

Use the debugger to see what happens.

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

Saved successfully!

Ooh no, something went wrong!