Example: tourism industry

A Primer on Scientific Programming with Python

A Primer on ScientificProgramming with PythonHans Petter Langtangen1,21 Center for Biomedical Computing, Simula Research Laboratory2 Department of Informatics, University of OsloAug 21, 2014 PrefaceThe aim of this book is to teach computer Programming using examplesfrom mathematics and the natural sciences. We have chosen to use thePython Programming language because it combines remarkable expressivepower with very clean, simple, and compact syntax. Python is easy tolearn and very well suited for an introduction to computer is also quite similar to MATLAB and a good language for doingmathematical computing. It is easy to combine Python with compiledlanguages, like Fortran, C, and C++, which are widely used languagesfor scientific examples in this book integrate Programming with applicationsto mathematics, physics, biology, andfinance.

A Primer on Scientific Programming with Python Hans Petter Langtangen1,2 1Center for Biomedical Computing, ... study the book’s v2.7 examples, but program in Python 3. Anyway, running 2to3 on the example files generates the corresponding Python 3 ... basic programming concepts in Chapters 1-5, more advanced programming concepts in Chapters ...

Tags:

  Programming, Python, Basics, With, Example, Primer, Scientific, Primer on scientific programming with python

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of A Primer on Scientific Programming with Python

1 A Primer on ScientificProgramming with PythonHans Petter Langtangen1,21 Center for Biomedical Computing, Simula Research Laboratory2 Department of Informatics, University of OsloAug 21, 2014 PrefaceThe aim of this book is to teach computer Programming using examplesfrom mathematics and the natural sciences. We have chosen to use thePython Programming language because it combines remarkable expressivepower with very clean, simple, and compact syntax. Python is easy tolearn and very well suited for an introduction to computer is also quite similar to MATLAB and a good language for doingmathematical computing. It is easy to combine Python with compiledlanguages, like Fortran, C, and C++, which are widely used languagesfor scientific examples in this book integrate Programming with applicationsto mathematics, physics, biology, andfinance.

2 The reader is expected tohave knowledge of basic one-variable calculus as taught in mathematics-intensive programs in high schools. It is certainly an advantage to take auniversity calculus course in parallel, preferably containing both classicaland numerical aspects of calculus. Although not strictly required, abackground in high school physics makes many of the examples introductory Programming books are quite compact and focuson listing functionality of a Programming language. However, learningto program is learning how to think as a programmer. This book hasits main focus on the thinking process, or equivalently: programmingas a problem solving technique.

3 That is why most of the pages aredevoted to case studies in Programming , where we define a problem andexplain how to create the corresponding program. New constructions andprogramming styles (what we could call theory) is also usually introducedvia examples. Particular attention is paid to verification of programsand tofinding errors. These topics are very demanding for mathematicalsoftware, because the unavoidable numerical approximation errors arepossibly mixed with Programming studying the many examples in the book, I hope readers willlearn how to think right and thereby write programs in a quicker andmore reliable way.

4 Remember, nobody can learn Programming by justreading - one has to solve a large amount of exercises hands on. The bookis therefore full of exercises of various types: modifications of existingexamples, completely new problems, or debugging of given work with this book, I recommend using Python version ForChapters 5-9 and Appendices A-E you need the NumPy and Matplotlibpackages, preferably also the IPython and SciTools packages, and forAppendix G Cython is required. Other packages used occasionally in thetext are nose andsympy. Section has more information on how youcan get access to Python and the mentioned is a web page associated with this book, , containing all the example programs fromthe book as well as information on installation of the software on version 2 or 3?

5 A common problem among Python programmersis to choose between version 2 or 3, which at the time of this writing meanschoosing between version and The general recommendation is togo for Python 3, because this is the version that will be developed in thefuture. However, there is still a problem that much useful mathematicalsoftware in Python has not yet been ported to Python 3. Therefore,scientific computing with Python still goes mostly with version 2. Awidely used strategy for software developers who want to write Pythoncode that works with both versions, is to develop for version , whichis very close to what is found version , and then use the translationtool 2to3 to automatically translate from Python 2 to Python using , you should employ the newest syntax and modulesthat make the differences between Python 2 and 3 very small.

6 Thisstrategy is adopted in the present book. Only two differences betweenversions 2 and 3 are expected to be significant for the programs in thebook:a/bfor integersaandbimpliesfloat division Python 3 and integerdivision in Python 2. Moreover,print Hello in Python 2 must beturned into a function callprint( Hello )in Python 3. None of thesedifferences should lead to any annoying problems when future readersstudy the book s examples, but program in Python 3. Anyway,running2to3on the examplefiles generates the corresponding Python 1 introduces variables, objects, modules, and textformatting through examples concerning evaluation of mathematicalformulas.

7 Chapter 2 presents Programming withwhileandforloopsas well as lists, including nested lists. The next chapter deals with twoother fundamental concepts in Programming : functions andif-elsetests. Successful further reading of the book demands that Chapters 1-3are to read data into programs and deal with errors in input are thesubjects of Chapter 4. Chapter 5 introduces arrays and array computing(including vectorization) and how this is used for plottingy=f(x)curves and making animation of curves. Many of the examples in thefirstfive chapters are strongly related. Typically, formulas from thefirstchapter are used to produce tables of numbers in the second the formulas are encapsulated in functions in the third the next chapter, the input to the functions are fetched from thecommand line, or from a question-answer dialog with the user, andvalidity checks of the input are added.

8 The formulas are then shownas graphs in Chapter 5. After having studied Chapters 1- 5, the readershould have enough knowledge of Programming to solve mathematicalproblems by what many refer to as MATLAB-style 6 explains how to work dictionaries and strings, especiallyfor interpreting text data infiles and storing the extracted informationinflexible data structures. Class Programming , including user-definedtypes for mathematical computations ( with overloaded operators), isintroduced in Chapter 7. Chapter 8 deals with random numbers andstatistical computing with applications to games and random Programming , in the meaning of class hierarchies andinheritance, is the subject of Chapter 9.

9 The key examples here deal withbuilding toolkits for numerical differentiation and integration as well A introduces mathematical modeling, using sequences anddifference equations. Only Programming concepts from Chapters 1-5 areused in this appendix, the aim being to consolidate basic programmingknowledge and apply it to mathematical problems. Some importantmathematical topics are introduced via difference equations in a simpleway: Newton s method, Taylor series, inverse functions, and B deals with functions on a mesh, numerical differentiation,and numerical integration. A simple introduction to ordinary differentialequations and their numerical treatment is provided in Appendix D shows how a complete project in physics can be solved bymathematical modeling, numerical methods, and Programming elementsfrom Chapters 1-5.

10 This project is a good example on problem solvingin computational science, where it is necessary to integrate physics,mathematics, numerics, and computer to create software for solving ordinary differential equations, usingboth function-based and object-oriented Programming , is the subject ofAppendix E. The material in this appendix brings together many partsof the book in the context of physical F is devoted to the art of debugging, and in fact problemsolving in general. Speeding up numerical computations in Python byPrefacemigrating code to C via Cython is exemplified in Appendix G,. Finally,Appendix H deals with various more advanced technical of the examples and exercises in this book are quite , many of the exercises are related, and together they form largerprojects, for example on Fourier Series ( , , , , ),numerical integration ( , , , , ), Taylor series( , , , , , ), piecewise constant functions( , , , , ), inverse functions ( ),falling objects ( , , , ), oscillatory population growth( , , , ), epidemic disease modeling ( ), op-timization andfinance ( , , ), statistics and probability( , , , ), hazard games ( ), random walk andstatistical physics ( )


Related search queries