def efficient_rowdict2mat(rowdict): col_labels = value(rowdict).D M = Mat((set(keys(rowdict)), col_labels), {}) for r in rowdict: for c in rowdict[r].f: M[r, c] = rowdict[r][c] return M
def identity(D, one): """Given a set D and the field's one, returns the DxD identity matrix e.g.: >>> identity({0,1,2}, 1) Mat(({0, 1, 2}, {0, 1, 2}), {(0, 0): 1, (1, 1): 1, (2, 2): 1}) """ return Mat((D, D), {(d, d): one for d in D})
def listlist2mat(L): """Given a list of lists of field elements, return a matrix whose ith row consists of the elements of the ith list. The row-labels are {0...len(L)}, and the column-labels are {0...len(L[0])} >>> A=listlist2mat([[10,20,30,40],[50,60,70,80]]) >>> print(A) <BLANKLINE> 0 1 2 3 ------------- 0 | 10 20 30 40 1 | 50 60 70 80 <BLANKLINE> """ m, n = len(L), len(L[0]) return Mat((set(range(m)), set(range(n))), {(r, c): L[r][c] for r in range(m) for c in range(n)})
def rowdict2mat(rowdict): """ Given a dictionary or list whose values are Vecs, returns the Mat having these Vecs as its rows. This is the inverse of mat2rowdict. Assumes all the Vecs have the same label-set. Assumes row_dict is nonempty. If rowdict is a dictionary then its keys will be the row-labels of the Mat. If rowdict is a list then {0...len(rowdict)-1} will be the row-labels of the Mat. e.g.: >>> A = {0:Vec({0,1},{0:1,1:2}),1:Vec({0,1},{0:3,1:4})} >>> B = [Vec({0,1},{0:1,1:2}),Vec({0,1},{0:3,1:4})] >>> mat2rowdict(rowdict2mat(A)) == A True >>> rowdict2mat(A) Mat(({0, 1}, {0, 1}), {(0, 1): 2, (1, 0): 3, (0, 0): 1, (1, 1): 4}) >>> rowdict2mat(A) == rowdict2mat(B) True """ col_labels = value(rowdict).D return Mat((set(keys(rowdict)), col_labels), {(r, c): rowdict[r][c] for r in keys(rowdict) for c in col_labels})
def coldict2mat(coldict): """ Given a dictionary or list whose values are Vecs, returns the Mat having these Vecs as its columns. This is the inverse of mat2coldict. Assumes all the Vecs have the same label-set. Assumes coldict is nonempty. If coldict is a dictionary then its keys will be the column-labels of the Mat. If coldict is a list then {0...len(coldict)-1} will be the column-labels of the Mat. e.g.: >>> A = {0:Vec({0,1},{0:1,1:2}),1:Vec({0,1},{0:3,1:4})} >>> B = [Vec({0,1},{0:1,1:2}),Vec({0,1},{0:3,1:4})] >>> mat2coldict(coldict2mat(A)) == A True >>> coldict2mat(A) Mat(({0, 1}, {0, 1}), {(0, 1): 3, (1, 0): 2, (0, 0): 1, (1, 1): 4}) >>> coldict2mat(A) == coldict2mat(B) True """ row_labels = value(coldict).D return Mat((row_labels, set(keys(coldict))), {(r, c): coldict[c][r] for c in keys(coldict) for r in row_labels})
def submatrix(M, rows, cols): return Mat((M.D[0] & rows, M.D[1] & cols), {(r, c): val for (r, c), val in M.f.items() if r in rows and c in cols})
topRight = make_equations(329, 597, 1, 0) bottomLeft = make_equations(592, 157, 0, 1) bottomRight = make_equations(580, 483, 1, 1) w = Vec(D, {('y1', 'x1'): 1}) e = {0, 1, 2, 3, 4, 5, 6, 7, 8} rowDict = { 0: topLeft[0], 1: topLeft[1], 2: topRight[0], 3: topRight[1], 4: bottomLeft[0], 5:bottomLeft[1], 6: bottomRight[0], 7: bottomRight[1], 8: w } # print(rowDict) # L1Matrix = rowdict2mat(rowDict) # bVec = zero_vec(e) # bVec[8] = 1 # print(L1Matrix.D[0] == bVec.D) # print(bVec) # solve(L1Matrix, bVec) # print(solve) A = Mat(({'a','b'},{'A','B'}), {('a','A'):one, ('a','B'):one, ('b','B'):one}) b = Vec({'a','b'}, {'a':one}) x = solver.solve(A, b) print(x)