CS 1678: Homework 1

Due: 9/15/2021, 11:59pm

This assignment is worth 20 points, and is a programming exercise. Please go through this tutorial before you begin.

Please use Python3 for all assignments (Python3.5+ recommended). You will also use numpy/scipy, scikit-image and matplotlib libraries for this class.

It is fair game to look up the Python documentation on the web, but do not look at entire code blocks for content you are asked to implement, and do not copy-paste anything.


We have starter code hw1.py on Canvas. Your task is to complete the functions in the starter file. The specific function that you need to complete is listed in the brackets for each bullet below.
  1. [3 pts] Generate a 1000000x1 (one million by one) vector of random numbers from a Gaussian (normal) distribution with mean of 0 and standard deviation of 5. (generate_random_numbers)
  2. [3 pts] Add 1 to every value in the previous list, by using a loop. To determine how many times to loop, use the size or shape functions. Time this operation and print the number in the code. (add_one_by_loop, measure_time_consumptions)
  3. [2 pt] Now add 1 to every value in the original random vector, without using a loop. Time this operation, print the time and write it down. (add_one_without_loop, measure_time_consumptions)
  4. [3 pts] Plot the exponential function 2**x, for non-negative even values of x smaller than 30, without using loops. Save the figure into a file called exponential.png for submission. (plot_without_loop)
  5. [3 pts] Generate two random matrices A and B, and compute their product by hand, using loops. It is guaranteed that the two matrices could be multiplied. Your code should generate the same results as Python's A@B operation or numpy's np.matmul(). (matrix_multiplication_by_loop)
  6. [3 pts] Generate a matrix of shape [10, 10] containing numbers from 0 to 99 by manipulation of a given vector. Specifically, given a vector containing numbers ranging from 0 to 9, you need to perform some matrix manipulations on the vector (addition, transpose, broadcast, etc.), and generate a matrix containing 0 to 99. You should not initialze the desired matrix manually. (matrix_manipulation)
  7. [3 pts] Write a function normalize_rows which uses a single command (one line and no loops) to make the sum in each row of the matrix 1. More specifically, row-wise normalization requires the following property to hold:
    1. Sum of the entries in each row should be 1.
    2. If the elements in a row were not identical before the normalization, they should remain different after your normalization; however, the relative order should be preserved.
    Assume the input matrix to your function is (1) non-negative and (2) all rows contain at least 1 non-zero element. (normalize_rows)

Submission: Please include the following files in your submission zip file: