Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (3 votes)
148 views

Get Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib - 2nd Edition Robert Johansson free all chapters

Science

Uploaded by

hilemenabii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
148 views

Get Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib - 2nd Edition Robert Johansson free all chapters

Science

Uploaded by

hilemenabii
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Get ebook downloads in full at ebookmeta.

com

Numerical Python: Scientific Computing and Data


Science Applications with Numpy, SciPy and
Matplotlib - 2nd Edition Robert Johansson

https://ebookmeta.com/product/numerical-python-scientific-
computing-and-data-science-applications-with-numpy-scipy-
and-matplotlib-2nd-edition-robert-johansson-2/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Numerical Python: Scientific Computing and Data Science


Applications with Numpy, SciPy and Matplotlib - 2nd
Edition Robert Johansson
https://ebookmeta.com/product/numerical-python-scientific-computing-
and-data-science-applications-with-numpy-scipy-and-matplotlib-2nd-
edition-robert-johansson-2/
ebookmeta.com

Python Data Analytics: With Pandas, NumPy, and Matplotlib,


3rd Edition Fabio Nelli

https://ebookmeta.com/product/python-data-analytics-with-pandas-numpy-
and-matplotlib-3rd-edition-fabio-nelli/

ebookmeta.com

Python Data Analytics: With Pandas, NumPy, and Matplotlib,


3rd Edition Fabio Nelli

https://ebookmeta.com/product/python-data-analytics-with-pandas-numpy-
and-matplotlib-3rd-edition-fabio-nelli-2/

ebookmeta.com

Civil War Battles First Battle of Bull Run Battle of


Shiloh Battle of Antietam Battle of Gettysburg Battle of
Chickamauga 1st Edition Hourly History
https://ebookmeta.com/product/civil-war-battles-first-battle-of-bull-
run-battle-of-shiloh-battle-of-antietam-battle-of-gettysburg-battle-
of-chickamauga-1st-edition-hourly-history/
ebookmeta.com
Scholarship and Controversy Centenary Essays on the Life
and Work of Sir Kenneth Dover 1st Edition Stephen
Halliwell
https://ebookmeta.com/product/scholarship-and-controversy-centenary-
essays-on-the-life-and-work-of-sir-kenneth-dover-1st-edition-stephen-
halliwell/
ebookmeta.com

Beauty and the Boss Modern Fairytales 1 1st Edition Diane


Alberts Alberts Diane

https://ebookmeta.com/product/beauty-and-the-boss-modern-
fairytales-1-1st-edition-diane-alberts-alberts-diane/

ebookmeta.com

Science Voyage Class 7 Teacher s Manual CBSE For India Dr


Deepti Srivastava

https://ebookmeta.com/product/science-voyage-class-7-teacher-s-manual-
cbse-for-india-dr-deepti-srivastava/

ebookmeta.com

Make Electronics Learning by Discovery A hands on primer


for the new electronics enthusiast Charles Platt

https://ebookmeta.com/product/make-electronics-learning-by-discovery-
a-hands-on-primer-for-the-new-electronics-enthusiast-charles-platt/

ebookmeta.com

Filaments Theological Profiles Selected Essays Volume 2


1st Edition David Tracy

https://ebookmeta.com/product/filaments-theological-profiles-selected-
essays-volume-2-1st-edition-david-tracy/

ebookmeta.com
Migration, New Nationalisms and Populism: An
Epistemological Perspective on the Closure of Rich
Countries 1st Edition Rada Ivekovic
https://ebookmeta.com/product/migration-new-nationalisms-and-populism-
an-epistemological-perspective-on-the-closure-of-rich-countries-1st-
edition-rada-ivekovic/
ebookmeta.com
Numerical
Python
Scientific Computing and Data Science
Applications with Numpy,
SciPy and Matplotlib

Second Edition

Robert Johansson
Numerical Python
Scientific Computing and Data
Science Applications with Numpy,
SciPy and Matplotlib
Second Edition

Robert Johansson
Numerical Python: Scientific Computing and Data Science Applications with
Numpy, SciPy and Matplotlib
Robert Johansson
Urayasu-shi, Chiba, Japan

ISBN-13 (pbk): 978-1-4842-4245-2 ISBN-13 (electronic): 978-1-4842-4246-9


https://doi.org/10.1007/978-1-4842-4246-9
Library of Congress Control Number: 2018966798

Copyright © 2019 by Robert Johansson


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Todd Green
Development Editor: James Markham
Coordinating Editor: Jill Balzano
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail editorial@apress.com; for reprint, paperback, or audio rights,
please email bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book's product page, located at www.apress.com/9781484242452. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
To Mika and Erika.
Table of Contents
About the Author�����������������������������������������������������������������������������������������������������xv

About the Technical Reviewers�����������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xxi

Chapter 1: Introduction to Computing with Python�������������������������������������������������� 1


Environments for Computing with Python������������������������������������������������������������������������������������ 5
Python������������������������������������������������������������������������������������������������������������������������������������������� 6
Interpreter������������������������������������������������������������������������������������������������������������������������������� 7
IPython Console���������������������������������������������������������������������������������������������������������������������������� 8
Input and Output Caching�������������������������������������������������������������������������������������������������������� 9
Autocompletion and Object Introspection����������������������������������������������������������������������������� 11
Documentation���������������������������������������������������������������������������������������������������������������������� 11
Interaction with the System Shell����������������������������������������������������������������������������������������� 12
IPython Extensions���������������������������������������������������������������������������������������������������������������� 13
Jupyter���������������������������������������������������������������������������������������������������������������������������������������� 19
The Jupyter QtConsole���������������������������������������������������������������������������������������������������������� 20
The Jupyter Notebook����������������������������������������������������������������������������������������������������������� 21
Jupyter Lab���������������������������������������������������������������������������������������������������������������������������� 24
Cell Types������������������������������������������������������������������������������������������������������������������������������� 25
Editing Cells��������������������������������������������������������������������������������������������������������������������������� 26
Markdown Cells��������������������������������������������������������������������������������������������������������������������� 28
Rich Output Display��������������������������������������������������������������������������������������������������������������� 30
nbconvert������������������������������������������������������������������������������������������������������������������������������ 34

v
Table of Contents

Spyder: An Integrated Development Environment���������������������������������������������������������������������� 37


Source Code Editor���������������������������������������������������������������������������������������������������������������� 38
Consoles in Spyder���������������������������������������������������������������������������������������������������������������� 40
Object Inspector�������������������������������������������������������������������������������������������������������������������� 40
Summary������������������������������������������������������������������������������������������������������������������������������������ 41
Further Reading�������������������������������������������������������������������������������������������������������������������������� 41
References���������������������������������������������������������������������������������������������������������������������������������� 41

Chapter 2: Vectors, Matrices, and Multidimensional Arrays��������������������������������� 43


Importing the Modules���������������������������������������������������������������������������������������������������������������� 44
The NumPy Array Object������������������������������������������������������������������������������������������������������������� 44
Data Types����������������������������������������������������������������������������������������������������������������������������� 46
Order of Array Data in Memory���������������������������������������������������������������������������������������������� 49
Creating Arrays��������������������������������������������������������������������������������������������������������������������������� 50
Arrays Created from Lists and Other Array-Like Objects������������������������������������������������������� 52
Arrays Filled with Constant Values���������������������������������������������������������������������������������������� 52
Arrays Filled with Incremental Sequences���������������������������������������������������������������������������� 54
Arrays Filled with Logarithmic Sequences���������������������������������������������������������������������������� 54
Meshgrid Arrays�������������������������������������������������������������������������������������������������������������������� 55
Creating Uninitialized Arrays������������������������������������������������������������������������������������������������� 56
Creating Arrays with Properties of Other Arrays�������������������������������������������������������������������� 56
Creating Matrix Arrays����������������������������������������������������������������������������������������������������������� 57
Indexing and Slicing�������������������������������������������������������������������������������������������������������������������� 58
One-Dimensional Arrays�������������������������������������������������������������������������������������������������������� 58
Multidimensional Arrays�������������������������������������������������������������������������������������������������������� 60
Views������������������������������������������������������������������������������������������������������������������������������������� 62
Fancy Indexing and Boolean-Valued Indexing����������������������������������������������������������������������� 63
Reshaping and Resizing�������������������������������������������������������������������������������������������������������������� 66
Vectorized Expressions��������������������������������������������������������������������������������������������������������������� 70
Arithmetic Operations������������������������������������������������������������������������������������������������������������ 72
Elementwise Functions��������������������������������������������������������������������������������������������������������� 76

