2 # Copyright (C) 2006-2008 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.
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
21 """generate a YACS graph for computation of the Pascal triangle
23 parameter: rank of the triangle.
24 Use module decimal for an exact calculation with big numbers.
25 The last node gives the sum of rank n (=2**n) and also a direct calculation of 2**n.
33 <inline name="node_0_0" >
36 from decimal import *"""
37 print "getcontext().prec = " + str(1+n/3)
46 <inport name="a" type="string"/>
47 <inport name="b" type="string"/>
48 <outport name="c" type="string"/>
52 <inline name="collect" >
55 print "from decimal import *"
56 print "getcontext().prec = " + str(1+n/3)
57 print "tot = Decimal(0)"
58 print "for i in range (" + str(n+1) + "):"
59 print " v='a' + str(i)"
60 print " tot+=Decimal(eval(v))"
62 print "result=str(tot)"
63 print "ref=Decimal(2)**" + str(n)
64 print "reference=str(ref)"
66 print "</code></script>"
68 inport='<inport name="a' + str(i) + '" type="string"/>'
71 print '<outport name="result" type="string"/>'
72 print '<outport name="reference" type="string"/>'
76 for i in range (1,n+1):
78 node="node_" + str(i) +"_" + str(j)
79 nodetxt='<node name="'+node+'" type="node_0_0"></node>'
93 fromnode="node_" + str(i) +"_" + str(j)
94 tonode1="node_" + str(i+1) +"_" + str(j)
95 tonode2="node_" + str(i+1) +"_" + str(j+1)
96 control1='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode1+'</tonode> </control>'
97 control2='<control> <fromnode>'+fromnode+'</fromnode> <tonode>'+tonode2+'</tonode> </control>'
102 for i in range (n+1):
103 fromnode="node_" + str(n) +"_" + str(i)
104 control='<control> <fromnode>'+fromnode+'</fromnode> <tonode>collect</tonode> </control>'
115 for j in range (i+1):
116 fromnode="node_" + str(i) +"_" + str(j)
117 tonode1="node_" + str(i+1) +"_" + str(j)
118 tonode2="node_" + str(i+1) +"_" + str(j+1)
119 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
120 datato1 ='<tonode>' + tonode1 + '</tonode> <toport>b</toport>'
121 datato2 ='<tonode>' + tonode2 + '</tonode> <toport>a</toport>'
132 for i in range (n+1):
133 fromnode="node_" + str(n) +"_" + str(i)
134 datafrom='<fromnode>' + fromnode + '</fromnode> <fromport>c</fromport>'
136 datato ='<tonode>collect</tonode> <toport>' + toport + '</toport>'
151 <tonode>node_0_0</tonode> <toport>a</toport>
152 <value><string>0</string></value>
155 <tonode>node_0_0</tonode> <toport>b</toport>
156 <value><string>1</string></value>
160 for i in range (1,n+1):
161 node1="node_" + str(i) +"_" + str(0)
162 node2="node_" + str(i) +"_" + str(i)
163 tonode1 =' <tonode>' + node1 + '</tonode> <toport>a</toport>'
164 tonode2 =' <tonode>' + node2 + '</tonode> <toport>b</toport>'
167 print ' <value><string>0</string></value>'
172 print ' <value><string>0</string></value>'
180 if __name__ == "__main__":
182 usage ="""Usage: %s rank > file.xml
183 where rank is positive integer > 2
186 rank = int(sys.argv[1])
188 raise ValueError("rank must be >1")
189 except (IndexError, ValueError):
190 print usage%(sys.argv[0])