A Primer on Scientfic Programming with Python

圆桌大骑士 圆桌大骑士 2021-01-13 1307

The aim of this book is to teach computer programming using exam

ples from mathematics and the natural sciences. We have chosen to use

the Python programming language because it combines remarkable ex

pressive power with very clean, simple, and compact syntax. Python is

easy to learn and very well suited for an introduction to computer pro

gramming. Python is also quite similar to Matlab and a good language

for doing mathematical computing. It is easy to combine Python with

compiled languages, like Fortran, C, and C++, which are widely used

languages for scientifific computations. A seamless integration of Python

with Java is offffered by a special version of Python called Jython.

The examples in this book integrate programming with applications

to mathematics, physics, biology, and fifinance. The reader is expected to

have knowledge of basic one-variable calculus as taught in mathematics

intensive programs in high schools. It is certainly an advantage to take

a university calculus course in parallel, preferably containing both clas

sical and numerical aspects of calculus. Although not strictly required,

a background in high school physics makes many of the examples more

meaningful.


A Primer on Scientfic Programming with Python


Many introductory programming books are quite compact and focus

on listing functionality of a programming language. However, learning

to program is learning how to think as a programmer. This book has its

main focus on the thinking process, or equivalently: programming as a

problem solving technique. That is why most of the pages are devoted

to case studies in programming, where we defifine a problem and explain

how to create the corresponding program. New constructions and pro

gramming styles (what we could call theory) is also usually introduced

via examples. Special attention is paid to verifification of programs and

to fifinding errors. These topics are very demanding for mathematical

software, because the unavoidable numerical approximation errors are

possibly mixed with programming mistakes.

vvi

Preface

By studying the many examples in the book, I hope readers will

learn how to think right and thereby write programs in a quicker and

more reliable way. Remember, nobody can learn programming by just

reading – one has to solve a large amount of exercises hands on. The

book is therefore full of exercises of various types: modififications of

existing examples, completely new problems, or debugging of given

programs.

To work with this book, I recommend to use Python version 2.7 (al

though version 2.6 will work for most of the material). For Chapters 5–9

and Appendices A–E you need the NumPy, Matplotlib, SciTools pack

ages, and for Appendix G Cython is also required. There is a web page

associated with this book, http:/hplgit.github.com/scipro-primer,

which lists the software you need and explains brieflfly how to install

it. This page also contains all the fifiles associated with the program

examples in this book.

Python Version 2 or 3? A common problem among Python program

mers is to choose between version 2 or 3, which at the time of this

writing means choosing between version 2.7 and 3.3. The general rec

ommendation is to go for version 3, but programs are then not com

patible with version 2 and vice versa. There is still a problem that

much useful mathematical software in Python has not yet been ported

to version 3. Therefore, scientifific computing with Python still goes

mostly with version 2. A widely used strategy for software developers

who want to write Python code that works with both versions, is to

develop for v2.7, which is very close to what is accepted in version 3,

and then use the translation tool 2to3 to automatically translate the

code to version 3.

When using v2.7, one should employ the newest syntax and modules

that make the difffferences between version 2 and 3 very small. This

strategy is adopted in the present book. Only two difffferences between

versions 2 and 3 are expected to be signifificant for the programs in

the book: a/b implies flfloat division in version 3 if a and b are inte

gers, and print ’Hello’ in version 2 must be turned into a function

call print(’Hello’) in version 3. None of these difffferences should lead

to any annoying problems when future readers study the book’s v2.7

examples, but program in version 3. Anyway, running 2to3 on the ex

ample fifiles generates the corresponding version 3 code.

Contents. Chapter 1 introduces variables, objects, modules, and text

formatting through examples concerning evaluation of mathematical

formulas. Chapter 2 presents programming with while and for loops

as well as lists, including nested lists. The next chapter deals with two

other fundamental concepts in programming: functions and if-else

tests. Successful further reading of the book demands that Chapters 1–

3 are digested.Preface

vii

How to read data into programs and deal with errors in input are the

subjects of Chapter 4. Chapter 5 introduces arrays and array computing

(including vectorization) and how this is used for plotting y = f(x)

curves and making animation of curves. Many of the examples in the

fifirst fifive chapters are strongly related. Typically, formulas from the fifirst

chapter are used to produce tables of numbers in the second chapter.

Then the formulas are encapsulated in functions in the third chapter.

In the next chapter, the input to the functions are fetched from the

