Changeset 79c813 in git for Singular/cnf2ideal.py
- Timestamp:
- Jul 24, 2006, 3:30:40 PM (17 years ago)
- Branches:
- (u'spielwiese', 'ec94ef7a30b928574c0c3daf41f6804dff5f6b69')
- Children:
- 979c4c40c8ef1d50d1dbd2e982d786a5187c888e
- Parents:
- aff0bb7c7395a8ac4267e97e61f62e8eae06df47
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/cnf2ideal.py
raff0bb7 r79c813 1 1 import re 2 2 import sys 3 from probstat import Cartesian 4 from optparse import OptionParser 5 from itertools import repeat 6 from string import Template 7 parser = OptionParser() 8 PB="PB" 9 SING="SING" 10 parser.add_option("-f", "--format", 11 action="store", dest="format", type="string", default=PB, 12 help="select format from SING/PB") 13 parser.add_option("-i", "--invert", 14 action="store_true", dest="invert", default=False, 15 help="invert mapping to true/false") 16 3 17 def iscomment(line): 4 18 return re.match("^c",line) … … 46 60 act.append(i) 47 61 return erg 62 63 def xor(a,b): 64 return (a and not b) or (b and not a) 48 65 def gen_poly_Singular(clause): 49 66 def num2factor(f): … … 61 78 polys.extend(["".join(["x(",str(i),")*(x(",str(i)+")-1)"]) for i in xrange(1,vars+1)]) 62 79 ideal="".join(["ideal i=",",\n".join(polys),";"]) 63 command= "std(i);"80 command='LIB "digimult.lib";\n option(prot);\n satisfiable(i);' 64 81 return "\n".join([ring_def,ideal,command,"$;\n"]) 65 if __name__=='__main__': 82 83 def gen_poly_PB(clause): 84 def term2string(t): 85 if len(t)==0: 86 return "1" 87 return "*".join(["x("+str(v) +")" for v in t]) 88 89 vars=tuple([v for v in clause if v>0]) 90 negated_vars=tuple([-v for v in clause if v<0]) 91 if len(negated_vars)>0: 92 terms=[tuple([negated_vars[i] for (i,j) in enumerate(combination) if j==1])\ 93 + vars for combination\ 94 in Cartesian(list(repeat([0,1],len(negated_vars))))] 95 else: 96 terms=[vars] 97 res="+".join([term2string(t) for t in terms]) 98 return res 99 #add_vars=[negated_var[i] for (i,j) in enumerate(combination) if j==1] 100 101 def gen_PB(clauses): 102 start_str=Template("""from PyPolyBoRi import * 103 r=Ring($vars) 104 def x(i): 105 return Monomial(Variable(i)) 106 ideal=\ 107 [ 108 """) 109 start_str=start_str.substitute(vars=vars) 110 111 112 113 poly_str=",\\\n ".join([gen_poly_PB(c) for c in clauses]) 114 end_str="""] 115 ideal=[Polynomial(p) for p in ideal] 116 """ 117 return start_str+poly_str+end_str 118 119 if __name__=='__main__': 120 (options, args) = parser.parse_args() 66 121 clauses=gen_clauses(process_input(sys.stdin)) 122 if options.invert: 123 clauses=[[-i for i in c] for c in clauses] 124 # 67 125 # print clauses 68 print gen_Singular(clauses) 126 #print gen_Singular(clauses) 127 print gen_PB(clauses) 128
Note: See TracChangeset
for help on using the changeset viewer.