vi
Table of Contents

Aggregate Functions������������������������������������������������������������������������������������������������������������� 79
Boolean Arrays and Conditional Expressions������������������������������������������������������������������������ 82
Set Operations����������������������������������������������������������������������������������������������������������������������� 85
Operations on Arrays������������������������������������������������������������������������������������������������������������� 87
Matrix and Vector Operations������������������������������������������������������������������������������������������������������ 88
Summary������������������������������������������������������������������������������������������������������������������������������������ 95
Further Reading�������������������������������������������������������������������������������������������������������������������������� 95
References���������������������������������������������������������������������������������������������������������������������������������� 96

Chapter 3: Symbolic Computing����������������������������������������������������������������������������� 97


Importing SymPy������������������������������������������������������������������������������������������������������������������������� 98
Symbols�������������������������������������������������������������������������������������������������������������������������������������� 99
Numbers������������������������������������������������������������������������������������������������������������������������������ 102
Expressions������������������������������������������������������������������������������������������������������������������������������� 109
Manipulating Expressions��������������������������������������������������������������������������������������������������������� 110
Simplification����������������������������������������������������������������������������������������������������������������������� 111
Expand��������������������������������������������������������������������������������������������������������������������������������� 112
Factor, Collect, and Combine����������������������������������������������������������������������������������������������� 114
Apart, Together, and Cancel������������������������������������������������������������������������������������������������� 115
Substitutions����������������������������������������������������������������������������������������������������������������������� 115
Numerical Evaluation���������������������������������������������������������������������������������������������������������������� 117
Calculus������������������������������������������������������������������������������������������������������������������������������������ 118
Derivatives��������������������������������������������������������������������������������������������������������������������������� 119
Integrals������������������������������������������������������������������������������������������������������������������������������ 121
Series���������������������������������������������������������������������������������������������������������������������������������� 123
Limits����������������������������������������������������������������������������������������������������������������������������������� 125
Sums and Products������������������������������������������������������������������������������������������������������������� 126
Equations���������������������������������������������������������������������������������������������������������������������������������� 127
Linear Algebra��������������������������������������������������������������������������������������������������������������������������� 130

vii
Table of Contents

Summary���������������������������������������������������������������������������������������������������������������������������������� 134
Further Reading������������������������������������������������������������������������������������������������������������������������ 134
Reference���������������������������������������������������������������������������������������������������������������������������������� 134

Chapter 4: Plotting and Visualization������������������������������������������������������������������� 135


Importing Modules�������������������������������������������������������������������������������������������������������������������� 136
Getting Started�������������������������������������������������������������������������������������������������������������������������� 137
Interactive and Noninteractive Modes��������������������������������������������������������������������������������� 141
Figure���������������������������������������������������������������������������������������������������������������������������������������� 143
Axes������������������������������������������������������������������������������������������������������������������������������������������ 145
Plot Types���������������������������������������������������������������������������������������������������������������������������� 146
Line Properties�������������������������������������������������������������������������������������������������������������������� 147
Legends������������������������������������������������������������������������������������������������������������������������������� 152
Text Formatting and Annotations����������������������������������������������������������������������������������������� 153
Axis Properties�������������������������������������������������������������������������������������������������������������������� 156
Advanced Axes Layouts������������������������������������������������������������������������������������������������������������ 168
Insets����������������������������������������������������������������������������������������������������������������������������������� 168
Subplots������������������������������������������������������������������������������������������������������������������������������ 170
Subplot2grid������������������������������������������������������������������������������������������������������������������������ 172
GridSpec������������������������������������������������������������������������������������������������������������������������������ 173
Colormap Plots�������������������������������������������������������������������������������������������������������������������������� 174
3 D Plots������������������������������������������������������������������������������������������������������������������������������������ 177
Summary���������������������������������������������������������������������������������������������������������������������������������� 180
Further Reading������������������������������������������������������������������������������������������������������������������������ 180
References�������������������������������������������������������������������������������������������������������������������������������� 181

Chapter 5: Equation Solving��������������������������������������������������������������������������������� 183


Importing Modules�������������������������������������������������������������������������������������������������������������������� 184
Linear Equation Systems���������������������������������������������������������������������������������������������������������� 185
Square Systems������������������������������������������������������������������������������������������������������������������ 186
Rectangular Systems���������������������������������������������������������������������������������������������������������� 192

viii
Table of Contents

Eigenvalue Problems���������������������������������������������������������������������������������������������������������������� 196


Nonlinear Equations������������������������������������������������������������������������������������������������������������������ 198
Univariate Equations������������������������������������������������������������������������������������������������������������ 199
Systems of Nonlinear Equations������������������������������������������������������������������������������������������ 207
Summary���������������������������������������������������������������������������������������������������������������������������������� 212
Further Reading������������������������������������������������������������������������������������������������������������������������ 212
References�������������������������������������������������������������������������������������������������������������������������������� 212

Chapter 6: Optimization���������������������������������������������������������������������������������������� 213


Importing Modules�������������������������������������������������������������������������������������������������������������������� 214
Classification of Optimization Problems����������������������������������������������������������������������������������� 214
Univariate Optimization������������������������������������������������������������������������������������������������������������� 217
Unconstrained Multivariate Optimization���������������������������������������������������������������������������������� 221
Nonlinear Least Square Problems��������������������������������������������������������������������������������������������� 230
Constrained Optimization���������������������������������������������������������������������������������������������������������� 232
Linear Programming������������������������������������������������������������������������������������������������������������ 238
Summary���������������������������������������������������������������������������������������������������������������������������������� 241
Further Reading������������������������������������������������������������������������������������������������������������������������ 241
References�������������������������������������������������������������������������������������������������������������������������������� 242

Chapter 7: Interpolation���������������������������������������������������������������������������������������� 243


Importing Modules�������������������������������������������������������������������������������������������������������������������� 244
Interpolation������������������������������������������������������������������������������������������������������������������������������ 244
Polynomials������������������������������������������������������������������������������������������������������������������������������� 245
Polynomial Interpolation����������������������������������������������������������������������������������������������������������� 249
Spline Interpolation������������������������������������������������������������������������������������������������������������������� 255
Multivariate Interpolation���������������������������������������������������������������������������������������������������������� 258
Summary���������������������������������������������������������������������������������������������������������������������������������� 265
Further Reading������������������������������������������������������������������������������������������������������������������������ 265
References�������������������������������������������������������������������������������������������������������������������������������� 265

ix
Table of Contents

Chapter 8: Integration������������������������������������������������������������������������������������������ 267


Importing Modules�������������������������������������������������������������������������������������������������������������������� 268
Numerical Integration Methods������������������������������������������������������������������������������������������������ 269
Numerical Integration with SciPy���������������������������������������������������������������������������������������������� 274
Tabulated Integrand������������������������������������������������������������������������������������������������������������� 277
Multiple Integration������������������������������������������������������������������������������������������������������������������� 280
Symbolic and Arbitrary-Precision Integration��������������������������������������������������������������������������� 285
Line Integrals����������������������������������������������������������������������������������������������������������������������� 288
Integral Transforms������������������������������������������������������������������������������������������������������������������� 289
Summary���������������������������������������������������������������������������������������������������������������������������������� 292
Further Reading������������������������������������������������������������������������������������������������������������������������ 293
References�������������������������������������������������������������������������������������������������������������������������������� 293

Chapter 9: Ordinary Differential Equations����������������������������������������������������������� 295


Importing Modules�������������������������������������������������������������������������������������������������������������������� 296
Ordinary Differential Equations������������������������������������������������������������������������������������������������� 296
Symbolic Solution to ODEs�������������������������������������������������������������������������������������������������������� 298
Direction Fields�������������������������������������������������������������������������������������������������������������������� 304
Solving ODEs Using Laplace Transformations��������������������������������������������������������������������� 309
Numerical Methods for Solving ODEs��������������������������������������������������������������������������������������� 313
Numerical Integration of ODEs Using SciPy������������������������������������������������������������������������������ 317
Summary���������������������������������������������������������������������������������������������������������������������������������� 332
Further Reading������������������������������������������������������������������������������������������������������������������������ 333
References�������������������������������������������������������������������������������������������������������������������������������� 333

Chapter 10: Sparse Matrices and Graphs������������������������������������������������������������� 335


