1 |
#!/usr/bin/python |
2 |
# $Id$ |
3 |
|
4 |
""" |
5 |
program tests for differemtials of symbols |
6 |
""" |
7 |
test_header="" |
8 |
test_header+="import unittest\n" |
9 |
test_header+="import numarray\n" |
10 |
test_header+="from esys.escript import *\n" |
11 |
test_header+="class Test_util2(unittest.TestCase):\n" |
12 |
test_header+=" RES_TOL=1.e-7\n" |
13 |
test_tail="" |
14 |
test_tail+="suite = unittest.TestSuite()\n" |
15 |
test_tail+="suite.addTest(unittest.makeSuite(Test_util2))\n" |
16 |
test_tail+="unittest.TextTestRunner(verbosity=2).run(suite)\n" |
17 |
|
18 |
t_prog="" |
19 |
|
20 |
import random |
21 |
import numarray |
22 |
import math |
23 |
|
24 |
def makeArray(shape,rng): |
25 |
l=rng[1]-rng[0] |
26 |
out=numarray.zeros(shape,numarray.Float64) |
27 |
if isinstance(l,int): |
28 |
if len(shape)==0: |
29 |
out=int(l*random.random()+rng[0])*1. |
30 |
elif len(shape)==1: |
31 |
for i0 in range(shape[0]): |
32 |
out[i0]=int(l*random.random()+rng[0]) |
33 |
elif len(shape)==2: |
34 |
for i0 in range(shape[0]): |
35 |
for i1 in range(shape[1]): |
36 |
out[i0,i1]=int(l*random.random()+rng[0]) |
37 |
elif len(shape)==3: |
38 |
for i0 in range(shape[0]): |
39 |
for i1 in range(shape[1]): |
40 |
for i2 in range(shape[2]): |
41 |
out[i0,i1,i2]=int(l*random.random()+rng[0]) |
42 |
elif len(shape)==4: |
43 |
for i0 in range(shape[0]): |
44 |
for i1 in range(shape[1]): |
45 |
for i2 in range(shape[2]): |
46 |
for i3 in range(shape[3]): |
47 |
out[i0,i1,i2,i3]=int(l*random.random()+rng[0]) |
48 |
elif len(shape)==5: |
49 |
for i0 in range(shape[0]): |
50 |
for i1 in range(shape[1]): |
51 |
for i2 in range(shape[2]): |
52 |
for i3 in range(shape[3]): |
53 |
for i4 in range(shape[4]): |
54 |
out[i0,i1,i2,i3,i4]=int(l*ranm.random()+rng[0]) |
55 |
else: |
56 |
raise SystemError,"rank is restricted to 5" |
57 |
else: |
58 |
if len(shape)==0: |
59 |
out=l*random.random()+rng[0] |
60 |
elif len(shape)==1: |
61 |
for i0 in range(shape[0]): |
62 |
out[i0]=l*random.random()+rng[0] |
63 |
elif len(shape)==2: |
64 |
for i0 in range(shape[0]): |
65 |
for i1 in range(shape[1]): |
66 |
out[i0,i1]=l*random.random()+rng[0] |
67 |
elif len(shape)==3: |
68 |
for i0 in range(shape[0]): |
69 |
for i1 in range(shape[1]): |
70 |
for i2 in range(shape[2]): |
71 |
out[i0,i1,i2]=l*random.random()+rng[0] |
72 |
elif len(shape)==4: |
73 |
for i0 in range(shape[0]): |
74 |
for i1 in range(shape[1]): |
75 |
for i2 in range(shape[2]): |
76 |
for i3 in range(shape[3]): |
77 |
out[i0,i1,i2,i3]=l*random.random()+rng[0] |
78 |
elif len(shape)==5: |
79 |
for i0 in range(shape[0]): |
80 |
for i1 in range(shape[1]): |
81 |
for i2 in range(shape[2]): |
82 |
for i3 in range(shape[3]): |
83 |
for i4 in range(shape[4]): |
84 |
out[i0,i1,i2,i3,i4]=l*ranm.random()+rng[0] |
85 |
else: |
86 |
raise SystemError,"rank is restricted to 5" |
87 |
return out |
88 |
|
89 |
for sh0 in [ (3,), (3,4), (3,4,3) ,(4,3,5,3)]: |
90 |
# get perm: |
91 |
if len(sh0)==2: |
92 |
check=[[1,0]] |
93 |
elif len(sh0)==3: |
94 |
check=[[1,0,2], |
95 |
[1,2,0], |
96 |
[2,1,0], |
97 |
[2,0,2], |
98 |
[0,2,1]] |
99 |
elif len(sh0)==4: |
100 |
check=[[0,1,3,2], |
101 |
[0,2,1,3], |
102 |
[0,2,3,1], |
103 |
[0,3,2,1], |
104 |
[0,3,1,2] , |
105 |
[1,0,2,3], |
106 |
[1,0,3,2], |
107 |
[1,2,0,3], |
108 |
[1,2,3,0], |
109 |
[1,3,2,0], |
110 |
[1,3,0,2], |
111 |
[2,0,1,3], |
112 |
[2,0,3,1], |
113 |
[2,1,0,3], |
114 |
[2,1,3,0], |
115 |
[2,3,1,0], |
116 |
[2,3,0,1], |
117 |
[3,0,1,2], |
118 |
[3,0,2,1], |
119 |
[3,1,0,2], |
120 |
[3,1,2,0], |
121 |
[3,2,1,0], |
122 |
[3,2,0,1]] |
123 |
else: |
124 |
check=[] |
125 |
|
126 |
# create the test cases: |
127 |
processed=[] |
128 |
l=["R","U","L","P","C","N"] |
129 |
c=[""] |
130 |
for i in range(len(sh0)): |
131 |
tmp=[] |
132 |
for ci in c: |
133 |
tmp+=[ci+li for li in l] |
134 |
c=tmp |
135 |
# SHUFFLE |
136 |
c2=[] |
137 |
while len(c)>0: |
138 |
i=int(random.random()*len(c)) |
139 |
c2.append(c[i]) |
140 |
del c[i] |
141 |
c=c2 |
142 |
for ci in c: |
143 |
t="" |
144 |
sh=() |
145 |
for i in range(len(ci)): |
146 |
if ci[i]=="R": |
147 |
s="%s:%s"%(1,sh0[i]-1) |
148 |
sh=sh+(sh0[i]-2,) |
149 |
if ci[i]=="U": |
150 |
s=":%s"%(sh0[i]-1) |
151 |
sh=sh+(sh0[i]-1,) |
152 |
if ci[i]=="L": |
153 |
s="2:" |
154 |
sh=sh+(sh0[i]-2,) |
155 |
if ci[i]=="P": |
156 |
s="%s"%(int(sh0[i]/2)) |
157 |
if ci[i]=="C": |
158 |
s=":" |
159 |
sh=sh+(sh0[i],) |
160 |
if ci[i]=="N": |
161 |
s="" |
162 |
sh=sh+(sh0[i],) |
163 |
if len(s)>0: |
164 |
if not t=="": t+="," |
165 |
t+=s |
166 |
N_found=False |
167 |
noN_found=False |
168 |
process=len(t)>0 |
169 |
for i in ci: |
170 |
if i=="N": |
171 |
if not noN_found and N_found: process=False |
172 |
N_found=True |
173 |
else: |
174 |
if N_found: process=False |
175 |
noNfound=True |
176 |
# is there a similar one processed allready |
177 |
if process and ci.find("N")==-1: |
178 |
for ci2 in processed: |
179 |
for chi in check: |
180 |
is_perm=True |
181 |
for i in range(len(chi)): |
182 |
if not ci[i]==ci2[chi[i]]: is_perm=False |
183 |
if is_perm: process=False |
184 |
# if not process: print ci," rejected" |
185 |
if process: |
186 |
processed.append(ci) |
187 |
for sh_diff in [ (), (2,), (3,2)]: #, (2,3,4) ,(2,4,3,2)]: |
188 |
text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" |
189 |
tname="test_slice_rank%s_%s_D_rank%s"%(len(sh0),ci,len(sh_diff)) |
190 |
text+=" def %s(self):\n"%tname |
191 |
text+=" u=Symbol(%s)\n"%str(sh_diff) |
192 |
ind=[[]] |
193 |
for i in sh_diff: |
194 |
ind0=[] |
195 |
for j in ind: |
196 |
for k in range(i): ind0.append(j+[k]) |
197 |
ind=ind0 |
198 |
coeff={} |
199 |
for k in ind: |
200 |
coeff[tuple(k)]=makeArray(sh0,[-5,5]) |
201 |
a=makeArray(sh0,[-5,5]) |
202 |
text2="" |
203 |
if len(sh_diff)==0: |
204 |
text2+="u*numarray.array(%s)"%(coeff[tuple()].tolist()) |
205 |
else: |
206 |
for k in coeff.keys(): |
207 |
if len(text2)>0: text2+="+" |
208 |
text2+="u%s*numarray.array(%s)"%(list(k),coeff[k].tolist()) |
209 |
text+=" arg=%s\n"%text2 |
210 |
text+=" res=arg[%s]\n"%s |
211 |
text+=" dres=res.diff(u)\n" |
212 |
text+=" |
213 |
print text |
214 |
1/0 |