def reflect_x(): ''' Inpute: None. Output: 3x3 X-reflection matrix. ''' M = identity() M[('y','y')] = -1 return M
def reflect_y(): ''' Input: None. Output: 3x3 Y-reflection matrix. ''' M = identity() M[('x','x')] = -1 return M
def reflect_y(): ''' Input: None. Output: 3x3 Y-reflection matrix. ''' M = identity() M[('x', 'x')] = -1 return M
def reflect_x(): ''' Inpute: None. Output: 3x3 X-reflection matrix. ''' M = identity() M[('y', 'y')] = -1 return M
def translation(x,y): ''' Input: An x and y value by which to translate an image. Output: Corresponding 3x3 translation matrix. ''' M = identity() M[('x','u')] = x M[('y','u')] = y return M
def scale(a, b): ''' Input: Scaling parameters for the x and y direction. Output: Corresponding 3x3 scaling matrix. ''' M = identity() M[('x','x')] = a M[('y','y')] = b return M
def scale(a, b): ''' Input: Scaling parameters for the x and y direction. Output: Corresponding 3x3 scaling matrix. ''' M = identity() M[('x', 'x')] = a M[('y', 'y')] = b return M
def translation(x, y): ''' Input: An x and y value by which to translate an image. Output: Corresponding 3x3 translation matrix. ''' M = identity() M[('x', 'u')] = x M[('y', 'u')] = y return M
def scale_color(scale_r, scale_g, scale_b): ''' Input: 3 scaling parameters for the colors of the image. Output: Corresponding 3x3 color scaling matrix. ''' M = identity({'r', 'g', 'b'}) M[('r', 'r')] = scale_r M[('g', 'g')] = scale_g M[('b', 'b')] = scale_b return M
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' R = mat2rowdict(identity(A.D[0], 1)) return coldict2mat([solve(A, R[i]) for i in range(len(R))])
def scale_color(scale_r,scale_g,scale_b): ''' Input: 3 scaling parameters for the colors of the image. Output: Corresponding 3x3 color scaling matrix. ''' M = identity({'r','g','b'}) M[('r','r')] = scale_r M[('g','g')] = scale_g M[('b','b')] = scale_b return M
def identity(labels = {'x','y','u'}): ''' In case you have never seen this notation for a parameter before, the way it works is that identity() now defaults to having labels equal to {'x','y','u'}. So you should write your procedure as if it were defined 'def identity(labels):'. However, if you want the labels of your identity matrix to be {'x','y','u'}, you can just call identity(). Additionally, if you want {'r','g','b'}, or another set, to be the labels of your matrix, you can call identity({'r','g','b'}). ''' return matutil.identity(labels, 1)
def find_matrix_inverse(A): ''' input: An invertible matrix, A, over GF(2) output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0}) True ''' R = mat2rowdict(identity(A.D[0], one)) return coldict2mat([solve(A, R[i]) for i in range(len(R))])
def find_triangular_matrix_inverse(A): L=[] label_list = [] I = identity(A.D[0],1) R = mat2rowdict(A) w = mat2rowdict(I) label_list.extend(range(len(R))) for k,v in w.items(): print(k,v,) s = triangular_solve(R, label_list, v) L.append(s) return coldict2mat(L)
def find_matrix_inverse(A): ''' input: An invertible matrix, A, over GF(2) output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0}) True ''' solution = identity(A.D[0],one) solutionrowdict = mat2rowdict(solution) return coldict2mat({key:solve(A,vec) for key,vec in solutionrowdict.items()})
def identity(labels={'x', 'y', 'u'}): ''' In case you have never seen this notation for a parameter before, the way it works is that identity() now defaults to having labels equal to {'x','y','u'}. So you should write your procedure as if it were defined 'def identity(labels):'. However, if you want the labels of your identity matrix to be {'x','y','u'}, you can just call identity(). Additionally, if you want {'r','g','b'}, or another set, to be the labels of your matrix, you can call identity({'r','g','b'}). ''' from matutil import identity return identity(labels, 1)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' solution = identity(A.D[0],1) solutionrowdict = mat2rowdict(solution) Arowveclist = list(mat2rowdict(A).values()) label_list = list(range(len(Arowveclist))) return coldict2mat({key:triangular_solve(Arowveclist, label_list, vec) for key,vec in solutionrowdict.items()})
def rotation(angle): ''' Input: An angle in radians to rotate an image. Output: Corresponding 3x3 rotation matrix. Note that the math module is imported. ''' from math import sin,cos M = identity() M[('x','x')] = cos(angle) M[('y','y')] = cos(angle) M[('y','x')] = sin(angle) M[('x','y')] = -sin(angle) return M
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' from matutil import mat2coldict, coldict2mat, identity I = identity(A.D[0], 1) colsDict = mat2coldict(I) solvedVecs = {i: solve(A, vector) for i, vector in colsDict.items()} return coldict2mat(solvedVecs)
def rotation(angle): ''' Input: An angle in radians to rotate an image. Output: Corresponding 3x3 rotation matrix. Note that the math module is imported. ''' from math import sin, cos M = identity() M[('x', 'x')] = cos(angle) M[('y', 'y')] = cos(angle) M[('y', 'x')] = sin(angle) M[('x', 'y')] = -sin(angle) return M
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' from matutil import mat2coldict, coldict2mat, identity I = identity(A.D[0], 1) colsDict = mat2coldict(I) solvedVecs = {i:solve(A,vector) for i,vector in colsDict.items()} return coldict2mat(solvedVecs)
def find_triangular_matrix_inverse(A): """ input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True """ bRows = [] idMat = list(mat2coldict(identity(A.D[0], 1)).values()) for i in idMat: bRows.append(ts(list(mat2rowdict(A).values()), list(range(len(A.D[0]))), i)) return coldict2mat(bRows)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' bRows = [] idMat = list(mat2coldict(identity(A.D[0], 1)).values()) for i in idMat: bRows.append( ts(list(mat2rowdict(A).values()), list(range(len(A.D[0]))), i)) return coldict2mat(bRows)
def find_matrix_inverse(A): ''' input: An invertible matrix, A, over GF(2) output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0}) True ''' bRows = [] idMat = list(mat2coldict(identity(A.D[0], one)).values()) for i in idMat: bRows.append(solve(A, i)) return coldict2mat(bRows)
def find_matrix_inverse(A): """ input: An invertible matrix, A, over GF(2) output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0}) True """ bRows = [] idMat = list(mat2coldict(identity(A.D[0], one)).values()) for i in idMat: bRows.append(solve(A, i)) return coldict2mat(bRows)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' cols = [] i = identity(A.D[0], 1) i_cols = mat2coldict(i) for k in A.D[0]: cols.append( triangular_solve([ mat2rowdict(A)[k] for k in mat2rowdict(A) ], list(range(len(A.D[0]))), i_cols[k]) ) return coldict2mat(cols)
def rotate_about(x, y, angle): ''' Input: An x and y coordinate to rotate about, and an angle in radians to rotate about. Output: Corresponding 3x3 rotation matrix. It might be helpful to use procedures you already wrote. ''' # Auxiliar matrix to translate to the origin M_translate_to_origin_point = identity() M_translate_to_origin_point[('x', 'u')] = -x M_translate_to_origin_point[('y', 'u')] = -y # Auxiliar matrix to translate to the origin M_translate_to_original_position = identity() M_translate_to_original_position[('x', 'u')] = x M_translate_to_original_position[('y', 'u')] = y # Rotate the point in the origin matrix_rotated = rotation(angle) * M_translate_to_origin_point # Matrix rotated respecto point matrix_rotated_respect_point = M_translate_to_original_position * matrix_rotated return matrix_rotated_respect_point
def find_matrix_inverse(A): ''' Input: - A: an invertible Mat over GF(2) Output: - A Mat that is the inverse of A Examples: >>> M1 = Mat(({0,1,2}, {0,1,2}), {(0, 1): one, (1, 0): one, (2, 2): one}) >>> find_matrix_inverse(M1) == Mat(M1.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True >>> M2 = Mat(({0,1,2,3},{0,1,2,3}),{(0,1):one,(1,0):one,(2,2):one}) >>> find_matrix_inverse(M2) == Mat(M2.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True ''' return coldict2mat([solve(A,i) for i in mat2coldict(identity(A.D[0],one)).values()])
def find_matrix_inverse(A): ''' input: An invertible matrix, A, over GF(2) output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0}) True ''' cols = [] i = identity(A.D[0], one) i_cols = mat2coldict(i) for k in A.D[0]: cols.append( solve(A, i_cols[k]) ) return coldict2mat(cols)
def rotate_about(x,y,angle): ''' Input: An x and y coordinate to rotate about, and an angle in radians to rotate about. Output: Corresponding 3x3 rotation matrix. It might be helpful to use procedures you already wrote. ''' # Auxiliar matrix to translate to the origin M_translate_to_origin_point = identity() M_translate_to_origin_point[('x','u')] = -x M_translate_to_origin_point[('y','u')] = -y # Auxiliar matrix to translate to the origin M_translate_to_original_position = identity() M_translate_to_original_position[('x','u')] = x M_translate_to_original_position[('y','u')] = y # Rotate the point in the origin matrix_rotated = rotation(angle)*M_translate_to_origin_point # Matrix rotated respecto point matrix_rotated_respect_point = M_translate_to_original_position*matrix_rotated return matrix_rotated_respect_point
def find_matrix_inverse(A): ''' input: An invertible matrix, A, over GF(2) output: Inverse of A >>> M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0}) >>> find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0}) True ''' identity_matrix = identity(A.D[0], one) dict1_colmatrix = mat2coldict(identity_matrix) newdict1 = {} for key in dict1_colmatrix: newdict1[key] = solve(A,dict1_colmatrix[key]) result = coldict2mat(newdict1) return result
def find_matrix_inverse(A): ''' Input: - A: an invertible Mat over GF(2) Output: - A Mat that is the inverse of A Examples: >>> M1 = Mat(({0,1,2}, {0,1,2}), {(0, 1): one, (1, 0): one, (2, 2): one}) >>> find_matrix_inverse(M1) == Mat(M1.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True ''' coldict = {} identity_mat = mat2coldict(identity(A.D[0], one)) for col_label in A.D[0]: coldict[col_label] = solve(A, identity_mat[col_label]) return coldict2mat(coldict)
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' aa = [a for a in mat2rowdict(A).values()] dd = [d for d in A.D[0]] return coldict2mat([triangular_solve(aa, dd, i) for i in mat2coldict(identity(A.D[0], 1)).values()])
def find_matrix_inverse(A): ''' Input: - A: an invertible Mat over GF(2) Output: - A Mat that is the inverse of A Examples: >>> M1 = Mat(({0,1,2}, {0,1,2}), {(0, 1): one, (1, 0): one, (2, 2): one}) >>> find_matrix_inverse(M1) == Mat(M1.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True >>> M2 = Mat(({0,1,2,3},{0,1,2,3}),{(0,1):one,(1,0):one,(2,2):one}) >>> find_matrix_inverse(M2) == Mat(M2.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True ''' cols = mat2coldict(identity(A.D[0], one)) B = coldict2mat({x:solve(A, cols[x]) for x in cols}) return B
def find_matrix_inverse(A): ''' Input: - A: an invertible Mat over GF(2) Output: - A Mat that is the inverse of A Examples: >>> M1 = Mat(({0,1,2}, {0,1,2}), {(0, 1): one, (1, 0): one, (2, 2): one}) >>> find_matrix_inverse(M1) == Mat(M1.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True >>> M2 = Mat(({0,1,2,3},{0,1,2,3}),{(0,1):one,(1,0):one,(2,2):one}) >>> find_matrix_inverse(M2) == Mat(M2.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True ''' cols = mat2coldict(identity(A.D[0], one)) B = coldict2mat({x: solve(A, cols[x]) for x in cols}) return B
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' identity_matrix = identity(A.D[0], one) dict1_rowmatrix = mat2rowdict(identity_matrix) newdict1 = {} for key in dict1_rowmatrix: newdict1[key] = solve(A,dict1_rowmatrix[key]) result = rowdict2mat(newdict1) final_result = transpose(result) return final_result
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' inverseColDict = dict() rowList = [val for val in mat2rowdict(A).values()] label_list = [d for d in A.D[0]] identVecList = [ val for val in mat2coldict(identity(A.D[0] , 1)).values()] for k in range(len(rowList)): inverseColDict[k] = triangular_solve(rowList, label_list, identVecList[k]) return coldict2mat(inverseColDict)
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rows = mat2rowdict(A) cols = mat2coldict(identity(A.D[0], 1)) B = coldict2mat({x:triangular_solve_n(rows, cols[x]) for x in cols}) return B
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rows = mat2rowdict(A) cols = mat2coldict(identity(A.D[0], 1)) B = coldict2mat({x: triangular_solve_n(rows, cols[x]) for x in cols}) return B
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. ''' M = identity({'r','g','b'}) M[('r','r')] = 77/256 M[('r','g')] = 151/256 M[('r','b')] = 28/256 M[('g','r')] = 77/256 M[('g','g')] = 151/256 M[('g','b')] = 28/256 M[('b','r')] = 77/256 M[('b','g')] = 151/256 M[('b','b')] = 28/256 return M
def grayscale(): ''' Input: None Output: 3x3 greyscale matrix. ''' M = identity({'r', 'g', 'b'}) M[('r', 'r')] = 77 / 256 M[('r', 'g')] = 151 / 256 M[('r', 'b')] = 28 / 256 M[('g', 'r')] = 77 / 256 M[('g', 'g')] = 151 / 256 M[('g', 'b')] = 28 / 256 M[('b', 'r')] = 77 / 256 M[('b', 'g')] = 151 / 256 M[('b', 'b')] = 28 / 256 return M
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' from matutil import identity,mat2coldict,mat2rowdict from triangular import triangular_solve_n identity_matrix = identity(A.D[0],one) identity_dict = mat2coldict(identity_matrix) rowsdict = mat2rowdict(A) rowslist = [ rowsdict[i] for i in rowsdict.keys() ] return coldict2mat([ triangular_solve_n(rowslist,identity_dict[i]) for i in identity_dict.keys() ])
def find_matrix_inverse(A): ''' Input: - A: an invertible Mat over GF(2) Output: - A Mat that is the inverse of A Examples: >>> M1 = Mat(({0,1,2}, {0,1,2}), {(0, 1): one, (1, 0): one, (2, 2): one}) >>> find_matrix_inverse(M1) == Mat(M1.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True >>> M2 = Mat(({0,1,2,3},{0,1,2,3}),{(0,1):one,(1,0):one,(2,2):one}) >>> find_matrix_inverse(M2) == Mat(M2.D, {(0, 1): one, (1, 0): one, (2, 2): one}) True ''' cols = range(len(mat2coldict(A))) iden = mat2coldict(identity(A.D[0], one)) B = list() for n in cols: ones = iden[n] B.append(solve(A, ones)) return coldict2mat(B)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' L = [] label_list = [] I = identity(A.D[0], 1) R = mat2rowdict(A) w = mat2rowdict(I) label_list.extend(range(len(R))) for k, v in w.items(): s = triangular_solve(R, label_list, v) L.append(s) return coldict2mat(L)
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' from matutil import identity, mat2coldict, mat2rowdict from triangular import triangular_solve_n identity_matrix = identity(A.D[0], one) identity_dict = mat2coldict(identity_matrix) rowsdict = mat2rowdict(A) rowslist = [rowsdict[i] for i in rowsdict.keys()] return coldict2mat([ triangular_solve_n(rowslist, identity_dict[i]) for i in identity_dict.keys() ])
def find_triangular_matrix_inverse(A): ''' input: An upper triangular Mat, A, with nonzero diagonal elements output: Inverse of A >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' L=[] label_list = [] I = identity(A.D[0],1) R = mat2rowdict(A) w = mat2rowdict(I) label_list.extend(range(len(R))) for k,v in w.items(): s = triangular_solve(R, label_list, v) L.append(s) return coldict2mat(L)
def find_triangular_matrix_inverse(A): ''' Supporting GF2 is not required. Input: - A: an upper triangular Mat with nonzero diagonal elements Output: - Mat that is the inverse of A Example: >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]]) >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1): 0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0}) True ''' rows = range(len(mat2rowdict(A))) b = mat2coldict(identity(A.D[0], 1)) rowlist = mat2rowdict(A) ret = list() for n in rows: ones = b[n] ret.append(triangular_solve(list(rowlist.values()), list(A.D[1]), ones)) return coldict2mat(ret)
def find_matrix_inverse(A): return coldict2mat( [solve(A, b) for (a, b) in mat2coldict(identity(A.D[0], one)).items()])
def find_eigenvector(A, eigenvalue): A_eigenvalue = A - eigenvalue * identity(A.D) return find_null_basis(A_eigenvalue)