Importing Modules�������������������������������������������������������������������������������������������������������������������� 336
Sparse Matrices in SciPy���������������������������������������������������������������������������������������������������������� 336
Functions for Creating Sparse Matrices������������������������������������������������������������������������������ 342
Sparse Linear Algebra Functions����������������������������������������������������������������������������������������� 345

x
Table of Contents

Linear Equation Systems����������������������������������������������������������������������������������������������������� 345


Graphs and Networks���������������������������������������������������������������������������������������������������������� 352
Summary���������������������������������������������������������������������������������������������������������������������������������� 360
Further Reading������������������������������������������������������������������������������������������������������������������������ 361
References�������������������������������������������������������������������������������������������������������������������������������� 361

Chapter 11: Partial Differential Equations������������������������������������������������������������ 363


Importing Modules�������������������������������������������������������������������������������������������������������������������� 364
Partial Differential Equations���������������������������������������������������������������������������������������������������� 365
Finite-Difference Methods�������������������������������������������������������������������������������������������������������� 366
Finite-Element Methods������������������������������������������������������������������������������������������������������������ 373
Survey of FEM Libraries������������������������������������������������������������������������������������������������������ 377
Solving PDEs Using FEniCS������������������������������������������������������������������������������������������������������� 378
Summary���������������������������������������������������������������������������������������������������������������������������������� 403
Further Reading������������������������������������������������������������������������������������������������������������������������ 403
References�������������������������������������������������������������������������������������������������������������������������������� 404

Chapter 12: Data Processing and Analysis����������������������������������������������������������� 405


Importing Modules�������������������������������������������������������������������������������������������������������������������� 406
Introduction to Pandas�������������������������������������������������������������������������������������������������������������� 407
Series���������������������������������������������������������������������������������������������������������������������������������� 407
DataFrame��������������������������������������������������������������������������������������������������������������������������� 410
Time Series�������������������������������������������������������������������������������������������������������������������������� 422
The Seaborn Graphics Library��������������������������������������������������������������������������������������������������� 434
Summary���������������������������������������������������������������������������������������������������������������������������������� 440
Further Reading������������������������������������������������������������������������������������������������������������������������ 440
References�������������������������������������������������������������������������������������������������������������������������������� 441

Chapter 13: Statistics������������������������������������������������������������������������������������������� 443


Importing Modules�������������������������������������������������������������������������������������������������������������������� 444
Review of Statistics and Probability����������������������������������������������������������������������������������������� 444
Random Numbers��������������������������������������������������������������������������������������������������������������������� 446
xi
Table of Contents

Random Variables and Distributions����������������������������������������������������������������������������������������� 451


Hypothesis Testing�������������������������������������������������������������������������������������������������������������������� 460
Nonparametric Methods����������������������������������������������������������������������������������������������������������� 466
Summary���������������������������������������������������������������������������������������������������������������������������������� 469
Further Reading������������������������������������������������������������������������������������������������������������������������ 470
References�������������������������������������������������������������������������������������������������������������������������������� 470

Chapter 14: Statistical Modeling�������������������������������������������������������������������������� 471


Importing Modules�������������������������������������������������������������������������������������������������������������������� 472
Introduction to Statistical Modeling������������������������������������������������������������������������������������������ 473
Defining Statistical Models with Patsy�������������������������������������������������������������������������������������� 474
Linear Regression��������������������������������������������������������������������������������������������������������������������� 485
Example Datasets���������������������������������������������������������������������������������������������������������������� 494
Discrete Regression������������������������������������������������������������������������������������������������������������������ 496
Logistic Regression������������������������������������������������������������������������������������������������������������� 496
Poisson Model��������������������������������������������������������������������������������������������������������������������� 502
Time Series������������������������������������������������������������������������������������������������������������������������������� 506
Summary���������������������������������������������������������������������������������������������������������������������������������� 511
Further Reading������������������������������������������������������������������������������������������������������������������������ 511
References�������������������������������������������������������������������������������������������������������������������������������� 511

Chapter 15: Machine Learning����������������������������������������������������������������������������� 513


Importing Modules�������������������������������������������������������������������������������������������������������������������� 514
Brief Review of Machine Learning�������������������������������������������������������������������������������������������� 515
Regression�������������������������������������������������������������������������������������������������������������������������������� 518
Classification����������������������������������������������������������������������������������������������������������������������������� 529
Clustering���������������������������������������������������������������������������������������������������������������������������������� 535
Summary���������������������������������������������������������������������������������������������������������������������������������� 540
Further Reading������������������������������������������������������������������������������������������������������������������������ 540
References�������������������������������������������������������������������������������������������������������������������������������� 541

xii
Table of Contents

Chapter 16: Bayesian Statistics���������������������������������������������������������������������������� 543


Importing Modules�������������������������������������������������������������������������������������������������������������������� 544
Introduction to Bayesian Statistics������������������������������������������������������������������������������������������� 545
Model Definition������������������������������������������������������������������������������������������������������������������������ 548
Sampling Posterior Distributions����������������������������������������������������������������������������������������� 553
Linear Regression���������������������������������������������������������������������������������������������������������������� 558
Summary���������������������������������������������������������������������������������������������������������������������������������� 571
Further Reading������������������������������������������������������������������������������������������������������������������������ 572
References�������������������������������������������������������������������������������������������������������������������������������� 572

Chapter 17: Signal Processing����������������������������������������������������������������������������� 573


Importing Modules�������������������������������������������������������������������������������������������������������������������� 574
Spectral Analysis����������������������������������������������������������������������������������������������������������������������� 574
Fourier Transforms�������������������������������������������������������������������������������������������������������������� 575
Windowing��������������������������������������������������������������������������������������������������������������������������� 581
Spectrogram������������������������������������������������������������������������������������������������������������������������ 585
Signal Filters����������������������������������������������������������������������������������������������������������������������������� 590
Convolution Filters��������������������������������������������������������������������������������������������������������������� 590
FIR and IIR Filters���������������������������������������������������������������������������������������������������������������� 593
Summary���������������������������������������������������������������������������������������������������������������������������������� 598
Further Reading������������������������������������������������������������������������������������������������������������������������ 599
References�������������������������������������������������������������������������������������������������������������������������������� 599

Chapter 18: Data Input and Output����������������������������������������������������������������������� 601


Importing Modules�������������������������������������������������������������������������������������������������������������������� 602
Comma-Separated Values��������������������������������������������������������������������������������������������������������� 603
HDF5����������������������������������������������������������������������������������������������������������������������������������������� 608
h5py������������������������������������������������������������������������������������������������������������������������������������� 610
PyTables������������������������������������������������������������������������������������������������������������������������������ 623
Pandas HDFStore����������������������������������������������������������������������������������������������������������������� 629

xiii
Table of Contents

JSON����������������������������������������������������������������������������������������������������������������������������������������� 631
Serialization������������������������������������������������������������������������������������������������������������������������������ 636
Summary���������������������������������������������������������������������������������������������������������������������������������� 639
Further Reading������������������������������������������������������������������������������������������������������������������������ 639
Reference���������������������������������������������������������������������������������������������������������������������������������� 640

Chapter 19: Code Optimization����������������������������������������������������������������������������� 641


Importing Modules�������������������������������������������������������������������������������������������������������������������� 644
Numba�������������������������������������������������������������������������������������������������������������������������������������� 644
Cython��������������������������������������������������������������������������������������������������������������������������������������� 652
Summary���������������������������������������������������������������������������������������������������������������������������������� 664
Further Reading������������������������������������������������������������������������������������������������������������������������ 665
References�������������������������������������������������������������������������������������������������������������������������������� 665

Appendix: Installation������������������������������������������������������������������������������������������� 667


Miniconda and Conda��������������������������������������������������������������������������������������������������������������� 668
A Complete Environment���������������������������������������������������������������������������������������������������������� 676
Summary���������������������������������������������������������������������������������������������������������������������������������� 680
Further Reading������������������������������������������������������������������������������������������������������������������������ 680

Index��������������������������������������������������������������������������������������������������������������������� 683

xiv
About the Author
Robert Johansson is an experienced Python programmer
and computational scientist, with a Ph.D. 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 he is now working as a data scientist in the
IT industry.

xv
Other documents randomly have
different content
Infants, the children of the spring!
How can an infant die
When butterflies are on the wing,
Green grass, and such a sky?
How can they die at spring?

The writer of these lines was a poet worth rediscovering, and


