Docsity

numerical python, Notas de estudo de Engenharia Elétrica

Toggle sidebar
bg1
Johansson
Shelve in
Programming Languages/General
User level:
Intermediate
www.apress.com
SOURCE CODE ONLINE
RELATED
BOOKS FOR PROFESSIONALS BY PROFESSIONALS®
Numerical Python
Numerical Python by Robert Johansson shows you how to leverage the numerical
and mathematical modules in Python and its Standard Library as well as popular
open source numerical Python packages like NumPy, FiPy, matplotlib and more to
numerically compute solutions and mathematically model applications in a number
of areas like big data, cloud computing, financial engineering, business management
and more.
After reading and using this book, you’ll have some case study examples of
applications that can be found in areas like business management, big data/cloud
computing, financial engineering (i.e., options trading investment alternatives), and
even games.
Up until very recently, Python was mostly regarded as just a web scripting
langua ge. Well, computational scientists and engineers have recently discovered
the fl exibil ity an d powe r of Python to do more. Big data analytics and cloud
computing programmers are seeing Python’s immense use. Financial engineers are
also now employing Python in their work. Python seems to be evolving as a language
that can even rival C++, Fortran, and Pascal/Delphi for numerical and mathematical
computations.
• How to plot and graph with matplotlib
• How to construct vectors and matrices with NumPy
• How to solve linear/non-linear equations with NumPy
• How to solve equations with finite element methods using FiPy
• How to handle image processing with Pillow, matplotlib and OpenCV
• How to do numerical computations, interpolations and optimizations using SciPy
• How to handle file I/O, data encoding/compression, and text/date/time processing
9781484 205549
55999
ISBN 978-1-4842-0554-9
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe numerical python e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

Numerical Python

A Practical Techniques Approach

for Industry

Robert Johansson

To Mika and Erika.

v

Contents at a Glance

About the Author ���������������������������������������������������������������������������������������������������xvii

About the Technical Reviewer ��������������������������������������������������������������������������������xix

vii

Contents

About the Author ���������������������������������������������������������������������������������������������������xvii

About the Technical Reviewer ��������������������������������������������������������������������������������xix

■ Contents

■ Contents

■ Contents

■ Contents

■ Contents

