home

Scientific Computing (Psychology 9040a)

Fall, 2020

Assignment 5

Due: Oct 27 by 11:55 pm (London ON time)


NumPy Exercises

Complete the following 5 questions.

Please note that you are not required to complete each question using a single Python command. You may use multiple lines of code. Also please note that there is not one single correct way of completing each question. There are many ways you might code up the solution to each.

  1. Create a 5x5 2D array with \(\pi\) on the border and 0 inside:

    [[3.14159265 3.14159265 3.14159265 3.14159265 3.14159265]
     [3.14159265 0.         0.         0.         3.14159265]
     [3.14159265 0.         0.         0.         3.14159265]
     [3.14159265 0.         0.         0.         3.14159265]
     [3.14159265 3.14159265 3.14159265 3.14159265 3.14159265]]
  2. Consider the 1x10 1D array Z:

    Z = np.array([3,1,4,1,5,9,2,6,5,3])

    Compute the sum of squared elements of Z

  3. Create a 10x5 2D array containing random integers between 10 and 20, inclusive.

  4. Consider the 1x100 1D array of integers Z:

    Z = np.random.randint(0,10,100)

    Print out the unique elements of Z, sorted in descending order (largest first, smallest last)

  5. Construct a 1x50 array Z containing the sequence 5,4,3,2,1 repeated 10 times:

    [5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 5 4
     3 2 1 5 4 3 2 1 5 4 3 2 1]