command line, or from a question-answer dialog with the user, and

validity checks of the input are added. The formulas are then shown

as graphs in Chapter 5. After having studied Chapters 1–5, the reader

should have enough knowledge of programming to solve mathematical

problems by what many refer to as “Matlab-style” programming.

Chapter 6 explains how to work with fifiles and text data. Class pro

gramming, including user-defifined types for mathematical computations

(with overloaded operators), is introduced in Chapter 7. Chapter 8

deals with random numbers and statistical computing with applica

tions to games and random walks. Object-oriented programming, in the

meaning of class hierarchies and inheritance, is the subject of Chap

ter 9. The key examples here deal with building toolkits for numerical

difffferentiation and integration as well as graphics.

Appendix A introduces mathematical modeling, using sequences and

difffference equations. We also treat sound as a sequence. Only program

ming concepts from Chapters 1–5 are used in this appendix, the aim

being to consolidate basic programming knowledge and apply it to

mathematical problems. Some important mathematical topics are in

troduced via difffference equations in a simple way: Newton’s method,

Taylor series, inverse functions, and dynamical systems.

Appendix B deals with functions on a mesh, numerical difffferenti

ation, and numerical integration. A simple introduction to ordinary

difffferential equations and their numerical treatment is provided in Ap

pendix C. Appendix D shows how a complete project in physics can be

solved by mathematical modeling, numerical methods, and program

ming elements from Chapters 1–5. This project is a good example on

problem solving in computational science, where it is necessary to in

tegrate physics, mathematics, numerics, and computer science.

How to create software for solving systems of ordinary difffferential

equations, primarily using classes and object-oriented programming,

is the subject of Appendix E. The material in this appendix brings

together many of the programming concepts from Chapters 1–9 in a

mathematical setting and ends up with a flflexible and general tool for

solving difffferential equations.

Appendix F is devoted to the art of debugging, and in fact problem

solving in general. Speeding up numerical computations in Python byviii

Preface

migrating code to C via Cython is exemplifified in Appendix G. Finally,

Appendix H deals with various more advanced technical topics.

Most of the examples and exercises in this book are quite short.

However, many of the exercises are related, and together they form

larger projects, for example on Fourier Series (3.13, 4.18–4.20, 5.30,

5.31), numerical integration (3.5–3.8, 5.38–5.39, A.16), Taylor series

(3.30, 5.21, 5.28, A.18–A.19, 7.29), piecewise constant functions (3.24–

3.28, 5.23, 5.36–5.37, 7.19–7.25), inverse functions (7.26, E.7–E.10),

falling objects (E.11–E.13, E.31–E.32), oscillatory population growth

(A.23–A.25, 7.40–7.41), epidemic disease modeling (E.35–E.42), analy

sis of web data (6.22, 6.27–6.29), optimization and fifinance (A.26, 8.44–

8.45), statistics and probability (4.24–4.26, 8.23–8.25), hazard games

(8.8–8.14), random walk and statistical physics (8.34–8.42), noisy data

analysis (8.46–8.50), numerical methods (5.14–5.16, 7.8–7.9, A.12, 7.28,

9.16–9.18, E.21–E.29), building a calculus calculator (7.42, 7.43, 9.19,

9.20), and creating a toolkit for simulating vibrating engineering sys

tems (E.44–E.49).

Chapters 1–9 and Appendix E have from 2007 formed the core of an

introductory fifirst semester course on scientifific programming (INF1100)

at the University of Oslo.

Changes to the First Edition. Besides numerous corrections of mis

prints, the second edition features a major reorganization of several

chapters. Chapter 2 in the fifirst edition, Basic Constructions, was a

comprehensive chapter, both with respect to length and topics. This

chapter has therefore been split in two for the second edition: a new

Chapter 2 Loops and Lists and a new Chapter 3 Functions and Branch

ing. A new Chapter 2.1.4 explicitly explains how to implement a sum

mation expression by a loop, and later examples present alternative

implementations.

All text and program fifiles that used the getopt module to parse

command-line options in the fifirst edition now make use of the sim

pler and more flflexible argparse module (new in Python v2.7/3.1). The

material on curve plotting in Chapter 5 has been thoroughly revised.

Now we give an introduction to plotting with Matplotlib as well as

SciTools/Easyviz.

While the fifirst edition almost exclusively used “star import” for con

venience (e.g., from numpy import * and from scitools.std import *),

the second edition tries to adhere to the standard import numpy as

