def find_error_matrix(S): """ Input: a matrix S whose columns are error syndromes Output: a matrix whose cth column is the error corresponding to the cth column of S. Example: >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]]) >>> find_error_matrix(S) Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0}) """ return coldict2mat([find_error(mat2coldict(S)[k]) for k in mat2coldict(S)])
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] return Mat( (A.D[0], B.D[1]), { (row, col): matutil.mat2rowdict(A)[row] * matutil.mat2coldict(B)[col] for row in matutil.mat2rowdict(A) for col in matutil.mat2coldict(B) }, )
def find_null_basis(A): Q, R = factor(A) R_inverse = find_matrix_inverse(R) R_inverse_list = mat2coldict(R_inverse) Q_list = mat2coldict(Q) zero_vector = zero_vec(Q_list[0].D) return [ R_inverse_list[i] for i in range(len(Q_list)) if Q_list[i] is zero_vector ]
def find_error_matrix(S): """ Input: a matrix S whose columns are error syndromes Output: a matrix whose cth column is the error corresponding to the cth column of S. Example: >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]]) >>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0}) True """ return coldict2mat({k: find_error(mat2coldict(S)[k]) for k in mat2coldict(S).keys()})
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' if M.D[0] != M.D[1]: return False if (len(list(mat2coldict(M).values())) == rank(list(mat2coldict(M).values()))): return True else: return False
def find_error_matrix(S): """ Input: a matrix S whose columns are error syndromes Output: a matrix whose cth column is the error corresponding to the cth column of S. Example: >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]]) >>> find_error_matrix(S) Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0}) """ from matutil import mat2coldict from matutil import coldict2mat return coldict2mat([find_error(mat2coldict(S)[i]) for i in mat2coldict(S)])
def dot_product_vec_mat_mult(v, M): assert v.D == M.D[0] result = Vec(M.D[1], {r: 0 for r in M.D[1]}) cols = mat2coldict(M) for k in M.D[1]: result.f[k] = v * cols[k] return result
def lin_comb_mat_vec_mult(M, v): assert M.D[1] == v.D result = Vec(M.D[0], {c: 0 for c in M.D[0]}) cols = mat2coldict(M) for k, value in v.f.items(): result = result + value * (cols[k]) return result
def dot_product_vec_mat_mult(v, M): assert(v.D == M.D[0]) res = Vec(M.D[1], {}) dct = matutil.mat2coldict(M) for k,n in dct.items(): res[k] = n * v return res
def lin_comb_mat_vec_mult(M, v): assert(M.D[1] == v.D) res = Vec(M.D[0], {}) dct = matutil.mat2coldict(M) for k in v.D: res = res + v[k]*dct[k] return res
def lin_comb_mat_vec_mult(M, v): assert M.D[1] == v.D from matutil import mat2coldict m = mat2coldict(M) y = sum([v[c] * m[c] for c in m.D[1]]) return y
def QR_solve(A): col_labels = sorted(A.D[1], key=repr) Acolms = dict2list(mat2coldict(A), col_labels) (Qlist, Rlist) = aug_orthogonalize(Acolms) Q = coldict2mat(list2dict(Qlist, col_labels)) R = coldict2mat(list2dict(Rlist, col_labels)) return Q, R
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] cols = mat2coldict(B) rcols = {} for index, col in cols.items(): rcols[index] = A * col return coldict2mat(rcols)
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2rowdict from matutil import mat2coldict return Mat( (A.D[0], B.D[1]), {(i, j): mat2rowdict(A)[i] * mat2coldict(B)[j] for i in A.D[0] for j in B.D[1]})
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' R = [] C = [] R_dict = mat2rowdict(M) C_dict = mat2coldict(M) for k, v in R_dict.items(): R.append(v) for k, v in C_dict.items(): C.append(v) if rank(R) != rank(C): return False elif my_is_independent(R) == False: return False elif my_is_independent(C) == False: return False return True
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] # print (str(A.D) + str(A.f)) # print (str(B.D) + str(B.f)) # print('\n') colsB = utils.mat2coldict(B) return utils.coldict2mat({k:A*colsB[k] for k in colsB})
def lin_comb_mat_vec_mult(M, v): ''' Input: -M: a matrix -v: a vector Output: M*v The following doctests are not comprehensive; they don't test the main question, which is whether the procedure uses the appropriate linear-combination definition of matrix-vector multiplication. Examples: >>> M=Mat(({'a','b'},{'A','B'}), {('a','A'):7, ('a','B'):1, ('b','A'):-5, ('b','B'):2}) >>> v=Vec({'A','B'},{'A':4, 'B':2}) >>> lin_comb_mat_vec_mult(M,v) == Vec({'a', 'b'},{'a': 30, 'b': -16}) True >>> M1=Mat(({'a','b'},{'A','B'}), {('a','A'):8, ('a','B'):2, ('b','A'):-2, ('b','B'):1}) >>> v1=Vec({'A','B'},{'A':4,'B':3}) >>> lin_comb_mat_vec_mult(M1,v1) == Vec({'a', 'b'},{'a': 38, 'b': -5}) True ''' assert (M.D[1] == v.D) # represents as a linear combination cols = mat2coldict(M) comb = [(v[i], cols[i]) for i in v.D] # sums up into a vector return sum(coef * col for coef, col in comb)
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), ... {('y1',0):2, ('y2',0):4, ('y3',0):8, ... ('y1',1):10, ('y2',1):5, ('y3',1):5, ... ('y1',2):4, ('y2',2):25, ('y3',2):2, ... ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) <BLANKLINE> 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 <BLANKLINE> >>> print(mat_move2board(Y_in)) <BLANKLINE> 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 <BLANKLINE> ''' coldict = mat2coldict(Y) return coldict2mat({key:move2board(val) for key, val in coldict.items()})
def lin_comb_mat_vec_mult(M, v): assert M.D[1] == v.D import matutil from matutil import mat2coldict mat2col = mat2coldict(M) return sum([getitem(v, key) * mat2col[key] for key in v.D])
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] col_dict = mat2coldict(B) res = dict() for c in col_dict.keys(): res[c] = A * col_dict[c] return coldict2mat(res)
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' nullspace = solve(M, list2vec([0])) ker_zero = nullspace == Vec(nullspace.D, {}) im_f_eq_codomain = independence.rank(subset_basis(matutil.mat2coldict(M).values())) == len(M.D[1]) # invertible if its one-to-one, onto, and square matrix return ker_zero and im_f_eq_codomain and len(matutil.mat2coldict(M)) == len(matutil.mat2rowdict(M))
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] import vec, matutil A_dict = matutil.mat2rowdict(A) B_dict = matutil.mat2coldict(B) dp = {(i,j): A_dict[i]*B_dict[j] for j in B_dict.keys() for i in A_dict.keys()} return Mat((set(A_dict.keys()), set(B_dict.keys())), dp)
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), ... {('y1',0):2, ('y2',0):4, ('y3',0):8, ... ('y1',1):10, ('y2',1):5, ('y3',1):5, ... ('y1',2):4, ('y2',2):25, ('y3',2):2, ... ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) <BLANKLINE> 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 <BLANKLINE> >>> print(mat_move2board(Y_in)) <BLANKLINE> 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 <BLANKLINE> ''' return coldict2mat({c: move2board(r) for c, r in mat2coldict(Y).items()})
def matrix_matrix_mul(A, B): "Returns the product of A and B" assert A.D[1] == B.D[0] from matutil import mat2coldict, mat2rowdict return Mat( (A.D[0], B.D[1]), {(r, c): mat2rowdict(A)[r] * mat2coldict(B)[c] for r in A.D[0] for c in B.D[1]})
def lin_comb_mat_vec_mult(M, v): assert(M.D[1] == v.D) # vec_list = [] # # # col_dict = matutil.mat2coldict(M) # for key, val in col_dict.items(): # vec_list.append(mult_vec_by_int(val, v[key])) # # new_v = Vec(M.D[0], {}) # # for vec in vec_list: # new_v += vec # # return new_v new_v = Vec(M.D[0], {}) col_dict = matutil.mat2coldict(M) for key, val in col_dict.items(): new_v += v[key] * val return new_v
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), {('y1',0):2, ('y2',0):4, ('y3',0):8, ('y1',1):10, ('y2',1):5, ('y3',1):5, ('y1',2):4, ('y2',2):25, ('y3',2):2, ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 >>> print(mat_move2board(Y_in)) 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 ''' y_in = mat2coldict(Y) y_out = {x:move2board(y_in[x]) for x in y_in.keys()} return coldict2mat(y_out)
def mat_move2board(Y): ''' Input: - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec giving the whiteboard coordinates of a point q. Output: - a Mat each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). Example: >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), {('y1',0):2, ('y2',0):4, ('y3',0):8, ('y1',1):10, ('y2',1):5, ('y3',1):5, ('y1',2):4, ('y2',2):25, ('y3',2):2, ('y1',3):5, ('y2',3):10, ('y3',3):4}) >>> print(Y_in) 0 1 2 3 ------------ y1 | 2 10 4 5 y2 | 4 5 25 10 y3 | 8 5 2 4 >>> print(mat_move2board(Y_in)) 0 1 2 3 ------------------ y1 | 0.25 2 2 1.25 y2 | 0.5 1 12.5 2.5 y3 | 1 1 1 1 ''' y_in = mat2coldict(Y) y_out = {x: move2board(y_in[x]) for x in y_in.keys()} return coldict2mat(y_out)
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' R=[] C=[] R_dict = mat2rowdict(M) C_dict = mat2coldict(M) for k,v in R_dict.items(): R.append(v) for k,v in C_dict.items(): C.append(v) if rank(R) != rank(C): return False elif my_is_independent(R) == False: return False elif my_is_independent(C) == False: return False return True
def lin_comb_mat_vec_mult(M, v): ''' Input: -M: a matrix -v: a vector Output: M*v The following doctests are not comprehensive; they don't test the main question, which is whether the procedure uses the appropriate linear-combination definition of matrix-vector multiplication. Examples: >>> M=Mat(({'a','b'},{0,1}), {('a',0):7, ('a',1):1, ('b',0):-5, ('b',1):2}) >>> v=Vec({0,1},{0:4, 1:2}) >>> lin_comb_mat_vec_mult(M,v) == Vec({'a', 'b'},{'a': 30, 'b': -16}) True >>> M1=Mat(({'a','b'},{0,1}), {('a',0):8, ('a',1):2, ('b',0):-2, ('b',1):1}) >>> v1=Vec({0,1},{0:4,1:3}) >>> lin_comb_mat_vec_mult(M1,v1) == Vec({'a', 'b'},{'a': 38, 'b': -5}) True ''' assert (M.D[1] == v.D) cols = matutil.mat2coldict(M) result = Vec(M.D[0], {}) for j in M.D[1]: result += v[j] * cols[j] return result
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2rowdict, mat2coldict rowsA = mat2rowdict(A) colsB = mat2coldict(B) rset = A.D[0] cset = B.D[1] return Mat((rset, cset), {(i,j):rowsA[i]*colsB[j] for i in rset for j in cset})
def lin_comb_mat_vec_mult(M, v): assert(M.D[1] == v.D) cols = mat2coldict(M) new_dict= Vec(M.D[0],{}) for k,col_vec in cols.items(): tmp = v[k] * col_vec new_dict = new_dict + tmp return new_dict
def matrix_matrix_mul(A, B): "Returns the product of A and B" assert A.D[1] == B.D[0] from matutil import mat2coldict from matutil import mat2rowdict rows = mat2rowdict(A) columns = mat2coldict(B) return Mat((A.D[0],B.D[1]), { (i,j):rows[i]*columns[j] for i in rows for j in columns })
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2coldict from matutil import coldict2mat columns = mat2coldict(B) for column,colVec in columns.items(): columns[column] = A*colVec return coldict2mat(columns)
def QR_factor(A): col_labels = sorted(A.D[1], key=repr) Acols = dict2list(mat2coldict(A),col_labels) Qlist, Rlist = aug_orthonormalize(Acols) #Now make Mats Q = coldict2mat(Qlist) R = coldict2mat(list2dict(Rlist, col_labels)) return Q,R
def dot_product_vec_mat_mult(v, M): assert(v.D == M.D[0]) output = Vec(M.D[1], {}) from matutil import mat2coldict M_col = mat2coldict(M) for i in M.D[1]: output[i] = output[i] + v*M_col[i] return output
def QR_factor(A): col_labels = sorted(A.D[1], key=repr) Acols = dict2list(mat2coldict(A), col_labels) Qlist, Rlist = aug_orthonormalize(Acols) #Now make Mats Q = coldict2mat(Qlist) R = coldict2mat(list2dict(Rlist, col_labels)) return Q, R
def lin_comb_mat_vec_mult(M, v): assert(M.D[1] == v.D) cols = mat2coldict(M) s = Vec(M.D[0],{}) for k,vec in cols.items(): s=s+v[k]*vec return s
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] import matutil from matutil import mat2coldict from matutil import coldict2mat mat2col_B = mat2coldict(B) return coldict2mat({key: dot_product_mat_vec_mult(A, mat2col_B[key]) for key in B.D[1]})
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] import matutil from matutil import mat2coldict from matutil import coldict2mat mat2col_B = mat2coldict(B) return coldict2mat({key: A * v for (key, v) in zip(mat2col_B.keys(), mat2col_B.values())})
def dot_product_vec_mat_mult(v, M): assert v.D == M.D[0] import matutil from matutil import mat2coldict mat2col = mat2coldict(M) return Vec(M.D[1], {key: v * u for (key, u) in zip(mat2col.keys(), mat2col.values())})
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] dct = matutil.mat2coldict(B) #return (matutil.coldict2mat(dict([ (c , lin_comb_mat_vec_mult(A, dct[c])) for c in dct]))) v = Vec(dct.keys(), dict()) for c in dct: print(A * dct[c]) v[c] = A * dct[c]
def mat_move2board(Y): ''' Input: - Y: Mat instance, each column of which is a 'y1', 'y2', 'y3' vector giving the whiteboard coordinates of a point q. Output: - Mat instance, each column of which is the corresponding point in the whiteboard plane (the point of intersection with the whiteboard plane of the line through the origin and q). ''' col_dict = matutil.mat2coldict(Y) new_col_dic = {} for key, val in col_dict.items(): new_col_dic[key] = Vec(val.D, {k: v / val.f['y3'] for k, v in val.f.items()}) return matutil.coldict2mat(new_col_dic) # import perspective_lab # from mat import Mat # import vecutil # import matutil # import image_mat_util # from vec import Vec # from GF2 import one # from solver import solve # row_dict = {} # row_dict[0] = perspective_lab.make_equations(358, 36, 0, 0)[0] # row_dict[1] = perspective_lab.make_equations(358, 36, 0, 0)[1] # row_dict[2] = perspective_lab.make_equations(329, 597, 0, 1)[0] # row_dict[3] = perspective_lab.make_equations(329, 597, 0, 1)[1] # row_dict[4] = perspective_lab.make_equations(592, 157, 1, 0)[0] # row_dict[5] = perspective_lab.make_equations(592, 157, 1, 0)[1] # row_dict[6] = perspective_lab.make_equations(580, 483, 1, 1)[0] # row_dict[7] = perspective_lab.make_equations(580, 483, 1, 1)[1] # foo = perspective_lab.make_equations(0, 0, 0, 0)[0] # foo[('y1', 'x1')] = 1 # foo[('y1', 'x3')] = 0 # row_dict[8] = foo # M = matutil.rowdict2mat(row_dict) # print(M) # solve(M, vecutil.list2vec([0, 0, 0, 0, 0, 0, 0, 0, 1])) # Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}), # {('y1',0):2, ('y2',0):4, ('y3',0):8, # ('y1',1):10, ('y2',1):5, ('y3',1):5, # ('y1',2):4, ('y2',2):25, ('y3',2):2, # ('y1',3):5, ('y2',3):10, ('y3',3):4}) # print(Y_in) # print(perspective_lab.mat_move2board(Y_in)) # (X_pts, colors) = image_mat_util.file2mat('board.png', ('x1','x2','x3')) # H = Mat(({'y1', 'y3', 'y2'}, {'x2', 'x3', 'x1'}), {('y3', 'x1'): -0.7219356810710031, ('y2', 'x1'): -0.3815213180054361, ('y2', 'x2'): 0.7378180860600992, ('y1', 'x1'): 1.0, ('y2', 'x3'): 110.0231807477826, ('y3', 'x3'): 669.4762699006177, ('y1', 'x3'): -359.86096256684493, ('y3', 'x2'): -0.011690730864965311, ('y1', 'x2'): 0.05169340463458105}) # Y_pts = H * X_pts # Y_board = perspective_lab.mat_move2board(Y_pts) # image_mat_util.mat2display(Y_board, colors, ('y1', 'y2', 'y3'), # scale=100, xmin=None, ymin=None)
def dot_prod_mat_mat_mult(A, B): assert A.D[1] == B.D[0] from matutil import mat2rowdict, mat2coldict rowsA = mat2rowdict(A) colsB = mat2coldict(B) rset = A.D[0] cset = B.D[1] return Mat((rset, cset), {(i, j): rowsA[i] * colsB[j] for i in rset for j in cset})
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' nullspace = solve(M, list2vec([0])) ker_zero = nullspace == Vec(nullspace.D, {}) im_f_eq_codomain = independence.rank( subset_basis(matutil.mat2coldict(M).values())) == len(M.D[1]) # invertible if its one-to-one, onto, and square matrix return ker_zero and im_f_eq_codomain and len( matutil.mat2coldict(M)) == len(matutil.mat2rowdict(M))
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] output = {} from matutil import mat2coldict from matutil import coldict2mat B_col = mat2coldict(B) for i in B.D[1]: output[i] = A * B_col[i] return coldict2mat(output)
def matrix_matrix_mul(A, B): "Returns the product of A and B" assert A.D[1] == B.D[0] import matutil from matutil import mat2coldict from matutil import coldict2mat mat2col_B = mat2coldict(B) return coldict2mat({key: matrix_vector_mul(A, mat2col_B[key]) for key in B.D[1]})
def lin_comb_mat_vec_mult(M, v): assert(M.D[1] == v.D) from matutil import mat2coldict columns = mat2coldict(M) vector = Vec(M.D[0], {c:0 for c in M.D[0]}) for c, column in columns.items(): altered = v[c] * column vector = vector + altered return vector
def vector_matrix_mul(v, M): "Returns the product of vector v and matrix M" assert M.D[0] == v.D import matutil from matutil import mat2coldict from vec import dot mat2col = mat2coldict(M) return Vec(M.D[1], {key: dot(v, mat2col[key]) for key in M.D[1]})
def is_invertible(M): L = mat2coldict(M) L = list(L.values()) cols = len(L) rows = len(L[0].D) if cols == rows and my_is_independent(L): return True else: return False
def lin_comb_mat_vec_mult(M, v): assert (M.D[1] == v.D) cols = mat2coldict(M) new_dict = Vec(M.D[0], {k: 0 for k in M.D[0]}) for k, col_vec in cols.items(): tmp = v[k] * col_vec new_dict = new_dict + tmp return new_dict
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' L = mat2coldict(M).values() return True if ( my_is_independent(L) and ( len(M.D[0]) == len(M.D[1]) ) ) else False
def is_invertible(M): ''' input: A matrix, M outpit: A boolean indicating if M is invertible. >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0}) >>> is_invertible(M) True ''' return len(M.D[0]) == len(M.D[1]) and is_independent([v for k,v in mat2coldict(M).items() ])
def matrix_vector_mul(M, v): "Returns the product of matrix M and vector v" assert M.D[1] == v.D from matutil import mat2coldict columns = mat2coldict(M) vector = Vec(M.D[0], {c: 0 for c in M.D[0]}) for column, colVec in columns.items(): mult = v[column] * colVec vector = vector + mult return vector
def Mv_mat_mat_mult(A, B): assert A.D[1] == B.D[0] col_dict = matutil.mat2coldict(B) foo_dict = {} for key, val in col_dict.items(): foo_dict[key] = A * val return matutil.coldict2mat(foo_dict)