Messrs. Blunden and Porter have given us a book in which we can
wander at will, peering into hedges and at the traffic of the grass, as
in few even of the great poets. Mr. Blunden has also written an
admirable, though needlessly pugnacious account of the life of The
Green Man, as Clare was called in Lamb’s circle because of his
clothes. It is a story of struggle, poverty, drink, a moment’s fame
without money to correspond, a long family, and the madness of a
man who, escaping from the asylum, ate “the grass on the roadside
which seemed to taste something like bread.” Knowing the events of
his life, we read Clare’s poetry with all the more intense curiosity.
And, if we do not expect to find a Blake or a Wordsworth, we shall
not be disappointed. Certainly this is a book that must go on the
shelf near the works of Mr. Hudson.
XII
HISTORIANS AS ENTERTAINERS

Herodotus is one of the oldest illustrations of the fact that a test of


good literature is its capacity to entertain us. There are two sorts of
writing—the entertaining and the dull—and the dull is outside
literature. This is a fact which, though it is perfectly obvious, tends
to be forgotten by many writers, even by many able writers, in every
century. Authors fall in love with their own ponderosity, forgetting
that a huge tome is too often a huge tomb. That is the explanation
of the long lives and the still longer histories that the publishers and
the authors of the nineteenth century loved. Biographies became
life-size, and histories rivalled in length the wars they chronicled. A
Victorian biographer appeared to think that he was performing more
ambitious work in writing a life of Milton in six volumes than if he
were to write it in one. Similarly, a historian instead of giving us a
Cromwell that the eye could take in as one absorbing figure, would
devote one volume to this bit of him and another to that, and would
leave us with a mass of information about his disjecta membra,
which we might or might not piece together, just as we pleased. This
was called scientific history. Its disciples forgot that history is an art
and that, like all other arts, whatever its ultimate object, it should be
subject to the law of entertainment. Nothing else will keep history
alive, except as a schoolbook or a source-book. An inaccurate history
that entertains will outlive an accurate history that wearies.
Herodotus did not cease to be read, even when he was generally
regarded as the father of lies. It is true that scholars no longer
regard him as a liar, and that Mr. Godley, in the preface to his
admirable translation in the Loeb Library, claims with Dr. Macan that
“the most stringent application of historical and critical methods to
the text of Herodotus leaves the work irrevocably and irreplaceably
at the head of European prose literature, whether in its scientific or
in its artistic character.” At the same time, even if we did not know
about the scientific value of Herodotus, his artistic value would be
indisputable. He was as indefatigably interested in the world as Mr.
Pepys was in himself, and he can infect us with the thrill of his
delightful curiosity.
Curiosity, on the other hand, implies interest in some sort of
truth, and the pursuit of some sort of truth seems to be an essential
in a book that is to entertain us permanently. The artist is moralist as
well as entertainer, and the truth that lies in him shapes his work,
whether he is Æschylus or Plato, Herodotus or Sallust. Sallust’s
Jugurtha, Professor Rolfe warns us in a preface to the Loeb
translation, “is rather like a historical novel of the better class than
like sober history” and the Cataline, we are told, “is inaccurate in
many of its details ... with inevitable distortion of the facts.” Even so,
both works are entertaining statements of a great moral idea—the
idea of the corruption of human nature by success. Sallust, it may be
argued, had the propagandist purpose of attacking the corruption of
the nobles rather than the moral purpose of exhibiting the corruption
of human nature, but he writes his history with an amazing dramatic
sense of the catastrophe that occurs even in great souls. It occurred
in the soul of Jugurtha, and in the soul of Rome. “When Carthage,
the rival of Rome’s sway, had perished root and branch, and all seas
and lands were open, then Fortune began to grow cruel and to bring
confusion into all our affairs. Those who had found it easy to bear
hardship and dangers, anxiety and adversity, found leisure and
wealth, desirable under other circumstances, a burden and a curse.
Hence the lust for power, then for money, grew upon them; these
were, I may say, the root of all evils.” What is all literature but the
fable of such things? It may be an inspiring fable or a derisive fable,
a tragic fable or a comic fable, but in any event it cannot be good
literature unless it is an entertaining fable.
Herodotus, certainly, never forgets for long that history is a
fable. That wonderful anecdote of Gyges and the infatuated King
who compelled him to hide behind the door and to look on the
Queen when she was naked, with the result that the Queen on
discovering him, ordered him to kill the King and marry her or die
himself, is not a mere unrelated scene as from a ballet, but has its
tragic signature five generations later, when the power of Crœsus,
the descendant of Gyges, is destroyed and he is a prisoner in the
camp of Cyrus. Crœsus, being unable to understand how the
disaster had happened, obtained permission from Cyrus to send
messengers to Delphi to enquire of the Oracle why it had deceived
him. And the priestess replied: “None may escape his destined lot,
not even a god. Crœsus hath paid for the sin of his ancestor of the
fifth generation: who, being of the guard of the Heraclidæ, was led
by the guile of a woman to slay his master, and took to himself the
royal state of that master, whereto he had no right.” We find in
pagan literature a sense of the divine government of the world that
is missing from the greater part of modern Christian literature. The
pagan historians, I think, have a profounder sense of sin and of the
sufferings that result from sin than most of the Christian historians.
Nowadays, we hesitate before allowing even Richard III or Judge
Jeffreys to have been a sinner. And, as we have found no substitute
for those ancient colours of vice and virtue, much of our history is
colourless and uninteresting. The sense of sin is of infinite value to
an artist, if only because it enables him to see how striking are the
contrasts that exist in every human being. He sees the great man as
a miserable sinner, and he sees him all the more truthfully for this.
He sees the beautiful woman as a miserable sinner, and he sees her
all the more truthfully for this. Aristotle, indeed, thought that it was
impossible to write great tragic literature except about a noble
character who was seen to be a sinner. It was probably never done
till the appearance of the Gospels, and I am not sure whether it has
been done since. Shakespeare had as compassionate a sense of the
flaw in human nature even at its greatest as the Greek dramatists
and the supreme Greek biographer.
There is, no doubt, a school of writers who have so keen a sense
of the flaws that they can see scarcely anything else. This is inimical
to art. Suetonius provides us with a feast of flaws, from which we
rise with the feeling that we have been dining on spiced and putrid
dishes. His was not a disinterested observation of human character.
He was a specialist in the vices. It is appalling to think what he
would have made of Sempronia, one of the many Roman ladies
whom Catiline enticed into his conspiracy. Sallust’s portrait of her is a
masterpiece:

Now among these women was Sempronia, who had often


committed many crimes of masculine daring. In birth and
beauty, in her husband and children, she was abundantly
favoured by fortune; well read in the literature of Greece and
Rome, able to play the lyre and dance more skilfully than an
honest woman need, and having many other
accomplishments which minister to voluptuousness. But there
was nothing which she held so cheap as modesty and
chastity; you could not easily say whether she was less
sparing of her money or her honour; her desires were so
ardent that she sought men more often than she was sought
by them. Even before the time of the conspiracy, she had
often broken her word, repudiated her debts, been privy to
murder; poverty and extravagance combined had driven her
headlong. Nevertheless, she was a woman of no mean
endowments; she could write verses, bandy jests, and use
language which was modest, or tender, or wanton; in fine,
she possessed a high degree of wit and charm.

A miserable sinner, undoubtedly. How odious and how interesting!


But, even as gossips, how these ancient historians still keep their
hold on us! Herodotus is the father of nursery tales as well as of
moral tales. His account of Egypt in the second book of his history
may appeal to the anthropologist in some of us; it also appeals to
the child in all of us. He must have omitted thousands of the stories
that he heard on his travels, but he had a genius for finding room for
the interesting story. His pages are rich in attractive stories like that
which tells how Psammetichus decided whether the Egyptians or the
Phrygians were the oldest nation:

Now before Psammetichus became king of Egypt, the


Egyptians deemed themselves to be the oldest nation on
earth.... Psammetichus, being nowise able to discover by
enquiry what men had first come into being, devised a plan
whereby he took two new-born children of common men and
gave them to a shepherd to bring up among his flock. He
gave charge that none should speak any word in their
hearing; they were to lie by themselves in a lonely hut, and in
due season the shepherd was to bring goats and give the
children their milk and do all else needful. Psammetichus did
this, and gave this charge, because he desired to hear what
speech would first break from the children, when they were
past the age of indistinct babbling. And he had his wish; for,
when the shepherd had done as he was bidden for two years,
one day as he opened the door and entered, both the
children ran to him, stretching out their hands and calling
“Bekos.” When he first heard this he said nothing of it; but
coming often and taking careful note, he was ever hearing
this same word, till at last he told the matter to his master,
and on command brought the children into the King’s
presence. Psammetichus heard them himself, and enquired to
what language this word “Bekos” might belong; he found it to
be a Phrygian word signifying bread. Reasoning from this fact
the Egyptians confessed that the Phrygians were older than
they.

