Book Image

Scientific Computing with Python 3

By : Claus Führer, Jan Erik Solem, Olivier Verdier
Book Image

Scientific Computing with Python 3

By: Claus Führer, Jan Erik Solem, Olivier Verdier

Overview of this book

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.
Table of Contents (23 chapters)
Scientific Computing with Python 3
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Acknowledgement
Preface
References

Array views and copies


In order to control precisely how memory is used, NumPy offers the concept of view of an array. Views are smaller arrays that share the same data as a larger array. This works just like a reference to one single object (refer to section Basic Types in Chapter 1, Getting Started).

Array views

The simplest example of a view is given by a slice of an array:

M = array([[1.,2.],[3.,4.]])
v = M[0,:] # first row of M

The preceding slice is a view of M. It shares the same data as M. Modifying v will modify M as well:

v[-1] = 0.
v # array([[1.,0.]])
M # array([[1.,0.],[3.,4.]]) # M is modified as well

It is possible to access the object that owns the data using the array attribute base:

v.base # array([[1.,0.],[3.,4.]])
v.base is M # True

If an array owns its data, the attribute base is none :

M.base # None

Slices as views

There are precise rules on which slices will return views and which ones will return copies. Only basic slices (mainly index expressions with :...