4 """generate a YACS graph for computation of the Pascal triangle
6 parameter: rank of the triangle.
7 Use module decimal for an exact calculation with big numbers.
8 The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
16 <inline name="node_0_0" >
19 from decimal import *"""
20 print "getcontext().prec = " + str(1+n/3)
29 <inport name="a" type="string"/>
30 <inport name="b" type="string"/>
31 <outport name="c" type="string"/>
35 <inline name="collect" >
38 print "from decimal import *"
39 print "getcontext().prec = " + str(1+n/3)
40 print "tot = Decimal(0)"
41 print "for i in range (" + str(n+1) + "):"
42 print " v='a' + str(i)"
43 print " tot+=Decimal(eval(v))"
45 print "result=str(tot)"
46 print "ref=Decimal(2)**" + str(n)
47 print "reference=str(ref)"
49 print "</code></script>"
51 inport='<inport name="a' + str(i) + '" type="string"/>'
54 print '<outport name="result" type="string"/>'
55 print '<outport name="reference" type="string"/>'
59 for i in range (1,n+1):
61 node="node_" + str(i) +"_" + str(j)
62 nodetxt='<node name="'+node+'" type="node_0_0"></node>'
76 fromnode="node_" + str(i) +"_" + str(j)
77 tonode1="node_" + str(i+1) +"_" + str(j)
78 tonode2="node_" + str(i+1) +"_" + str(j+1)
79 control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
80 control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
86 fromnode="node_" + str(n) +"_" + str(i)
87 control='<control> <fromnode>'+fromnode+'</fromnode> <tonode>collect</tonode> </control>'
99 fromnode="node_" + str(i) +"_" + str(j)
100 tonode1="node_" + str(i+1) +"_" + str(j)
101 tonode2="node_" + str(i+1) +"_" + str(j+1)
102 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
103 datato1 ='<tonode>' + tonode1 + '</tonode> <toport>b</toport>'
104 datato2 ='<tonode>' + tonode2 + '</tonode> <toport>a</toport>'
115 for i in range (n+1):
116 fromnode="node_" + str(n) +"_" + str(i)
117 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
119 datato ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
134 <tonode>node_0_0</tonode> <toport>a</toport>
135 <value><string>0</string></value>
138 <tonode>node_0_0</tonode> <toport>b</toport>
139 <value><string>1</string></value>
143 for i in range (1,n+1):
144 node1="node_" + str(i) +"_" + str(0)
145 node2="node_" + str(i) +"_" + str(i)
146 tonode1 =' <tonode>' + node1 + '</tonode> <toport>a</toport>'
147 tonode2 =' <tonode>' + node2 + '</tonode> <toport>b</toport>'
150 print ' <value><string>0</string></value>'
155 print ' <value><string>0</string></value>'
163 if __name__ == "__main__":
165 usage ="""Usage: %s rank > file.xml
166 where rank is positive integer > 2
169 rank = int(sys.argv[1])
171 raise ValueError("rank must be >1")
172 except (IndexError, ValueError):
173 print usage%(sys.argv[0])