# This imports a package called "numpy" that will make working with matrices # simpler. We choose to call this package by the abbreviation np # 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. print( 'x = ' ) print( x ) print( 'y = ' ) print( y ) print( 'y = ' ) print( y ) x[ 1,0 ] = # notice that Python starts indexing at 0. (You count 0, 1, ...) # Given that a vector of length n is an (n x 1) matrix, x[ 1,0 ] refers to # the second component [1] of vector x. The [0] idicates that we are working # with the first column of the matrix, but here the matrix only has one column! print( 'x = ' ) print( x ) print( 'y = ' ) print( y ) x = np.matrix( '1;2;3' ) print( 'x = ' ) print( x ) y = np.matrix( '0;-1;-2' ) print( 'y = ' ) print( y ) print( 'y = ' ) print( y ) x[ 1,0 ] = -999 print( 'x = ' ) print( x ) print( 'y = ' ) print( y ) copy( x, y ) print( 'x = ' ) print( x ) print( 'y = ' ) print( y ) x[ 2,0 ] = 111 print( 'x = ' ) print( x ) print( 'y = ' ) print( y ) from numpy import matrix from numpy import shape def copy(x, y): """ Compute y = x, overwriting y x and y can be row and/or column vectors. If necessary, an implicit transposition happens. """ assert type(x) is matrix and len(x.shape) is 2, \ "laff.copy: vector x must be a 2D numpy.matrix" assert type(y) is matrix and len(y.shape) is 2, \ "laff.copy: vector y must be a 2D numpy.matrix" m_x, n_x = x.shape m_y, n_y = y.shape assert m_x is 1 or n_x is 1, "laff.copy: x is not a vector" assert m_y is 1 or n_y is 1, "laff.copy: y is not a vector" if m_x is 1 and m_y is 1: # x is a row, y is a row assert n_x == n_y, "laff.copy: size mismatch between x and y" for i in range(n_x): y[0, i] = x[0, i] elif n_x is 1 and n_y is 1: # x is a column, y is a column assert m_x == m_y, "laff.copy: size mismatch between x and y" for i in range(m_x): y[i, 0] = x[i, 0] elif m_x is 1 and n_y is 1: # x is a row, y is a column assert n_x == m_y, "laff.copy: size mismatch between x and y" for i in range(n_x): y[i, 0] = x[0, i] elif n_x is 1 and m_y is 1: # x is a column, y is a row assert m_x == n_y, "laff.copy: size mismatch between x and y" for i in range(m_x): y[0, i] = x[i, 0] x = np.matrix( '1 2 3' ) y = np.matrix('0 -2 -3') print( "x = " ) print( x ) print( "y = " ) print( y ) copy( x, y ) print( "y = " ) print( y ) assert np.array_equal(x,y), "error!" import numpy.matlib print("-----SLAP_copy tests-----\n") x = np.matrix( '1 2 3' ) y = np.matlib.zeros( (1,3), dtype=int) # create a 1 x 3 matrix of zeros print("From row to row", "x = ", x, "y = ", y, sep="\n") copy(x,y) print("\ny = ", y, "________________", sep="\n") x = np.matrix( '1;\ 2;\ 3' ) y = np.matlib.zeros( (3,1), dtype=int) print( "\nFrom column to column", "x = ", x, "y = ", y, sep="\n") copy(x,y) print("\ny = ", y, "________________", sep="\n") x = np.matrix( '1 2 3' ) y = np.matlib.zeros( (3,1), dtype=int) print( "\nFrom row to column", "x = ", x, "y = ", y, sep="\n") copy(x, y) print("\ny = ", y, "________________", sep="\n") x = np.matrix( '1;\ 2;\ 3' ) y = np.matlib.zeros(( 1,3), dtype=int) print( "\nFrom column to row", "x = ", x, "y = ", y, sep="\n") copy(x, y) print("\ny = ", y, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") x = np.matrix( '1 2 3 4' ) y = np.matlib.zeros( (1,3), dtype=int) print("From row to row", "x = ", x, "y = ", y, sep="\n") copy(x,y) print("\ny = ", y, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") x = np.matrix( '1;\ 2;\ 3;\ 4' ) y = np.matlib.zeros( (3,1), dtype=int) print( "\nFrom column to column", "x = ", x, "y = ", y, sep="\n") copy(x,y) print("\ny = ", y, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") x = np.matrix( '1 2 3 4' ) y = np.matlib.zeros( (3,1), dtype=int) print( "\nFrom row to column", "x = ", x, "y = ", y, sep="\n") copy(x, y) print("\ny = ", y, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") x = np.matrix( '1;\ 2;\ 3;\ 4' ) y = np.matlib.zeros(( 1,3), dtype=int) print( "\nFrom column to row", "x = ", x, "y = ", y, sep="\n") copy(x, y) print("\ny = ", y, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") A = np.matrix( '1 2 3 4;\ 5 6 7 8;\ 9 10 11 12' ) B = np.matlib.zeros( (3,4), dtype=int) print("From row to row", "A = ", A, "B = ", B, sep="\n") copy(A[1,:],B[0,:]) print("\nB = ", B, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") A = np.matrix( '1 2 3 4;\ 5 6 7 8;\ 9 10 11 12' ) B = np.matlib.zeros( (3,4), dtype=int) print("From column to column", "A = ", A, "B = ", B, sep="\n") copy(A[:,0],B[:,1]) print("\nB = ", B, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") A = np.matrix( '1 2 3 4; \ 5 6 7 8; \ 9 10 11 12' ) B = np.matlib.zeros( (4,3), dtype=int) print("From row to column", "A = ", A, "B = ", B, sep="\n") copy(A[1,:],B[:,0]) print("\nB = ", B, "________________", sep="\n") import numpy.matlib print("-----SLAP_copy tests-----\n") A = np.matrix( '1 2 3 4;\ 5 6 7 8;\ 9 10 11 12' ) B = np.matlib.zeros( (4,3), dtype=int) print("From column to row", "A = ", A, "B = ", B, sep="\n") copy(A[:,1],B[0,:]) print("\nB = ", B, "________________", sep="\n") import laff x = np.matrix( '1;\ 2;\ 3' ) y = np.matrix( '-1;\ 0;\ 2' ) laff.copy( x, y ) print( "x = " ) print( x ) print( "y = " ) print( y )