■ Contents

  • ■Chapter 1: Introduction to Computing with Python ����������������������������������������������� Introduction ������������������������������������������������������������������������������������������������������������xxi
  • ■Chapter 2: Vectors, Matrices, and Multidimensional Arrays �������������������������������
  • ■Chapter 3: Symbolic Computing ��������������������������������������������������������������������������
  • ■Chapter 4: Plotting and Visualization ������������������������������������������������������������������
  • ■Chapter 5: Equation Solving ������������������������������������������������������������������������������
  • ■Chapter 6: Optimization �������������������������������������������������������������������������������������
  • ■Chapter 7: Interpolation ������������������������������������������������������������������������������������
  • ■Chapter 8: Integration ���������������������������������������������������������������������������������������
  • ■Chapter 9: Ordinary Differential Equations ��������������������������������������������������������
  • ■Chapter 10: Sparse Matrices and Graphs ����������������������������������������������������������
  • ■Chapter 11: Partial Differential Equations ���������������������������������������������������������
  • ■Chapter 12: Data Processing and Analysis��������������������������������������������������������
  • ■Chapter 13: Statistics ����������������������������������������������������������������������������������������
  • ■Chapter 14: Statistical Modeling �����������������������������������������������������������������������
  • ■Chapter 15: Machine Learning ��������������������������������������������������������������������������
  • ■Chapter 16: Bayesian Statistics�������������������������������������������������������������������������
  • ■Chapter 1: Introduction to Computing with Python ����������������������������������������������� Introduction ������������������������������������������������������������������������������������������������������������xxi
  • Environments for Computing with Python ������������������������������������������������������������������������
  • Python ������������������������������������������������������������������������������������������������������������������������������
  • Interpreter�����������������������������������������������������������������������������������������������������������������������������������������������
  • IPython Console ����������������������������������������������������������������������������������������������������������������
  • Input and Output Caching �����������������������������������������������������������������������������������������������������������������������
  • Autocompletion and Object Introspection�����������������������������������������������������������������������������������������������
  • Documentation ���������������������������������������������������������������������������������������������������������������������������������������
  • Interaction with the System Shell�����������������������������������������������������������������������������������������������������������
  • IPython Extensions ���������������������������������������������������������������������������������������������������������������������������������
  • The IPython Qt Console �������������������������������������������������������������������������������������������������������������������������
  • IPython Notebook �����������������������������������������������������������������������������������������������������������
  • Cell Types����������������������������������������������������������������������������������������������������������������������������������������������
  • Editing Cells ������������������������������������������������������������������������������������������������������������������������������������������
  • Markdown Cells ������������������������������������������������������������������������������������������������������������������������������������
  • nbconvert����������������������������������������������������������������������������������������������������������������������������������������������
  • Spyder: An Integrated Development Environment ����������������������������������������������������������
  • Source Code Editor �������������������������������������������������������������������������������������������������������������������������������
  • Consoles in Spyder �������������������������������������������������������������������������������������������������������������������������������
  • Object Inspector ����������������������������������������������������������������������������������������������������������������������������������
  • Summary ������������������������������������������������������������������������������������������������������������������������ viii
  • Further Reading��������������������������������������������������������������������������������������������������������������
  • References ���������������������������������������������������������������������������������������������������������������������
  • ■Chapter 2: Vectors, Matrices, and Multidimensional Arrays �������������������������������
  • Importing NumPy������������������������������������������������������������������������������������������������������������
  • The NumPy Array Object�������������������������������������������������������������������������������������������������
  • Data Types ��������������������������������������������������������������������������������������������������������������������������������������������
  • Order of Array Data in Memory �������������������������������������������������������������������������������������������������������������
  • Creating Arrays ���������������������������������������������������������������������������������������������������������������
  • Arrays Created from Lists and Other Array-like Objects �����������������������������������������������������������������������
  • Arrays Filled with Constant Values �������������������������������������������������������������������������������������������������������
  • Arrays Filled with Incremental Sequences �������������������������������������������������������������������������������������������
  • Arrays Filled with Logarithmic Sequences �������������������������������������������������������������������������������������������
  • Mesh-grid Arrays ����������������������������������������������������������������������������������������������������������������������������������
  • Creating Uninitialized Arrays ����������������������������������������������������������������������������������������������������������������
  • Creating Arrays with Properties of Other Arrays �����������������������������������������������������������������������������������
  • Creating Matrix Arrays ��������������������������������������������������������������������������������������������������������������������������
  • Indexing and Slicing �������������������������������������������������������������������������������������������������������
  • One-dimensional Arrays �����������������������������������������������������������������������������������������������������������������������
  • Multidimensional Arrays �����������������������������������������������������������������������������������������������������������������������
  • Views ����������������������������������������������������������������������������������������������������������������������������������������������������
  • Fancy Indexing and Boolean-valued Indexing ��������������������������������������������������������������������������������������
  • Reshaping and Resizing �������������������������������������������������������������������������������������������������
  • Vectorized Expressions ��������������������������������������������������������������������������������������������������
  • Arithmetic Operations ���������������������������������������������������������������������������������������������������������������������������
  • Elementwise Functions ������������������������������������������������������������������������������������������������������������������������
  • Aggregate Functions�����������������������������������������������������������������������������������������������������������������������������
  • Boolean Arrays and Conditional Expressions����������������������������������������������������������������������������������������
  • Set Operations ��������������������������������������������������������������������������������������������������������������������������������������
  • Operations on Arrays ����������������������������������������������������������������������������������������������������������������������������
  • Matrix and Vector Operations ����������������������������������������������������������������������������������������� ix
  • Summary ������������������������������������������������������������������������������������������������������������������������
  • Further Reading��������������������������������������������������������������������������������������������������������������
  • References ���������������������������������������������������������������������������������������������������������������������
  • ■Chapter 3: Symbolic Computing ��������������������������������������������������������������������������
  • Importing SymPy ������������������������������������������������������������������������������������������������������������
  • Symbols��������������������������������������������������������������������������������������������������������������������������
  • Numbers �����������������������������������������������������������������������������������������������������������������������������������������������
  • Expressions ��������������������������������������������������������������������������������������������������������������������
  • Manipulating Expressions ����������������������������������������������������������������������������������������������
  • Simplification����������������������������������������������������������������������������������������������������������������������������������������
  • Expand ��������������������������������������������������������������������������������������������������������������������������������������������������
  • Factor, Collect, and Combine ����������������������������������������������������������������������������������������������������������������
  • Apart, Together, and Cancel ������������������������������������������������������������������������������������������������������������������
  • Substitutions�����������������������������������������������������������������������������������������������������������������������������������������
  • Numerical Evaluation �����������������������������������������������������������������������������������������������������
  • Calculus��������������������������������������������������������������������������������������������������������������������������
  • Derivatives ��������������������������������������������������������������������������������������������������������������������������������������������
  • Integrals������������������������������������������������������������������������������������������������������������������������������������������������
  • Series����������������������������������������������������������������������������������������������������������������������������������������������������
  • Limits ����������������������������������������������������������������������������������������������������������������������������������������������������
  • Sums and Products ������������������������������������������������������������������������������������������������������������������������������
  • Equations������������������������������������������������������������������������������������������������������������������������
  • Linear Algebra ����������������������������������������������������������������������������������������������������������������
  • Summary ������������������������������������������������������������������������������������������������������������������������
  • Further Reading��������������������������������������������������������������������������������������������������������������
  • References ���������������������������������������������������������������������������������������������������������������������
  • ■Chapter 4: Plotting and Visualization ������������������������������������������������������������������ x
  • Importing Matplotlib �������������������������������������������������������������������������������������������������������
  • Getting Started ���������������������������������������������������������������������������������������������������������������
  • Interactive and Noninteractive Modes ��������������������������������������������������������������������������������������������������
  • Figure �����������������������������������������������������������������������������������������������������������������������������
  • Axes��������������������������������������������������������������������������������������������������������������������������������
  • Plot Types ���������������������������������������������������������������������������������������������������������������������������������������������
  • Line Properties �������������������������������������������������������������������������������������������������������������������������������������
  • Legends ����������������������������������������������������������������������������������������������������������������������������������������������
  • Text Formatting and Annotations ��������������������������������������������������������������������������������������������������������
  • Axis Properties������������������������������������������������������������������������������������������������������������������������������������
  • Advanced Axes Layouts ������������������������������������������������������������������������������������������������
  • Insets ��������������������������������������������������������������������������������������������������������������������������������������������������
  • Subplots����������������������������������������������������������������������������������������������������������������������������������������������
  • Subplot2grid ���������������������������������������������������������������������������������������������������������������������������������������
  • GridSpec ���������������������������������������������������������������������������������������������������������������������������������������������
  • Colormap Plots �������������������������������������������������������������������������������������������������������������
  • 3D plots ������������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 5: Equation Solving ������������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Linear Equation Systems ����������������������������������������������������������������������������������������������
  • Square Systems����������������������������������������������������������������������������������������������������������������������������������
  • Rectangular Systems��������������������������������������������������������������������������������������������������������������������������
  • Eigenvalue Problems ����������������������������������������������������������������������������������������������������
  • Nonlinear Equations ����������������������������������������������������������������������������������������������������� xi
  • Univariate Equations ���������������������������������������������������������������������������������������������������������������������������
  • Systems of Nonlinear Equations ���������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 6: Optimization �������������������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Classification of Optimization Problems �����������������������������������������������������������������������
  • Univariate Optimization ������������������������������������������������������������������������������������������������
  • Unconstrained Multivariate Optimization ���������������������������������������������������������������������
  • Nonlinear Least Square Problems ��������������������������������������������������������������������������������
  • Constrained Optimization ���������������������������������������������������������������������������������������������
  • Linear Programming ���������������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 7: Interpolation ������������������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Interpolation �����������������������������������������������������������������������������������������������������������������
  • Polynomials ������������������������������������������������������������������������������������������������������������������
  • Polynomial Interpolation�����������������������������������������������������������������������������������������������
  • Spline Interpolation ������������������������������������������������������������������������������������������������������
  • Multivariate Interpolation ���������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 8: Integration ��������������������������������������������������������������������������������������� xii
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Numerical Integration Methods ������������������������������������������������������������������������������������
  • Numerical Integration with SciPy ���������������������������������������������������������������������������������
  • Tabulated Integrand ����������������������������������������������������������������������������������������������������������������������������
  • Multiple Integration ������������������������������������������������������������������������������������������������������
  • Symbolic and Arbitrary-Precision Integration ��������������������������������������������������������������
  • Integral Transforms ������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 9: Ordinary Differential Equations ��������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Ordinary Differential Equations ������������������������������������������������������������������������������������
  • Symbolic Solution to ODEs �������������������������������������������������������������������������������������������
  • Direction Fields �����������������������������������������������������������������������������������������������������������������������������������
  • Solving ODEs using Laplace Transformations�������������������������������������������������������������������������������������
  • Numerical Methods for Solving ODEs���������������������������������������������������������������������������
  • Numerical Integration of ODEs using SciPy������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 10: Sparse Matrices and Graphs ����������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Sparse Matrices in SciPy����������������������������������������������������������������������������������������������
  • Functions for Creating Sparse Matrices ���������������������������������������������������������������������������������������������
  • Sparse Linear Algebra Functions ��������������������������������������������������������������������������������������������������������
  • Linear Equation Systems ��������������������������������������������������������������������������������������������������������������������
  • Graphs and Networks ���������������������������������������������������������������������������������������������������
  • Summary ���������������������������������������������������������������������������������������������������������������������� xiii
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 11: Partial Differential Equations ���������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Partial Differential Equations����������������������������������������������������������������������������������������
  • Finite-Difference Methods��������������������������������������������������������������������������������������������
  • Finite-Element Methods �����������������������������������������������������������������������������������������������
  • Survey of FEM Libraries����������������������������������������������������������������������������������������������������������������������
  • Solving PDEs using FEniCS ������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 12: Data Processing and Analysis��������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Introduction to Pandas �������������������������������������������������������������������������������������������������
  • Series��������������������������������������������������������������������������������������������������������������������������������������������������
  • DataFrame ������������������������������������������������������������������������������������������������������������������������������������������
  • Time Series �����������������������������������������������������������������������������������������������������������������������������������������
  • The Seaborn Graphics Library ��������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 13: Statistics ����������������������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Review of Statistics and Probability �����������������������������������������������������������������������������
  • Random Numbers���������������������������������������������������������������������������������������������������������
  • Random Variables and Distributions ����������������������������������������������������������������������������
  • Hypothesis Testing ������������������������������������������������������������������������������������������������������� xiv
  • Nonparametric Methods �����������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 14: Statistical modeling �����������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Introduction to Statistical Modeling �����������������������������������������������������������������������������
  • Defining Statistical Models with Patsy �������������������������������������������������������������������������
  • Linear Regression ��������������������������������������������������������������������������������������������������������
  • Example Datasets �������������������������������������������������������������������������������������������������������������������������������
  • Discrete Regression �����������������������������������������������������������������������������������������������������
  • Logistic Regression ����������������������������������������������������������������������������������������������������������������������������
  • Poisson Model ������������������������������������������������������������������������������������������������������������������������������������
  • Time Series ������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 15: Machine Learning ��������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Brief Review of Machine Learning �������������������������������������������������������������������������������
  • Regression �������������������������������������������������������������������������������������������������������������������
  • Classification ����������������������������������������������������������������������������������������������������������������
  • Clustering ���������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 16: Bayesian Statistics������������������������������������������������������������������������� xv
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Introduction to Bayesian Statistics�������������������������������������������������������������������������������
  • Model Definition �����������������������������������������������������������������������������������������������������������
  • Sampling Posterior Distributions ��������������������������������������������������������������������������������������������������������
  • Linear Regression �������������������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 17: Signal Processing ��������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Spectral Analysis ����������������������������������������������������������������������������������������������������������
  • Fourier Transforms �����������������������������������������������������������������������������������������������������������������������������
  • Windowing ������������������������������������������������������������������������������������������������������������������������������������������
  • Spectogram ����������������������������������������������������������������������������������������������������������������������������������������
  • Signal Filters ����������������������������������������������������������������������������������������������������������������
  • Convolution Filters ������������������������������������������������������������������������������������������������������������������������������
  • FIR and IIR Filters �������������������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 18: Data Input and Output ��������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Comma-Separated Values ��������������������������������������������������������������������������������������������
  • HDF5 �����������������������������������������������������������������������������������������������������������������������������
  • h5py ����������������������������������������������������������������������������������������������������������������������������������������������������
  • PyTables ���������������������������������������������������������������������������������������������������������������������������������������������
  • Pandas HDFStore ��������������������������������������������������������������������������������������������������������������������������������
  • JSON����������������������������������������������������������������������������������������������������������������������������� xvi
  • Serialization �����������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Chapter 19: Code Optimization ��������������������������������������������������������������������������
  • Importing Modules �������������������������������������������������������������������������������������������������������
  • Numba ��������������������������������������������������������������������������������������������������������������������������
  • Cython ��������������������������������������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • References �������������������������������������������������������������������������������������������������������������������
  • ■Appendix A: Installation ������������������������������������������������������������������������������������
  • Miniconda and Conda ���������������������������������������������������������������������������������������������������
  • A Complete Environment ����������������������������������������������������������������������������������������������
  • Summary ����������������������������������������������������������������������������������������������������������������������
  • Further Reading������������������������������������������������������������������������������������������������������������
  • Index ���������������������������������������������������������������������������������������������������������������������

