Preface
The world is built on C++ (and its C subset).
Herb Sutter
The infrastructures at Google, Amazon, and Facebook are built using components and services designed and implemented in the C++ programming language. A considerable portion of the technology stack of operating systems, networking equipment, and storage systems is implemented in C++. In telecommunication systems, almost all landline and cellular phone connections are orchestrated by C++ software. And key components in industrial and transportation systems, including automated toll collection systems, and autonomous cars, trucks, and autobuses depend on C++.
In science and engineering, most high-quality software packages today are implemented in C++. The strength of the language is evidenced when projects exceed a certain size and data structures and algorithms become non-trivial. It is no wonder that manyif not mostsimulation software programs in computational science are realized today in C++; these include FLUENT, Abaqus, deal.II, FEniCS, OpenFOAM, and G+Smo. Even embedded systems are increasingly realized in C++ thanks to more powerful embedded processors and improved compilers. And the new application domains of the Internet of Things (IoT) and embedded edge intelligence are all driven by C++ platforms such as TensorFlow, Caffe2, and CNTK.
Core services you use every day are based on C++: your cell phone, your car, communication and industrial infrastructure, and key elements in media and entertainment services all contain C++ components. C++ services and applications are omnipresent in modern society. The reason is simple. The C++ language has progressed with its demands, and in many ways leads the innovations in programming productivity and execution efficiency. Both attributes make it the language of choice for applications that need to run at scale.
Reasons to Learn C++
Like no other language, C++ masters the full spectrum from programming sufficiently close to the hardware on one end to abstract high-level programming on the other. The lower-level programminglike user-definable memory managementempowers you as a programmer to understand what really happens during execution, which in turn helps you understand the behavior of programs in other languages. In C++ you can write extremely efficient programs that can only be slightly outperformed by code written in machine language with ridiculous effort. However, you should wait a little with the hardcore performance tuning and focus first on clear and expressive software.
This is where the high-level features of C++ come into play. The language supports a wide variety of programming paradigms directly: object-oriented programming (), among others.
Several programming techniqueslike RAII () were invented in and for C++. As the language is so expressive, it was often possible to establish these new techniques without changing the language. And who knows, maybe one day you will invent a new technique.
Reasons to Read This Book
The material in the book has been tested on real humans. The author taught his class, C++ for Scientists, over three years (i.e., three times two semesters). The students, mostly from the mathematics department, plus some from the physics and engineering departments, often did not know C++ before the class and were able to implement advanced techniques like expression templates (.
The Beauty and the Beast
C++ programs can be written in so many ways. In this book, we will lead you smoothly to the more sophisticated styles. This requires the use of advanced features that might be intimidating at first but will become less so once you get used to them. Actually, high-level programming is not only more widely applicable but is usually equally or more efficient and readable.
We will give you a first impression with a simple example: gradient descent with constant step size. The principle is extremely simple: we compute the steepest descent of f(x) with its gradient, say g(x), and follow this direction with fixed-size steps to the next local minimum. Even the algorithmic pseudo-code is as simple as this description:
Algorithm 1: Gradient descent algorithm
Input: Start value x, step size s, termination criterion , function f, gradient g
Output: Local minimum x
do
| x = xsg(x)
while |f(x)| > ;
For this simple algorithm, we wrote two quite different implementations. Please have a look and let it sink in without trying to understand the technical details.
void gradient_descent(double* x,double* y, double s, double eps,double(*f)(double, double),double(*gx)(double, double),double(*gy)(double, double)){ double val= f(*x, *y), delta; do { *x-= s * gx(*x, *y); *y-= s * gy(*x, *y); double new_val= f(*x, *y); delta= abs(new_val - val); val= new_val; } while (delta > eps);} | template <typename Value, typename P1,typename P2, typename F,typename G>Value gradient_descent(Value x, P1 s,P2 eps, F f, G g){ auto val= f(x), delta= val; do { x-= s * g(x); auto new_val= f(x); delta= abs(new_val - val); val= new_val; } while (delta > eps); return x;} |
At first glance, they look pretty similar, and we will tell you soon which one we like more. The first version is in principle pure C, i.e., compilable with a C compiler too. The benefit is that what is optimized is directly visible: a 2D function with double
values (indicated by the highlighted function parameters). We prefer the second version as it is more widely usable: two functions of arbitrary dimension with arbitrary value types (visible by the marked type and function parameters). Surprisingly, the versatile implementation is not less efficient. To the contrary, the functions given for F
and G
may be inlined (see ) so that the function call overhead is saved, whereas the explicit use of (ugly) function pointers in the first version makes this code acceleration difficult for the compiler.
A longer example comparing old and new styles is found in ) for the really patient reader. There, the benefit of modern programming is much more evident than in the toy example here. But we do not want to hold you back too long with preliminary skirmishing.
Languages in Science and Engineering
It would be nice if every kind of numeric software could be written in C++ without loss of efficiency, but unless something can be found that achieves this without compromising the C++-type system it may be preferable to rely on Fortran, assembler or architecture-specific extensions.
Bjarne Stroustrup
Scientific and engineering software is written in different languages, and which one is the most appropriate depends on the goals and available resources:
Math tools like MATLAB, Mathematica, or R are excellent when we can use their existing algorithms. When we implement our own algorithms with fine-grained (e.g., scalar) operations, we will experience a significant decrease in performance. This might not be an issue when the problems are small or the user is infinitely patient; otherwise we should consider alternative languages.