np. However, in mathematical formulas that are to work with scalar

and array variables, we do not want an explicit prefifix. Avoiding the

namespace prefifixes is important for making formulas as close to the

mathematical notation as possible as well as for making the transition

from or to Matlab smooth. The two import styles have difffferent merits

and applications. The choice of style in various examples is carefully

thought through in the second edition.Preface

ix

Chapter 5 in the fifirst edition, Sequences and Difffference Equations,

has now become Appendix A. Chapter 6 in the fifirst edition, Files,

Strings, and Dictionaries, has been substantially revised. Chapter 6.4

on downloading and interpreting data from web pages have now com

pletely new examples. Exercises are also reworked to fifit with the new

examples.

The material on difffferential equations in chapters on classes (Ch. 7

and 9 in the fifirst edition) has been extracted, reworked, slightly ex

panded, and placed in Appendix E. This restructuring allows a more

flflexible treatment of difffferential equations, and parts of this important

topic can be addressed right after Chapter 3, if desired.

To distinguish between Python’s random module and the one in

numpy, we have in Chapter 8 changed practice compared with the fifirst

edition. Now random always refers to Python’s random module, while

the random module in numpy is normally invoked as np.random (or oc

casionally as numpy.random). The associated software has been revised

similarly.

Changes to the Second Edition. Many typos have been fifixed in the

third edition, a lot of examples have been improved, some material has

been reorganized and extended, some material is new, several exercises

have been removed and many new ones added, and numerous exercises

are reformulated after feedback from teachers. The associated SciTools

package is also extensively upgraded.

The reorganized and extended material covers Chapter 4.2.4

on command-line parsing, Chapter 5.5 on vectorization, and Ap

pendix E.2.8 on building simulation software for ODEs. The new ma

terial consists of Chapter 6.2.4 on dictionaries with default values and

ordering, Chapter 9.4 on making a drawing program, Appendix A.1.7

on integrals as difffference equations, Appendix G on using Cython and

combining Python with fast C code, and the bioinformatics examples

in Chapters 3.3, 6.6, 8.3.4, and 9.5.

Four new projects are added: numerical integration (Exercises 3.5–

3.8, 5.38–5.39, A.16), piecewise constant functions (Exercises 3.24–3.28,

5.23, 5.36–5.37, 7.19–7.25), inverse functions (Exercises 7.26, E.7–E.10),

and epidemic modeling (Exercises E.35–E.42).

The software for ODEs derived in Appendix E and the drawing pro

gram from Chapter 9.4 have been much further developed into the

packages Odespy and Pysketcher, both available from github.com.

Acknowledgments. First, I want to express my thanks to Aslak Tveito

for his enthusiastic role in the initiation of this book project and for

writing Appendices B and C about numerical methods. Without Aslak

there would be no book. Another key contributor is Ilmar Wilbers.

His extensive efffforts with assisting the book project and help estab

lishing the associated course (INF1100) at the University of Oslo arex

Preface

greatly appreciated. Without Ilmar and his solutions to numerous tech

nical problems the book would never have been completed. Johannes

H. Ring also deserves a special acknowledgment for the development of

the Easyviz graphics tool and for his careful maintenance and support

of software associated with the book. Professor Loyce Adams studied

the entire book, solved all the exercises, found numerous errors, and

suggested many improvements. Her contributions are so much appre

ciated. I also want to thank Geir Kjetil Sandve for being the primary

author of the third edition’s new series of computational bioinformat

ics examples in Chapters 3.3, 6.6, 8.3.4, and 9.5, with contributions

from Sveinung Gundersen, Ksenia Khelik, Halfdan Rydbeck, and Kai

Trengereid.

Several people have helped to make substantial improvements of the

text, the exercises, and the associated software components. The author

is thankful to Ingrid Eide, St˚ale Zerener Haugnæss, Kristian Hiorth,

Arve Knudsen, Tobias Vidarssønn Langhoffff, Martin Vonheim Larsen,

Kine Veronica Lund, Solveig Masvie, H˚akon Møller, Rebekka Mørken,

Mathias Nedrebø, Marit Sandstad, Helene Norheim Semmerud, Lars

Storjord, Fredrik Heffffer Valdmanis, and Torkil Vederhus for their con

tributions. Hakon Adler is greatly acknowledged for his careful reading

of early various versions of the manuscript. The professors Fred Es

pen Bent, Ørnulf Borgan, Geir Dahl, Knut Mørken, and Geir Pedersen

