Time for action – creating a matrix from other matrices

We will create a matrix from two smaller matrices, as follows:

  1. First create a two-by-two identity matrix:
    A = np.eye(2)
    print "A", A

    The identity matrix looks like this:

    A [[ 1.  0.]
     [ 0.  1.]]
    

    Create another matrix like A and multiply by 2:

    B = 2 * A
    print "B", B

    The second matrix is as follows:

    B [[ 2.  0.]
     [ 0.  2.]]
    
  2. Create the compound matrix from a string. The string uses the same format as the mat function; only, you can use matrices instead of numbers.
    print "Compound matrix
    ", np.bmat("A B; A B")

    The compound matrix is shown as follows:

    Compound matrix
    [[ 1.  0.  2.  0.]
     [ 0.  1.  0.  2.]
     [ 1.  0.  2.  0.]
     [ 0.  1.  0.  2.]]
    

What just happened?

We created a block matrix from two smaller matrices, with the bmat function. We gave the function a string containing the names of matrices instead of numbers (see bmatcreation.py):

import numpy as np

A = np.eye(2)
print "A", A
B = 2 * A
print "B", B
print "Compound matrix
", np.bmat("A B; A B")

Pop quiz – defining a matrix with a string

Q1. What is the row delimiter in a string accepted by the mat and bmat functions?

  1. Semicolon
  2. Colon
  3. Comma
  4. Space
..................Content has been hidden....................

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