Book description
Leverage this example-packed, comprehensive guide for all your Python computational needs
Key Features
- Learn the first steps within Python to highly specialized concepts
- Explore examples and code snippets taken from typical programming situations within scientific computing.
- Delve into essential computer science concepts like iterating, object-oriented programming, testing, and MPI presented in strong connection to applications within scientific computing.
Book Description
Python has tremendous potential within the scientific computing domain. This updated edition of Scientific Computing with Python features new chapters on graphical user interfaces, efficient data processing, and parallel computing to help you perform mathematical and scientific computing efficiently using Python.
This book will help you to explore new Python syntax features and create different models using scientific computing principles. The book presents Python alongside mathematical applications and demonstrates how to apply Python concepts in computing with the help of examples involving Python 3.8. You'll use pandas for basic data analysis to understand the modern needs of scientific computing, and cover data module improvements and built-in features. You'll also explore numerical computation modules such as NumPy and SciPy, which enable fast access to highly efficient numerical algorithms. By learning to use the plotting module Matplotlib, you will be able to represent your computational results in talks and publications. A special chapter is devoted to SymPy, a tool for bridging symbolic and numerical computations.
By the end of this Python book, you'll have gained a solid understanding of task automation and how to implement and test mathematical algorithms within the realm of scientific computing.
What you will learn
- Understand the building blocks of computational mathematics, linear algebra, and related Python objects
- Use Matplotlib to create high-quality figures and graphics to draw and visualize results
- Apply object-oriented programming (OOP) to scientific computing in Python
- Discover how to use pandas to enter the world of data processing
- Handle exceptions for writing reliable and usable code
- Cover manual and automatic aspects of testing for scientific programming
- Get to grips with parallel computing to increase computation speed
Who this book is for
This book is for students with a mathematical background, university teachers designing modern courses in programming, data scientists, researchers, developers, and anyone who wants to perform scientific computation in Python.
Table of contents
- Title Page
- Copyright and Credits
- Contributors
- Acknowledgement
- Preface
-
Getting Started
- 1.1 Installation and configuration instructions
- 1.1.1 Installation
- 1.1.2 Anaconda
- 1.1.3 Spyder
- 1.1.4 Configuration
- 1.1.5 Python shell
- 1.1.6 Executing scripts
- 1.1.7 Getting help
- 1.1.8 Jupyter – Python notebook
- 1.2 Program and program flow
- 1.2.1 Comments
- 1.2.2 Line joining
- 1.3 Basic data types in Python
- 1.3.1 Numbers
- 1.3.2 Strings
- 1.3.3 Variables
- 1.3.4 Lists
- Operations on lists
- 1.3.6 Boolean expressions
- 1.4 Repeating statements with loops
- 1.4.1 Repeating a task
- 1.4.2 break and else
- 1.5 Conditional statements
- 1.6 Encapsulating code with functions
- 1.7 Understanding scripts and modules
- 1.7.1 Simple modules – collecting functions
- 1.7.2 Using modules and namespaces
- 1.8 Python interpreter
- Summary
-
Variables and Basic Types
- 2.1 Variables
- 2.2 Numeric types
- 2.2.1 Integers
- Plain integers
- 2.2.2 Floating-point numbers
- Floating-point representation
- Infinite and not a number
- Underflow – Machine epsilon
- Other float types in NumPy
- 2.2.3 Complex numbers
- Complex numbers in mathematics
- The j notation
- Real and imaginary parts
- 2.3 Booleans
- 2.3.1 Boolean operators
- 2.3.2 Boolean casting
- Automatic Boolean casting
- 2.3.3 Return values of and and or
- 2.3.4 Booleans and integers
- 2.4 Strings
- 2.4.1 Escape sequences and raw strings
- 2.4.2 Operations on strings and string methods
- 2.4.3 String formatting
- 2.5 Summary
- 2.6 Exercises
-
Container Types
- 3.1 Lists
- 3.1.1 Slicing
- Strides
- 3.1.2 Altering lists
- 3.1.3 Belonging to a list
- 3.1.4 List methods
- In-place operations
- 3.1.5 Merging lists – zip
- 3.1.6 List comprehension
- 3.2 A quick glance at the concept of arrays
- 3.3 Tuples
- 3.3.1 Packing and unpacking variables
- 3.4 Dictionaries
- 3.4.1 Creating and altering dictionaries
- 3.4.2 Looping over dictionaries
- 3.5 Sets
- 3.6 Container conversions
- 3.7 Checking the type of a variable
- 3.8 Summary
- 3.9 Exercises
-
Linear Algebra - Arrays
- 4.1 Overview of the array type
- 4.1.1 Vectors and matrices
- 4.1.2 Indexing and slices
- 4.1.3 Linear algebra operations
- Solving a linear system
- 4.2 Mathematical preliminaries
- 4.2.1 Arrays as functions
- 4.2.2 Operations are elementwise
- 4.2.3 Shape and number of dimensions
- 4.2.4 The dot operations
- 4.3 The array type
- 4.3.1 Array properties
- 4.3.2 Creating arrays from lists
- Array and Python parentheses
- 4.4 Accessing array entries
- 4.4.1 Basic array slicing
- 4.4.2 Altering an array using slices
- 4.5 Functions to construct arrays
- 4.6 Accessing and changing the shape
- 4.6.1 The function shape
- 4.6.2 Number of dimensions
- 4.6.3 Reshape
- Transpose
- 4.7 Stacking
- 4.7.1 Stacking vectors
- 4.8 Functions acting on arrays
- 4.8.1 Universal functions
- Built-in universal functions
- Creation of universal functions
- 4.8.2 Array functions
- 4.9 Linear algebra methods in SciPy
- 4.9.1 Solving several linear equation systems with LU
- 4.9.2 Solving a least square problem with SVD
- 4.9.3 More methods
- 4.10 Summary
- 4.11 Exercises
-
Advanced Array Concepts
- 5.1 Array views and copies
- 5.1.1 Array views
- 5.1.2 Slices as views
- 5.1.3 Generating views by transposing and reshaping
- 5.1.4 Array copies
- 5.2 Comparing arrays
- 5.2.1 Boolean arrays
- 5.2.2 Checking for array equality
- 5.2.3 Boolean operations on arrays
- 5.3 Array indexing
- 5.3.1 Indexing with Boolean arrays
- 5.3.2 Using the command where
- 5.4 Performance and vectorization
- 5.4.1 Vectorization
- 5.5 Broadcasting
- 5.5.1 Mathematical views
- Constant functions
- Functions of several variables
- General mechanism
- Conventions
- 5.5.2 Broadcasting arrays
- The broadcasting problem
- Shape mismatch
- 5.5.3 Typical examples
- Rescale rows
- Rescale columns
- Functions of two variables
- 5.6. Sparse matrices
- 5.6.1 Sparse matrix formats
- Compressed sparse row format (CSR)
- Compressed sparse column format (CSC)
- Row-based linked list format (LIL)
- Altering and slicing matrices in LIL format
- 5.6.2 Generating sparse matrices
- 5.6.3 Sparse matrix methods
- 5.7 Summary
-
Plotting
- 6.1 Making plots with basic plotting commands
- 6.1.1 Using the plot command and some of its variants
- 6.1.2 Formatting
- 6.1.3 Working with meshgrid and contours
- 6.1.4 Generating images and contours
- 6.2 Working with Matplotlib objects directly
- 6.2.1 Creating axes objects
- 6.2.2 Modifying line properties
- 6.2.3 Making annotations
- 6.2.4 Filling areas between curves
- 6.2.5 Defining ticks and tick labels
- 6.2.6 Setting spines makes your plot more instructive – a comprehensive example
- 6.3 Making 3D plots
- 6.4 Making movies from plots
- 6.5 Summary
- 6.6 Exercises
-
Functions
- 7.1 Functions in mathematics and functions in Python
- 7.2 Parameters and arguments
- 7.2.1 Passing arguments – by position and by keyword
- 7.2.2 Changing arguments
- 7.2.3 Access to variables defined outside the local namespace
- 7.2.4 Default arguments
- Beware of mutable default arguments
- 7.2.5 Variable number of arguments
- 7.3 Return values
- 7.4 Recursive functions
- 7.5 Function documentation
- 7.6 Functions are objects
- 7.6.1 Partial application
- 7.6.2 Using closures
- 7.7 Anonymous functions – the keyword lambda
- 7.7.1 The lambda construction is always replaceable
- 7.8 Functions as decorators
- 7.9 Summary
- 7.10 Exercises
-
Classes
- 8.1 Introduction to classes
- 8.1.1 A guiding example: Rational numbers
- 8.1.2 Defining a class and making an instance
- 8.1.3 The __init__ method
- 8.1.4 Attributes and methods
- 8.1.5 Special methods
- Reverse operations
- Methods mimicking function calls and iterables
- 8.2 Attributes that depend on each other
- 8.2.1 The function property
- 8.3 Bound and unbound methods
- 8.4 Class attributes and class methods
- 8.4.1 Class attributes
- 8.4.2 Class methods
- 8.5 Subclasses and inheritance
- 8.6 Encapsulation
- 8.7 Classes as decorators
- 8.8 Summary
- 8.9 Exercises
-
Iterating
- 9.1 The for statement
- 9.2 Controlling the flow inside the loop
- 9.3 Iterable objects
- 9.3.1 Generators
- 9.3.2 Iterators are disposable
- 9.3.3 Iterator tools
- 9.3.4 Generators of recursive sequences
- 9.3.5 Examples for iterators in mathematics
- Arithmetic geometric mean
- Convergence acceleration
- 9.4 List-filling patterns
- 9.4.1 List filling with the append method
- 9.4.2 List from iterators
- 9.4.3 Storing generated values
- 9.5 When iterators behave as lists
- 9.5.1 Generator expressions
- 9.5.2 Zipping iterators
- 9.6 Iterator objects
- 9.7 Infinite iterations
- 9.7.1 The while loop
- 9.7.2 Recursion
- 9.8 Summary
- 9.9 Exercises
-
Series and Dataframes - Working with Pandas
- 10. 1 A guiding example: Solar cells
- 10.2 NumPy arrays and pandas dataframes
- 10.2.1 Indexing rules
- 10.3 Creating and modifying dataframes
- 10.3.1 Creating a dataframe from imported data
- 10.3.2 Setting the index
- 10.3.3 Deleting entries
- 10.3.4 Merging dataframes
- 10.3.5 Missing data in a dataframe
- 10.4 Working with dataframes
- 10.4.1 Plotting from dataframes
- 10.4.2 Calculations within dataframes
- 10.4.3 Grouping data
- 10.5 Summary
- Communication by a Graphical User Interface
-
Error and Exception Handling
- 12.1 What are exceptions?
- 12.1.1 Basic principles
- Raising exceptions
- Catching exceptions
- 12.1.2 User-defined exceptions
- 12.1.3 Context managers – the with statement
- 12.2 Finding errors: debugging
- 12.2.1 Bugs
- 12.2.2 The stack
- 12.2.3 The Python debugger
- 12.2.4 Overview – debug commands
- 12.2.5 Debugging in IPython
- 12.3 Summary
- Namespaces, Scopes, and Modules
- Input and Output
-
Testing
- 15.1 Manual testing
- 15.2 Automatic testing
- 15.2.1 Testing the bisection algorithm
- 15.2.2 Using the unittest module
- 15.2.3 Test setUp and tearDown methods
- Setting up testdata when a test case is created
- 15.2.4 Parameterizing tests
- 15.2.5 Assertion tools
- 15.2.6 Float comparisons
- 15.2.7 Unit and functional tests
- 15.2.8 Debugging
- 15.2.9 Test discovery
- 15.3 Measuring execution time
- 15.3.1 Timing with a magic function
- 15.3.2 Timing with the Python module timeit
- 15.3.3 Timing with a context manager
- 15.4 Summary
- 15.5 Exercises
-
Symbolic Computations - SymPy
- 16.1 What are symbolic computations?
- 16.1.1 Elaborating an example in SymPy
- 16.2 Basic elements of SymPy
- 16.2.1 Symbols – the basis of all formulas
- 16.2.2 Numbers
- 16.2.3 Functions
- Undefined functions
- 16.2.4 Elementary functions
- 16.2.5 Lambda functions
- 16.3 Symbolic linear algebra
- 16.3.1 Symbolic matrices
- 16.3.2 Examples for linear algebra methods in SymPy
- 16.4 Substitutions
- 16. 5 Evaluating symbolic expressions
- 16.5.1 Example: A study on the convergence order of Newton's method
- 16.5.2 Converting a symbolic expression into a numeric function
- A study on the parameter dependency of polynomial coefficients
- 16.6 Summary
-
Interacting with the Operating System
- 17.1 Running a Python program in a Linux shell
- 17.2 The module sys
- 17.2.1 Command-line arguments
- 17.2.2 Input and output streams
- Redirecting streams
- Building a pipe between a Linux command and a Python script
- 17.3 How to execute Linux commands from Python
- 17.3.1 The modules subprocess and shlex
- A complete process: subprocess.run
- Creating processes: subprocess.Popen
- 17.4 Summary
-
Python for Parallel Computing
- 18.1 Multicore computers and computer clusters
- 18.2 Message passing interface (MPI)
- 18.2.1 Prerequisites
- 18.3 Distributing tasks to different cores
- 18.3.1 Information exchange between processes
- 18.3.2 Point-to-point communication
- 18.3.3 Sending NumPy arrays
- 18.3.4 Blocking and non-blocking communication
- 18.3.5 One-to-all and all-to-one communication
- Preparing the data for communication
- The commands – scatter and gather
- A final data reduction operation – the command reduce
- Sending the same message to all
- Buffered data
- 18.4 Summary
- Comprehensive Examples
- About Packt
- Other Books You May Enjoy
- References
Product information
- Title: Scientific Computing with Python - Second Edition
- Author(s):
- Release date: July 2021
- Publisher(s): Packt Publishing
- ISBN: 9781838822323
You might also like
book
Practical Data Science with Python
Learn to effectively manage data and execute data science projects from start to finish using Python …
book
Mastering Numerical Computing with NumPy
Enhance the power of NumPy and start boosting your scientific computing capabilities About This Book Grasp …
book
Hands-On Explainable AI (XAI) with Python
Resolve the black box models in your AI applications to make them fair, trustworthy, and secure. …
book
Python Tools for Scientists
Python Tools for Scientists introduces you to the most popular coding tools for scientific research, such …