

Computing with Formulas...................................1The First Programming Encounter: a Formula....................1Using a Program as a Calculator.........................2About Programs and Programming.......................2Tools for Writing Programs............................3Writing and Running Your First Python Program..............4Warning About Typing Program Text.....................5Verifying the Result.................................6Using Variables....................................6Names of Variables.................................6Reserved Words in Python............................7Comments.......................................8Formatting Text and Numbers..........................9Computer Science Glossary...............................12Another Formula: Celsius-Fahrenheit Conversion................16Potential Error: Integer Division........................16Objects in Python..................................17Avoiding Integer Division............................18Arithmetic Operators and Precedence.....................20Evaluating Standard Mathematical Functions...................20Example: Using the Square Root Function.................20Example: Computing with sinh x........................23A First Glimpse of Rounding Errors.....................23Interactive Computing..................................24Using the Python Shell..............................25Type Conversion..................................26IPython........................................27Complex Numbers.....................................29Complex Arithmetics in Python........................30Complex Functions in Python..........................31Unified Treatment of Complex and Real Functions............31Symbolic Computing...................................33Basic Differentiation and Integration.....................33Equation Solving..................................34xiTaylor Series and More..............................35Summary...........................................35Chapter Topics...................................35Example: Trajectory of a Ball..........................39About Typesetting Conventions in This Book...............40Exercises...........................................41Loops and Lists..........................................51While Loops.........................................51A Naive Solution..................................51While Loops.....................................52Boolean Expressions................................54Loop Implementation of a Sum.........................56Lists 57Basic List Operations...............................57For Loops.......................................60Alternative Implementations with Lists and Loops................62While Loop Implementation of a for Loop.................62The Range Construction.............................63For Loops with List Indices...........................64Changing List Elements.............................65List Comprehension................................66Traversing Multiple Lists Simultaneously..................66Nested Lists.........................................67A table as a List of Rows or Columns....................67Printing Objects...................................68Extracting Sublists.................................70Traversing Nested Lists..............................72Tuples.............................................74Summary...........................................75Chapter Topics...................................75Example: Analyzing List Data.........................78How to Find More Python Information....................80Exercises...........................................82Functions and Branching...................................91Functions...........................................91Mathematical Functions as Python Functions...............91Understanding the Program Flow.......................93Local and Global Variables...........................94Multiple Arguments................................96Function Argument or Global Variable?...................97Beyond Mathematical Functions........................98Multiple Return Values..............................99Computing Sums.................................100Functions with No Return Values......................101Keyword Arguments...............................103Doc Strings.....................................105Functions as Arguments to Functions....................107The Main Program................................109Lambda Functions................................110Branching..........................................110If-else Blocks...................................111Inline if Tests...................................113Mixing Loops, Branching, and Functions in BioinformaticsExamples......................................113Counting Letters in DNA Strings.......................114Efficiency Assessment..............................118Verifying the Implementations........................120Summary..........................................121Chapter Topics...................................121Example: Numerical Integration.......................123Exercises..........................................127User Input and Error Handling..............................149Asking Questions and Reading Answers......................150Reading Keyboard Input............................150Reading from the Command Line..........................151Providing Input on the Command Line...................151A Variable Number of Command-Line Arguments...........152More on Command-Line Arguments....................153Turning User Text into Live Objects........................154The Magic Eval Function............................154The Magic Exec Function...........................158Turning String Expressions into Functions................160Option-Value Pairs on the Command Line....................161Basic Usage of the Argparse Module....................162Mathematical Expressions as Values....................163Reading Data from File.................................165Reading a File Line by Line..........................166Alternative Ways of Reading a File.....................167Reading a Mixture of Text and Numbers..................169Writing Data to File...................................171Example: Writing a Table to File.......................171Standard Input and Output as File Objects.................173What is a File, Really?..............................176Handling Errors......................................179Exception Handling...............................180Raising Exceptions................................183A Glimpse of Graphical User Interfaces......................185Making Modules.....................................188Example: Interest on Bank Deposits.....................188Collecting Functions in a Module File...................189Test Block.....................................190Verification of the Module Code.......................192Getting Input Data................................193Doc Strings in Modules.............................195Using Modules..................................196Distributing Modules..............................199Making Software Available on the Internet................200Making Code for Python 2 and 3...........................201Basic Differences Between Python 2 and 3................201Turning Python 2 Code into Python 3 Code................202Summary..........................................204Chapter Topics..................................204Example: Bisection Root Finding......................208Exercises..........................................216Array Computing and Curve Plotting.........................227Vectors...........................................228The Vector Concept...............................228Mathematical Operations on Vectors....................229Vector Arithmetics and Vector Functions.................231Arrays in Python Programs..............................232Using Lists for Collecting Function Data.................232Basics of Numerical Python Arrays.....................233Computing Coordinates and Function Values...............235Vectorization....................................236Curve Plotting.......................................238MATLAB-Style Plotting with Matplotlib.................238Matplotlib; Pyplot Prefix............................243SciTools and Easyviz..............................244Making Animations...............................249Making Videos..................................254Curve Plots in Pure Text............................255Plotting Difficulties...................................256Piecewisely Defined Functions........................256Rapidly Varying Functions...........................259More Advanced Vectorization of Functions...................260Vectorization of StringFunction Objects..................260Vectorization of the Heaviside Function..................261Vectorization of a Hat Function........................265More on Numerical Python Arrays.........................267Copying Arrays..................................267In-Place Arithmetics...............................268Allocating Arrays.................................269Generalized Indexing..............................269Testing for the Array Type...........................270Compact Syntax for Array Generation...................271Shape Manipulation...............................271High-Performance Computing with Arrays....................272Scalar Implementation..............................272Vectorized Implementation..........................273Memory-Saving Implementation.......................273Analysis of Memory Usage..........................275Analysis of the CPU Time...........................276Higher-Dimensional Arrays..............................277Matrices and Arrays...............................277Two-Dimensional Numerical Python Arrays...............278Array Computing.................................281Matrix Objects...................................282Some Common Linear Algebra Operations....................283Inverse, Determinant, and Eigenvalues...................283Products.......................................283Norms 284Sum and Extreme Values............................284Indexing.......................................286Transpose and Upper/Lower Triangular Parts..............286Solving Linear Systems.............................287Matrix Row and Column Operations....................287Computing the Rank of a Matrix.......................288Symbolic Linear Algebra............................289Plotting of Scalar and Vector Fields.........................292Installation.....................................292Surface Plots....................................293Parameterized Curve...............................293Contour Lines...................................294The Gradient Vector Field...........................294Matplotlib.........................................296Surface Plots....................................296Contour Plots....................................297Vector Field Plots.................................299Mayavi...........................................299Surface Plots....................................300Contour Plots....................................303Vector Field Plots.................................303A 3D Scalar Field and Its Gradient Field..................304Animations.....................................306Summary..........................................307Chapter Topics...................................307Example: Animating a Function.......................308Exercises..........................................313Dictionaries and Strings...................................333Dictionaries........................................333Making Dictionaries...............................334Dictionary Operations..............................334Example: Polynomials as Dictionaries...................336Dictionaries with Default Values and Ordering.............338Example: Storing File Data in Dictionaries................341Example: Storing File Data in Nested Dictionaries...........342Example: Reading and Plotting Data Recorded at Specific Dates..347Strings............................................351Common Operations on Strings........................351Example: Reading Pairs of Numbers....................355Example: Reading Coordinates........................358Reading Data from Web Pages............................360About Web Pages.................................361How to Access Web Pages in Programs..................362Example: Reading Pure Text Files......................363Example: Extracting Data from HTML...................365Handling Non-English Text..........................366Reading and Writing Spreadsheet Files......................369CSV Files......................................369Reading CSV Files................................370Processing Spreadsheet Data.........................371Writing CSV Files................................372Representing Number Cells with Numerical Python Arrays 373Using More High-Level Numerical Python Functionality . 374Examples from Analyzing DNA...........................375Computing Frequencies.............................375Analyzing the Frequency Matrix.......................382Finding Base Frequencies...........................385Translating Genes into Proteins........................388Some Humans Can Drink Milk, While Others Cannot.........393Making Code that is Compatible with Python 2 and 3.............394More Basic Differences Between Python 2 and 3............394Turning Python 2 Code into Python 3 Code................396Summary..........................................396Chapter Topics..................................396Example: A File Database...........................398Exercises..........................................402Introduction to Classes....................................409Simple Function Classes................................409Challenge: Functions with Parameters...................410Representing a Function as a Class.....................412The Self Variable.................................417Another Function Class Example.......................419Alternative Function Class Implementations...............420Making Classes Without the Class Construct...............422Closures.......................................424More Examples on Classes..............................426Bank Accounts..................................426Phone Book....................................428A Circle.......................................430Special Methods.....................................432The Call Special Method............................432Example: Automagic Differentiation....................433Example: Automagic Integration.......................438Turning an Instance into a String.......................440Example: Phone Book with Special Methods...............441Adding Objects..................................443Example: Class for Polynomials.......................443Arithmetic Operations and Other Special Methods...........449Special Methods for String Conversion...................449Example: Class for Vectors in the Plane......................451Some Mathematical Operations on Vectors................451Implementation..................................452Usage 454Example: Class for Complex Numbers.......................455Implementation..................................455Illegal Operations.................................457Mixing Complex and Real Numbers....................457Dynamic, Static, Strong, Weak, and Duck Typing............459Special Methods for “Right” Operands...................460Inspecting Instances...............................461Static Methods and Attributes.............................463Summary..........................................464Chapter Topics...................................464Example: Interval Arithmetic.........................466Exercises..........................................470Random Numbers and Simple Games.........................489Drawing Random Numbers..............................489The Seed......................................490Uniformly Distributed Random Numbers.................491Visualizing the Distribution..........................492Vectorized Drawing of Random Numbers.................493Computing the Mean and Standard Deviation..............494The Gaussian or Normal Distribution....................496Drawing Integers.....................................497Random Integer Functions...........................498Example: Throwing a Die...........................498Drawing a Random Element from a List..................501Example: Drawing Cards from a Deck...................502Example: Class Implementation of a Deck................504Computing Probabilities................................507Principles of Monte Carlo Simulation....................507Example: Throwing Dice............................508Example: Drawing Balls from a Hat.....................511Random Mutations of Genes..........................513Example: Policies for Limiting Population Growth...........519Simple Games.......................................522Guessing a Number...............................522Rolling Two Dice.................................523Monte Carlo Integration................................526Derivation of Monte Carlo Integration...................526Implementation of Standard Monte Carlo Integration.........528Area Computing by Throwing Random Points..............531Random Walk in One Space Dimension......................534Basic Implementation..............................534Visualization....................................535Random Walk as a Difference Equation..................536Computing Statistics of the Particle Positions..............536Vectorized Implementation..........................537Random Walk in Two Space Dimensions.....................539Basic Implementation..............................539Vectorized Implementation..........................541Summary..........................................542Chapter Topics..................................542Example: Random Growth...........................544Exercises..........................................549Object-Oriented Programming..............................567Inheritance and Class Hierarchies..........................567A Class for Straight Lines...........................568A First Try on a Class for Parabolas.....................569A Class for Parabolas Using Inheritance..................569Checking the Class Type............................571Attribute vs Inheritance: has-a vs is-a Relationship...........572Superclass for Defining an Interface.....................574Class Hierarchy for Numerical Differentiation..................576Classes for Differentiation...........................577Verification.....................................579A flexible Main Program............................581Extensions.....................................582Alternative Implementation via Functions.................585Alternative Implementation via Functional Programming . 586Alternative Implementation via a Single Class..............587Class Hierarchy for Numerical Integration....................589Numerical Integration Methods........................589Classes for Integration..............................590Verification.....................................594Using the Class Hierarchy...........................595About Object-Oriented Programming....................597Class Hierarchy for Making Drawings.......................599Using the Object Collection..........................600Example of Classes for Geometric Objects................609Adding Functionality via Recursion.....................614Scaling, Translating, and Rotating a Figure................618Classes for DNA Analysis...............................620Class for Regions.................................620Class for Genes..................................621Subclasses......................................626Summary..........................................627Chapter Topics...................................627Example: Input Data Reader..........................629Exercises..........................................635Sequences and Difference Equations..........................645Mathematical Models Based on Difference Equations.............646Interest Rates....................................647The Factorial as a Difference Equation...................649Fibonacci Numbers................................650Growth of a Population.............................651Logistic Growth..................................652Payback of a Loan................................654The Integral as a Difference Equation....................655Taylor Series as a Difference Equation...................657Making a Living from a Fortune.......................658Newton’s Method.................................659The Inverse of a Function............................663Programming with Sound...............................665Writing Sound to File..............................666Reading Sound from File............................667Playing Many Notes...............................667Music of a Sequence...............................668Exercises..........................................671Introduction to Discrete Calculus.............................683Discrete Functions....................................683The Sine Function................................684Interpolation....................................685Evaluating the Approximation........................686Generalization...................................687Differentiation Becomes Finite Differences....................688Differentiating the Sine Function.......................689Differences on a Mesh..............................690Generalization...................................692Integration Becomes Summation...........................693Dividing into Subintervals...........................693Integration on Subintervals...........................695Adding the Subintervals.............................696Generalization...................................697Taylor Series........................................699Approximating Functions Close to One Point...............699Approximating the Exponential Function.................699More Accurate Expansions...........................700Accuracy of the Approximation.......................702Derivatives Revisited..............................704More Accurate Difference Approximations................705Second-Order Derivatives...........................707Exercises..........................................709Introduction to differential equations..........................715The simplest case.....................................716Exponential Growth...................................718Logistic Growth......................................723A Simple Pendulum...................................724A Model for the Spreading of a Disease......................727Exercises..........................................729A Complete Differential Equation Project.......................731About the Problem: Motion and Forces in Physics...............731The Physical Problem..............................731The Computational Algorithm........................733Derivation of the Mathematical Model...................734Derivation of the Algorithm..........................736Program Development and Testing.........................737Implementation..................................737Callback Functionality.............................740Making a Module.................................742Verification.....................................743Visualization........................................746Simultaneous Computation and Plotting..................746Some Applications................................748Remark on Choosing L1t............................749Comparing Several Quantities in Subplots.................750Comparing Approximate and Exact Solutions..............751Evolution of the Error as L1t Decreases..................752Exercises..........................................755Programming of Differential Equations........................757Scalar Ordinary Differential Equations.......................758Examples on Right-Hand-Side Functions.................758The Forward Euler Scheme..........................759Function Implementation............................760Verifying the Implementation.........................761From Discrete to Continuous Solution...................763Switching Numerical Method.........................764Class Implementation..............................764Logistic Growth via a Function-Based Approach............769Logistic Growth via a Class-Based Approach..............769Systems of Ordinary Differential Equations...................772Mathematical Problem.............................773Example of a System of ODEs........................774Function Implementation............................775Class Implementation..............................777The ODESolver Class Hierarchy...........................779E.3.1 Numerical Methods . . . . . . . . . . . . . . . . . . . . . . . .779E.3.2 Construction of a Solver Hierarchy . . . . . . . . . . . . . . .780E.3.3 The Backward Euler Method . . . . . . . . . . . . . . . . . . .783E.3.4 Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .785E.3.5 Example: Exponential Decay . . . . . . . . . . . . . . . . . . .787E.3.6 Example: The Logistic Equation with Problem and SolverClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .789E.3.7 Example: An Oscillating System . . . . . . . . . . . . . . . .797E.3.8 Application 4: The Trajectory of a Ball . . . . . . . . . . . .799E.3.9 Further Developments of ODESolver . . . . . . . . . . . . . .801E.4Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .802Debugging.............................................835Using a Debugger....................................835How to Debug.......................................838A Recipe for Program Writing and Debugging..............838Application of the Recipe............................841Getting Help from a Code Analyzer.....................853G Migrating Python to Compiled Code . . . . . . . . . . . . . . . . . . . . .857G.1 Pure Python Code for Monte Carlo Simulation . . . . . . . . . . . .857G.1.1 The Computational Problem . . . . . . . . . . . . . . . . . . .858G.1.2 A Scalar Python Implementation . . . . . . . . . . . . . . . .858G.1.3 A Vectorized Python Implementation . . . . . . . . . . . . . .859G.2 Migrating Scalar Python Code to Cython . . . . . . . . . . . . . . . .860G.2.1 A Plain Cython Implementation . . . . . . . . . . . . . . . . .860G.2.2 A Better Cython Implementation . . . . . . . . . . . . . . . .863G.3 Migrating Code to C . . . . . . . . . . . . . . . . . . . . . . . . . . . .865G.3.1 Writing a C Program . . . . . . . . . . . . . . . . . . . . . . . .865G.3.2 Migrating Loops to C Code via F2PY . . . . . . . . . . . . .866G.3.3 Migrating Loops to C Code via Cython . . . . . . . . . . . .867G.3.4 Comparing Efficiency . . . . . . . . . . . . . . . . . . . . . . .868HTechnical Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .871H.1 Getting Access to Python . . . . . . . . . . . . . . . . . . . . . . . . .871H.1.1 Required Software . . . . . . . . . . . . . . . . . . . . . . . . .871H.1.2 Installing Software on Your Laptop: Mac OS X andWindows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .872H.1.3 Anaconda and Spyder . . . . . . . . . . . . . . . . . . . . . . .873H.1.4 VMWare Fusion Virtual Machine . . . . . . . . . . . . . . . .874H.1.5 Dual Boot on Windows . . . . . . . . . . . . . . . . . . . . . .877H.1.6 Vagrant Virtual Machine . . . . . . . . . . . . . . . . . . . . .877H.2 How to Write and Run a Python Program . . . . . . . . . . . . . . .878H.2.1 The Need for a Text Editor . . . . . . . . . . . . . . . . . . . .878H.2.2 Terminal Windows . . . . . . . . . . . . . . . . . . . . . . . . .880H.3 The SageMathCloud and Wakari Web Services . . . . . . . . . . . .880H.3.1 Basic Intro to SageMathCloud . . . . . . . . . . . . . . . . . .880H.3.2 Basic Intro to Wakari . . . . . . . . . . . . . . . . . . . . . . . .881H.3.3 Installing Your Own Python Packages...............881Writing IPython Notebooks..............................882A Simple Program in the Notebook.....................882Mixing Text, Mathematics, Code, and Graphics.............882Different Ways of Running Python Programs..................884Executing Python Programs in iPython...................884Executing Python Programs in Unix.....................884Executing Python Programs in Windows..................885Executing Python Programs in Mac OS X.................887Making a Complete Stand-Alone Executable...............887Doing Operating System Tasks in Python.....................888Variable Number of Function Arguments.....................891Variable Number of Positional Arguments................891Variable Number of Keyword Arguments.................894Evaluating Program Efficiency............................896Making Time Measurements.........................896Profiling Python Programs...........................898Software Testing.....................................899Requirements of the Test Function......................900Writing the Test Function; Precomputed Data..............900Writing the Te
show more...Just click on START button on Telegram Bot