Академический Документы
Профессиональный Документы
Культура Документы
Part I
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
Chapter 1
Introduction
This textbook is the result of seven years of experience with teaching scientic programming in both science and engineering departments. The book has a single, clearly dened goal to convey as broad an understanding of the entire scientic computing eld as possible within a single term course. Accordingly, while the C++ programming language is explained concisely, its conceptual foundation is emphasized. Once this framework is understood, the complex language syntax can be far more easily retained. Further, all features of modern programming of relevance to scientic programming are surveyed with emphasis on strategies for simplifying coding. Free software tools are included that minimize the technical hurdles of coding and running programs.
1.1 Objective
As stated above, this textbook presents a broad introduction to modern scientic programming. This includes numerical analysis, object-oriented programming, scientic graphics, software engineering, and the modeling of advanced physical systems. Consequently, knowledge of the material will provide sufcient background to enable the reader to analyze and solve nearly all normally encountered scientic programming tasks.
1.2 Presentation
This text is concise, focusing on essential concepts. Examples are intentionally short and free of extraneous features. To promote retention, the book repeats key topics in cycles of gradually increasing difculty. Further, since the process of learning computer language shares many similarities with that of acquiring a spoken language, the most important sample program segments are highlighted in gray. Memorizing these greatly decreases the time required to achieve prociency in C++ programming.
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
Introduction
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
Features such as objects and classes introduce complex programming syntax. Accordingly, an object-oriented language requires far more time to learn than a procedural language. However, the enhanced feature set of C++ simplies many programming tasks and naturally structures a program into logically independent units. Object-oriented development is therefore often said to be required by programs with over 250,000 code lines. Since C++ constituted the rst widespread object-oriented language and additionally incorporated the earlier C procedural programming language, numerous scientic programming packages have been composed in or translated into C++. Further FORTRAN programs can be, with some effort, accessed from within a C++ program, as detailed in Appendix D. The additional functionality of C++ enables manipulations that, while rarely used, can introduce unexpected dependences among variables. The language cannot therefore normally optimize code as effectively as FORTRAN. Advanced C++ language features can, however, often be employed to circumvent these difculties, leading to execution speeds that even surpass those of equivalent FORTRAN programs, cf. Chapter 21. C++ and other object-oriented languages: A second criticism of C++ as a scientic programming language relates to certain structural limitations relative to more recently introduced object-oriented languages, in particular Java and C#. Java in particular provides a broader standard feature set than C++. Classes that, for example, handle graphics or internet communications are built into the language and are therefore supposedly the same across all Java implementations (although, in reality, version and machine dependencies exist). Java and C# additionally include newer, more exible procedures for structuring code. Unfortunately, since the Java and C++ languages are primarily oriented toward the much larger corporate business market, many of their underlying design choices are unfavorable to scientic programmers. Most signicantly, C and C++ provide nearly the same range of control of hardware resources through high-level language constructs that is available through the native machine instruction set. This range includes addressing and modifying the contents of individual memory locations and allocating and subsequently releasing the memory available to a program during execution. Since many programmers nd such facilities error prone, Java and C# instead perform much of this functionality automatically. Unfortunately, this can lead to unpredictably long execution times and precludes simple corrective remedies. Additionally, the built-in facilities in Java and C#, while useful, do not necessarily address the far more complex requirements of large scientic programs, and are not fully consistent over different software and hardware platforms (C# in particular is bound to the Windows operating system). Perhaps most signicantly, complex mathematical or scientic program libraries generally take years to adapt to new programming languages. Consequently, except for specialized uses such as receiving and viewing program results and data
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
Introduction
over the internet, C++ will likely remain the foremost object-oriented scientic programming language for years to come. Specialized programming environments: While the extensive C++ feature set benets large scientic programming projects, for small proof-of-principle computations the C++ syntax is often cumbersome. Specialized C++ scientic applications require external mathematical and graphical libraries that can be unreliable and platform dependent. In contrast, specialized scientic languages, such as M atlab , or symbolic manipulation languages, such as MAPLE or Mathematica , provide a simple, easily learned user interface to a unied built-in array of easily called and highly optimized numerical, scientic, and graphical libraries. Programs written in M atlab can be transformed into C++ code through an add-on product, while C++ and FORTRAN code can be called by a Matlab program after some effort. Further, a given version of a dedicated package such as Matlab originates from a single commercial source and therefore functions nearly identically across all supported platforms (assuming the same Matlab version number). On the negative side, the absence of high-level features, such as classes, type-checking, and user-controlled memory management, can lead to structural confusion, programming errors and runtime inefciency for larger problems. As well, the software is unavailable at many sites because of its substantial cost. A brief introduction to M atlab is presented in Appendix A. A working knowledge of the language can be quickly acquired from this appendix in conjunction with the extensive Matlab help system.
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
1.5 Summary
The organization of this book is as follows: After providing instructions for installing a free C++ programming environment, the basic structure and syntax of the C++ language and of computer hardware and software architecture are summarized in the initial chapters. Chapters 6 and 7 then provide a survey of the principles, structure, and syntax of modern object-oriented programming based on the industry-standard Rational Rose software engineering package. Subsequent chapters examine basic C++ constructs, such as control constructs, arrays, input and output, numerical methods, and functions that are required for elementary numerical programming. This is followed by an introduction to numerical analysis including such topics as differentiation, integration, root-nding methods, error estimation, and numerical interpolation. The last part of the text surveys complex aspects of C++ scientic programming. These include language features, such as pointers, references, dynamic memory allocation, templates, and inheritance. Program optimization and numerical techniques for Monte-Carlo methods and partial differential equation solvers, together with a series of appendices that discuss the Matlab languages, alternative free compilers, code proling, calling FORTRAN programs from C++, and, nally, C++ coding standards conclude the text. The programming exercises at the back of each chapter comprise both standard examination questions and fundamental scientic programming examples from different areas of physics, chemistry, biology, and engineering. A central feature of these examples is that object-oriented skeleton code is generally provided. In this manner, the student naturally absorbs object-oriented design, that is the translation of physical problems into a proper object-oriented framework, which is inevitably the most challenging topic for the beginning programmer. As well, by providing difcult code lines mistakes are avoided that would normally require many hours to resolve. As the book progresses, an increasing amount of detail is omitted from the examples. Solving these problems will rapidly lead to prociency in objectoriented scientic programming.
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
Introduction
(5) Finally complete the more challenging second part programming projects, which illustrate real-world scientic programming. Within this context, review again the material in the chapter.
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
implementation of advanced C++ features for certain mathematical operations, cf. Chapter 21. Commercial C++ mathematical and scientic routine packages include the standard IMSL and NAG libraries. One of the oldest and most stable source code libraries for scientic applications, the CERN library, is also transitioning from FORTRAN and C to C++; however, due to its size, installation is non-trivial and the number of supported compilers is restricted.
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
Chapter 2
We begin by presenting a step-by-step procedure for installing the freeware DevC++ development environment and DISLIN graphics software contained on the accompanying CD-ROM. The programs in this book can also be compiled and run somewhat more efciently through the stand-alone command line (MS-DOS window) Borland or mingw (g++) compilers. This software is contained in separate directories in the CD-ROM and discussed in Appendices B and C, respectively. Dev-C++ is a simple integrated set of development tools based on the Linux g++ compiler. It is available both on the native Linux platform and as a Windows port. The platform contains, among other programs, a FORTRAN 77 compiler (g77), a debugger (gdb), and a proler (gprof ). If you wish to install the latest versions of the software, as opposed to the version on the CD-ROM, the Dev-C++ homepage, which contains updated packages, is http://www.bloodshed.net/devcpp.html. Links to all free compilers, including the command line versions of Borland C++ and Microsoft Visual C++, are at the time of writing found at http://www.thefreecountry.com/compilers/cpp.shtml. This site currently lists all free C++ compilers. To install Dev-C++ from the enclosed CD-ROM simply navigate to and double click on the le devcpp4990setup.exe in the Dev-C++ directory. We assume below that you select to install this package into the directory X:\Dev-Cpp, where X: is the drive letter (partition).
10
www.cambridge.org
Cambridge University Press 0521827787 - A First Course in Computational Physics and Object-Oriented Programming with C++ David Yevick Excerpt More information
11
// // // //
Hello world v. 1.0 Aug. 11 2000 (your name) This program tests the C++ environment
#include <iostream.h> #include <windows.h> main () { cout << Hello World << endl; Sleep(4000); }
Note that any text on a line to the right of two forward slashes is treated as a comment and is not read as part of the C++ program. You may later want to try another variant of the program, which instead pauses the program indenitely until any key is depressed
#include <iostream.h> #include <conio.h> main () { cout << Hello World << endl; cout << Press any key to continue << endl; getch(); }
The two lines that start with #include together with the Sleep( ) or getch( ) function will not be repeated in most of the remaining programs in this textbook, although they must generally be present in every program that is run from within Dev-C++ (otherwise only the #include <iostream.h> line is required). In entering programs, names are frequently misspelled or improperly capitalized, leading to unexpected consequences, especially when characters that look nearly alike are substituted for one another. In particular 0 and O and 1 and l are very difcult to distinguish at low screen or printer resolution and can lead to subtle errors, such as when 1 < 10 (1.0 < 10) is used in place of 1 < 10 (the letter 1 < 10). As well, the semicolon at the end of a statement is often omitted and two successive lines are read by the compiler as a single statement. This often produces cryptic error messages with incorrect line numbering. Further, ne details of the program syntax, such as the use of double quotation marks rather than single quotation marks, braces instead of parentheses above and the lack of a semicolon after an #include statement, must be noted with great care, as these initially present major challenges. (b) Saving the le and running the program: Save the le by depressing the fourth icon from the left on the upper button bar, which generates the oat-over text Save when the cursor is positioned over it. Type in a name for the le (do not add a .cpp extension to your le name here this is done automatically). Then click on the third icon from the left on the lower button bar with oat-over text Compile and Run. A compilation progress window should appear after which the program executes.
www.cambridge.org