O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Scientific Computing with Python 3

Book Description

An example-rich, comprehensive guide for all of your Python computational needs

About This Book

  • Your ultimate resource for getting up and running with Python numerical computations
  • Explore numerical computing and mathematical libraries using Python 3.x code with SciPy and NumPy modules
  • A hands-on guide to implementing mathematics with Python, with complete coverage of all the key concepts

Who This Book Is For

This book is for anyone who wants to perform numerical and mathematical computations in Python. It is especially useful for developers, students, and anyone who wants to use Python for computation. Readers are expected to possess basic a knowledge of scientific computing and mathematics, but no prior experience with Python is needed.

What You Will Learn

  • The principal syntactical elements of Python
  • The most important and basic types in Python
  • The essential building blocks of computational mathematics, linear algebra, and related Python objects
  • Plot in Python using matplotlib to create high quality figures and graphics to draw and visualize your results
  • Define and use functions and learn to treat them as objects
  • How and when to correctly apply object-oriented programming for scientific computing in Python
  • Handle exceptions, which are an important part of writing reliable and usable code
  • Two aspects of testing for scientific programming: Manual and Automatic

In Detail

Python can be used for more than just general-purpose programming. It is a free, open source language and environment that has tremendous potential for use within the domain of scientific computing. This book presents Python in tight connection with mathematical applications and demonstrates how to use various concepts in Python for computing purposes, including examples with the latest version of Python 3. Python is an effective tool to use when coupling scientific computing and mathematics and this book will teach you how to use it for linear algebra, arrays, plotting, iterating, functions, polynomials, and much more.

Style and approach