have contributed with many exciting exercises from various application

fifields. Great thanks also go to Jan Olav Langseth for creating the cover

image.

This book and the associated course are parts of a comprehensive re

form at the University of Oslo, called Computing in Science Education.

The goal of the reform is to integrate computer programming and sim

ulation in all bachelor courses in natural science where mathematical

models are used. The present book lays the foundation for the modern

computerized problem solving technique to be applied in later courses.

It has been extremely inspiring to work with the driving forces behind

this reform, especially the professors Morten Hjorth–Jensen, Anders

Malthe–Sørenssen, Knut Mørken, and Arnt Inge Vistnes.

The excellent assistance from the Springer system, in particular

Martin Peters, Thanh-Ha Le Thi, Ruth Allewelt, Peggy Glauch-Ruge,

Nadja Kroke, Thomas Schmidt, Patrick Waltemate, Donatas Akma

naviˇcius, and Edita Baronait˙e, is highly appreciated, and ensured a

smooth and rapid production of all editions of this book.

Oslo Hans Petter Langtangen


【下载地址】

链接:https://pan.baidu.com/s/1m3eqHR0jhQ6qui-tlidiPg

提取码:kzob


相关文章


FlashFXP绿色版网盘下载,附激活教程

FlashFxp百度网盘下载链接:https://pan.baidu.com/s/1MBQ5gkZY1TCFY8A7fnZCfQ。FlashFxp是功能强大的FTP工具

Adobe Fireworks CS6 Ansifa绿色精简版网盘下载

firework可以制作精美或是可以闪瞎眼的gif,这在广告领域是需要常用的,还有firework制作下logo,一些原创的图片还是很便捷的,而且fireworks用法简单,配合dw在做网站这一块往往会发挥出很强大的效果。百度网盘下载链接:https://pan.baidu.com/s/1fzIZszfy8VX6VzQBM_bdZQ

navicat for mysql中文绿色版网盘下载

Navicat for Mysql是用于Mysql数据库管理的一款图形化管理软件,非常的便捷和好用,可以方便的增删改查数据库、数据表、字段、支持mysql命令,视图等等。百度网盘下载链接:https://pan.baidu.com/s/1T_tlgxzdQLtDr9TzptoWQw 提取码:y2yq

火车头采集器(旗舰版)绿色版网盘下载

火车头采集器是站长常用的工具,相比于八爪鱼,简洁好用,易于配置。火车头能够轻松的抓取网页内容,并通过自带的工具对内容进行处理。站长圈想要做网站,火车头采集器是必不可少的。百度网盘链接:https://pan.baidu.com/s/1u8wUqS901HgOmucMBBOvEA

Photoshop(CS-2015-2023)绿色中文版软件下载

安装文件清单(共46G)包含Window和Mac OS各个版本的安装包,从cs到cc,从绿色版到破解版,从安装文件激活工具,应有尽有,一次性打包。 Photoshop CC绿色精简版 Photoshop CS6 Mac版 Photoshop CC 2015 32位 Photoshop CC 2015 64位 Photoshop CC 2015 MAC版 Photoshop CC 2017 64位 Adobe Photoshop CC 2018 Adobe_Photoshop_CC_2018 Photoshop CC 2018 Win32 Photoshop CC 2018 Win64

windows10原装正版ISO镜像下载,可装VM(附官方升级工具)

1、原版镜像直接安装 (1)双击iso文件,进入DVD驱动器。 (2)点击setup。 (3)点击“是”。 (4)勾选上进入安装下一步。(由于笔者机器已经安win10,所以变成了安装更新,win7的电脑可能会进入真正的安装界面)

microsoft office办公软件网盘下载(附破解激活教程)

1、双击office镜像文件,会进入虚拟DVD驱动器。 2、点击“office”文件夹,点击“setup64”运行64位安装程序。 3、弹出提示框,选择“是”。 4、进入安装过程。 5、安装完成。

IDM下载器绿色版网盘下载(附安装使用教程)

1、将安装文件夹解压出来。双击文件夹。 2、点击“绿化”。 3、弹出窗口,选着“是”。 4、弹出黑窗口,接下来基本上几秒钟,就完成安装。 5、桌面上会出现IDM下载器图标。

Microsoft project项目管理软件版网盘下载(附激活教程)

