0%

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

  1. Title Page
  2. Copyright and Credits
    1. Scientific Computing with Python Second Edition
  3. Contributors
    1. About the authors
    2. About the reviewer
  4. Acknowledgement
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Reviews
  6. Getting Started
    1. 1.1 Installation and configuration instructions
    2. 1.1.1 Installation
    3. 1.1.2 Anaconda
    4. 1.1.3 Spyder
    5. 1.1.4 Configuration
    6. 1.1.5 Python shell
    7. 1.1.6 Executing scripts
    8. 1.1.7 Getting help
    9. 1.1.8 Jupyter – Python notebook
    10. 1.2 Program and program flow
    11. 1.2.1 Comments
    12. 1.2.2 Line joining
    13. 1.3 Basic data types in Python
    14. 1.3.1 Numbers
    15. 1.3.2 Strings
    16. 1.3.3 Variables
    17. 1.3.4 Lists
    18. Operations on lists
    19. 1.3.6 Boolean expressions
    20. 1.4 Repeating statements with loops
    21. 1.4.1 Repeating a task
    22. 1.4.2 break and else
    23. 1.5 Conditional statements
    24. 1.6 Encapsulating code with functions
    25. 1.7 Understanding scripts and modules
    26. 1.7.1 Simple modules – collecting functions
    27. 1.7.2 Using modules and namespaces
    28. 1.8 Python interpreter
    29. Summary
  7. Variables and Basic Types
    1. 2.1 Variables
    2. 2.2 Numeric types
    3. 2.2.1 Integers
    4. Plain integers
    5. 2.2.2 Floating-point numbers
    6. Floating-point representation
    7. Infinite and not a number
    8. Underflow – Machine epsilon
    9. Other float types in NumPy
    10. 2.2.3 Complex numbers
    11. Complex numbers in mathematics
    12. The j notation
    13. Real and imaginary parts
    14. 2.3 Booleans
    15. 2.3.1 Boolean operators
    16. 2.3.2 Boolean casting
    17. Automatic Boolean casting
    18. 2.3.3 Return values of and and or
    19. 2.3.4 Booleans and integers
    20. 2.4 Strings
    21. 2.4.1 Escape sequences and raw strings
    22. 2.4.2 Operations on strings and string methods
    23. 2.4.3 String formatting
    24. 2.5 Summary
    25. 2.6 Exercises
  8. Container Types
    1. 3.1 Lists
    2. 3.1.1 Slicing
    3. Strides
    4. 3.1.2 Altering lists
    5. 3.1.3 Belonging to a list
    6. 3.1.4 List methods
    7. In-place operations
    8. 3.1.5 Merging lists – zip
    9. 3.1.6 List comprehension
    10. 3.2 A quick glance at the concept of arrays
    11. 3.3 Tuples
    12. 3.3.1 Packing and unpacking variables
    13. 3.4 Dictionaries
    14. 3.4.1 Creating and altering dictionaries
    15. 3.4.2 Looping over dictionaries
    16. 3.5 Sets
    17. 3.6 Container conversions
    18. 3.7 Checking the type of a variable
    19. 3.8 Summary
    20. 3.9 Exercises
  9. Linear Algebra - Arrays
    1. 4.1 Overview of the array type
    2. 4.1.1 Vectors and matrices
    3. 4.1.2 Indexing and slices
    4. 4.1.3 Linear algebra operations
    5. Solving a linear system
    6. 4.2 Mathematical preliminaries
    7. 4.2.1 Arrays as functions
    8. 4.2.2 Operations are elementwise
    9. 4.2.3 Shape and number of dimensions
    10. 4.2.4 The dot operations
    11. 4.3 The array type
    12. 4.3.1 Array properties
    13. 4.3.2 Creating arrays from lists
    14. Array and Python parentheses
    15. 4.4 Accessing array entries
    16. 4.4.1 Basic array slicing
    17. 4.4.2 Altering an array using slices
    18. 4.5 Functions to construct arrays
    19. 4.6 Accessing and changing the shape
    20. 4.6.1 The function shape
    21. 4.6.2 Number of dimensions
    22. 4.6.3 Reshape
    23. Transpose
    24. 4.7 Stacking
    25. 4.7.1 Stacking vectors
    26. 4.8 Functions acting on arrays
    27. 4.8.1 Universal functions
    28. Built-in universal functions
    29. Creation of universal functions
    30. 4.8.2 Array functions
    31. 4.9 Linear algebra methods in SciPy
    32. 4.9.1 Solving several linear equation systems with LU
    33. 4.9.2 Solving a least square problem with SVD
    34. 4.9.3 More methods
    35. 4.10 Summary
    36. 4.11 Exercises
  10. Advanced Array Concepts
    1. 5.1 Array views and copies
    2. 5.1.1 Array views
    3. 5.1.2 Slices as views
    4. 5.1.3 Generating views by transposing and reshaping
    5. 5.1.4 Array copies
    6. 5.2 Comparing arrays
    7. 5.2.1 Boolean arrays
    8. 5.2.2 Checking for array equality
    9. 5.2.3 Boolean operations on arrays
    10. 5.3 Array indexing
    11. 5.3.1 Indexing with Boolean arrays
    12. 5.3.2 Using the command where
    13. 5.4 Performance and vectorization
    14. 5.4.1 Vectorization
    15. 5.5 Broadcasting
    16. 5.5.1 Mathematical views
    17. Constant functions
    18. Functions of several variables
    19. General mechanism
    20. Conventions
    21. 5.5.2 Broadcasting arrays
    22. The broadcasting problem
    23. Shape mismatch
    24. 5.5.3 Typical examples
    25. Rescale rows
    26. Rescale columns
    27. Functions of two variables
    28. 5.6. Sparse matrices
    29. 5.6.1 Sparse matrix formats
    30. Compressed sparse row format (CSR)
    31. Compressed sparse column format (CSC)
    32. Row-based linked list format (LIL)
    33. Altering and slicing matrices in LIL format
    34. 5.6.2 Generating sparse matrices
    35. 5.6.3 Sparse matrix methods
    36. 5.7 Summary
  11. Plotting
    1. 6.1 Making plots with basic plotting commands
    2. 6.1.1 Using the plot command and some of its variants
    3. 6.1.2 Formatting
    4. 6.1.3 Working with meshgrid and contours
    5. 6.1.4 Generating images and contours
    6. 6.2 Working with Matplotlib objects directly
    7. 6.2.1 Creating axes objects
    8. 6.2.2 Modifying line properties
    9. 6.2.3 Making annotations
    10. 6.2.4 Filling areas between curves
    11.  6.2.5 Defining ticks and tick labels
    12. 6.2.6 Setting spines makes your plot more instructive – a comprehensive example
    13. 6.3 Making 3D plots
    14. 6.4 Making movies from plots
    15. 6.5 Summary
    16. 6.6 Exercises
  12. Functions
    1. 7.1 Functions in mathematics and functions in Python
    2. 7.2 Parameters and arguments
    3. 7.2.1 Passing arguments – by position and by keyword
    4. 7.2.2 Changing arguments
    5. 7.2.3 Access to variables defined outside the local namespace
    6. 7.2.4 Default arguments
    7. Beware of mutable default arguments
    8. 7.2.5 Variable number of arguments
    9. 7.3 Return values
    10. 7.4 Recursive functions
    11. 7.5 Function documentation
    12. 7.6 Functions are objects
    13. 7.6.1 Partial application
    14. 7.6.2 Using closures
    15. 7.7 Anonymous functions – the keyword lambda
    16. 7.7.1 The lambda construction is always replaceable
    17. 7.8 Functions as decorators
    18. 7.9 Summary
    19. 7.10 Exercises
  13. Classes
    1. 8.1 Introduction to classes
    2. 8.1.1 A guiding example: Rational numbers
    3. 8.1.2 Defining a class and making an instance
    4. 8.1.3 The __init__ method
    5. 8.1.4 Attributes and methods
    6. 8.1.5 Special methods
    7. Reverse operations
    8. Methods mimicking function calls and iterables
    9. 8.2 Attributes that depend on each other
    10. 8.2.1 The function property
    11. 8.3 Bound and unbound methods
    12. 8.4 Class attributes and class methods
    13. 8.4.1 Class attributes
    14. 8.4.2 Class methods
    15. 8.5 Subclasses and inheritance
    16. 8.6 Encapsulation
    17. 8.7 Classes as decorators
    18. 8.8 Summary
    19. 8.9 Exercises
  14. Iterating
    1. 9.1 The for statement
    2. 9.2 Controlling the flow inside the loop
    3. 9.3 Iterable objects
    4. 9.3.1 Generators
    5. 9.3.2 Iterators are disposable
    6. 9.3.3 Iterator tools
    7. 9.3.4 Generators of recursive sequences
    8. 9.3.5 Examples for iterators in mathematics
    9. Arithmetic geometric mean
    10. Convergence acceleration
    11. 9.4 List-filling patterns
    12. 9.4.1 List filling with the append method
    13. 9.4.2 List from iterators
    14. 9.4.3 Storing generated values
    15. 9.5 When iterators behave as lists
    16. 9.5.1 Generator expressions
    17. 9.5.2 Zipping iterators
    18. 9.6 Iterator objects
    19. 9.7 Infinite iterations
    20. 9.7.1 The while loop
    21. 9.7.2 Recursion
    22. 9.8 Summary
    23. 9.9 Exercises
  15. Series and Dataframes - Working with Pandas
    1. 10. 1 A guiding example: Solar cells
    2. 10.2 NumPy arrays and pandas dataframes
    3. 10.2.1 Indexing rules
    4. 10.3 Creating and modifying dataframes
    5. 10.3.1 Creating a dataframe from imported data
    6. 10.3.2 Setting the index
    7. 10.3.3 Deleting entries
    8. 10.3.4 Merging dataframes
    9. 10.3.5 Missing data in a dataframe
    10. 10.4 Working with dataframes
    11. 10.4.1 Plotting from dataframes
    12. 10.4.2 Calculations within dataframes
    13. 10.4.3 Grouping data
    14. 10.5 Summary
  16. Communication by a Graphical User Interface
    1. 11.1 A guiding example to widgets
    2. 11.1.1 Changing a value with a slider bar
    3. An example with two sliders
    4. 11.2 The button widget and mouse events
    5. 11.2.1 Updating curve parameters with a button
    6. 11.2.2 Mouse events and textboxes
    7. 11.3 Summary
  17. Error and Exception Handling
    1. 12.1 What are exceptions?
    2. 12.1.1 Basic principles
    3. Raising exceptions
    4. Catching exceptions
    5. 12.1.2 User-defined exceptions
    6. 12.1.3 Context managers – the with statement
    7. 12.2 Finding errors: debugging
    8. 12.2.1 Bugs
    9. 12.2.2 The stack
    10. 12.2.3 The Python debugger
    11. 12.2.4 Overview – debug commands
    12. 12.2.5 Debugging in IPython
    13. 12.3 Summary
  18. Namespaces, Scopes, and Modules
    1. 13.1 Namespaces
    2. 13.2 The scope of a variable
    3. 13.3 Modules
    4. 13.3.1 Introduction
    5. 13.3.2 Modules in IPython
    6. The IPython magic command – run
    7. 13.3.3 The variable __name__
    8. 13.3.4 Some useful modules
    9. 13.4 Summary
  19. Input and Output
    1. 14.1 File handling
    2. 14.1.1 Interacting with files
    3. 14.1.2 Files are iterables
    4. 14.1.3 File modes
    5. 14.2 NumPy methods
    6. 14.2.1 savetxt
    7. 14.2.3 loadtxt
    8. 14.3 Pickling
    9. 14.4 Shelves
    10. 14.5 Reading and writing Matlab data files
    11. 14.6 Reading and writing images
    12. 14.7 Summary
  20. Testing
    1. 15.1 Manual testing
    2. 15.2 Automatic testing
    3. 15.2.1 Testing the bisection algorithm
    4. 15.2.2 Using the unittest module
    5. 15.2.3 Test setUp and tearDown methods
    6. Setting up testdata when a test case is created
    7. 15.2.4 Parameterizing tests
    8. 15.2.5 Assertion tools
    9. 15.2.6 Float comparisons
    10. 15.2.7 Unit and functional tests
    11. 15.2.8 Debugging
    12. 15.2.9 Test discovery
    13. 15.3 Measuring execution time
    14. 15.3.1 Timing with a magic function
    15. 15.3.2 Timing with the Python module timeit
    16. 15.3.3 Timing with a context manager
    17. 15.4 Summary
    18. 15.5 Exercises
  21. Symbolic Computations - SymPy
    1. 16.1 What are symbolic computations?
    2. 16.1.1 Elaborating an example in SymPy
    3. 16.2 Basic elements of SymPy
    4. 16.2.1 Symbols – the basis of all formulas
    5. 16.2.2 Numbers
    6. 16.2.3 Functions
    7. Undefined functions
    8. 16.2.4 Elementary functions
    9. 16.2.5 Lambda functions
    10. 16.3 Symbolic linear algebra
    11. 16.3.1 Symbolic matrices
    12. 16.3.2 Examples for linear algebra methods in SymPy
    13. 16.4 Substitutions
    14. 16. 5 Evaluating symbolic expressions
    15. 16.5.1 Example: A study on the convergence order of Newton's method
    16. 16.5.2 Converting a symbolic expression into a numeric function
    17. A study on the parameter dependency of polynomial coefficients
    18. 16.6 Summary
  22. Interacting with the Operating System
    1. 17.1 Running a Python program in a Linux shell
    2. 17.2 The module sys
    3. 17.2.1 Command-line arguments
    4. 17.2.2 Input and output streams
    5. Redirecting streams
    6. Building a pipe between a Linux command and a Python script
    7. 17.3 How to execute Linux commands from Python
    8. 17.3.1 The modules subprocess and shlex
    9. A complete process: subprocess.run
    10. Creating processes: subprocess.Popen
    11. 17.4 Summary
  23. Python for Parallel Computing
    1. 18.1 Multicore computers and computer clusters
    2. 18.2 Message passing interface (MPI)
    3. 18.2.1 Prerequisites
    4. 18.3 Distributing tasks to different cores
    5. 18.3.1 Information exchange between processes
    6. 18.3.2 Point-to-point communication
    7. 18.3.3 Sending NumPy arrays
    8. 18.3.4 Blocking and non-blocking communication
    9. 18.3.5 One-to-all and all-to-one communication
    10. Preparing the data for communication
    11. The commands – scatter and gather
    12. A final data reduction operation – the command reduce
    13. Sending the same message to all
    14. Buffered data
    15. 18.4 Summary
  24. Comprehensive Examples
    1. 19.1 Polynomials
    2. 19.1.1 Theoretical background
    3. 19.1.2 Tasks
    4. 19.1.3 The polynomial class
    5. 19.1.4 Usage examples of the polynomial class
    6. 19.1.5 Newton polynomial
    7. 19.2 Spectral clustering
    8. 19.3 Solving initial value problems
    9. 19.4 Summary
    10. 19.5 Exercises
  25. About Packt
    1. Why subscribe?
  26. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think
  27. References
3.144.151.106