This book takes a concept-based approach to the language rather than a systematic introduction. It is a complete Python tutorial and introduces computing principles, using practical examples to and showing you how to correctly implement them in Python. You’ll learn to focus on high-level design as well as the intricate details of Python syntax. Rather than providing canned problems to be solved, the exercises have been designed to inspire you to think about your own code and give you real-world insight.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Scientific Computing with Python 3
    1. Scientific Computing with Python 3
    2. Credits
    3. About the Authors
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Acknowledgement
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
        1. Python vs Other Languages
      4. Other Python literature
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started
      1. Installation and configuration instructions
        1. Installation
        2. Anaconda
        3. Configuration
        4. Python Shell
        5. Executing scripts
        6. Getting Help
        7. Jupyter – Python notebook
      2. Program and program flow
        1. Comments
        2. Line joining
      3. Basic types
        1. Numbers
        2. Strings
        3. Variables
        4. Lists
        5. Operations on lists
        6. Boolean expressions
      4. Repeating statements with loops
        1. Repeating a task
        2. Break and else
      5. Conditional statements
      6. Encapsulating code with functions
      7. Scripts and modules
        1. Simple modules - collecting functions
        2. Using modules and namespaces
      8. Interpreter
      9. Summary
    9. 2. Variables and Basic Types
      1. Variables
      2. Numeric types
        1. Integers
          1. Plain integers
        2. Floating point numbers
          1. Floating point representation
          2. Infinite and not a number
          3. Underflow - Machine Epsilon
          4. Other float types in NumPy
        3. Complex numbers
          1. Complex Numbers in Mathematics
          2. The j notation
          3. Real and imaginary parts
      3. Booleans
        1. Boolean operators
        2. Boolean casting
        3. Automatic Boolean casting
        4. Return values of and and or
        5. Boolean and integer
      4. Strings
        1. Operations on strings and string methods
          1. String formatting
      5. Summary
      6. Exercises
    10. 3. Container Types
      1. Lists
        1. Slicing
        2. Strides
        3. Altering lists
        4. Belonging to a list
        5. List methods
        6. In–place operations
        7. Merging lists – zip
        8. List comprehension
      2. Arrays
      3. Tuples
      4. Dictionaries
        1. Creating and altering dictionaries
        2. Looping over dictionaries
      5. Sets
      6. Container conversions
      7. Type checking
      8. Summary
      9. Exercises
    11. 4. Linear Algebra – Arrays
      1. Overview of the array type
        1. Vectors and matrices
        2. Indexing and slices
        3. Linear algebra operations
          1. Solving a linear system
      2. Mathematical preliminaries
        1. Arrays as functions
        2. Operations are elementwise
        3. Shape and number of dimensions
        4. The dot operations
      3. The array type
        1. Array properties
        2. Creating arrays from lists
      4. Accessing array entries
        1. Basic array slicing
        2. Altering an array using slices
      5. Functions to construct arrays
      6. Accessing and changing the shape
        1. The shape function
        2. Number of dimensions
        3. Reshape
          1. Transpose
      7. Stacking
        1. Stacking vectors
      8. Functions acting on arrays
        1. Universal functions
          1. Built-in universal functions
          2. Create universal functions
        2. Array functions
      9. Linear algebra methods in SciPy
        1. Solving several linear equation systems with LU
        2. Solving a least square problem with SVD
        3. More methods
      10. Summary
      11. Exercises
    12. 5. Advanced Array Concepts
      1. Array views and copies
        1. Array views
        2. Slices as views
        3. Transpose and reshape as views
        4. Array copy
      2. Comparing arrays
        1. Boolean arrays
          1. Checking for equality
      3. Boolean operations on arrays
      4. Array indexing
        1. Indexing with Boolean arrays
        2. Using where
      5. Performance and Vectorization
        1. Vectorization
      6. Broadcasting
        1. Mathematical view
          1. Constant functions
          2. Functions of several variables
          3. General mechanism
          4. Conventions
        2. Broadcasting arrays
          1. The broadcasting problem
          2. Shape mismatch
        3. Typical examples
          1. Rescale rows
          2. Rescale columns
          3. Functions of two variables
      7. Sparse matrices
        1. Sparse matrix formats
          1. Compressed sparse row
          2. Compressed Sparse Column
          3. Row-based linked list format
            1. Altering and slicing matrices in LIL format
        2. Generating sparse matrices
        3. Sparse matrix methods
      8. Summary
    13. 6. Plotting
      1. Basic plotting
      2. Formatting
      3. Meshgrid and contours
      4. Images and contours
      5. Matplotlib objects
        1. The axes object
        2. Modifying line properties
        3. Annotations
        4. Filling areas between curves
        5. Ticks and ticklabels
      6. Making 3D plots
      7. Making movies from plots
      8. Summary
      9. Exercises
    14. 7. Functions
      1. Basics
      2. Parameters and arguments
        1. Passing arguments - by position and by keyword
        2. Changing arguments
        3. Access to variables defined outside the local namespace
        4. Default arguments
          1. Beware of mutable default arguments
      3. Variable number of arguments
      4. Return values
      5. Recursive functions
      6. Function documentation
      7. Functions are objects
        1. Partial application
          1. Using Closures
      8. Anonymous functions - the  lambda keyword
        1. The lambda construction is always replaceable
      9. Functions as decorators
      10. Summary
      11. Exercises
    15. 8. Classes
      1. Introduction to classes
        1. Class syntax
        2. The __init__ method
      2. Attributes and methods
        1. Special methods
          1. Reverse operations
      3. Attributes that depend on each other
        1. The property function
      4. Bound and unbound methods
      5. Class attributes
      6. Class methods
      7. Subclassing and inheritance
      8. Encapsulation
      9. Classes as decorators
      10. Summary
      11. Exercises
    16. 9. Iterating
      1. The for statement
      2. Controlling the flow inside the loop
      3. Iterators
        1. Generators
        2. Iterators are disposable
        3. Iterator tools
        4. Generators of recursive sequences
          1.  Arithmetic geometric mean
      4. Convergence acceleration
      5. List filling patterns
        1. List filling with the append method
        2. List from iterators
        3. Storing generated values
      6. When iterators behave as lists
        1. Generator expression
        2. Zipping iterators
      7. Iterator objects
      8. Infinite iterations
        1. The while loop
        2. Recursion
      9. Summary
      10. Exercises
    17. 10. Error Handling
      1. What are exceptions?
        1. Basic principles
          1. Raising exceptions
          2. Catching exceptions
        2. User-defined exceptions
        3. Context managers - the with statement
      2. Finding Errors: Debugging
        1. Bugs
        2. The stack
        3. The Python debugger
        4. Overview - debug commands
        5. Debugging in IPython
      3. Summary
    18. 11. Namespaces, Scopes, and Modules
      1. Namespace
      2. Scope of a variable
      3. Modules
        1. Introduction
        2. Modules in IPython
          1. The IPython magic command
        3. The variable __name__
        4. Some useful modules
      4. Summary
    19. 12. Input and Output
      1. File handling
        1. Interacting with files
        2. Files are iterable
        3. File modes
      2. NumPy methods
        1. savetxt
        2.  loadtxt
      3. Pickling
      4. Shelves
      5. Reading and writing Matlab data files
      6. Reading and writing images
      7. Summary
    20. 13. Testing
      1. Manual testing
      2. Automatic testing
        1. Testing the bisection algorithm
      3. Using unittest package
        1. Test setUp and tearDown methods
      4. Parameterizing tests
      5. Assertion tools
      6. Float comparisons
      7. Unit and functional tests
      8. Debugging
      9. Test discovery
      10. Measuring execution time
        1. Timing with a magic function
        2. Timing with the Python module timeit
        3. Timing with a context manager
      11. Summary
      12. Exercises
    21. 14. Comprehensive Examples
      1. Polynomials
        1. Theoretical background
        2. Tasks
      2. The polynomial class
      3. Newton polynomial
      4. Spectral clustering
      5. Solving initial value problems
      6. Summary
      7. Exercises
    22. 15. Symbolic Computations - SymPy
      1. What are symbolic computations?
        1. Elaborating an example in SymPy
      2. Basic elements of SymPy
        1. Symbols - the basis of all formulas
        2. Numbers
        3. Functions
          1. Undefined functions
      3. Elementary Functions
        1. Lambda - functions
      4. Symbolic Linear Algebra
        1. Symbolic matrices
      5. Examples for Linear Algebra Methods in SymPy
      6. Substitutions
      7. Evaluating symbolic expressions
        1. Example: A study on the convergence order of Newton's Method
      8. Converting a symbolic expression into a numeric function
        1. A study on the parameter dependency of polynomial coefficients
      9. Summary
    23. References