File: generatediff

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (214 lines) | stat: -rwxr-xr-x 6,691 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/python
# $Id$

"""
program tests for differentials of symbols
"""
test_header=""
test_header+="import esys.escriptcore.utestselect as unittest\n"
test_header+="import numarray\n"
test_header+="from esys.escript import *\n"
test_header+="class Test_util2(unittest.TestCase):\n"
test_header+="   RES_TOL=1.e-7\n"
test_tail=""
test_tail+="suite = unittest.TestSuite()\n"
test_tail+="suite.addTest(unittest.makeSuite(Test_util2))\n"
test_tail+="unittest.TextTestRunner(verbosity=2).run(suite)\n"

t_prog=""

import random 
import numarray
import math

def makeArray(shape,rng):
   l=rng[1]-rng[0]
   out=numarray.zeros(shape,numarray.Float64)
   if isinstance(l,int):
     if len(shape)==0:  
         out=int(l*random.random()+rng[0])*1.
     elif len(shape)==1:
         for i0 in range(shape[0]):
                     out[i0]=int(l*random.random()+rng[0])
     elif len(shape)==2:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
                     out[i0,i1]=int(l*random.random()+rng[0])
     elif len(shape)==3:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
               for i2 in range(shape[2]):
                     out[i0,i1,i2]=int(l*random.random()+rng[0])
     elif len(shape)==4:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
               for i2 in range(shape[2]):
                  for i3 in range(shape[3]):
                     out[i0,i1,i2,i3]=int(l*random.random()+rng[0])
     elif len(shape)==5:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
               for i2 in range(shape[2]):
                  for i3 in range(shape[3]):
                    for i4 in range(shape[4]):
                     out[i0,i1,i2,i3,i4]=int(l*ranm.random()+rng[0])
     else:
         raise SystemError,"rank is restricted to 5"
   else:
     if len(shape)==0:  
         out=l*random.random()+rng[0]
     elif len(shape)==1:
         for i0 in range(shape[0]):
                     out[i0]=l*random.random()+rng[0]
     elif len(shape)==2:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
                     out[i0,i1]=l*random.random()+rng[0]
     elif len(shape)==3:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
               for i2 in range(shape[2]):
                     out[i0,i1,i2]=l*random.random()+rng[0]
     elif len(shape)==4:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
               for i2 in range(shape[2]):
                  for i3 in range(shape[3]):
                     out[i0,i1,i2,i3]=l*random.random()+rng[0]
     elif len(shape)==5:
         for i0 in range(shape[0]):
            for i1 in range(shape[1]):
               for i2 in range(shape[2]):
                  for i3 in range(shape[3]):
                    for i4 in range(shape[4]):
                     out[i0,i1,i2,i3,i4]=l*ranm.random()+rng[0]
     else:
         raise SystemError,"rank is restricted to 5"
   return out         

for sh0 in [ (3,), (3,4), (3,4,3) ,(4,3,5,3)]:
    # get perm:
    if len(sh0)==2:
        check=[[1,0]]
    elif len(sh0)==3:
        check=[[1,0,2],
               [1,2,0],
               [2,1,0],
               [2,0,2],
               [0,2,1]]
    elif len(sh0)==4:
        check=[[0,1,3,2],
               [0,2,1,3],
               [0,2,3,1],
               [0,3,2,1],
               [0,3,1,2] ,          
               [1,0,2,3],
               [1,0,3,2],
               [1,2,0,3],
               [1,2,3,0],
               [1,3,2,0],
               [1,3,0,2],
               [2,0,1,3],
               [2,0,3,1],
               [2,1,0,3],
               [2,1,3,0],
               [2,3,1,0],
               [2,3,0,1],
               [3,0,1,2],
               [3,0,2,1],
               [3,1,0,2],
               [3,1,2,0],
               [3,2,1,0],
               [3,2,0,1]]
    else:
         check=[]
    
    # create the test cases:
    processed=[]
    l=["R","U","L","P","C","N"]
    c=[""]
    for i in range(len(sh0)):
       tmp=[]
       for ci in c:
          tmp+=[ci+li for li in l]
       c=tmp
    # SHUFFLE
    c2=[]
    while len(c)>0:
        i=int(random.random()*len(c))
        c2.append(c[i])
        del c[i]
    c=c2
    for ci in c:
      t=""
      sh=()
      for i in range(len(ci)):
          if ci[i]=="R":
             s="%s:%s"%(1,sh0[i]-1)
             sh=sh+(sh0[i]-2,)             
          if ci[i]=="U":
              s=":%s"%(sh0[i]-1)
              sh=sh+(sh0[i]-1,)             
          if ci[i]=="L":
              s="2:"
              sh=sh+(sh0[i]-2,)             
          if ci[i]=="P":
              s="%s"%(int(sh0[i]/2))
          if ci[i]=="C":
              s=":"
              sh=sh+(sh0[i],)             
          if ci[i]=="N":
              s=""
              sh=sh+(sh0[i],)
          if len(s)>0:
             if not t=="": t+=","
             t+=s
      N_found=False
      noN_found=False
      process=len(t)>0
      for i in ci:
         if i=="N":
            if not noN_found and N_found: process=False
            N_found=True
         else:
           if N_found: process=False
           noNfound=True
      # is there a similar one processed allready
      if process and ci.find("N")==-1:
         for ci2 in processed:
           for chi in check:
               is_perm=True
               for i in range(len(chi)): 
                   if not ci[i]==ci2[chi[i]]: is_perm=False
               if is_perm: process=False
      # if not process: print ci," rejected" 
      if process:
       processed.append(ci)
       for sh_diff in [ (), (2,), (3,2)]: #, (2,3,4) ,(2,4,3,2)]:
         text="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
         tname="test_slice_rank%s_%s_D_rank%s"%(len(sh0),ci,len(sh_diff))
         text+="   def %s(self):\n"%tname
         text+="      u=Symbol(%s)\n"%str(sh_diff)
         ind=[[]]
         for i in sh_diff:
           ind0=[]
           for j in ind:
              for k in range(i): ind0.append(j+[k])
           ind=ind0
         coeff={}
         for k in ind:
          coeff[tuple(k)]=makeArray(sh0,[-5,5])
         a=makeArray(sh0,[-5,5])
         text2=""
         if len(sh_diff)==0:
              text2+="u*numarray.array(%s)"%(coeff[tuple()].tolist())
         else:
           for k in coeff.keys():
              if len(text2)>0: text2+="+"
              text2+="u%s*numarray.array(%s)"%(list(k),coeff[k].tolist())
         text+="      arg=%s\n"%text2
         text+="      res=arg[%s]\n"%s
         text+="      dres=res.diff(u)\n"
         text+="      
         print text
         1/0