Chapter 3. SciPy for Linear Algebra

In this chapter, we will continue exploring the different SciPy modules through meaningful examples. We will start with the treatment of matrices (whether normal or sparse) with the modules on Linear Algebra—linalg and sparse. Note that linalg expands on the NumPy module with the same name.

This discipline of mathematics studies vector spaces and linear mappings between them. Matrices represent objects in this field in such a way that any property of the underlying objects may be obtained by performing adequate operations on the representing matrices. In this chapter, we assume that you are familiar with at least the basics of linear algebra, in particular with the notion of matrix multiplication, finding the determinant and inverse of a matrix, as well as their immediate applications in vector calculus.

Accordingly, in this chapter, we will explore how vectors and matrices are handled in Numpy/SciPy, how to create them, how to program standard mathematical operations between them, and how to represent this on a functional form. Next, we will solve linear system of equations expressed in the matrix form involving dense or sparse matrices. The corresponding IPython Notebook will help you test the functionality of the modules involved and modify each illustrative example according to your specific needs.

Vector creation

As mentioned in Chapter 2, Working with the NumPy Array As a First Step to SciPy, SciPy depends on NumPy's main object's ndarray data structure. You can look at one-dimensional arrays as vectors and vice versa (oriented points in an n-dimensional space). Consequently, a vector can be created via Numpy as follows:

>>> import numpy
>>> vectorA = numpy.array([1,2,3,4,5,6,7])
>>> vectorA

The output is shown as follows:

array([1, 2, 3, 4, 5, 6, 7])

We can also use already defined arrays to create a new candidate. Some examples were presented in the previous chapter. Here we can reverse the already created vector and assign it to a new one:

>>> vectorB = vectorA[::-1].copy()
>>> vectorB

The output is shown as follows:

array([7, 6, 5, 4, 3, 2, 1])

Notice that in this example, we have to make a copy of the reverse of the elements of vectorA and assign it to vectorB. This way, by changing elements of vectorB, the elements of vectorA remain unchanged, as shown here:

>>> vectorB[0]=123
>>> vectorB

The output is shown as follows:

array([123,   6,   5,   4,   3,   2,   1])

Let's look at vectorA:

>>> vectorA

The output is shown as follows:

array([1, 2, 3, 4, 5, 6, 7])

Let's make a copy of vectorA by reversing its elements and assigning it to vectorB:

>>> vectorB = vectorA[::-1].copy()
>>> vectorB

The output is shown as follows:

array([7, 6, 5, 4, 3, 2, 1])

In the last code statement, we repeated the previous assignment to vectorB, bringing it back to its initial values taking the reverse of vectorA, once again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.224.226