Scientific? Perhaps not. And yet science and art may embrace in the
recording of such stories as this. But it is in the museum of the arts,
not in that of the sciences, that Herodotus holds his immortal place.
He may not be the first of the scientific historians: he is certainly the
first of the European masters of the art of entertaining prose.
XIII
A WORDSWORTH DISCOVERY

A good many people were pleased—not without malice—when


Professor Harper discovered a few years ago that Wordsworth had
an illegitimate daughter. It was like hearing a piece of scandal about
an archbishop. As a matter of fact, the story, as Professor Harper
tells it, is not a scandal; it is merely a puzzle. The figures in the
episode are names and shadows: we know almost nothing as
regards their feelings for each other or what it was that prevented
the lovers from marrying. Professor Harper believes that Wordsworth
has left a disguised version of the story in Vaudracour and Julia.
Wordsworth himself says of Vaudracour and Julia that “the facts are
true,” and the main “facts” in the poem are that the lovers wish to
marry, cannot gain their parent’s consent, and give way to passion,
and that after this their parents, instead of softening in their
attitude, insist more harshly than ever on keeping them apart.
Wordsworth is vehement in his contention that Vaudracour was no
common seducer yielding to the lusts of the flesh, and the
suggestion is fairly clear that the youth thought he was taking the
only way to make marriage inevitable. Consider these lines, which
impute honourable motives, if not honourable conduct, to the lover:
So passed the time, till whether through effect
Of some unguarded moment that dissolved
Virtuous restraint—ah, speak it, think it, not!
Deem rather that the fervent youth, who saw
So many bars between his present state
And the dear haven where he wished to be
In honourable wedlock with his love,
Was in his judgment tempted to decline
To perilous weakness, and entrust his cause
To nature for a happy end of all;
Deem that by such fond hope the youth was swayed
And bear with their transgression, when I add
That Julia, wanting yet the name of wife,
Carried about her for a secret grief,
The promise of a mother.

These lines have an ethical rather than a poetical interest. Whether


Wordsworth, in writing them, was consciously or subconsciously
attempting his own moral justification, we do not know. But
Professor Harper has collected a number of facts that make it appear
likely that he was. Certainly, the story of Wordsworth and Marie-
Anne Vallon at Orleans in 1792, so far as we know it, might without
violence be dramatised as the story of Vaudracour and Julia.
Bear in mind, for example, the “many bars” that stood in the
way of Wordsworth’s marriage to Marie-Anne, or “Annette,” Vallon.
They were not, as in the poem, barriers of class, but they were the
equally insurmountable barriers of creed, both political and religious.
Wordsworth was a young Englishman, full of the ardour of the
Revolution, and a Protestant of so sceptical a cast that Coleridge
described him as a “semi-atheist.” Annette, for her part, was the
child of parents who were zealots in the cause of Royalism and
Catholicism. They must have regarded the coming of such a suitor
as Wordsworth with the same horror with which a reader of the
Morning Post would learn that his daughter had fallen in love with a
Catholic Sinn Feiner or a Jewish Bolshevist. The position was even
more bitter than this suggests. The sectarian and political passions
that raged in France were more comparable to the passions of
Orange Belfast than to any that can be imagined in the atmosphere
of modern England. Wordsworth may well have appeared to these
orthodox parents a representative of Satan. He was the murder-
gang personified. Nor, to make up for this, was he even a good
match. He was an exceedingly poor young man who had just come
of age. Add to this the fact that it was almost impossible at the time
for an orthodox Catholic and Royalist to marry a Revolutionary
sceptic. Marriage had become a State affair under the Revolution,
and no Catholic could permit his daughter to go through a marriage
ceremony that seemed to deny that marriage was a sacrament. It is
true that marriages could still be performed by the clergy, but only
by such clergy as accepted their position under the new constitution
as functionaries of the State. Republican clergy of this kind would be
regarded by the Vallon family as traitors and scarcely better than
atheists. Marriages celebrated by them would be looked on as invalid
—as mere licences to live in sin. Had Wordsworth become a Catholic,
or had he been of a compromising disposition, it would have been
easy enough to find a non-juring priest to perform the ceremony.
But it is unlikely that a priest, who was zealous enough to face
persecution rather than recognise the Republic, would have been
willing to marry one of his flock to a free-thinking revolutionary.
Respectability might urge that, when the lovers had already gone so
far, nothing remained but to make the best of it and permit them to
marry. Fanaticism, however, might well regard such a marriage as
but the adding of one sin to another. The Church itself, by marrying
the sinners, would make itself a partner in the sin. We have to
reflect how adamantine is the faith of the orthodox in order to
understand the “many bars” that hindered the marriage of
Wordsworth and Annette. Remembering this, we cannot dismiss as
improbable Professor Harper’s theory that Wordsworth abandoned
Marie-Anne reluctantly, and that when he settled in Blois, he did so
because he had been driven away by her relatives and yet desired to
remain near her.
All we know of Wordsworth, and all the facts in Professor
Harper’s story, make it impossible to believe that he would willingly
have deserted Marie-Anne and his daughter. The baptism of the child
was entered in the registry of baptisms in the parish of Sainte-Croix,
“Williams Wordsodsth” in his absence being represented by a local
official. She was baptised Anne Caroline, and it was as Anne Caroline
Wordsworth, daughter of “Williams Wordsworth, landowner,” that
she was married in Paris about twenty-four years later. Wordsworth
appears to have kept constantly in touch with her and her mother in
the meantime, and, when peace was in sight in 1802, he and his
sister Dorothy determined to cross to France and see them. A
meeting took place in Calais. It was the preliminary to a marriage,
but not to marriage with Annette, who, indeed, never married, but
went through life as Madame Vallon. Two months after the Calais
meeting Wordsworth married Mary Hutchinson. That he had been
deeply moved by the meeting with his child rather than with her
mother is suggested by the mood of the sonnet he wrote at the
time: “It is a beauteous evening, calm and free.”
Professor Harper is of opinion that Wordsworth’s love for Marie-
Anne Vallon was an event of supreme importance in his life. He
holds that the facts he has discovered throw “light upon many of
Wordsworth’s poems.” I do not think that on this point he has proved
his case. In his two-volume life of Wordsworth, it may be
remembered, he even goes so far as to assign the “Lucy” of so many
beautiful poems to a French original. Lovers of a great poet are
naturally led to speculate as to the experiences out of which his
poems grew. There is nothing of the vice of Paul Pry in attempting
thus to discover the sources of the experiences the poet
communicates in his verse. The theme of every poet is the
experiences that have moved his soul most profoundly. And many, or
most, of those experiences spring from his relations with other
human beings. At the same time, there is no evidence that
Wordsworth in his work was ever influenced by Marie-Anne Vallon as
Keats was influenced by Fanny Brawne. It is doubtful if any women
every really took the place of his sister in his heart. “She gave me
eyes, she gave me ears,” could be said only of Dorothy. It was the
fire of affection, not the fire of passion, that glowed in Wordsworth’s
soul. “Oh, my dear, dear sister!” he cried in one of his letters, “with
what transport shall I again meet you! With what rapture shall I
again wear out the day in your sight. So eager is my desire to see
you that all other obstacles vanish. I see you in a moment running,
or rather flying, to my arms.” He was in life as in literature a devoted
brother rather than a devoted lover. Even Professor Harper can give
no other woman but Dorothy the position of presiding genius over
his life and work. This does not necessarily involve our acceptance of
the common theory that Dorothy was the original around whom the
“Lucy” poems were written. But, had Lucy been a Frenchwoman,
Wordsworth would hardly have written:

I travelled among unknown men


In lands beyond the sea;
Nor England did I know till then
What love I bore to thee....

Among thy mountains did I feel


The joy of my desire;
And she I cherished turned her wheel
Beside an English fire.

To interpret this as a dramatisation of his early passion in France is


1
to strain probability.