About the Author

Robert Johansson is an experienced Python programmer and computational scientist, with a PhD in Theoretical Physics from Chalmers University of Technology, Sweden. He has worked with scientific computing in academia and industry for over 10 years, and he has participated in both open source development and proprietary research projects. His open source contributions include work on QuTiP, a popular Python framework for simulating the dynamics of quantum systems; and he has also contributed to several other popular Python libraries in the scientific computing landscape. Robert is passionate about scientific computing and software development, and about teaching and communicating best practices for bringing these fields together with optimal outcome: novel, reproducible, and extensible computational results. Robert’s background includes 5 years of postdoctoral research in theoretical and computational physics, and more recently he has taken on a role as a data scientist in the IT industry.

xvii

Introduction

Scientific and numerical computing is a booming field in research, engineering, and analytics. The revolution in the computer industry over the last several decades has provided new and powerful tools for computational practitioners. This has enabled computational undertakings of previously unprecedented scale and complexity. Entire fields and industries have sprung up as a result. This development is still on going, and it is creating new opportunities as hardware, software, and algorithms keep improving. Ultimately the enabling technology for this movement is the powerful computing hardware that has been developed in recent decades. However, for a computational practitioner, the software environment used for computational work is as important as, if not more important than, the hardware on which the computations are carried out. This book is about one popular and fast growing environment for numerical computing: the Python programming language and its vibrant ecosystem of libraries and extensions for computational work. Computing is an interdisciplinary activity that requires experience and expertise in both theoretical and practical subjects: a firm understanding of mathematics and scientific thinking is a fundamental requirement for effective computational work. Equally important is solid training in computer programming and computer science. The role of this book is to bridge these two subjects by introducing how scientific computing can be done using the Python programming language and the computing environment that has appeared around this language. In this book the reader is assumed to have some previous training in mathematics and numerical methods, and basic knowledge about Python programming. The focus of the book is to give a practical introduction to computational problem solving with Python. Brief introductions to the theory of the covered topics are given in each chapter, to introduce notation and remind readers of the basic methods and algorithms. However, this book is not a self-consistent treatment of numerical methods. To assist readers that are not previously familiar with some of the topics of this book, references for further reading are given at the end of each chapter. Likewise, readers without experience in Python programming will probably find it useful to read this book together with a book that focus on the Python programming language itself.

