def linear_solve(sparse_mat_rep, sub_cvector, fvars, iofvars, fvargen, newfvars, tempgen, tempvars, len_oldfvars): #return sympy.solve_linear_system(augmatrix,*fvars) sparse_str = ('SparseArray[{' + ','.join(['{'+str(ind[0]+1)+','+str(ind[1]+1)+'}' for ind in sparse_mat_rep]) + '}->{' + ','.join([mstr(value) for ind, value in sparse_mat_rep.items()]) + '}]') cvector_str = '{'+','.join([mstr(val) for val in sub_cvector]) + '}' script = ('Print[' 'M = ' + sparse_str + ';' 'b = SparseArray[' + cvector_str + '];' 'ToString[' '{' 'Simplify[LinearSolve[M, b]], TBS,' 'NullSpace[M]' '}' ', InputForm] ];') script_file = tempfile.NamedTemporaryFile(mode = 'wt', delete=False) checkstring = "Not set." try: script_file.write(script) script_file.close() del script checkstring = check_output([command, '-script', script_file.name])[2:-3].decode("utf-8").split(', TBS, ') solstring, nullstring = checkstring #print(solstring) #print(check_output([command,parameter])[2:-3].decode("utf-8").split(', TBS, ')) #ipdb.set_trace() sols_list = [mathematica_parser(sol) for sol in solstring[:-1].split(',')] except Exception as e: shutil.copy(script_file.name, './failed_script.m') print(str(e)) print(checkstring) return {} finally: os.remove(script_file.name) if len(nullstring) > 2: sepvecs = nullstring[1:-1].split('}, ') for nullvec in sepvecs: nullvec = nullvec[1:].split(',') if any(nullvecel != 0 for nullvecel in nullvec[len(nullvec)-len_oldfvars:]): newfvar = next(tempgen) iofvars.append(newfvar) tempvars.append(newfvar) else: newfvar = next(fvargen) newfvars.append(newfvar) sols_list = [sol+newfvar*mathematica_parser(nullvecel) for sol, nullvecel in zip(sols_list, nullvec)] #ipdb.set_trace() if not sols_list: return {} sols = dict(zip(fvars, sols_list)) return {var: sol for var, sol in sols.items() if var is not sol}
def linear_solve(sparse_mat_rep, sub_cvector, length, fvars, iofvars, fvargen, newfvars, vardict): mathematica_parser.vardict = vardict if len(sparse_mat_rep) == 1: return {fvars[0]: sub_cvector[0]/sparse_mat_rep[(0,0)]} #return sympy.solve_linear_system(augmatrix,*fvars) sparse_str = ('SparseArray[{' + ','.join(['{'+str(ind[0]+1)+','+str(ind[1]+1)+'}' for ind in sparse_mat_rep]) + '}->{' + ','.join([mstr(value) for ind, value in sparse_mat_rep.items()]) + '}]') cvector_str = '{'+','.join([mstr(val) for val in sub_cvector]) + '}' script = ('Print[' 'M = ' + sparse_str + ';' 'b = SparseArray[' + cvector_str + '];' 'ToString[' '{' 'Simplify[LinearSolve[M, b]], TBS,' 'NullSpace[M]' '}' ', InputForm] ];') script_file = tempfile.NamedTemporaryFile(mode = 'wt', delete=False) checkstring = "Not set." try: script_file.write(script) script_file.close() del script checkstring = check_output([command, '-script', script_file.name])[2:-3].decode("utf-8").split(', TBS, ') solstring, nullstring = checkstring #print(solstring) #print(check_output([command,parameter])[2:-3].decode("utf-8").split(', TBS, ')) #ipdb.set_trace() sols_list = [mathematica_parser(sol) for sol in solstring[:-1].split(',')] except Exception as e: shutil.copy(script_file.name, './failed_script.m') print(str(e)) print(checkstring) return {} finally: os.remove(script_file.name) if len(nullstring) > 2: sepvecs = nullstring[1:-1].split('}, ') for nullvec in sepvecs: nullvec = nullvec[1:].split(',') newfvar = next(fvargen) newfvars.append(newfvar) sols_list = [sol+newfvar*mathematica_parser(nullvecel) for sol, nullvecel in zip(sols_list, nullvec)] #ipdb.set_trace() if not sols_list: return {} sols = dict(zip(fvars, sols_list)) return {var: sol for var, sol in sols.items() if var is not sol}
def mathematica_series(scalar, varlist, order): sstr = mstr(scalar) varsstr = ','.join(['{'+str(var) +', 0, ' + str(order) +'}' for var in varlist]) parameter = ('ToString[' + 'Series['+ sstr+ ',' +varsstr+']' ', InputForm]') simpstring = check_output([qcommand,parameter])[0:-1].decode("utf-8") return mathematica_parser(simpstring)
def linear_solve(augmatrix, fvars, iofvars, fvargen, newfvars, tempgen, tempvars, len_oldfvars): # sols_set = linsolve(augmatrix, *fvars) # if not sols_set: # return {}, False # sols = dict(zip(fvars, list(sols_set)[0])) # return {var: default_simplify(sol) for var, sol in sols.items() if var is not sol}, True # #return sympy.solve_linear_system(augmatrix,*fvars) augstr = mstr(augmatrix) script = ('Print[' 'M = ' + augstr + ';' 'ToString[' '{' 'Simplify[LinearSolve[M[[1 ;; -1, 1 ;; -2]], M[[All, -1]]]], TBS,' 'NullSpace[M[[1 ;; -1, 1 ;; -2]]]' '}' ', InputForm] ];') script_file = tempfile.NamedTemporaryFile(mode = 'wt', delete=False) checkstring = "Not set." try: script_file.write(script) script_file.close() del script checkstring = check_output([command, '-script', script_file.name])[2:-3].decode("utf-8").split(', TBS, ') solstring, nullstring = checkstring #print(solstring) #print(check_output([command,parameter])[2:-3].decode("utf-8").split(', TBS, ')) #ipdb.set_trace() sols_list = [mathematica_parser(sol) for sol in solstring[:-1].split(',')] except Exception as e: print(str(e)) print(checkstring) return {} finally: os.remove(script_file.name) if len(nullstring) > 2: sepvecs = nullstring[1:-1].split('}, ') for nullvec in sepvecs: nullvec = nullvec[1:].split(',') if any(nullvecel != 0 for nullvecel in nullvec[len(nullvec)-len_oldfvars:]): newfvar = next(tempgen) iofvars.append(newfvar) tempvars.append(newfvar) else: newfvar = next(fvargen) newfvars.append(newfvar) sols_list = [sol+newfvar*mathematica_parser(nullvecel) for sol, nullvecel in zip(sols_list, nullvec)] #ipdb.set_trace() if not sols_list: return {}, False sols = dict(zip(fvars, sols_list)) return {var: sol for var, sol in sols.items() if var is not sol}, True
def linear_solve(sparse_mat_rep, sub_cvector, length, fvars, iofvars, fvargen, newfvars, vardict): matrix = zeros(len(sub_cvector), length) for index, value in sparse_mat_rep.items(): matrix[index[0], index[1]] = value if matrix.cols == 1 and matrix.rows == 1: return {fvars[0]: sub_cvector[0] / matrix[0, 0]} M_str = mstr(matrix) b_str = '{' + ','.join('{' + mstr(el) + '}' for el in sub_cvector) + '}' script = M_str + '\n' + b_str script_file = tempfile.NamedTemporaryFile(mode='wt', delete=False, dir='./', suffix=".m") checkstring = "Not set." script_file.write(script) script_file.close() del script try: checkstring = check_output([command, script_file.name])[2:-3].decode("utf-8") auto_symbol_add.newfvars = newfvars auto_symbol_add.fvargen = fvargen temp_vardict = vardict.copy() sols_list = [ parse_expr(sol, transformations=transformations, local_dict=temp_vardict) for sol in checkstring.split('],[') ] except Exception as e: shutil.copy(script_file.name, './failed_ginac_solve.txt') os.remove(script_file.name) print(str(e)) print(checkstring) return {} else: os.remove(script_file.name) if not sols_list: return {} sols = dict(zip(fvars, sols_list)) return {var: sol for var, sol in sols.items() if var is not sol}
def mathematica_simplify(scalar): sstr = mstr(scalar) parameter = ('ToString[' 'Simplify[' + sstr +']' ', InputForm]') simpstring = check_output([qcommand,parameter])[0:-1].decode("utf-8") try: return mathematica_parser(simpstring) except Exception as e: print(str(e)) print(simpstring)
def main(argv): """Print norm of psi.""" if len(argv) < 1 or len(argv) > 2: print("Usage: python3 print_error.py psi_name [max_order]") NAME = argv[0] iofvars = [] split_orders = [] normdict = {} psi = load_group(NAME, iofvars=iofvars, split_orders=split_orders, normdict=normdict) max_order = int(str(argv[1])) if len(argv) > 2 else len(split_orders - 2) zeroth_order = psi[split_orders[0]:split_orders[1]] psi = substitute_group(psi, normdict) for order in range(max_order + 1): norm = trace_inner_product( zeroth_order, psi[split_orders[0]:split_orders[order + 1]]) print(mstr(simplify(norm)))
def mathematica_simplify(scalar, use_tempfile = False): sstr = mstr(scalar) parameter = ('ToString[' 'Simplify[' + sstr +']' ', InputForm]') simpstring = "Not set." if use_tempfile: script_file = tempfile.NamedTemporaryFile(mode = 'wt', delete=False) script_file.write("Print["+parameter+"]") script_file.close() simpstring = check_output([command, '-script', script_file.name])[0:-1].decode("utf-8") os.remove(script_file.name) else: simpstring = check_output([qcommand,parameter])[0:-1].decode("utf-8") try: return mathematica_parser(simpstring) except Exception as e: print(str(e)) print(simpstring)