2 # Copyright (C) 2006-2021 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
26 <inline name="node_0_0" >
31 <inport name="a" type="int"/>
32 <inport name="b" type="int"/>
33 <outport name="c" type="int"/>
41 """generate a YACS graph for computation of the Pascal triangle
43 parameter: rank of the triangle.
44 Use integers, so rank is limited to 31 (2**31)
45 The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
51 <inline name="collect" >
54 print("for i in range (" + str(n+1) + "):")
55 print(" v='a' + str(i)")
56 print(" tot+=eval(v)")
59 print("reference=2**" + str(n))
60 print("print reference")
61 print("</code></script>")
63 inport='<inport name="a' + str(i) + '" type="int"/>'
66 print('<outport name="result" type="int"/>')
67 print('<outport name="reference" type="int"/>')
71 for i in range (1,n+1):
73 node="node_" + str(i) +"_" + str(j)
74 nodetxt='<node name="'+node+'" type="node_0_0"></node>'
88 fromnode="node_" + str(i) +"_" + str(j)
89 tonode1="node_" + str(i+1) +"_" + str(j)
90 tonode2="node_" + str(i+1) +"_" + str(j+1)
91 control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
92 control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
105 for j in range (i+1):
106 fromnode="node_" + str(i) +"_" + str(j)
107 tonode1="node_" + str(i+1) +"_" + str(j)
108 tonode2="node_" + str(i+1) +"_" + str(j+1)
109 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
110 datato1 ='<tonode>' + tonode1 + '</tonode> <toport>b</toport>'
111 datato2 ='<tonode>' + tonode2 + '</tonode> <toport>a</toport>'
113 print(' ' + datafrom)
117 print(' ' + datafrom)
123 for i in range (n+1):
124 fromnode="node_" + str(n) +"_" + str(i)
125 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
127 datato ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
129 print(' ' + datafrom)
141 <tonode>node_0_0</tonode> <toport>a</toport>
142 <value><int>0</int></value>
145 <tonode>node_0_0</tonode> <toport>b</toport>
146 <value><int>1</int></value>
150 for i in range (1,n+1):
151 node1="node_" + str(i) +"_" + str(0)
152 node2="node_" + str(i) +"_" + str(i)
153 tonode1 =' <tonode>' + node1 + '</tonode> <toport>a</toport>'
154 tonode2 =' <tonode>' + node2 + '</tonode> <toport>b</toport>'
157 print(' <value><int>0</int></value>')
158 print('</parameter>')
162 print(' <value><int>0</int></value>')
163 print('</parameter>')
170 if __name__ == "__main__":
172 usage ="""Usage: %s rank > file.xml
173 where rank is positive integer >2 and <32
176 rank = int(sys.argv[1])
178 raise ValueError("rank must be >1")
180 raise ValueError("rank must be <32")
181 except (IndexError, ValueError):
182 print(usage%(sys.argv[0]))