Scientific Computing with Python - Claus Führer - E-Book

Scientific Computing with Python E-Book

Claus Führer

0,0
31,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 398

Veröffentlichungsjahr: 2021

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Scientific Computing with PythonSecond Edition

 

 

 

High-performance scientific computing with NumPy, SciPy, and pandas

 

 

 

 

 

 

Claus Führer Jan Erik Solem
Olivier Verdier

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Scientific Computing with PythonSecond Edition

Copyright © 2021 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors nor Packt Publishing or its dealers and distributors will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

 

Group Product Manager: Kunal ParikhPublishing Product Manager: Ali AbidiSenior Editor: Mohammed Yusuf ImaratwaleContent Development Editor: Sean LoboTechnical Editor: Manikandan KurupCopy Editor: Safis EditingProject Coordinator: Aparna Ravikumar NairProofreader: Safis EditingIndexer: Rekha NairProduction Designer: Joshua Misquitta

First published: December 2016 Second edition: July 2021

Production reference: 2280721

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-83882-232-3

www.packt.com

Contributors

About the authors

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.

Jan Erik Solem is a Python enthusiast, former associate professor, and computer vision entrepreneur. He co-founded several computer vision startups, most recently Mapillary, a street imagery computer vision company, and has worked in the tech industry for two decades. Jan Erik is a World Economic Forum technology pioneer and won the Best Nordic Thesis Award 2005-2006 for his dissertation on image analysis and pattern recognition. He is also the author of Programming Computer Vision with Python.

Olivier Verdier began using Python for scientific computing back in 2007 and received a Ph.D. in mathematics from Lund University in 2009. He has held post-doctoral positions in Cologne, Trondheim, Bergen, and Ume and is now an associate professor of mathematics at Bergen University College, Norway.

 

About the reviewer

Helmut Podhaisky works in the Institute of Mathematics at the Martin Luther University Halle-Wittenberg, where he teaches mathematics and scientific computing. He has co-authored a book on numerical methods for time integration and several papers on numerical methods. For work and fun, he uses Python, Julia, Mathematica, and Rust.

Acknowledgement

We want to acknowledge the competent and helpful comments and suggestions by Helmut Podhaisky, Halle University, Germany. To have such a partner in the process of writing a book is big luck and chance for the authors.A book has to be tested in teaching. And here, we had fantastic partners: the teaching assistants from the course "Beräkningsprogramering med Python" during the years, especially, Najmeh Abiri, Christian Andersson, Peter Meisrimel, Azahar Monge, Fatemeh Mohammadi, Tony Stillfjord, Peter Meisriemel, Lea Versbach, Sadia Asim and Anna-Mariya Otsetova, Lund University.A lot of input to the book came from a didactic project in higher education leading to a Ph.D. thesis by Dara Maghdid. Together with him, the material of the book was tested and commented on by students from Soran University in Kurdistan Region, Iraq.Most of the examples in the new chapter on GUI's in this second edition were inspired by our colleague Malin Christersson. Co-teaching this course with her, Alexandros Sopasakis, Tony Stillfjord, and Robert Klöfkorn were fun. Hopefully not only for the teaching team but also for our students—undergraduates and Ph.D. students. Special thanks also to Anne-Maria Persson, friend, director of studies, and supporter of Python in mathematics and physics higher education.A book has not only to be written, but it also has to be published, and in this process, Sean Lobo and Gebin George, Packt Publishing, were always constructive, friendly, and helpful partners bridging different time zones and often quite challenging text processing tools. They gave this book project momentum in its final stage to be completed—even under hard Covid19 work conditions.Claus Führer, Jan-Erik Solem, Olivier Verdier , 2021

Table of Contents

Title Page

Copyright and Credits

Scientific Computing with Python Second Edition

Contributors

About the authors

About the reviewer

Acknowledgement

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Get in touch

Reviews

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

11.1 A guiding example to widgets

11.1.1 Changing a value with a slider bar

An example with two sliders

11.2 The button widget and mouse events

11.2.1 Updating curve parameters with a button

11.2.2 Mouse events and textboxes

11.3 Summary

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

13.1 Namespaces

13.2 The scope of a variable

13.3 Modules

13.3.1 Introduction

13.3.2 Modules in IPython

The IPython magic command – run

13.3.3 The variable __name__

13.3.4 Some useful modules

13.4 Summary

Input and Output

14.1 File handling

14.1.1 Interacting with files

14.1.2 Files are iterables

14.1.3 File modes

14.2 NumPy methods

14.2.1 savetxt

14.2.3 loadtxt

14.3 Pickling

14.4 Shelves

14.5 Reading and writing Matlab data files

14.6 Reading and writing images

14.7 Summary

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

19.1 Polynomials

19.1.1 Theoretical background

19.1.2 Tasks

19.1.3 The polynomial class

19.1.4 Usage examples of the polynomial class

19.1.5 Newton polynomial

19.2 Spectral clustering

19.3 Solving initial value problems

19.4 Summary

19.5 Exercises

About Packt

Why subscribe?

Other Books You May Enjoy

Packt is searching for authors like you

Leave a review - let other readers know what you think

References

Variables and Basic Types

In this chapter, we will present the most important and basic types