2 # Copyright (C) 2006-2016 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 """generate a YACS graph for computation of the Pascal triangle
24 parameter: rank of the triangle.
25 Use module decimal for an exact calculation with big numbers.
26 The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
34 <inline name="node_0_0" >
37 from decimal import *""")
38 print("getcontext().prec = " + str(1+n/3))
47 <inport name="a" type="string"/>
48 <inport name="b" type="string"/>
49 <outport name="c" type="string"/>
53 <inline name="collect" >
56 print("from decimal import *")
57 print("getcontext().prec = " + str(1+n/3))
58 print("tot = Decimal(0)")
59 print("for i in range (" + str(n+1) + "):")
60 print(" v='a' + str(i)")
61 print(" tot+=Decimal(eval(v))")
63 print("result=str(tot)")
64 print("ref=Decimal(2)**" + str(n))
65 print("reference=str(ref)")
66 print("time.sleep(1)")
67 print("</code></script>")
69 inport='<inport name="a' + str(i) + '" type="string"/>'
72 print('<outport name="result" type="string"/>')
73 print('<outport name="reference" type="string"/>')
77 for i in range (1,n+1):
79 node="node_" + str(i) +"_" + str(j)
80 nodetxt='<node name="'+node+'" type="node_0_0"></node>'
94 fromnode="node_" + str(i) +"_" + str(j)
95 tonode1="node_" + str(i+1) +"_" + str(j)
96 tonode2="node_" + str(i+1) +"_" + str(j+1)
97 control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
98 control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
103 for i in range (n+1):
104 fromnode="node_" + str(n) +"_" + str(i)
105 control='<control> <fromnode>'+fromnode+'</fromnode> <tonode>collect</tonode> </control>'
116 for j in range (i+1):
117 fromnode="node_" + str(i) +"_" + str(j)
118 tonode1="node_" + str(i+1) +"_" + str(j)
119 tonode2="node_" + str(i+1) +"_" + str(j+1)
120 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
121 datato1 ='<tonode>' + tonode1 + '</tonode> <toport>b</toport>'
122 datato2 ='<tonode>' + tonode2 + '</tonode> <toport>a</toport>'
124 print(' ' + datafrom)
128 print(' ' + datafrom)
133 for i in range (n+1):
134 fromnode="node_" + str(n) +"_" + str(i)
135 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
137 datato ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
139 print(' ' + datafrom)
152 <tonode>node_0_0</tonode> <toport>a</toport>
153 <value><string>0</string></value>
156 <tonode>node_0_0</tonode> <toport>b</toport>
157 <value><string>1</string></value>
161 for i in range (1,n+1):
162 node1="node_" + str(i) +"_" + str(0)
163 node2="node_" + str(i) +"_" + str(i)
164 tonode1 =' <tonode>' + node1 + '</tonode> <toport>a</toport>'
165 tonode2 =' <tonode>' + node2 + '</tonode> <toport>b</toport>'
168 print(' <value><string>0</string></value>')
169 print('</parameter>')
173 print(' <value><string>0</string></value>')
174 print('</parameter>')
181 if __name__ == "__main__":
183 usage ="""Usage: %s rank > file.xml
184 where rank is positive integer > 2
187 rank = int(sys.argv[1])
189 raise ValueError("rank must be >1")
190 except (IndexError, ValueError):
191 print(usage%(sys.argv[0]))