Python for Mathematics. The Python Series

Python for Mathematics. The Python Series

Python for Mathematics. The Python Series
Автор: Knight Vincent
Дата выхода: 2025
Издательство: CRC Press is an imprint of Taylor & Francis Group, LLC
Количество страниц: 268
Размер файла: 4,0 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

Preface xv
Section I Overview
Chapter 1 ■ Introduction 3
1.1 WHO IS THIS BOOK FOR? 3
1.1.1 How is this book different from similar books? 4
1.2 WHAT IS IN THIS BOOK? 6
1.2.1 How is this book organised? 6
1.2.2 How to use this book? 7
1.2.3 How is coded is played in this book? 7
1.3 WHAT IS NOT THIS BOOK? 9
Section II Tools for Mathematics
Chapter 2 ■ Using Notebooks 13
2.1 INTRODUCTION 13
2.2 TUTORIAL 13
2.2.1 Installation 13
2.2.2 Starting a Jupyter notebook server 14
2.2.3 Creating a new notebook 14
2.2.4 Organising your files 14
2.2.5 Writing some basic Python code 17
2.2.6 Writing markdown 20
2.2.7 Saving your notebook to a different format 20
2.3 HOWTO 21
2.3.1 Install Anaconda 21
2.3.2 Start a Jupyter notebook server 21
2.3.3 Create a new notebook 21
2.3.4 Find/open a notebook 21
2.3.5 Run Python code 21
2.3.6 Carry out basic arithmetic operations 22
2.3.7 Write markdown 22
2.3.8 Write basic LaTeX 22
2.3.9 Save the output in a different format 23
2.4 EXERCISES 24
2.5 FURTHER INFORMATION 24
2.5.1 Why use anaconda? 24
2.5.2 Why use Jupyter? 24
2.5.3 Why can I not double click on a Jupyter notebook file? 25
2.5.4 Where can I find keyboard shortcuts for using Jupyter 25
2.5.5 What is markdown? 25
2.5.6 What is LaTeX? 25
2.5.7 Can I use \( and \) instead of $ for LATEX? 26
2.5.8 What is a mark up language? 26
Chapter 3 ■ Algebra 27
3.1 INTRODUCTION 27
3.2 TUTORIAL 27
3.3 HOWTO 33
3.3.1 Createasymbolicnumericvalue 33
3.3.2 Getthenumericalvalueofasymbolicexpression 34
3.3.3 Factoranexpression 35
3.3.4 Expandanexpression 35
3.3.5 Simplifyanexpression 36
3.3.6 Solveanequation 36
3.3.7 Substituteavalueintoanexpression 38
3.4 EXERCISES 38
3.5 FURTHERINFORMATION 39
3.5.1 Whyissomecodeinseparatelibraries? 39
3.5.2 WhydoIneedtousesympy? 39
3.5.3 WhydoIsometimessee from sympyimport*? 40
3.5.4 Howtoextractasolutionfromtheoutputof sympy.solveset? 42
3.5.5 WhydoIsometimessee import sympyassym? 44
Chapter 4 ■ Calculus 45
4.1 TUTORIAL 45
4.2 HOWTO 50
4.2.1 Calculatethederivativeofanexpression. 50
4.2.2 Calculatetheindefiniteintegralofanexpression. 51
4.2.3 Calculatethedefiniteintegralofanexpression. 51
4.2.4 Use ∞ 52
4.2.5 Calculatelimitsofanexpression 52
4.3 EXERCISES 53
4.4 FURTHERINFORMATION 53
4.4.1 Howcanyouplotafunction 53
Chapter 5 ■ Matrices 56
5.1 TUTORIAL 56
5.2 HOWTO 59
5.2.1 Createamatrix 59
5.2.2 Calculatethedeterminantofamatrix 60
5.2.3 Calculatetheinverseofamatrix 60
5.2.4 Multiplymatricesbyascalar 61
5.2.5 Addmatricestogether 61
5.2.6 Multiplymatricestogether 62
5.2.7 Createavector 62
5.2.8 Solvealinearsystem 62
5.3 EXERCISES 63
5.4 FURTHERINFORMATION 64
5.4.1 Whydoesthisbooknotdiscusscommentingofcode? 64
5.4.2 Whyuse @ for matrixmultiplicationandnot *? 64
5.4.3 IsNumpyalibrarythatcanbeusedforlinearalgebra? 65
Chapter 6 ■ Combinatorics 66
6.1 TUTORIAL 66
6.2 HOWTO 70
6.2.1 Createatuple 70
6.2.2 Howtoaccessparticularelementsinatuple 70
6.2.3 Createbooleanvariables 70
6.2.4 Createaniterablewithagivennumberofitems 73
6.2.5 Createpermutationsofagivensetofelements 73
6.2.6 Createcombinationsofagivensetofelements 74
6.2.7 Summingitemsinaniterable 75
6.2.8 Directlycompute n! 76
6.2.9 Directlycompute
n
i
76
6.2.10 Directlycompute nPi 77
6.3 EXERCISES 77
6.4 FURTHERINFORMATION 78
6.4.1 Arethereotherwaystoaccesselementsintuples? 78
6.4.2 Whydo range, itertools.permutations, and
itertools.combinations not directlygivetheelements? 78
6.4.3 Howdoesthesummationnotation correspondtothecode?78
x ■ Contents
Chapter 7 ■ Probability 80
7.1 TUTORIAL 80
7.2 HOWTO 87
7.2.1 Createalist 87
7.2.2 Defineafunction 89
7.2.3 Callafunction 89
7.2.4 Runcodebasedonacondition 90
7.2.5 Createalistusingalistcomprehension 92
7.2.6 Summingitemsinalist 93
7.2.7 Samplefromaniterable 94
7.2.8 Samplearandomnumber 95
7.2.9 Reproducerandomevents 95
7.3 EXERCISES 96
7.4 FURTHERINFORMATION 97
7.4.1 WhatisthedifferencebetweenaPythonlistandaPythontuple?97
7.4.2 Whydoesthesumofbooleanscountthe Trues? 97
7.4.3 Whatisthedifferencebetween print and return? 98
7.4.4 HowdoesPythonsamplerandomness? 99
7.4.5 Whatisthedifferencebetweenadocstringandacomment 99
Chapter 8 ■ Sequences 102
8.1 TUTORIAL 102
8.2 HOWTO 104
8.2.1 Defineafunctionusingrecursion 104
8.3 EXERCISES 105
8.4 FURTHERINFORMATION 106
8.4.1 Whatarethedifferencesbetweenrecursionanditeration? 106
8.4.2 Whatiscaching? 108
Chapter 9 ■ Statistics 110
9.1 TUTORIAL 110
9.2 HOWTO 118
9.2.1 Calculatemeasuresofspreadandtendency 118
9.2.2 Calculatethesamplecovariance 122
9.2.3 CalculatethePearsoncorrelationcoefficient 123
9.2.4 Fitalineofbestfit 123
9.2.5 Createaninstanceofthenormaldistribution 124
9.2.6 Usethecumulativedistributionfunctionofanormaldistribution125
9.2.7 Usetheinversecumulativedistributionfunctionofanormal
distribution 125
9.3 EXERCISES 126
Contents ■ xi
9.4 FURTHERINFORMATION 129
9.4.1 Whatisthedifferencebetweenthesampleandthepopulation
varianceandstandarddeviation? 129
9.4.2 Howtoplotalineofbestfit? 130
9.4.3 WhatotherstatisticstoolsexistinPython? 131
9.4.4 Whatisthedifferencebetweenmachinelearningandstatistics?131
Chapter 10 ■ DifferentialEquations 132
10.1 TUTORIAL 132
10.2 HOWTO 134
10.2.1 Createasymbolicfunction 134
10.2.2 Createadifferentialequation 135
10.2.3 Obtainthegeneralsolutionofadifferentialequation 136
10.2.4 Obtaintheparticularsolutionofadifferentialequation 137
10.3 EXERCISES 137
10.4 FURTHERINFORMATION 138
10.4.1 Howtosolveasystemofdifferentialequations? 138
10.4.2 Howtosolvedifferentialequationsnumerically? 139
Section III Building Tools
Chapter 11 ■ Variables,ConditionalsandLoops 145
11.1 TUTORIAL 145
11.2 HOWTO 148
11.2.1 Defineanintegervariable 148
11.2.2 Defineafloatvariable 149
11.2.3 Defineastringvariable 149
11.2.4 Defineabooleanvariable 150
11.2.5 Checkthetypeofavariable 150
11.2.6 Manipulatenumericvariables 151
11.2.7 Includevariablesinstrings 152
11.2.8 Combinecollectionsofbooleanvariables 153
11.2.9 Runcode if a conditionholds 153
11.2.10 Repeatcode for a givensetofvariables 154
11.2.11 Repeatcode while a givenconditionholds 155
11.2.12 Iterateoverpairsofitemsfromtwoiterables 156
11.2.13 Iterateoverandindexitemsfromaniterable 156
11.3 EXERCISES 157
11.4 FURTHERINFORMATION 158
11.4.1 WhycanInotonlyusea while loop? 158
11.4.2 WhyshouldInotcheckifabooleanisequalto True or False 159
xii ■ Contents
Chapter 12 ■ Functions andDataStructures 161
12.1 TUTORIAL 161
12.2 HOWTO 164
12.2.1 Defineafunction 164
12.2.2 Writeadocstring 164
12.2.3 Createatuple 165
12.2.4 Createalist 165
12.2.5 Createalistusingalistcomprehension 165
12.2.6 Combinelists 165
12.2.7 Appendanelementtoalist 166
12.2.8 Removeanelementfromalist 166
12.2.9 Sortalist 167
12.2.10 Createasortedlistfromaniterable 167
12.2.11 Accessanelementofaniterable 168
12.2.12 Findtheindexofanelementinaniterable 168
12.2.13 Accessanelementofaniterableusingnegativeindexing 168
12.2.14 Sliceaniterable 169
12.2.15 Findthenumberofelementsinaniterable 169
12.2.16 Createaset 170
12.2.17 Dosetoperations 170
12.2.18 Createhashtables 171
12.2.19 Accesselementinahashtable 172
12.2.20 Iterateoverkeysinahashtable 173
12.2.21 Iterateovervaluesinahashtable 173
12.2.22 Iterateoverpairsofkeysandvalueinahashtable 173
12.3 EXERCISES 174
12.4 FURTHERINFORMATION 175
12.4.1 Whatformatscanbeusedtowriteadocstring? 175
12.4.2 Aretheretoolsavailabletoassistwithwritingdocstrings?175
12.4.3 Apartfromremovingduplicatesandsetoperationswhatarethe
advantagesofusing set? 175
Chapter 13 ■ Object-OrientedProgramming 176
13.1 TUTORIAL 176
13.2 HOWTO 181
13.2.1 Defineaclass 181
13.2.2 Createaninstanceoftheclass 181
13.2.3 Createanattribute 182
13.2.4 Createandcallamethod 183
13.2.5 Howtocreateandcallmagicmethods 184
13.2.6 Useinheritance 186
Contents ■ xiii
13.3 EXERCISES 187
13.4 FURTHERINFORMATION 188
13.4.1 Howtopronouncethedoubleunderscore? 188
13.4.2 Whatisthe self variablefor? 188
13.4.3 Whyuse CamelCase for classesbut snake case for functions?190
13.4.4 Whatisthedifferencebetweenamethodandafunction? 190
Chapter 14 ■ Using aCommandLineandanEditor 191
14.1 TUTORIAL 191
14.2 HOWTO 196
14.2.1 Navigatedirectoriesusingthecommandline 196
14.2.2 Createanewdirectoryusingthecommandline 197
14.2.3 Seethecontentsofadirectoryinthecommandline 197
14.2.4 RunPythoncodeinafile 198
14.2.5 RunPythoncodewithoutusingafileorJupyter 198
14.2.6 InstallVScodeplugins 198
14.3 EXERCISES 199
14.4 FURTHERINFORMATION 199
14.4.1 Whydoyouneedtousethe print function withaneditor?199
14.4.2 CanyouuseaPythonplugintorunmycodefrominsidemyeditor?199
14.4.3 CanIopenaJupyternotebookinsideVScode? 200
14.4.4 WhatisthedifferencebetweenanIntegratedDevelopment
Environmentandaneditor? 200
14.4.5 CanIuse \( and \) instead of $ for LATEX? 200
Chapter 15 ■ Modularisation 201
15.1 TUTORIAL 201
15.2 HOWTO 207
15.2.1 ImportcodefromPythonfiles 207
15.2.2 Breakupcodeintomodularcomponents 208
15.3 EXERCISES 210
15.4 FURTHERINFORMATION 212
15.4.1 Whymodularise? 212
15.4.2 WhydoIgetanimporterror? 213
15.4.3 HowdoImakemyfileimportablefromotherdirectories? 213
Chapter 16 ■ Documentation 214
16.1 TUTORIAL 214
16.1.1 Writingatutorial 214
16.1.2 Writingthehow-toguides 216
16.1.3 Writingtheexplanationssection 218
xiv ■ Contents
16.1.4 Writingthereferencesection 221
16.2 HOWTO 222
16.2.1 Writedocumentation 222
16.2.2 Writemarkdown 223
16.3 EXERCISES 225
16.4 FURTHERINFORMATION 225
16.4.1 Whatisdocumentation? 225
16.4.2 Whatisthepurposeofthefourseparatesectionsindocumentation?226
16.4.3 Whatalternativesaretheretowritingdocumentationinthe
README.md file? 226
Chapter 17 ■ Testing 227
17.1 TUTORIAL 227
17.1.1 Writingtestsforcode 227
17.1.2 Testingdocumentation 231
17.1.3 Documentingthetests 236
17.2 HOWTO 237
17.2.1 Writean assert statement 237
17.2.2 Write assert statementsforcodethatactsrandomly 238
17.2.3 Writeatestfile 239
17.2.4 Formatdoctests 241
17.2.5 Rundoctests 242
17.3 EXERCISES 242
17.4 FURTHERINFORMATION 242
17.4.1 Whyaretestswrittenasfunctions? 242
17.4.2 Isthereamoreefficientwaytoruntests? 242
17.4.3 Whatshouldbetested? 243
17.4.4 Whydoyouneeddoctests? 243
17.4.5 Whatistestdrivendevelopment? 243
17.4.6 Howaremodularisation,documentationandtestingrelated?244
Section IV AboutThisBook
Chapter 18 ■ HowThisBookIsWritten 247
Bibliography 249
Index 251

 Python for Mathematics introduces readers to effective methods for doing mathematics using the Python programming language. Most programming texts introduce readers to the building blocks of programming and build up to using more sophisticated tools for a specific purpose, like doing particular mathematical tasks. This is akin to teaching someone how to forge metal so as to make a nail, and then slowly working up to using sophisticated power tools so as to actually build something. This book does things in a different way, by first getting readers to begin using and understanding the tools that are going to be helpful to them as mathematicians, and only then moving onto the granular details. In this way, the practical application of the tools can aid in the understanding of the theory.

Features

  • Complete with engaging, practical exercises
  • Many useful and detailed coding examples
  • Suitable for undergraduates in mathematics, and other quantitative sciences
  • Empowers readers to design and create their own Python tools.

Похожее:

Список отзывов:

Нет отзывов к книге.