C++ for Scientists - Technische Universität Dresden
C++ for Scientists - Technische Universität Dresden C++ for Scientists - Technische Universität Dresden
228 CHAPTER 8. FINITE WORLD OF COMPUTERS namespace compiletime { template typename result of::set coeff::type set coeff(Polynomial const &p, Coeff const &c); } namespace runtime { template typename result of::set coeff::type set coeff(index type n, Polynomial const &p, Coeff const &c); } Write access is then available by: polynomial p; compiletime::set coeff(p, 1); runtime::set coeff(n, p, 1); The degree of the polynomial is defined as the maximum degree of all of its terms, where the degree of a term is given as the sum of the degree of all variables in this term. The polynomial library defines the degree as the index of the highest non-zero coefficient. To obtain the correct degree requires to use a polynomial for each variable and finally combine them: struct X; typedef polynomial< X, fusion::map< pair< mpl::int , double> > > inner poly; degree � 3 x 4 y 2� = 4 + 2 = 6 typedef polynomial< Y, fusion::map< pair< mpl::int , inner poly> > > the polynomial; By instantiating the polynomial the calculation of its degree is possible: the polynomial p; assert( degree(p) == 6 );
8.2. MORE NUMBERS AND BASIC STRUCTURE 229 8.2.3 Compile Time Programming The application of meta-programming is presented which utilizes the compiler to execute code at compile time and then reduce the result of the expressions. As an example the derivative of a second-degree polynomial is calculated and a second polynomial is added: d(3 + 4.5 x + 10 x 2 ) dx + (1 + 2x) = 5.5 + 22 x The type list represents the type of each coefficient starting from the zero to the second degree coefficient. struct X { } x; typedef fusion::vector coeffs; typedef polynomial poly; poly p(x, coeffs(3.0, 4.5, 10)); typedef result of::diff::type diffed; diffed d = diff(p, x); poly q(x, coeffs(1.0, 2.0, 0)); std::cout ≪ coeff(q + d); By compiling and evaluating the assembler code it is revealed that the calculations were performed at compile time and the binary only contains the final result of 22. 8.2.4 Arbitrary-Precision Arithmetic The application of the polynomial library to perform arbitrary-precision arithmetic (or “bignum arithmetic”) is also presented here. It uses the fact that a number is in essence a polynomial with a fixed base. 1372 = 1 · 10 3 + 3 · 10 2 + 7 · 10 + 2 This can easily be translated into C++ code by using the polynomial library. Note, that the first element in the array is the zero coefficient: typedef unsigned char byte t; typedef array coeffs t; coeffs t coeffs = {{2, 7, 3, 1}}; gsse::polynomial p(coeffs); Since computer systems usually operate on binary numbers base-2 is the optimal choice. The difference between polynomial arithmetic and arbitrary-precision arithmetic is that the coefficients need to be realigned to the base after each operation.
- 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 186 and 187: 186 CHAPTER 5. META-PROGRAMMING tem
- 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: 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
- Page 237 and 238: 10.2. DEBUGGING 237 T& glas::contin
- Page 239 and 240: 10.3. VALGRIND 239 Stepi and Nexti
- Page 241 and 242: 10.5. UNIX AND LINUX 241 • top: l
- Page 243 and 244: C ++ Libraries for Scientific Compu
- Page 245 and 246: 11.3. BOOST.BINDINGS 245 • Math a
- Page 247 and 248: 11.3. BOOST.BINDINGS 247 #include
- Page 249 and 250: 11.4. MATRIX TEMPLATE LIBRARY 249 c
- Page 251 and 252: 11.7. GEOMETRIC LIBRARIES 251 11.7.
- Page 253 and 254: Real-World Programming Chapter 12 1
- Page 255 and 256: 12.1. TRANSCENDING LEGACY APPLICATI
- Page 257 and 258: 12.1. TRANSCENDING LEGACY APPLICATI
- Page 259 and 260: Parallelism Chapter 13 13.1 Multi-T
- Page 261 and 262: 13.2. MESSAGE PASSING 261 int main
- Page 263 and 264: Numerical exercises Chapter 14 In t
- Page 265 and 266: 14.1. COMPUTING AN EIGENFUNCTION OF
- Page 267 and 268: 14.1. COMPUTING AN EIGENFUNCTION OF
- Page 269 and 270: 14.1. COMPUTING AN EIGENFUNCTION OF
- Page 271 and 272: 14.1. COMPUTING AN EIGENFUNCTION OF
- Page 273 and 274: 14.3. THE SOLUTION OF A SYSTEM OF D
- Page 275 and 276: 14.4. GOOGLE’S PAGE RANK 275 Taki
- Page 277 and 278: 14.5. THE BISECTION METHOD FOR FIND
8.2. MORE NUMBERS AND BASIC STRUCTURE 229<br />
8.2.3 Compile Time Programming<br />
The application of meta-programming is presented which utilizes the compiler to execute code<br />
at compile time and then reduce the result of the expressions. As an example the derivative of<br />
a second-degree polynomial is calculated and a second polynomial is added:<br />
d(3 + 4.5 x + 10 x 2 )<br />
dx<br />
+ (1 + 2x) = 5.5 + 22 x<br />
The type list represents the type of each coefficient starting from the zero to the second degree<br />
coefficient.<br />
struct X { } x;<br />
typedef fusion::vector coeffs;<br />
typedef polynomial poly;<br />
poly p(x, coeffs(3.0, 4.5, 10));<br />
typedef result of::diff::type diffed;<br />
diffed d = diff(p, x);<br />
poly q(x, coeffs(1.0, 2.0, 0));<br />
std::cout ≪ coeff(q + d);<br />
By compiling and evaluating the assembler code it is revealed that the calculations were per<strong>for</strong>med<br />
at compile time and the binary only contains the final result of 22.<br />
8.2.4 Arbitrary-Precision Arithmetic<br />
The application of the polynomial library to per<strong>for</strong>m arbitrary-precision arithmetic (or “bignum<br />
arithmetic”) is also presented here. It uses the fact that a number is in essence a polynomial<br />
with a fixed base.<br />
1372 = 1 · 10 3 + 3 · 10 2 + 7 · 10 + 2<br />
This can easily be translated into <strong>C++</strong> code by using the polynomial library. Note, that the<br />
first element in the array is the zero coefficient:<br />
typedef unsigned char byte t;<br />
typedef array coeffs t;<br />
coeffs t coeffs = {{2, 7, 3, 1}};<br />
gsse::polynomial p(coeffs);<br />
Since computer systems usually operate on binary numbers base-2 is the optimal choice. The<br />
difference between polynomial arithmetic and arbitrary-precision arithmetic is that the coefficients<br />
need to be realigned to the base after each operation.