How This Book is Organized The first chapter in this book introduces general principles for scientific computing, and the main development environments that are available for work with computing in Python: The focus is on IPython and its interactive Python prompt and its excellent notebook application, and the Spyder IDE. In Chapter 2, an introduction to the NumPy library is given, and here we also discuss more generally array-based computing and its virtues. In Chapter 3 we turn our attention to symbolic computing – which in many respects complements array-based computing – using the SymPy library. In Chapter 4 we cover plotting and visualization using the Matplotlib library. Together, Chapters 2 to 4 provide the basic computational tools that will be used for domain specific problems throughout the rest of the book: numerics, symbolics, and visualization. In Chapter 5, the topic of study is equation solving, which we explore with both numerical and symbolic methods, using the SciPy and SymPy libraries. In Chapter 6, we explore optimization, which is a natural extension of equation solving. Here we mainly work with the SciPy library, and briefly with the cvxopt library. Chapter 7 deals with interpolation, which is another basic mathematical method with many

xxi

■ IntroduCtIon

xxii

applications of its own, and important roles in higher-level algorithms and methods. In Chapter 8 we cover numerical and symbolic integration. Chapters 5 to 8 cover core computational techniques that are pervasive in all types of computational work. Most of the methods from these chapters are found in the SciPy library. In Chapter 9, we proceed to cover ordinary differential equations. Chapter 10 is a detour into sparse matrices and graph methods, which helps prepare the field for the following chapter. In Chapter 11, we discuss partial differential equations, which conceptually are closely related to ordinary differential equations, but require a different set of techniques that necessitates the introduction of sparse matrices, the topic of Chapter 10. Starting with Chapter 12, we make a change of direction and begin exploring data analysis and statistics. In Chapter 12 we introduce the Pandas library and its excellent data analysis framework. In Chapter 13, we cover basic statistical analysis and methods from the SciPy stats package. In Chapter 14, we move on to statistical modeling, using the statsmodels library. In Chapter 15, the theme of statistics and data analysis is continued with a discussion of machine learning, using the scikit-learn library. In Chapter 16, we wrap up the statistics-related chapters with a discussion of Bayesian statistics and the PyMC library. Together, Chapters 12 to 16 provide an introduction to the broad field of statistics and data analytics: a field that has been developing rapidly within and outside of the scientific Python community in recent years. In Chapter 17 we briefly return to a core subject in scientific computing: signal processing. In Chapter 18, we discuss data input and output, and several methods for reading and writing numerical data to files, which is a basic topic that is required for most types of computational work. In Chapter 19, the final regular chapter in this book, two methods for speeding up Python code are introduced, using the Numba and Cython libraries. The appendix covers the installation of the software used in this book. To install the required software (mostly Python libraries), we use the conda package manager. Conda can also be used to create virtual and isolated Python environments, which is an important topic for creating stable and reproducible computational environments. The appendix also discusses how to work with such environments using the conda package manager.

Source Code Listings Each chapter in this book has an accompanying IPython notebook that contains the chapter’s source code listings. These notebooks, and the data files required to run them, can be downloaded from the Source Code page on the Apress web site, at www.apress.com/9781484205549.