Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 35
# Share Link: https://cloud.sagemath.com/projects/026994b5-e264-4071-a9e1-127c5595f170/files/Matrix%20Manipulation.sagews # # Matrix arithmetic # # # Define a matrix # int_mat = matrix([[1,2,3],[4,5,6]]) print "int_mat =" print int_mat print # # We use binary numbers instead of integers in UEET2533. # Hence, declaring a binary (i.e. GF(2)) matrix # a_mat = matrix(GF(2), [[1,0,1], [0,1,0], [0,1,1]]) print "a_mat =" print a_mat print # Transpose a matrix print "a_mat.T =\n", a_mat.T print # Inverse a matrix print "a_mat.I =\n", a_mat.I print # Sum matrix b_mat = a_mat + a_mat.I print 'a_mat * a_mat.I' print b_mat print # Multiply matrix with * b_mat = a_mat * a_mat.I print 'a_mat * a_mat.I' print b_mat print # Determinant print 'a_mat.determinant() =', a_mat.determinant() print
int_mat = [1 2 3] [4 5 6] a_mat = [1 0 1] [0 1 0] [0 1 1] a_mat.T = [1 0 0] [0 1 1] [1 0 1] a_mat.I = [1 1 1] [0 1 0] [0 1 1] a_mat * a_mat.I [0 1 0] [0 0 0] [0 0 0] a_mat * a_mat.I [1 0 0] [0 1 0] [0 0 1] a_mat.determinant() = 1
# # Shortcut to create some special matrix. # # Define zero matrix zero_mat = zero_matrix(GF(2), 4,4) print 'zero_mat = zero_matrix(GF(2), 4,4)' print zero_mat print # Define identity matrix idn_mat = identity_matrix(GF(2), 4) print 'idn_mat = identity_matrix(GF(2), 4)' print idn_mat print # Random matrix rand_mat = random_matrix(GF(2), 3, 3) print 'rand_mat = random_matrix(GF(2), 3, 3)' print rand_mat print
zero_mat = zero_matrix(GF(2), 4,4) [0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0] idn_mat = identity_matrix(GF(2), 4) [1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1] rand_mat = random_matrix(GF(2), 3, 3) [1 0 1] [1 0 1] [0 1 0]
# # Manipulate matrices # a_mat = random_matrix(GF(2), 3, 4) b_mat = identity_matrix(GF(2), 4) c_mat = zero_matrix(GF(2), 3, 2) print 'a_mat = \n', a_mat print 'b_mat = \n', b_mat print 'c_mat = \n', c_mat print # print 'Stack matrices with: a_mat.stack(b_mat) ' print a_mat.stack(b_mat) print # print 'Augment matrices with: a_mat.augment(c_mat)' print a_mat.augment(c_mat) print # print 'Reprint a_mat =' print a_mat print 'Get the first two columns of a_mat with: a_mat.matrix_from_columns(range(2))' print a_mat.matrix_from_columns(range(2)) print print 'Get the first two rows of a_mat with: a_mat.matrix_from_rows(range(2))' print a_mat.matrix_from_rows(range(2)) print print 'Remove the first row from a_mat with: a_mat.delete_rows([0])' print a_mat.delete_rows([0]) print print 'swap the first two rows of a_mat with: a_mat.swap_rows(0, 1)' a_mat.swap_rows(0, 1) print a_mat print
a_mat = [0 1 0 1] [1 1 1 1] [0 1 1 1] b_mat = [1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1] c_mat = [0 0] [0 0] [0 0] Stack matrices with: a_mat.stack(b_mat) [0 1 0 1] [1 1 1 1] [0 1 1 1] [1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1] Augment matrices with: a_mat.augment(c_mat) [0 1 0 1 0 0] [1 1 1 1 0 0] [0 1 1 1 0 0] Reprint a_mat = [0 1 0 1] [1 1 1 1] [0 1 1 1] Get the first two columns of a_mat with: a_mat.matrix_from_columns(range(2)) [0 1] [1 1] [0 1] Get the first two rows of a_mat with: a_mat.matrix_from_rows(range(2)) [0 1 0 1] [1 1 1 1] Remove the first row from a_mat with: a_mat.delete_rows([0]) [1 1 1 1] [0 1 1 1] swap the first two rows of a_mat with: a_mat.swap_rows(0, 1) [1 1 1 1] [0 1 0 1] [0 1 1 1]