1
I understand that Professor Harper disclaims
what seemed to me the obvious interpretation of
a passage in his book.
Professor Harper, then, has discovered an interesting episode in
Wordsworth’s life, but I do not think he has discovered what may be
called a key episode. It may turn out to have had more influence on
Wordsworth’s destiny than at present appears. But we do not yet
know enough even about the circumstances to get any fresh light
from it either on his work or on his character.
As regards Annette, we learn from a letter of Dorothy’s, written
in 1815, that she shared, and continued to share, the Royalist
convictions of her people. She often, Dorothy affirms, “risked her life
in defence of adherents to that cause, and she despised and
detested Buonaparte.” In 1820, Wordsworth, his wife, and Dorothy
visited Paris and lived on intimate terms with Annette, Caroline, and
Caroline’s husband. They even went to lodge in the same street. Of
Caroline it was reported earlier that “she resembles her father most
strikingly.” For the rest, Christopher Wordsworth, Bishop of Lincoln,
when writing his uncle’s biography, said nothing about the matter.
He cannot be accused of having hidden anything of very great
significance. The truth is now out, and we know little more about
Wordsworth than we knew before.
XIV
THE POETRY OF POE

“My first object (as usual) was originality,” said Poe, in discussing the
versification of “The Raven.” It is a remarkable fact that the two
great poets of America—Poe and Whitman—were two of the most
deliberately original poets of the nineteenth century—in English at
least. They were both conscious frontiersmen of poetry, drawn to
unmapped territories, settlers on virgin soil. This may help to explain
some of their imperfections. Each of them gives us the impression of
a genius rich but imperfectly cultivated. Different though they were
from each other, they resembled each other in a certain lack of the
talent of order, of taste, of “finish.” They were both capable of lapses
from genius into incompetence, from beauty into provincialism, to an
unusual degree. A contemporary critic said of Poe that he had not
talent equal to his genius. Neither had Whitman. In the greatest
poets, genius and talent go hand in hand. Poe seldom wrote a poem
in which his mood seems to have attained its perfect expression. His
poetry does not get near perfection even in the sense in which
Coleridge’s fragments do. It seems, as a rule, like a first sketch for
greater things. His Complete Poems, indeed, is one of the most
wonderful sketch-books of a man of genius in literature.
Poe himself attributed the defects of his work to lack of leisure
rather than to lack of talent. “Events not to be controlled,” he said in
the preface to the 1845 edition of his poems, “have prevented me
from making, at any time, any serious effort in what, under happier
circumstances, would have been the field of my choice. With me
poetry has been not a purpose but a passion, and the passions
should be held in reverence; they must not—they cannot at will be
excited, with an eye to the petty compensations, or the more petty
commendations, of mankind.” Other poets, however, who have lived
in as bitter circumstances as Poe, have written an incomparably
greater body of good poetry. There was in him some flaw that kept
him, as a rule, from being more than a great beginner. It may have
been partly due to theatrical qualities that he inherited from his
actress mother. Again and again he mingles the landscape of
dreamland with the tawdry grandeur of the stage. He takes a
footlights view of romance when, having begun “Lenore” with the
lines——

Ah, broken is the golden bowl!—the spirit flown for ever!—


Let the bell toll!—a saintly soul floats on the Stygian river

he continues:

And, Guy de Vere, hast thou no tear?—weep now, or never more.

This, no doubt, was in tune with the fashionable romance of the day,
but Poe’s romantic conceptions at times were those of one who was
especially entranced by stage trappings. He made his heroines rich
and highborn as well as beautiful. In “Lenore” he cries:

Wretches, ye loved her for her wealth, and hated her for her pride!

In “The Sleeper” he speaks of:

The crested palls


Of her grand family funerals.
In “Annabel Lee” he made the very angels heroes of the green-
room:

Her highborn kinsmen came


And bore her away from me.

On the other hand, Poe’s theatricalism, though it explains some of


the faults of his poetry, leaves unexplained the fact that he has cast
a greater spell on succeeding poets than has even so great a
theatrical genius as Byron. Poe is one of those poets who are
sources of poetry. He discovered—though not without forerunners
such as Coleridge—a new borderland for the imagination, where
death and despair had a new strangeness. He seems to have
reached it, not through mere fancy, as his imitators do, but through
experience. When he was a youth he worshipped Mrs. Helen
Stannard, the mother of one of his friends. She went mad and died,
and for some time after her death Poe used to haunt her tomb by
night, and “when the autumnal rains fell and the winds wailed
mournfully over the graves, he lingered longest, and came away
most regretfully.” J. H. Ingram and other writers have found in these
“solitary churchyard vigils” the clue to “much that seems strange and
abnormal in the poet’s after life.” Love overshadowed by death,
beauty overshadowed by death, remained the recurrent theme of his
verse. It is the theme of his supreme poem, “Annabel Lee,” with its
haunting close:

In the sepulchre there by the sea,


In her tomb by the sounding sea.

Poe was a poet for whom life was darkened by experience and
illuminated only by visions. In the beginning, romance
loves to nod and sing
With drowsy head and painted wing,
Among the green leaves as they shake
Far down within some shadowy lake.

In time, however, this born day-dreamer can find no comfort in day-


dreaming:

Of late, eternal Condor years


So shake the very Heaven on high
With tumult as they thunder by,
I have no time for idle cares
Through gazing on the unquiet sky.
And when an hour with calmer wings
Its down upon my spirit flings—
That little time with lyre and rhyme
To while away—forbidden things!—
My heart would feel to be a crime
Unless it trembled with the strings.

There is a terrible sincerity in Poe’s sense of the presence of death.


His vision of mortal men, at least, was not theatrical in its gloom:

Mimes, in the form of God on high,


Mutter and mumble low,
And hither and thither fly—
Mere puppets they, who come and go
At bidding of vast formless things
That shift the scenery to and fro,
Flapping from out their Condor wings
Invisible Woe!
Poe and Whitman were both poets preoccupied with the thought of
death, but, whereas Whitman forced himself to praise it, Poe was in
revolt against it as the ultimate tyrant. He saw it as the one thing
that made dreadful those enchanted islands, those enchanted
valleys, those enchanted palaces in which, for him, so much of the
beauty of the world took refuge. He could not reconcile himself to a
world that was governed by mortality. There is the wistfulness of the
exile from a lost Paradise running through his verse. He is essentially
a man for whom the spiritual universe exists. His angels and demons
may not resemble the angels and demons of the churches—may,
indeed, be little more than formulæ in his dreamland. But they are
at least the formulæ of a poet into whose dreams has come the
rumour of immortality. He cannot believe that the City of Death, with
its awful stillness, can last for ever—that city where

Shrines and palaces and towers


(Time-eaten towers that tremble not!)
Resemble nothing that is ours.
Around, by lifting winds forgot,
Resignedly beneath the sky
The melancholy waters lie.

He feels that somewhere Eldorado is to be found, as it is by the


knight who sought it:

And as his strength


Failed him at length
He met a pilgrim shadow—
“Shadow,” said he,
“Where can it be—
This land of Eldorado?”
“Over the Mountains
Of the Moon,
Down the Valley of the Shadow,
Ride, boldly ride,”
The shade replied—
“If you seek for Eldorado!”

It is true that his vision, whether of life or immortality, has


something of the incoherence of the landscape of his “Dreamland”:

Mountains toppling evermore


Into seas without a shore.

If his imagination passes “out of space, out of time,” it is on the


wings of trance rather than of faith. At the same time, his dreams
would not have made so strong an appeal to generations of readers
if they had been mere sensational fancies, and had not seemed to
wander in a wider universe than we are conscious of in our everyday
life. They cannot be dismissed as the visions of a drugged man.
They are the questionings of a spirit.
It may be that, like some of the decadents of Europe, Poe was
preyed upon by a demon—that he was an outcast poet in whose sky
was

The cloud that took the form


(When the rest of Heaven was blue)
Of a demon in my view.

But in the best of the decadents the soul survived; and if they have
a place in literature it is because they have left a record of the
travels of the prodigal soul in a far country. Poe, though not sharing
their decadence, is also the poet of a far country. That loveliest of
his poems (if we except “Annabel Lee”), “To Helen”—what is it but a
triumphant cry of return? Unlike “The Raven,” it is a poem that never
loses its beauty with repetition. “Annabel Lee” may be the fullest
expression of his genius, but “To Helen” is the most exquisite. Even
to write it down, hackneyed though it is, renews one’s delight:

Helen, thy beauty is to me


