C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden C++ for Scientists - Technische Universität Dresden
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
- Page 136 and 137: 136 CHAPTER 5. META-PROGRAMMING dou
- Page 138 and 139: 138 CHAPTER 5. META-PROGRAMMING We
- Page 140 and 141: 140 CHAPTER 5. META-PROGRAMMING Fir
- Page 142 and 143: 142 CHAPTER 5. META-PROGRAMMING hig
- Page 144 and 145: 144 CHAPTER 5. META-PROGRAMMING The
- Page 146 and 147: 146 CHAPTER 5. META-PROGRAMMING tra
- Page 148 and 149: 148 CHAPTER 5. META-PROGRAMMING tem
- Page 150 and 151: 150 CHAPTER 5. META-PROGRAMMING 5.3
- Page 152 and 153: 152 CHAPTER 5. META-PROGRAMMING •
- Page 154 and 155: 154 CHAPTER 5. META-PROGRAMMING Dis
- Page 156 and 157: 156 CHAPTER 5. META-PROGRAMMING };
- Page 158 and 159: 158 CHAPTER 5. META-PROGRAMMING A s
- Page 160 and 161: 160 CHAPTER 5. META-PROGRAMMING ass
- Page 162 and 163: 162 CHAPTER 5. META-PROGRAMMING num
- Page 164 and 165: 164 CHAPTER 5. META-PROGRAMMING Usi
- Page 166 and 167: 166 CHAPTER 5. META-PROGRAMMING } v
- Page 168 and 169: 168 CHAPTER 5. META-PROGRAMMING The
- Page 170 and 171: 170 CHAPTER 5. META-PROGRAMMING onl
- Page 172 and 173: 172 CHAPTER 5. META-PROGRAMMING for
- Page 174 and 175: 174 CHAPTER 5. META-PROGRAMMING } u
- Page 176 and 177: 176 CHAPTER 5. META-PROGRAMMING } r
- Page 178 and 179: 178 CHAPTER 5. META-PROGRAMMING };
- Page 180 and 181: 180 CHAPTER 5. META-PROGRAMMING } t
- Page 182 and 183: 182 CHAPTER 5. META-PROGRAMMING };
- Page 184 and 185: 184 CHAPTER 5. META-PROGRAMMING Com
- Page 188 and 189: 188 CHAPTER 6. INHERITANCE { } std:
- Page 190 and 191: 190 CHAPTER 6. INHERITANCE 6.4.1 Ca
- Page 192 and 193: 192 CHAPTER 6. INHERITANCE dbp= sta
- Page 194 and 195: 194 CHAPTER 6. INHERITANCE Our comp
- Page 196 and 197: 196 CHAPTER 6. INHERITANCE Another
- Page 198 and 199: 198 CHAPTER 6. INHERITANCE
- Page 200 and 201: 200 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 202 and 203: 202 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 204 and 205: 204 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 206 and 207: 206 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 208 and 209: 208 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 210 and 211: 210 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 212 and 213: 212 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 214 and 215: 214 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 216 and 217: 216 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 218 and 219: 218 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 220 and 221: 220 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 222 and 223: 222 CHAPTER 7. EFFECTIVE PROGRAMMIN
- Page 225 and 226: Finite World of Computers Chapter 8
- Page 227 and 228: 8.2. MORE NUMBERS AND BASIC STRUCTU
- Page 229 and 230: 8.2. MORE NUMBERS AND BASIC STRUCTU
- Page 231 and 232: 8.4. THE OTHER WAY AROUND 231 As ca
- Page 233 and 234: How to Handle Physics on the Comput
- Page 235 and 236: Programming tools Chapter 10 In thi
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.