Ejemplo n.º 1
0
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}
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)