Like those Nicean barks of yore,
That gently, o’er the perfumed sea,
The weary, way-born wanderer bore
To his own native shore.

On desperate seas long wont to roam,


Thy hyacinth hair, thy classic face,
Thy Naiad airs have brought me home
To the glory that was Greece
And the grandeur that was Rome.

Lo! in yon brilliant window-niche


How statue-like I see thee stand
The agate lamp within thy hand!
Ah, Psyche, from the regions which
Are Holy Land!

Here, as nowhere else, Poe achieved coherent and consummate


grace of form. Here, if almost nowhere else, his talent was equal to
his genius.
XV
HAWTHORNE

Hawthorne is the only American admitted into the English Men of


Letters Series. This may be partly accidental, and due to the fact
that it was possible to get so fine a critic as Henry James to write
about him. It also suggests, however, that in 1879 Hawthorne was
held in higher esteem than he is held to-day. There are several
American writers about whom we are nowadays more curious.
Emerson does not soar at quite such an altitude as he once did, but
he is still an indubitable figure of genius on the sunny side of the
clouds. Thoreau, with the challenge of his sardonic simplicity, will
interest us so long as there is a society to protest against. Poe, after
we have refined him in the fiercest fires of criticism, remains gold of
the most precious. Whitman holds us as the giant aborigine of
democracy as well as the rhapsodist of brotherhood and death.
Washington Irving, on the other hand, has disappeared except from
the schoolbooks, and Oliver Wendell Holmes has ceased to be read
by people under fifty. Longfellow has become an exiguous
contributor to an anthology except in so far as he is taught, like
Irving, to schoolchildren, and Lowell is oftener quoted by politicians
than by critics of letters. There is no need to discuss just now
whether this waning of reputations is likely to be permanent. It is
enough to note that Hawthorne, though he has not waned to the
extent that Longfellow has, has ceased for most readers to be a star
of the first or second magnitude. How many critics would now place
him, as he was once placed, among the great masters of English
prose? How many editors of a series of lives of great writers would
unhesitatingly include in it a life of Nathaniel Hawthorne?
Hawthorne may nevertheless justly be regarded as a classic, and
there have been few writers whose short stories would bear re-
reading so well as Hawthorne’s three-quarters of a century after
their first appearance. The prose, as anyone may see by dipping into
Mr. Carl van Doren’s admirable selection from Twice-told Tales,
Mosses from an Old Manse, and The Snow Image, is beautiful prose,
even if it falls short of supreme greatness. It flows with a rhythm at
once charming and forceful. It is transparent, and through it we can
see life as Hawthorne’s imagination played on it like sunlight
refracted through water. He is a music-maker rather than a phrase-
maker in his use of words. Movement is more to him than metaphor,
though he can combine them attractively, as in the opening sentence
of The Seven Vagabonds:

Rambling on foot in the spring of my life and the summer


of the year, I came one afternoon to a point which gave me
the choice of three directions.

You may turn Hawthorne’s pages almost at random, and you can
scarcely help noticing example after example of this characteristic
rhythm of his. It is noticeable even in such a simple narrative
sentence as that with which The Artist of the Beautiful opens:

An elderly man, with his pretty daughter on his arm, was


passing along the street, and emerged from the gloom of the
cloudy evening into the light that fell across the pavement
from the window of a small shop.

And, again, we find it in a meditative passage such as:

I saw mankind, in this weary old age of the world, either


enduring a sluggish existence amid the smoke and dust of
cities, or, if they breathed a purer air, still lying down at night
with no hope but to wear out to-morrow, and all the to-
morrows which make up life, among the same dull scenes
and in the same wretched toil that had darkened the sunshine
of to-day.

This all flows with something of the noble ease of hexameters, yet
without falling into the vices of pseudo-poetic prose. The mere
sound of his sentences gives Hawthorne’s prose a wonderful
momentum that keeps us interested even when at times we begin to
wonder if his subject-matter is quite as interesting as it ought to be.
This grave and equable momentum is one of his greatest technical
qualities. It is a quality that cannot be adequately illustrated in single
sentences or detached passages, because its success is not the
success of occasional felicities but of something sustained and
pervasive. It may even be imputed as a fault to Hawthorne that he
can never, or almost never, escape from the equable rhythm of his
prose. He seldom ends a story with the slightly different momentum
due to an ending. It is not merely, however, that his stories end
quietly: he is like a rider who rides beautifully but does not know
how to dismount. He maintains his graceful ease of motion until the
last moment, and then he slides off as best he can.
But it would be folly to regard Hawthorne’s rhythm as wholly—or
even mainly—a technical quality. The rhythm of prose is never that,
and it is in vain to play the sedulous ape to the great masters if
nothing but their style is imitated. It is not an accident that the
greatest English prose is to be found in the Bible. The rhythm of the
greatest prose seems at times the rhythm of the spirit of man as it
contemplates the life of men in the light of eternity. The rhythm to a
Plato, a Milton, a Sir Thomas Browne, is inevitably of a kind that a
Jane Austen or a Thackeray, with all their genius, could never
achieve. It is the echo of the emotion felt by men to whom time and
place are fables with another meaning besides that which appears
on the surface. The realists can never write the greatest prose,
because to them the world they see is not fabulous but a hard fact.
The greatest writers all see the world as fabulous. Their men and
women are inhabited by angels or devils, or, on a lower plane, have
something of the nature of ghosts or fairies or goblins. If Othello
were not a fable as well as a man, he would be no better than a
criminal lunatic. If King Lear were not a fable as well as man, he
would be a subject for the psychoanalyst. Imagine either of them as
a modern Englishman, putting his case before a judge and jury, and
you will see how the artist, even though his characters as a rule are
characters such as may be found in reality, must remove them out of
and above reality into the region of fables in order to make them
permanently real to the imagination. Dickens turned Victorian
England into a myth peopled by goblins. Dostoievsky turned Russia
into a myth peopled by goblins and demons. It is not that they
denied the reality of the world before their eyes, but that they saw
within it and about it another world apart from which it had very
little meaning.
Hawthorne was a writer extremely conscious of this second
world within and about the world. He had abandoned the Puritanical
orthodoxy of his people, but none the less he was haunted like them
by a sense of a second meaning in life beyond the surface meaning
of the day’s work and the day’s play. Many of his stories are stories
in which, as in Young Goodman Brown, everyday reality passes into
fable and back again as swiftly as though the two worlds were but
different stages in a transformation scene. His genius turned more
naturally to allegory than any other writer’s since Bunyan. This is
generally counted a defect, and, indeed, if, instead of alternating the
everyday world with the fabulous world, he had interwoven them in
such a way that the world never became less real on account of the
fable it bore within it like an inner light, Hawthorne would have been
a greater writer. At the same time, it is better that he should have
sacrificed observation than that he should have sacrificed
imagination. He lived in an atmosphere in which it must have been
extraordinarily difficult to stand sufficiently remote from everyday life
to see it not merely with the eye but with the imagination. To the
eye, there must have been little enough of fantasy in the narrow
lives of the men and women about him. “Never comes any bird of
Paradise into that dismal region,” he wrote of the Custom-house in
which he passed so many years and that made “such havoc of his
wits.” He had to transform his surroundings into a strange land into
which a bird of Paradise might enter. He did this by the invention of
a sort of moral fairyland, into which he could project his vision of the
mystery of human life. He often offends our sense of reality, but he
never leaves us in doubt of the reality of this moral fairyland as the
image of all he knew and felt about human life. It is a Puritanical
fairyland into which sin has come. But, strong though his sense of
sin is, Hawthorne does not always in his view of sin agree with the
Puritans. He is more Christian, and he condemns the sin of self-
righteousness more than the sins of the flesh. Even so, his
imagination is very close to that of the Puritans, who believed in
witches and in men possessed by the Devil. The difference is that
Hawthorne was inclined to believe that the good church-going
people were also witches and men possessed by the Devil. Unless I
misunderstand Young Goodman Brown, Hawthorne is here telling us
how he was tempted to believe this, and reproaching himself for
having given way to temptation. In The Scarlet Letter, the egoism of
the vengeful husband, not the adultery of the wife or the cowardice
of the minister who sins with her, is the unpardonable sin of the
story. That Hawthorne’s imaginative morality had the vehemence of
genius is shown by the fact that The Scarlet Letter still holds us
under its spell in days in which moral values have subtly and swiftly
changed. People are no longer thrilled at the thought of a scarlet A
on a woman’s breast; they would scarcely be thrilled by the
spectacle of a whole scarlet alphabet hung round a woman’s neck
like a collar. Yet Hawthorne’s novel survives—a fable of the
permanent and dubious warfare between good and evil, in which
good changes its shape into that of evil, and evil is transmuted into
good through suffering. His genius survives, like that of Hans
Andersen, because, not only does it carry the burden of morality, but
it is led on its travels by a fancy wayward and caressing as the
summer wind. He is the first prose myth-maker of America, and he
has left no successors in his kind.
XVI
JONAH IN LANCASHIRE