1、将Project安装镜像解压到文件夹,解压后状态如下。2、双击。会进入到DVD驱动器。3、默认是“否”,这里要选择“是”。4、勾选“接受条款”,选择“继续”。百度网盘下载链接:https://pan.baidu.com/s/1KSYMvS7UKY4JreldctKxBw

流程图软件Microsoft visio版激活教程及网盘下载

1、双击下载文件,会进入DVD驱动器,双击setup文件。2、提示更改,选择"是"。3、进入安装界面。这个界面一般及比较久,也是看电脑配置,配置好的话一般安装会比较快。

飞刀象棋助手下载安装教程(附链接)

一、首先打开飞刀象棋助手官网。官网链接:www.fdxqzs.com。二、点击下载最新安装包,可以在官网直接进行下载,也可以通过下方链接下载。

象棋人工智能强软介绍及排名(附象棋强软链接)

目前市面上的强软并不多,主要有小虫象棋、飞刀象棋、天机象棋、象棋旋风,下面分别一一进行介绍。1、小虫象棋,2019年以前目前多次获得象棋比赛的冠军,有较多的强软比较喜欢套小虫象棋的壳。小虫象棋的历史棋力还是非常不错的,最新一版更新到2022年07月,后面也没看到新的版本出来。

CAD快捷键有哪些?(常用的CAD快捷键命令大全)

一、常用绘图快捷键。最基本的一些画图的功能操作,简单来说就是CAD制图的打底部分。(如下图)二、常用编辑快捷键CAD中对图形进行修改的操作。

chatgpt是谁开发的?

OpenAI官网显示,为ChatGPT项目做出贡献的人员不足百人(共87人)。从成员毕业高校分布看,校友最多的前5大高校是斯坦福大学(14人)、加州大学伯克利分校(10人)、麻省理工学院(7人)、剑桥大学(5人)、哈佛大学(4人)和佐治亚理工学院(4人)。

怎么注册chatgpt(chatgpt怎么使用)

第一步 上网工具。打开上网工具,工具基本是需要付费使用的。注册然后按照教程安装,直到能够测试上网打开即可。


文章热度: 21174
文章数量: 33
推荐阅读

FlashFXP绿色版网盘下载,附激活教程 1878

FlashFxp百度网盘下载链接:https://pan.baidu.com/s/1MBQ5gkZY1TCFY8A7fnZCfQ。FlashFxp是功能强大的FTP工具

Adobe Fireworks CS6 Ansifa绿色精简版网盘下载 1630

firework可以制作精美或是可以闪瞎眼的gif,这在广告领域是需要常用的,还有firework制作下logo,一些原创的图片还是很便捷的,而且fireworks用法简单,配合dw在做网站这一块往往会发挥出很强大的效果。百度网盘下载链接:https://pan.baidu.com/s/1fzIZszfy8VX6VzQBM_bdZQ

navicat for mysql中文绿色版网盘下载 1676

Navicat for Mysql是用于Mysql数据库管理的一款图形化管理软件,非常的便捷和好用,可以方便的增删改查数据库、数据表、字段、支持mysql命令,视图等等。百度网盘下载链接:https://pan.baidu.com/s/1T_tlgxzdQLtDr9TzptoWQw 提取码:y2yq

火车头采集器(旗舰版)绿色版网盘下载 1764

火车头采集器是站长常用的工具,相比于八爪鱼,简洁好用,易于配置。火车头能够轻松的抓取网页内容,并通过自带的工具对内容进行处理。站长圈想要做网站,火车头采集器是必不可少的。百度网盘链接:https://pan.baidu.com/s/1u8wUqS901HgOmucMBBOvEA

Photoshop(CS-2015-2023)绿色中文版软件下载 1887

安装文件清单(共46G)包含Window和Mac OS各个版本的安装包,从cs到cc,从绿色版到破解版,从安装文件激活工具,应有尽有,一次性打包。 Photoshop CC绿色精简版 Photoshop CS6 Mac版 Photoshop CC 2015 32位 Photoshop CC 2015 64位 Photoshop CC 2015 MAC版 Photoshop CC 2017 64位 Adobe Photoshop CC 2018 Adobe_Photoshop_CC_2018 Photoshop CC 2018 Win32 Photoshop CC 2018 Win64

知之

知之平台是全球领先的知识付费平台。提供各个领域的项目实战经验分享,提供优质的行业解决方案信息,来帮助您的工作和学习

使用指南 建议意见 用户协议 友情链接 隐私政策 Powered by NOOU ©2020 知之