Practical Numerical Computing Using Python 1st Edition Mahendra Verma download
Practical Numerical Computing Using Python 1st Edition Mahendra Verma download
https://ebookmeta.com/product/practical-numerical-computing-
using-python-1st-edition-mahendra-verma/
https://ebookmeta.com/product/practical-explainable-ai-using-
python-artificial-intelligence-model-explanations-using-python-
based-libraries-extensions-and-frameworks-pradeepta-mishra/
https://ebookmeta.com/product/architecture-of-advanced-numerical-
analysis-systems-designing-a-scientific-computing-system-using-
ocaml-1st-edition-liang-wang/
https://ebookmeta.com/product/the-practice-of-computing-using-
python-3-regular-edition-william-punch-richard-enbody/
https://ebookmeta.com/product/intelligence-in-energy-1st-edition-
gulgun-kayakutlu/
Rational Points on Elliptic Curves 2nd Edition Joseph H
Silverman John T Tate
https://ebookmeta.com/product/rational-points-on-elliptic-
curves-2nd-edition-joseph-h-silverman-john-t-tate/
https://ebookmeta.com/product/heritage-culture-and-identity-1st-
edition-hamzah-muzaini/
https://ebookmeta.com/product/contraception-and-persecution-1st-
edition-charles-e-rice-alyssa-bormes-steve-mosher/
https://ebookmeta.com/product/suits-a-woman-on-wall-street-1st-
edition-nina-godiwalla/
https://ebookmeta.com/product/music-and-mental-imagery-1st-
edition-mats-b-kussner/
The Independent Republic of Arequipa Making Regional
Culture in the Andes Thomas F Love
https://ebookmeta.com/product/the-independent-republic-of-
arequipa-making-regional-culture-in-the-andes-thomas-f-love/
Contents
Preface
Chapter One
Introduction to Computing
Computer Hardware
Supercomputers & Computation Complexity
Computer Software
Brief Overview of Python
Anaconda Python, Notebooks, and Prutor
Applications of Computers
Chapter Two
Integers
Floating Point and Complex Numbers
Python Variables and Logical Variables
Chapter Three
Chapter Four
Simple Statements
Conditional Flows in Python
Looping in Python
Chapter Five
Functions in Python
Python Namespace and Scope
Recursive Functions
Chapter Six
Chapter Eight
Chapter Nine
Error Analysis
Nondimensionalization of Equations
Numerical Methods
Chapter Ten
Lagrange Interpolation
Splines
Chapter Eleven
Newton-Cotes Formulas
Gaussian Quadrature
Python's Quad & Multidimensional Integrals
Chapter Twelve
Computing Numerical Derivatives
Chapter Thirteen
General Overview
Euler Forward Method, Accuracy & Stability
Implicit Schemes
Higher-order Methods
Multistep Method
Solving a System of Equations
Stiff Equations
Chapter Fourteen
Fourier Transform
One-dimensional Discrete Fourier Transforms
Mutlidimensional Fourier Transform
Chapter Fifteen
Chapter Sixteen
General Overview & Diffusion Equation Solver
Solving Wave Equation
Burgers and Navier-Stokes Equations
Schrodinger equation
Chapter Seventeen
Root Finders
Chapter Eighteen
Shooting Method
Eigenvalue Calculation
Chapter Nineteen
Chapter Twenty
Chapter Twenty-One
Random numbers
Integration Using Random Numbers
Regression Analysis
Applications in Statmech
Machine Learning
Epilogue
Appendix A: Errors in Lagrange Interopolation
Appendix B: Improving Accuracy Using Richardson Method
References
Preface
At present, friendly, yet, powerful tools have been developed for computer
programming. One such tool, the programming language Python is versatile, but
easy to learn. It is being used in wide range of applications: scientific computing,
data analysis, machine learning (ML) and artificial intelligence (AI), internet
programming, GUI, etc. At present, researchers are employing Python for
numerical computing, as well as for AI and ML.
Keeping this in mind, I chose Python as the programming language for
teaching numerical computing in my Computational Physics course
(PHY473A). I also use Python for post processing and for writing large
softwares including parallel programs. The present book is a compilation of the
course material and the tools developed in our computational laboratory. The
contents and the usage of the book is discussed below.
Contents of the book: The book has two parts. The first part covers the Python
programming language in a reasonable detail, while the second part contains the
numerical algorithms and associated Python codes. The book contains
discussions on important numerical tools: interpolation, integration,
differentiation, solvers for ordinary and partial differential equations, Fourier
transforms, boundary value problems, linear algebra, and Monte Carlo methods.
In addition, I also include plotting tools, error analysis, nondimensionalization,
and an overview of computer hardware and software.
The computer programs in the book have been tested. These codes are
available at the website https://sites.google.com/view/py-comp. The website will
also host affiliated material, such as PPTs, video lectures, color figures, etc.
Usage of the book: This book is suitable for advanced undergraduate and
graduate students. It does not assume any programming background, but it does
require basic understanding of calculus and differential equations. The material
could be covered in 40 lectures at a fast pace. However, I recommend that the
instructor and students can skip topics that they find complex or somewhat
unnecessary.
Programming is learnt by practice. Hence, I strongly urge the students to
program enthusiastically. One could start with the examples and then do all the
exercises of the book. In my course, we used Prutor (https://prutor.ai) for
evaluating the exercises submitted by the students. We plan to provide the
exercises of the book on Prutor.
Feedback request: I request the readers to send their comments and feedback to
me at mkv@iitk.ac.in. Even though I have strived to make this book error-free, I
am sure some lacuna still remain. I will be grateful if such errors are brought to
my attention.
Mahendra Verma
IIT Kanpur
CHAPTER ONE
Introduction
Synopsis
COMPUTER IS ONE device that has impacted all walks of life. It is employed
for scientific research, e-commerce, banking, cloud computing, etc. On a
personal level, we use computers for surfing internet, emailing, social
networking, etc. See Section Applications of Computers for some of the
computer applications.
ENIAC (Electronic Numerical Integrator and Computer, made in 1945) was
the first general-purpose digital computer. It was used for making Hydrogen
bomb and Monte Carlo simulations. A succession of better and faster computers
have been built since then. Scientists and engineers have used these machines for
research in physics, chemistry, biology, fluids, engineering, geophysics, and
astrophysics. In this book, we will focus on generic computation tools used in
science and engineering.
Both hardware and software have evolved leaps and bounds over the years.
In early days, Fortran, short form for Formula Translation, was the de facto
programming language for scientific applications. Even though Fortran is still a
dominant language in scientific programming, many scientists and engineers
have moved to modern languages such as C, C++, and Python. Among these
languages, Python has become very popular due to its simplicity and availability
of large number of Python modules. In this book, we will use Python language
for writing programs. An added benefit of Python is that it has excellent post-
processing tools, such as plotting, reading/writing data, etc. In addition, Python
is widely used in machine learning (ML) and artificial intelligence (AI). Python
is also becoming popular for high-performance computing (HPC). However, in
this book we will not cover ML, AI, and HPC.
Traditionally, a scientist was either an experimentalist or a theorists. The
former primarily work on experiments, while the latter on theoretical modelling.
However, after the prominence of computers, a new category of researchers
called computational scientists have emerged. These researchers excel in
designing computers and writing large softwares. Another important point to
note is that computers are often used to simulate physical systems, e.g., Earth’s
atmosphere, stars, galaxies, large molecules, automobiles, etc. Thus, computers
perform virtual experiments. Scientists strive to make accurate models of
physical systems using the inputs from experiments and/or computer
simulations. Thus, computers provide an interesting and powerful window of
opportunity for understanding the world.
Often, experiments and computer simulations complement each other. For
example, hydrodynamic experiments can achieve very high Reynolds numbers,
but computer simulations can reach only moderate Reynolds numbers (105).
However, computer simulations can probe velocity field at any point of the flow,
which may not be possible in many experiments (e.g., in an opaque liquid). Also,
it is impossible to perform experiments on a star, but we can comfortably
simulate stars on a computer, at least, approximately.
This book is ideally suited for advanced undergraduate and early graduate
students. Part-I of the book includes discussions on Python language. In this
part, I also cover error analysis and basics of computer hardware, whose
knowledge is required for estimating memory and time complexity of a
computational problem. Part-II of the book covers introduction to computational
methods—numerical interpolation; integration; differentiation; solvers of
ordinary and partial differential-equations; Fourier transforms; linear algebra;
Monte Carlo methods; etc. I have attempted to present these topics coherently by
highlighting connections among them. For example, the formulas of numerical
integration are derived using Lagrange interpolation.
I hope you will enjoy learning these tools.
COMPUTER HARDWARE
FOR AN EFFICIENT use of a car, it is best to know some of its details: its
milage, power of the engine, nature of the brakes, etc. Similarly, an optimal use
of a computer requires knowledge about its memory capacity and power of the
processors. In this chapter, we provide a basic overview of a computer and its
components.
von Neumann Architecture
Let us get a deeper perspectives on the processor, memory, and hard disk.
Processor: The processor, also called central processing unit (CPU), is the most
complex and critical component of a computer. This unit can perform billions of
arithmetic and logical operations per second. For example, the processor adds
two numbers A and B. An important thing to note is that the numbers A, B, and
A+B are stored in a small memory, called registers, of the CPU.
Now we provide a quick overview of the capabilities of one of the best
processors of today. Rome processor (EPY 7002) contains 64 cores residing in
16 blocks of 4 processors each (see Figure 2). The cores of the processor
communicate with each other via the memory block in the middle. Note that
each core has its own L1 and L2 caches, but 4 cores of a block share 16 MB of
L3 cache. Cache, being closer to the core, is faster than RAM. The cores
communicate with each other, as well as to the RAM and PCI devices, via buses.
The clock speed of the processor is 2.5 GHz, which can boosted up to 3.4
GHz. A Rome processor can perform 16 floating-point operations per clock
cycle. Hence, the peak performance of each core can be estimated to be 16x2.24
≈ 35 Giga floating-point operations/second (FLOPS in short). Consequently, a
Rome processor can perform maximum of 35 x 64 cores ≈ 2.24 TeraFLOPS or
TFLOPS. Note however that this is the peak performance, which is not achieved
in typical programs due to various constraints. Such information is useful for
estimating the time for a computing job.
Intel too has fast processors, which are not covered here. Apple’s M1 chip
has 8 compute cores, 8 GPU (graphical processing unit) cores, and 8 neural
engine cores.
Figure 2: Inside Rome 7742 processor, black bidirectional arrows are the buses
that transmit data both ways.
Memory: The data and programs reside in computer’s memory (the green strip
inside the motherboard of Figure 1), also called random access memory (RAM).
The CPU reads the program and data from RAM and write the results back on it.
Note that RAM is active only when the computer is powered on, hence it is a
temporary memory. We need to write the data to the hard disk for permanent
storage.
A laptop or desktop has RAM in the range of 4 Gigabytes to 64 Gigabytes.
Note that 1 byte = 8 bits, and
Discovering Diverse Content Through
Random Scribd Documents
ordinary plate racks of commerce with the wet sides accessible to a
draft of air. Two dozen plates separated from each other by a
quarter of an inch and left to dry spontaneously in a room of
ordinary humidity and living temperature will dry in two hours and a
half. If the surface be wiped with soft cheese-cloth or chamois, so as
to absorb all the surface moisture before the plates are placed on
the rack, this time may be appreciably reduced. By placing the plates
in a forced draft of air, from an electric fan, this time may be
reduced to an hour.
Extra rapid drying of plates may be accomplished by placing them in
a bath of alcohol before putting them in the racks. The alcohol
displaces all the water in the film, and is itself very quickly dissipated
into the atmosphere when the plate is taken from the tray. The plate
must be left in the alcohol tray long enough for the substitution of
the alcohol for the water in the film to take place. Five minutes is
long enough. The alcohol before use must be as nearly free from
water as possible. The best way to make sure of this is to place in
the bottle of alcohol some lumps of calcium oxide, which will take up
the water and form calcium hydroxide, which settles at the bottom
of the bottle.
Another method of quick plate drying takes advantage of the
extraordinary greediness of potassium carbonate for water. The wet
plates are placed in a saturated solution of potassium carbonate and
left for a minute. If a plate be now taken from the solution and its
surface wiped with a soft cloth, it will be found that the film has a
greasy, slippery feeling, but that it contains no water and can be
printed from at once. Plates so treated should be washed, however,
at some time in the succeeding four months, or the traces of
potassium carbonate left in the film cause deterioration.
Film Drying.—Unlike the drying of plates, drying of film negatives is
a very puzzling problem, and may be considered as the crux of the
successful use of film in aerial cameras.
Apron and similar machines have very poor drying efficiency if the
film is left in place, for not only the film but the apron or chain must
be freed of water. This may be hastened, as in the G. E. M. machine,
by blowing air through with fans, but even with their help drying a
35 foot film is a matter of two hours or more. Passing the film
through wringers or a squeegee to remove excess water is a
considerable aid; the film may either be re-wound on a dry reel, to
be put in a forced draft of air, or may be hung up in short lengths or
festooned, either method taking up a great deal of space. The use of
alcohol is not advisable as it may abstract camphor from the celluloid
and cause the film to become distorted.
The Eastman twin reel machine had an upper reel joined to the
lower or developing reel, with a chain and sprockets, so that the
upper reel revolved at the same time and rate of revolution as the
lower, when the lower was being revolved at the gentle speed
appropriate to the developing process. Fans blew a draft of air over
the upper reel. This method necessitated over an hour for drying.
Fig. 117.—U. S. Air Service film developing machine for film
24 centimeters wide.
The Air Service model of film developing and drying machine (Fig.
117) introduces an essential modification in the drying scheme of
the Eastman apparatus. The upper reel is quite independent of the
lower reel and is revolved at a high rate of speed, so that a whirling
action is introduced into the drying. Large rotating fans at the same
time drive a considerable volume of air across the film surface, and
the combination of the two agencies makes it possible to dry 35 feet
of 18 × 24 centimeter film in 20 to 30 minutes. This for large
numbers of pictures makes the use of film even quicker than that of
plates. The only practical drawback to the apparatus is its bulk,
which calls for a separate room or trailer. This, however, seems to be
inevitable in the use of large roll film.
Cut film can be dried with speed only if placed in a draft of warm air.
Drying boxes, with a chute or chimney and with fans to drive the air
through from an alcohol stove, will dry several dozen films in an
hour. The films must not be closer together than about one inch,
which makes the drying boxes rather cumbersome.
Marking Negatives.—After development and drying, and before
filing or printing, each plate should be marked with data for
purposes of future identification. This is most easily done with pen
and ink on the film side (in reversed lettering) either along an edge
in the unexposed portion covered by the sheath or in a corner, so as
to lose as little of the photograph as possible. Just what data shall
be inscribed is dictated by the purpose for which the negative was
made. The date, altitude, time of day, true north (from known
permanent features or from shadow direction and time of day),
number of the camera used, the focal length of the lens. Other
records, such as the plane and squadron numbers, or even the
pilot's and observer's initials, may be called for (Fig. 75). For
mapping work the scale of each of a set of negatives, once found,
may be marked, either in figures or by means of a line of length
corresponding to a fixed distance on the ground. Rectifying data can
similarly be inscribed, so that the negative can be printed in the
enlarging and rectifying camera with the minimum of delay.
CHAPTER XXIV
PRINTING AND ENLARGING
For the American Air Service, cut paper has been used exclusively.
For film printing, the Ansco machine has been equipped with roll
pivots to take film 24 centimeters wide which may be advanced in
either direction by turning large milled heads (Fig. 120). If we put
rollers on the two remaining sides of the box to handle paper we
transform the printer into the same form as a French machine, in
which paper and film are moved at right angles to each other. A
disadvantage of this modification, however, is the difficulty of
examining the negative to be printed.
Stereo Printing.—To make separate prints from the two elements
of a stereoscopic pair and mount them side by side after proper
orientation is too slow a process if quantities of prints are needed.
One method of multiple production is to make a master stereogram,
and then produce photographic copies of it, but there is inevitable
loss of quality in this copying process. An intermediate method is to
print from both negatives on the same sheet of paper. In order to do
this the negatives must be placed in rather large frames, with mats
properly located to guide the placing of the paper. The Richard
double printing frame is a practical device which simplifies the
necessary manipulations. It consists essentially of a platform pierced
with three illuminated openings. The two negatives are compared,
superposed, and orientated over the central opening and then
shifted laterally, one to each of the two side openings, which serve
both as printing frames and masks. The printing back slides on a
rod, permitting the paper to be lifted up and moved between
exposures. Once the negatives are properly placed, stereo prints can
be turned out quickly and easily.
Enlarging.—In the French service contact printing was the rule
during the war. The English practice, on the other hand, was to take
small negatives—4 × 5 inches, with 8 to 12 inch lenses—and enlarge
them, usually to 6½ × 8½ inches. For this purpose a regular part of
the English photo section equipment was the enlarging camera (Fig.
115). This may be briefly described as a short focus camera in which
the subject to be photographed is a negative, illuminated by
transmitted light, whose image is thrown by the camera lens on the
paper or other sensitive surface. By making the distance between
negative and lens less than that between lens and paper, the
resulting print is an enlargement, and vice versa. The scale of
enlargement or of reduction is varied over limits set only by the
length of the camera and the amount of light available.
The lens employed must of course possess sufficiently high quality
to preserve all the sharpness of the negative, and focussing must be
done with accuracy. Next to the lens the most important element is
the light source. This may be of the point form, such as a
concentrated filament electric lamp, an oxy-acetylene lime light, or
an acetylene flame. The latter was extensively used in the English
service, while acetylene generators for emergency purposes formed
part of each American photo truck equipment. With point light
sources we must use condensers to focus the light into the
projecting lens. Much less efficient, but the only recourse where
large condensers are not available, is a diffusing glass behind the
negative, illuminated either by a bank of electric lamps with mirrors
or by a U tube mercury vapor lamp, where proper current can be
got.
The device for holding the printing paper must permit quick
changing, but insure good contact. We may use either a spring plate
to hold the paper against plate glass from behind, or else a weight
acting on a lever arm of sufficient length.
The need for some automatic means of focussing an enlarging
camera has been very generally felt. An illustration of such an
enlarging camera is that put out by Williams, Brown & Earle, of
Philadelphia, known as the “Semperfocal” (Fig. 121). In this camera
the movements of the lens, paper easel and negative are so inter-
related and actuated with respect to each other that the correct
focus of the instrument is maintained for any degree of enlargement
or reduction. This feature is a great help in making up mosaic maps,
where prints of continuously varying scale ordinarily occasion serious
delay for individual focussing.
Fig. 121.—“Semperfocal” enlarging camera, with mechanism
for holding image in focus at any enlargement.