The author of Patience—the other Patience, I mean, not the Gilbert


opera—is beginning to be discovered even by the average reader. It
is not long since we had modernised versions of his two most
remarkable poems, Pearl and Sir Gawayne and the Green Knight.
Gaston Paris describes the latter as “the jewel of English mediæval
literature,” and even among those who read idly for amusement it
should become a favourite book in Mr. Ernest Kirtlan’s easy
rendering. Who the maker of these poems was we know not. Editors
have invented a personal history for him, but other editors have
ruthlessly pulled it to pieces. It was suggested that he wrote the
romance Sir Gawayne in his gaudy youth. Then, having lost a child,
he composed in Pearl a passionate lament for her. Afterwards, in the
evening of his life, he wrote Patience as an expression of his
submission to the will of God. Mr. Bateson will have nothing to do
either with this pathetic life-history or with the chronology. He
regards Patience as the earliest of the poems, and is of the opinion
that Pearl, far from being a lament for a lost child, is “largely a
theological discussion in elegiac form.” One would think there must
be something seriously wrong in a poem about which a dispute of
the kind could rage among the interpreters. But this is not
necessarily so. No one denies that the Song of Solomon is a great
poem, and yet men have quarrelled as to whether it should be read
as the holiest of symbolic poems or as an early masterpiece of the
fleshly school of literature. Coming to Patience itself, I fancy that the
man who could discover personal confessions in it could discover
personal confessions in Euclid. I find it difficult to believe in the
bereaved father who turned for a lesson in resignation to the story
of Jonah. It is the homilist, not the tortured human being, who
fishes in the Book of Jonah for comfortable morals. Patience is a
sermon addressed to other people, not to the poet’s own soul.
Feeling this, one may allow oneself to be amused by its quaintness
as well as to admire the hue and vigour of its narrative.
Patience is the story of Jonah told by an original artist. Jonah is
here painted in English colours. He is the Jonah not of a tragic-
hearted Hebrew but of a familiar Lancashire man who wrote in a
Lancashire dialect at the time of Chaucer. Tertullian had written a
Latin poem on the same theme, and Mr. Bateson gives us the text of
this in an appendix, suggesting, as other scholars have done, that it
is one of the sources of Patience. The Jonah of Tertullian, however,
is a formal figure compared to the Jonah of the Englishman. Jonah
in the old Lancashire poem is a lithe and live fellow from the
moment at which he steps aboard the ship to make his escape from
the perilous will of God.

Was neuer so joyful a Jue as Jonas was thenne,

we are told in a lively line at this point of the narrative. The storm
that follows is described with such a sense of reality that it has been
suggested that the poet himself must have experienced some such
tempest when making a pilgrimage to Compostella, “the favourite
journey of Englishmen at the time,” and a journey of the ancient
popularity of which we are still reminded in the streets of London
once a year when children set up their grottoes on the footpaths as
an excuse for begging pennies. Mr. Bateson attempts to bring home
to us the desperate circumstances of seafaring in the Middle Ages by
quoting the statement that “John of Gaunt, on one occasion, was
tossing about in the Channel for nine months, unable to land at
Calais.” I confess I cannot believe the story in this form, and we
need no such incredible example to enable us to realise the terrors
of the storm that swept down on Jonah, when the frightened sailors
attempted to lighten the ship by throwing overboard
Her bagges, and her feather-beddes, and her bryght
wedes.

The introduction of the feather-beds into the narrative would alone


be a sufficient reason for welcoming the Lancashire version of the
Jonah story. The description of the panic-stricken sailors “glewing,”
or calling, on their very various gods (who included Fernagu, a
French giant) is another addition that pleases by its strangeness:

Bot vchon glewed on his god thet gayned hym beste;


Summe to Vernagu ther vouched avowes solemne,
Summe to Diana deuout, and derf Nepturne,
To Mahoun and to Mergot, the Mone and the Sunne.

Both in Tertullian and in Patience Jonah is made not only to sleep


but to snore while the others pray during the storm. Tertullian puts
it:

Sternentem inflata resonabat nare soporem.

The English poet writes still more vividly that Jonah lay in the
bottom of the boat,

Slypped vpon a sloumbe-slepe, and sloberande he routes.

A “freke,” or man, was sent to rouse him and to prepare him for the
casting of lots:

The freke hym frunt with his fot, and bede hym ferk up.
Then came the casting of the lots:

And ay the lote, vpon laste, lymped on Jonas.

The sailors immediately began to upbraid Jonah in masculine


English:

What the deuel hest thou don, dotede wrech?


What seches thou on see, synful schrewe,
With thy lastes [crimes] so luther [evil] to lose vus vchone?

Soon after follows the decision to throw him overboard:

Now is Jonas the Jwe jugged to drowne.

“A wylde walteande whale” comes up opportunely to the side of the


boat:

And swyftely swenged hym to swepe, and his swallow opened...,


With-outen towche of any tothe he tult in his throte.

In spite of his safe passage beyond the whale’s teeth, however,


Jonah’s plight was not an enviable one:

Lorde! colde was his cumfort, and his care huge.

The poet describes him as passing down the throat like a “mote in at
a minster door”:
He glydes in by the gills ...;
Ay, hele ouer hed, hourlande aboute,
Til he blunt [staggered] in a blok as brod as a halle;
And ther he festnes the fete, and fathmes about,
And stod up in his stomak, that stank as the deuel.

So realistic is the description of the whale’s inside that Mr. Bateson


thinks it likely that the poet had been listening to the stories of
whalers. He also endorses the poet’s view of the horrors of the
situation by quoting one writer who states that “the breath of the
whale is frequently attended by such an insupportable smell as to
bring on disorder of the brain.” If the whale made Jonah feel sick,
however, Jonah, according to the poet, had much the same effect on
the whale. In a moving two lines on the whale’s discomforts we are
told:

For thet mote in his mawe made hym, I trowe,


Though hit lyttel were hym wyth, to wamel at his hert.

These two lines Mr. Bateson translates into colourless modern


English: “For the mote made him—though it were little as compared
with him—to feel sick,” and adds for our information that “the reader
of whaling stories will recall how frequently the whale suffers from
dyspepsia!”
We need not follow the poet in detail through the rest of the
narrative, which is full of life-giving detail till the end. After God had
commanded the whale——

That he hym sput spakly vpon spare drye,


we see Jonah washing his muddy mantle on the beach and
proceeding with his message of doom to the “burgesses and
bachelors” of Nineveh. The gourd under which he sleeps becomes a
“wodbynde” (some kind of convolvulus): it is “hedera,” or ivy, in the
Vulgate. Jonah’s delight, as he lay under it—“so glad of his gay
lodge”—is amusingly described. He——

Lys loltrande ther-inne lokande to toune.

So contentedly did he “loll” there, indeed—“so blithe of his wood-


bine”—that he cared not a penny for any “diet” that day; and when
it “nighed to night” and “nappe hym bihoued,” he slept the sleep of
the just “vnder leues.” In his account of Jonah’s anger against God,
and God’s argument in favour of sparing Nineveh, the poet
elaborates as ever the Bible narrative, and the appeal for the right of
the inhabitants to live is tenderer than in the more concise original.
God pleads, for instance, for the “lyttel bairnes on barme (breast)
that neuer bale wrought,” and the reference to “much cattle”
becomes:

And als ther ben doumbe bestes in the burgh many.

I do not suggest that Patience is better than the Book of Jonah,


or as good, but that it has the vitality of an original work. The poet
has a personal knowledge of character—a sense of drama, and a
sense of life. Mr. Bateson’s edition of the poem was first published
seven years ago. He has now largely recast and rewritten it. I have
taken some liberties with his text in quoting it, slightly modernising it
in places. It is an edition for students of mediæval literature rather
than for the general reader. But with the help of its excellent
glossary others than scholars should be able to enjoy it if they are
prepared to take a little pains. And it is worth taking pains to

You might also like