import numpy as np # This imports a package called "numpy" that will make working with matrices # simpler # create two two-dimensional matrices of only one column each. # In the future we will also think of (column) # vectors as matrices with only one column. x = np.matrix( '1.;2.;3.' ) print( 'x = ' ) print( x ) y = np.matrix( '-1.;0.;-2.' ) print( 'y = ' ) print( y ) def dot( x, y ): # Check how many elements there are in vector x. For this, # np.shape( x ) return the row and column size of x, where x is a matrix. m, n = np.shape( x ) alpha = 0.0 for i in range( m ): alpha = x[ i,0 ] * y[ i,0 ] + alpha return alpha alpha = 0. alpha = dot( x, y ) print( 'alpha' ) print( alpha ) print( 'Difference between alpha and np.transpose(x) * y:' ) alpha_reference = np.transpose(x) * y print( alpha - alpha_reference[0,0] ) # Insert Code here import laff alpha = np.matrix( '-2.0' ) # the way we are going to program, scalars, vectors, and matrices are # all just matrices. So, alpha here is a 1 x 1 matrix, which we # initialize to some random number, in this case -2.0. dot_unb( x, y, alpha ) # Takes x, y, and alpha as an input, and then updates alpha with the # result of dot( x, y ). Notice that the contents of variable alpha # are updated. This only works if alpha is passed in as an array # (a matrix in our case) print( 'alpha' ) print( alpha ) print( 'compare alpha to np.transpose(x) * y:' ) alpha_reference = np.transpose(x) * y print( alpha - alpha_reference[0,0] )