/[escript]/trunk/escript/py_src/generatediff
ViewVC logotype

Annotation of /trunk/escript/py_src/generatediff

Parent Directory Parent Directory | Revision Log Revision Log


Revision 157 - (hide annotations)
Wed Nov 9 10:01:06 2005 UTC (13 years, 11 months ago) by gross
Original Path: trunk/escript/py_src/generateutil
File size: 79524 byte(s)
initial checkin
1 jgs 154 #!/usr/bin/python
2 gross 157 # $Id:$
3 jgs 154
4     """
5     program generates parts of the util.py and the test_util.py script
6     """
7 gross 157 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+="from esys.finley import Rectangle\n"
12     test_header+="class Test_util2(unittest.TestCase):\n"
13     test_header+=" RES_TOL=1.e-7\n"
14     test_header+=" def setUp(self):\n"
15     test_header+=" self.__dom =Rectangle(11,11,2)\n"
16     test_header+=" self.functionspace = FunctionOnBoundary(self.__dom)\n"
17     test_tail=""
18     test_tail+="suite = unittest.TestSuite()\n"
19     test_tail+="suite.addTest(unittest.makeSuite(Test_util2))\n"
20     test_tail+="unittest.TextTestRunner(verbosity=2).run(suite)\n"
21    
22     case_set=["float","array","constData","taggedData","expandedData","Symbol"]
23     shape_set=[ (),(2,), (4,5), (6,2,2),(3,2,3,4)]
24    
25 jgs 154 t_prog=""
26 gross 157 t_prog_with_tags=""
27     t_prog_failing=""
28     u_prog=""
29 jgs 154
30 gross 157 def wherepos(arg):
31     if arg>0.:
32     return 1.
33     else:
34     return 0.
35    
36     class OPERATOR:
37     def __init__(self,nickname,rng=[-1000.,1000],test_expr="",math_expr=None,
38     numarray_expr="",symbol_expr=None,diff=None,name=""):
39     self.nickname=nickname
40     self.rng=rng
41     self.test_expr=test_expr
42     if math_expr==None:
43     self.math_expr=test_expr.replace("%a1%","arg")
44     else:
45     self.math_expr=math_expr
46     self.numarray_expr=numarray_expr
47     self.symbol_expr=symbol_expr
48     self.diff=diff
49     self.name=name
50    
51 jgs 154 import random
52     import numarray
53     import math
54     finc=1.e-6
55    
56 gross 157 def getResultCaseForBin(case0,case1):
57     if case0=="float":
58     if case1=="float":
59     case="float"
60     elif case1=="array":
61     case="array"
62     elif case1=="constData":
63     case="constData"
64     elif case1=="taggedData":
65     case="taggedData"
66     elif case1=="expandedData":
67     case="expandedData"
68     elif case1=="Symbol":
69     case="Symbol"
70     else:
71     raise ValueError,"unknown case1=%s"%case1
72     elif case0=="array":
73     if case1=="float":
74     case="array"
75     elif case1=="array":
76     case="array"
77     elif case1=="constData":
78     case="constData"
79     elif case1=="taggedData":
80     case="taggedData"
81     elif case1=="expandedData":
82     case="expandedData"
83     elif case1=="Symbol":
84     case="Symbol"
85     else:
86     raise ValueError,"unknown case1=%s"%case1
87     elif case0=="constData":
88     if case1=="float":
89     case="constData"
90     elif case1=="array":
91     case="constData"
92     elif case1=="constData":
93     case="constData"
94     elif case1=="taggedData":
95     case="taggedData"
96     elif case1=="expandedData":
97     case="expandedData"
98     elif case1=="Symbol":
99     case="Symbol"
100     else:
101     raise ValueError,"unknown case1=%s"%case1
102     elif case0=="taggedData":
103     if case1=="float":
104     case="taggedData"
105     elif case1=="array":
106     case="taggedData"
107     elif case1=="constData":
108     case="taggedData"
109     elif case1=="taggedData":
110     case="taggedData"
111     elif case1=="expandedData":
112     case="expandedData"
113     elif case1=="Symbol":
114     case="Symbol"
115     else:
116     raise ValueError,"unknown case1=%s"%case1
117     elif case0=="expandedData":
118     if case1=="float":
119     case="expandedData"
120     elif case1=="array":
121     case="expandedData"
122     elif case1=="constData":
123     case="expandedData"
124     elif case1=="taggedData":
125     case="expandedData"
126     elif case1=="expandedData":
127     case="expandedData"
128     elif case1=="Symbol":
129     case="Symbol"
130     else:
131     raise ValueError,"unknown case1=%s"%case1
132     elif case0=="Symbol":
133     if case1=="float":
134     case="Symbol"
135     elif case1=="array":
136     case="Symbol"
137     elif case1=="constData":
138     case="Symbol"
139     elif case1=="taggedData":
140     case="Symbol"
141     elif case1=="expandedData":
142     case="Symbol"
143     elif case1=="Symbol":
144     case="Symbol"
145     else:
146     raise ValueError,"unknown case1=%s"%case1
147     else:
148     raise ValueError,"unknown case0=%s"%case0
149     return case
150 jgs 154
151 gross 157
152 jgs 154 def makeArray(shape,rng):
153     l=rng[1]-rng[0]
154     out=numarray.zeros(shape,numarray.Float64)
155     if len(shape)==0:
156 gross 157 out=l*random.random()+rng[0]
157 jgs 154 elif len(shape)==1:
158     for i0 in range(shape[0]):
159     out[i0]=l*random.random()+rng[0]
160     elif len(shape)==2:
161     for i0 in range(shape[0]):
162     for i1 in range(shape[1]):
163     out[i0,i1]=l*random.random()+rng[0]
164     elif len(shape)==3:
165     for i0 in range(shape[0]):
166     for i1 in range(shape[1]):
167     for i2 in range(shape[2]):
168     out[i0,i1,i2]=l*random.random()+rng[0]
169     elif len(shape)==4:
170     for i0 in range(shape[0]):
171     for i1 in range(shape[1]):
172     for i2 in range(shape[2]):
173     for i3 in range(shape[3]):
174     out[i0,i1,i2,i3]=l*random.random()+rng[0]
175     else:
176     raise SystemError,"rank is restricted to 4"
177     return out
178    
179    
180 gross 157 def makeResult(val,test_expr):
181 jgs 154 if isinstance(val,float):
182 gross 157 out=eval(test_expr.replace("%a1%","val"))
183 jgs 154 elif len(val.shape)==0:
184 gross 157 out=eval(test_expr.replace("%a1%","val"))
185 jgs 154 elif len(val.shape)==1:
186     out=numarray.zeros(val.shape,numarray.Float64)
187     for i0 in range(val.shape[0]):
188 gross 157 out[i0]=eval(test_expr.replace("%a1%","val[i0]"))
189 jgs 154 elif len(val.shape)==2:
190     out=numarray.zeros(val.shape,numarray.Float64)
191     for i0 in range(val.shape[0]):
192     for i1 in range(val.shape[1]):
193 gross 157 out[i0,i1]=eval(test_expr.replace("%a1%","val[i0,i1]"))
194 jgs 154 elif len(val.shape)==3:
195     out=numarray.zeros(val.shape,numarray.Float64)
196     for i0 in range(val.shape[0]):
197     for i1 in range(val.shape[1]):
198     for i2 in range(val.shape[2]):
199 gross 157 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val[i0,i1,i2]"))
200 jgs 154 elif len(val.shape)==4:
201     out=numarray.zeros(val.shape,numarray.Float64)
202     for i0 in range(val.shape[0]):
203     for i1 in range(val.shape[1]):
204     for i2 in range(val.shape[2]):
205     for i3 in range(val.shape[3]):
206 gross 157 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val[i0,i1,i2,i3]"))
207 jgs 154 else:
208     raise SystemError,"rank is restricted to 4"
209     return out
210    
211 gross 157 def makeResult2(val0,val1,test_expr):
212     if isinstance(val0,float):
213     if isinstance(val1,float):
214     out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
215     elif len(val1.shape)==0:
216     out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
217     elif len(val1.shape)==1:
218     out=numarray.zeros(val1.shape,numarray.Float64)
219     for i0 in range(val1.shape[0]):
220     out[i0]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0]"))
221     elif len(val1.shape)==2:
222     out=numarray.zeros(val1.shape,numarray.Float64)
223     for i0 in range(val1.shape[0]):
224     for i1 in range(val1.shape[1]):
225     out[i0,i1]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1]"))
226     elif len(val1.shape)==3:
227     out=numarray.zeros(val1.shape,numarray.Float64)
228     for i0 in range(val1.shape[0]):
229     for i1 in range(val1.shape[1]):
230     for i2 in range(val1.shape[2]):
231     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2]"))
232     elif len(val1.shape)==4:
233     out=numarray.zeros(val1.shape,numarray.Float64)
234     for i0 in range(val1.shape[0]):
235     for i1 in range(val1.shape[1]):
236     for i2 in range(val1.shape[2]):
237     for i3 in range(val1.shape[3]):
238     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2,i3]"))
239     else:
240     raise SystemError,"rank of val1 is restricted to 4"
241     elif len(val0.shape)==0:
242     if isinstance(val1,float):
243     out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
244     elif len(val1.shape)==0:
245     out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
246     elif len(val1.shape)==1:
247     out=numarray.zeros(val1.shape,numarray.Float64)
248     for i0 in range(val1.shape[0]):
249     out[i0]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0]"))
250     elif len(val1.shape)==2:
251     out=numarray.zeros(val1.shape,numarray.Float64)
252     for i0 in range(val1.shape[0]):
253     for i1 in range(val1.shape[1]):
254     out[i0,i1]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1]"))
255     elif len(val1.shape)==3:
256     out=numarray.zeros(val1.shape,numarray.Float64)
257     for i0 in range(val1.shape[0]):
258     for i1 in range(val1.shape[1]):
259     for i2 in range(val1.shape[2]):
260     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2]"))
261     elif len(val1.shape)==4:
262     out=numarray.zeros(val1.shape,numarray.Float64)
263     for i0 in range(val1.shape[0]):
264     for i1 in range(val1.shape[1]):
265     for i2 in range(val1.shape[2]):
266     for i3 in range(val1.shape[3]):
267     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2,i3]"))
268     else:
269     raise SystemError,"rank of val1 is restricted to 4"
270     elif len(val0.shape)==1:
271     if isinstance(val1,float):
272     out=numarray.zeros(val0.shape,numarray.Float64)
273     for i0 in range(val0.shape[0]):
274     out[i0]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1"))
275     elif len(val1.shape)==0:
276     out=numarray.zeros(val0.shape,numarray.Float64)
277     for i0 in range(val0.shape[0]):
278     out[i0]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1"))
279     elif len(val1.shape)==1:
280     out=numarray.zeros(val0.shape,numarray.Float64)
281     for i0 in range(val0.shape[0]):
282     out[i0]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0]"))
283     elif len(val1.shape)==2:
284     out=numarray.zeros(val0.shape+val1.shape[1:],numarray.Float64)
285     for i0 in range(val0.shape[0]):
286     for j1 in range(val1.shape[1]):
287     out[i0,j1]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0,j1]"))
288     elif len(val1.shape)==3:
289     out=numarray.zeros(val0.shape+val1.shape[1:],numarray.Float64)
290     for i0 in range(val0.shape[0]):
291     for j1 in range(val1.shape[1]):
292     for j2 in range(val1.shape[2]):
293     out[i0,j1,j2]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0,j1,j2]"))
294     elif len(val1.shape)==4:
295     out=numarray.zeros(val0.shape+val1.shape[1:],numarray.Float64)
296     for i0 in range(val0.shape[0]):
297     for j1 in range(val1.shape[1]):
298     for j2 in range(val1.shape[2]):
299     for j3 in range(val1.shape[3]):
300     out[i0,j1,j2,j3]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0,j1,j2,j3]"))
301     else:
302     raise SystemError,"rank of val1 is restricted to 4"
303     elif len(val0.shape)==2:
304     if isinstance(val1,float):
305     out=numarray.zeros(val0.shape,numarray.Float64)
306     for i0 in range(val0.shape[0]):
307     for i1 in range(val0.shape[1]):
308     out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1"))
309     elif len(val1.shape)==0:
310     out=numarray.zeros(val0.shape,numarray.Float64)
311     for i0 in range(val0.shape[0]):
312     for i1 in range(val0.shape[1]):
313     out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1"))
314     elif len(val1.shape)==1:
315     out=numarray.zeros(val0.shape,numarray.Float64)
316     for i0 in range(val0.shape[0]):
317     for i1 in range(val0.shape[1]):
318     out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0]"))
319     elif len(val1.shape)==2:
320     out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
321     for i0 in range(val0.shape[0]):
322     for i1 in range(val0.shape[1]):
323     out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0,i1]"))
324     elif len(val1.shape)==3:
325     out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
326     for i0 in range(val0.shape[0]):
327     for i1 in range(val0.shape[1]):
328     for j2 in range(val1.shape[2]):
329     out[i0,i1,j2]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0,i1,j2]"))
330     elif len(val1.shape)==4:
331     out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
332     for i0 in range(val0.shape[0]):
333     for i1 in range(val0.shape[1]):
334     for j2 in range(val1.shape[2]):
335     for j3 in range(val1.shape[3]):
336     out[i0,i1,j2,j3]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0,i1,j2,j3]"))
337     else:
338     raise SystemError,"rank of val1 is restricted to 4"
339     elif len(val0.shape)==3:
340     if isinstance(val1,float):
341     out=numarray.zeros(val0.shape,numarray.Float64)
342     for i0 in range(val0.shape[0]):
343     for i1 in range(val0.shape[1]):
344     for i2 in range(val0.shape[2]):
345     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1"))
346     elif len(val1.shape)==0:
347     out=numarray.zeros(val0.shape,numarray.Float64)
348     for i0 in range(val0.shape[0]):
349     for i1 in range(val0.shape[1]):
350     for i2 in range(val0.shape[2]):
351     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1"))
352     elif len(val1.shape)==1:
353     out=numarray.zeros(val0.shape,numarray.Float64)
354     for i0 in range(val0.shape[0]):
355     for i1 in range(val0.shape[1]):
356     for i2 in range(val0.shape[2]):
357     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0]"))
358     elif len(val1.shape)==2:
359     out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
360     for i0 in range(val0.shape[0]):
361     for i1 in range(val0.shape[1]):
362     for i2 in range(val0.shape[2]):
363     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0,i1]"))
364     elif len(val1.shape)==3:
365     out=numarray.zeros(val0.shape,numarray.Float64)
366     for i0 in range(val0.shape[0]):
367     for i1 in range(val0.shape[1]):
368     for i2 in range(val0.shape[2]):
369     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0,i1,i2]"))
370     elif len(val1.shape)==4:
371     out=numarray.zeros(val0.shape+val1.shape[3:],numarray.Float64)
372     for i0 in range(val0.shape[0]):
373     for i1 in range(val0.shape[1]):
374     for i2 in range(val0.shape[2]):
375     for j3 in range(val1.shape[3]):
376     out[i0,i1,i2,j3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0,i1,i2,j3]"))
377     else:
378     raise SystemError,"rank of val1 is rargs[1]estricted to 4"
379     elif len(val0.shape)==4:
380     if isinstance(val1,float):
381     out=numarray.zeros(val0.shape,numarray.Float64)
382     for i0 in range(val0.shape[0]):
383     for i1 in range(val0.shape[1]):
384     for i2 in range(val0.shape[2]):
385     for i3 in range(val0.shape[3]):
386     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1"))
387     elif len(val1.shape)==0:
388     out=numarray.zeros(val0.shape,numarray.Float64)
389     for i0 in range(val0.shape[0]):
390     for i1 in range(val0.shape[1]):
391     for i2 in range(val0.shape[2]):
392     for i3 in range(val0.shape[3]):
393     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1"))
394     elif len(val1.shape)==1:
395     out=numarray.zeros(val0.shape,numarray.Float64)
396     for i0 in range(val0.shape[0]):
397     for i1 in range(val0.shape[1]):
398     for i2 in range(val0.shape[2]):
399     for i3 in range(val0.shape[3]):
400     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0]"))
401     elif len(val1.shape)==2:
402     out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
403     for i0 in range(val0.shape[0]):
404     for i1 in range(val0.shape[1]):
405     for i2 in range(val0.shape[2]):
406     for i3 in range(val0.shape[3]):
407     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0,i1]"))
408     elif len(val1.shape)==3:
409     out=numarray.zeros(val0.shape,numarray.Float64)
410     for i0 in range(val0.shape[0]):
411     for i1 in range(val0.shape[1]):
412     for i2 in range(val0.shape[2]):
413     for i3 in range(val0.shape[3]):
414     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0,i1,i2]"))
415     elif len(val1.shape)==4:
416     out=numarray.zeros(val0.shape,numarray.Float64)
417     for i0 in range(val0.shape[0]):
418     for i1 in range(val0.shape[1]):
419     for i2 in range(val0.shape[2]):
420     for i3 in range(val0.shape[3]):
421     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0,i1,i2,i3]"))
422     else:
423     raise SystemError,"rank of val1 is restricted to 4"
424     else:
425     raise SystemError,"rank is restricted to 4"
426     return out
427 jgs 154
428 gross 157
429     def mkText(case,name,a,a1=None,use_tagging_for_expanded_data=False):
430 jgs 154 t_out=""
431 gross 157 if case=="float":
432 jgs 154 if isinstance(a,float):
433     t_out+=" %s=%s\n"%(name,a)
434     elif len(a)==1:
435     t_out+=" %s=%s\n"%(name,a)
436     else:
437     t_out+=" %s=numarray.array(%s)\n"%(name,a.tolist())
438 gross 157 elif case=="array":
439     if isinstance(a,float):
440     t_out+=" %s=numarray.array(%s)\n"%(name,a)
441     elif len(a)==1:
442     t_out+=" %s=numarray.array(%s)\n"%(name,a)
443     else:
444     t_out+=" %s=numarray.array(%s)\n"%(name,a.tolist())
445 jgs 154 elif case=="constData":
446     if isinstance(a,float):
447     t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
448     elif len(a)==1:
449     t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
450     else:
451     t_out+=" %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
452     elif case=="taggedData":
453     if isinstance(a,float):
454     t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
455     t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
456     elif len(a)==1:
457     t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
458     t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
459     else:
460     t_out+=" %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
461 gross 157 t_out+=" %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())
462 jgs 154 elif case=="expandedData":
463 gross 157 if use_tagging_for_expanded_data:
464     if isinstance(a,float):
465     t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
466     t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
467     elif len(a)==1:
468     t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
469     t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
470     else:
471     t_out+=" %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
472     t_out+=" %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())
473     t_out+=" %s.expand()\n"%name
474     else:
475     t_out+=" msk_%s=whereNegative(self.functionspace.getX()[0]-0.5)\n"%name
476     if isinstance(a,float):
477     t_out+=" %s=msk_%s*(%s)+(1.-msk_%s)*(%s)\n"%(name,name,a,name,a1)
478     elif len(a)==1:
479     t_out+=" %s=msk_%s*numarray.array(%s)+(1.-msk_%s)*numarray.array(%s)\n"%(name,name,a,name,a1)
480     else:
481     t_out+=" %s=msk_%s*numarray.array(%s)+(1.-msk_%s)*numarray.array(%s)\n"%(name,name,a.tolist(),name,a1.tolist())
482 jgs 154 elif case=="Symbol":
483     if isinstance(a,float):
484     t_out+=" %s=Symbol(shape=())\n"%(name)
485     elif len(a)==1:
486     t_out+=" %s=Symbol(shape=())\n"%(name)
487     else:
488     t_out+=" %s=Symbol(shape=%s)\n"%(name,str(a.shape))
489    
490     return t_out
491    
492 gross 157 def mkTypeAndShapeTest(case,sh,argstr):
493     text=""
494     if case=="float":
495     text+=" self.failUnless(isinstance(%s,float),\"wrong type of result.\")\n"%argstr
496     elif case=="array":
497     text+=" self.failUnless(isinstance(%s,numarray.NumArray),\"wrong type of result.\")\n"%argstr
498     text+=" self.failUnlessEqual(%s.shape,%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
499     elif case in ["constData","taggedData","expandedData"]:
500     text+=" self.failUnless(isinstance(%s,Data),\"wrong type of result.\")\n"%argstr
501     text+=" self.failUnlessEqual(%s.getShape(),%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
502     elif case=="Symbol":
503     text+=" self.failUnless(isinstance(%s,Symbol),\"wrong type of result.\")\n"%argstr
504     text+=" self.failUnlessEqual(%s.getShape(),%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
505     return text
506 jgs 154
507 gross 157 def mkCode(txt,args=[],intend=""):
508     s=txt.split("\n")
509     if len(s)>1:
510     out=""
511     for l in s:
512     out+=intend+l+"\n"
513     else:
514     out="%sreturn %s\n"%(intend,txt)
515     c=1
516     for r in args:
517     out=out.replace("%%a%s%%"%c,r)
518     return out
519 jgs 154
520 gross 157 #=======================================================================================================
521     # basic binary operations (tests only!)
522     #=======================================================================================================
523     oper_range=[-5.,5.]
524     for oper in [["add" ,"+",[-5.,5.]],
525     ["mult","*",[-5.,5.]],
526     ["quotient" ,"/",[-5.,5.]],
527     ["power" ,"**",[0.01,5.]]]:
528     for case0 in case_set:
529     for case1 in case_set:
530     for sh in shape_set:
531     for sh_p in shape_set:
532     if len(sh_p)>0:
533     resource=[-1,1]
534     else:
535     resource=[1]
536     for sh_d in resource:
537     if sh_d>0:
538     sh0=sh
539     sh1=sh+sh_p
540 jgs 154 else:
541 gross 157 sh1=sh
542     sh0=sh+sh_p
543 jgs 154
544 gross 157 if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
545     len(sh0)<5 and len(sh1)<5:
546     use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
547     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
548     tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
549     text+=" def %s(self):\n"%tname
550     a_0=makeArray(sh0,oper[2])
551     if case0 in ["taggedData", "expandedData"]:
552     a1_0=makeArray(sh0,oper[2])
553     else:
554     a1_0=a_0
555 jgs 154
556 gross 157 a_1=makeArray(sh1,oper[2])
557     if case1 in ["taggedData", "expandedData"]:
558     a1_1=makeArray(sh1,oper[2])
559     else:
560     a1_1=a_1
561     r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
562     r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
563     text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
564     text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
565     text+=" res=%s(arg0,arg1)\n"%oper[0]
566    
567     case=getResultCaseForBin(case0,case1)
568     if case=="Symbol":
569     c0_res,c1_res=case0,case1
570     subs="{"
571     if case0=="Symbol":
572     text+=mkText("array","s0",a_0,a1_0)
573     subs+="arg0:s0"
574     c0_res="array"
575     if case1=="Symbol":
576     text+=mkText("array","s1",a_1,a1_1)
577     if not subs.endswith("{"): subs+=","
578     subs+="arg1:s1"
579     c1_res="array"
580     subs+="}"
581     text+=" sub=res.substitute(%s)\n"%subs
582     res="sub"
583     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
584     else:
585     res="res"
586     text+=mkText(case,"ref",r,r1)
587     if isinstance(r,float):
588     text+=mkTypeAndShapeTest(case,(),"res")
589     else:
590     text+=mkTypeAndShapeTest(case,r.shape,"res")
591     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
592    
593     if case0 == "taggedData" or case1 == "taggedData":
594     t_prog_with_tags+=text
595     else:
596     t_prog+=text
597     print test_header
598     # print t_prog
599     print t_prog_with_tags
600     print test_tail
601 jgs 154 1/0
602    
603 gross 157 #=======================================================================================================
604     # basic binary operation overloading (tests only!)
605     #=======================================================================================================
606     oper_range=[-5.,5.]
607     for oper in [["add" ,"+",[-5.,5.]],
608     ["sub" ,"-",[-5.,5.]],
609     ["mult","*",[-5.,5.]],
610     ["div" ,"/",[-5.,5.]],
611     ["pow" ,"**",[0.01,5.]]]:
612     for case0 in case_set:
613     for sh0 in shape_set:
614     for case1 in case_set:
615     for sh1 in shape_set:
616     if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
617     (sh0==() or sh1==() or sh1==sh0) and \
618     not (case0 in ["float","array"] and case1 in ["float","array"]):
619     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
620     tname="test_%s_overloaded_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
621     text+=" def %s(self):\n"%tname
622     a_0=makeArray(sh0,oper[2])
623     if case0 in ["taggedData", "expandedData"]:
624     a1_0=makeArray(sh0,oper[2])
625     else:
626     a1_0=a_0
627 jgs 154
628 gross 157 a_1=makeArray(sh1,oper[2])
629     if case1 in ["taggedData", "expandedData"]:
630     a1_1=makeArray(sh1,oper[2])
631 jgs 154 else:
632 gross 157 a1_1=a_1
633     r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
634     r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
635     text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
636     text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
637     text+=" res=arg0%sarg1\n"%oper[1]
638    
639     case=getResultCaseForBin(case0,case1)
640     if case=="Symbol":
641     c0_res,c1_res=case0,case1
642     subs="{"
643     if case0=="Symbol":
644     text+=mkText("array","s0",a_0,a1_0)
645     subs+="arg0:s0"
646     c0_res="array"
647     if case1=="Symbol":
648     text+=mkText("array","s1",a_1,a1_1)
649     if not subs.endswith("{"): subs+=","
650     subs+="arg1:s1"
651     c1_res="array"
652     subs+="}"
653     text+=" sub=res.substitute(%s)\n"%subs
654     res="sub"
655     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
656 jgs 154 else:
657 gross 157 res="res"
658     text+=mkText(case,"ref",r,r1)
659     if isinstance(r,float):
660     text+=mkTypeAndShapeTest(case,(),"res")
661     else:
662     text+=mkTypeAndShapeTest(case,r.shape,"res")
663     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
664    
665     if case0 in [ "constData","taggedData","expandedData"] and case1 == "Symbol":
666     t_prog_failing+=text
667     else:
668     if case0 == "taggedData" or case1 == "taggedData":
669     t_prog_with_tags+=text
670     else:
671     t_prog+=text
672 jgs 154
673 gross 157
674     # print u_prog
675     # 1/0
676     print test_header
677 jgs 154 print t_prog
678     1/0
679 gross 157 # print t_prog_with_tagsoper_range=[-5.,5.]
680     for oper in [["add" ,"+",[-5.,5.]],
681     ["sub" ,"-",[-5.,5.]],
682     ["mult","*",[-5.,5.]],
683     ["div" ,"/",[-5.,5.]],
684     ["pow" ,"**",[0.01,5.]]]:
685     for case0 in case_set:
686     for sh0 in shape_set:
687     for case1 in case_set:
688     for sh1 in shape_set:
689     if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
690     (sh0==() or sh1==() or sh1==sh0) and \
691     not (case0 in ["float","array"] and case1 in ["float","array"]):
692     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
693     tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
694     text+=" def %s(self):\n"%tname
695     a_0=makeArray(sh0,oper[2])
696     if case0 in ["taggedData", "expandedData"]:
697     a1_0=makeArray(sh0,oper[2])
698     else:
699     a1_0=a_0
700 jgs 154
701 gross 157 a_1=makeArray(sh1,oper[2])
702     if case1 in ["taggedData", "expandedData"]:
703     a1_1=makeArray(sh1,oper[2])
704 jgs 154 else:
705 gross 157 a1_1=a_1
706     r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
707     r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
708     text+=mkText(case0,"arg0",a_0,a1_0)
709     text+=mkText(case1,"arg1",a_1,a1_1)
710     text+=" res=arg0%sarg1\n"%oper[1]
711    
712     case=getResultCaseForBin(case0,case1)
713     if case=="Symbol":
714     c0_res,c1_res=case0,case1
715     subs="{"
716     if case0=="Symbol":
717     text+=mkText("array","s0",a_0,a1_0)
718     subs+="arg0:s0"
719     c0_res="array"
720     if case1=="Symbol":
721     text+=mkText("array","s1",a_1,a1_1)
722     if not subs.endswith("{"): subs+=","
723     subs+="arg1:s1"
724     c1_res="array"
725     subs+="}"
726     text+=" sub=res.substitute(%s)\n"%subs
727     res="sub"
728     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
729 jgs 154 else:
730 gross 157 res="res"
731     text+=mkText(case,"ref",r,r1)
732     if isinstance(r,float):
733     text+=mkTypeAndShapeTest(case,(),"res")
734     else:
735     text+=mkTypeAndShapeTest(case,r.shape,"res")
736     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
737    
738     if case0 in [ "constData","taggedData","expandedData"] and case1 == "Symbol":
739     t_prog_failing+=text
740     else:
741     if case0 == "taggedData" or case1 == "taggedData":
742     t_prog_with_tags+=text
743     else:
744     t_prog+=text
745 jgs 154
746 gross 157
747     # print u_prog
748     # 1/0
749     print test_header
750 jgs 154 print t_prog
751 gross 157 # print t_prog_with_tags
752     # print t_prog_failing
753     print test_tail
754     # print t_prog_failing
755     print test_tail
756 jgs 154
757 gross 157 #=======================================================================================================
758     # unary operations:
759     #=======================================================================================================
760     func= [
761     OPERATOR(nickname="log10",\
762     rng=[1.e-3,100.],\
763     test_expr="math.log10(%a1%)",\
764     math_expr="math.log10(%a1%)",\
765     numarray_expr="numarray.log10(%a1%)",\
766     symbol_expr="log(%a1%)/log(10.)",\
767     name="base-10 logarithm"),
768     OPERATOR(nickname="wherePositive",\
769     rng=[-100.,100.],\
770     test_expr="wherepos(%a1%)",\
771     math_expr="if arg>0:\n return 1.\nelse:\n return 0.",
772     numarray_expr="numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))",\
773     name="mask of positive values"),
774     OPERATOR(nickname="whereNegative",\
775     rng=[-100.,100.],\
776     test_expr="wherepos(-%a1%)",\
777     math_expr="if arg<0:\n return 1.\nelse:\n return 0.",
778     numarray_expr="numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))",\
779     name="mask of positive values"),
780     OPERATOR(nickname="whereNonNegative",\
781     rng=[-100.,100.],\
782     test_expr="1-wherepos(-%a1%)", \
783     math_expr="if arg<0:\n return 0.\nelse:\n return 1.",
784     numarray_expr="numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))",\
785     symbol_expr="1-wherePositive(%a1%)",\
786     name="mask of non-negative values"),
787     OPERATOR(nickname="whereNonPositive",\
788     rng=[-100.,100.],\
789     test_expr="1-wherepos(%a1%)",\
790     math_expr="if arg>0:\n return 0.\nelse:\n return 1.",
791     numarray_expr="numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))",\
792     symbol_expr="1-whereNegative(%a1%)",\
793     name="mask of non-positive values"),
794     OPERATOR(nickname="whereZero",\
795     rng=[-100.,100.],\
796     test_expr="1-wherepos(%a1%)-wherepos(-%a1%)",\
797     math_expr="if abs(%a1%)<=tol:\n return 1.\nelse:\n return 0.",
798     numarray_expr="numarray.less_equal(abs(%a1%)-tol,numarray.zeros(arg.shape,numarray.Float))",\
799     name="mask of zero entries"),
800     OPERATOR(nickname="whereNonZero",\
801     rng=[-100.,100.],\
802     test_expr="wherepos(%a1%)+wherepos(-%a1%)",\
803     math_expr="if abs(%a1%)>tol:\n return 1.\nelse:\n return 0.",\
804     numarray_expr="numarray.greater(abs(%a1%)-tol,numarray.zeros(arg.shape,numarray.Float))",\
805     symbol_expr="1-whereZero(arg,tol)",\
806     name="mask of values different from zero"),
807     OPERATOR(nickname="sin",\
808     rng=[-100.,100.],\
809     test_expr="math.sin(%a1%)",
810     numarray_expr="numarray.sin(%a1%)",\
811     diff="cos(%a1%)",\
812     name="sine"),
813     OPERATOR(nickname="cos",\
814     rng=[-100.,100.],\
815     test_expr="math.cos(%a1%)",
816     numarray_expr="numarray.cos(%a1%)",\
817     diff="-sin(%a1%)",
818     name="cosine"),
819     OPERATOR(nickname="tan",\
820     rng=[-100.,100.],\
821     test_expr="math.tan(%a1%)",
822     numarray_expr="numarray.tan(%a1%)",\
823     diff="1./cos(%a1%)**2",
824     name="tangent"),
825     OPERATOR(nickname="asin",\
826     rng=[-0.99,0.99],\
827     test_expr="math.asin(%a1%)",
828     numarray_expr="numarray.arcsin(%a1%)",
829     diff="1./sqrt(1.-%a1%**2)",
830     name="inverse sine"),
831     OPERATOR(nickname="acos",\
832     rng=[-0.99,0.99],\
833     test_expr="math.acos(%a1%)",
834     numarray_expr="numarray.arccos(%a1%)",
835     diff="-1./sqrt(1.-%a1%**2)",
836     name="inverse cosine"),
837     OPERATOR(nickname="atan",\
838     rng=[-100.,100.],\
839     test_expr="math.atan(%a1%)",
840     numarray_expr="numarray.arctan(%a1%)",
841     diff="1./(1+%a1%**2)",
842     name="inverse tangent"),
843     OPERATOR(nickname="sinh",\
844     rng=[-5,5],\
845     test_expr="math.sinh(%a1%)",
846     numarray_expr="numarray.sinh(%a1%)",
847     diff="cosh(%a1%)",
848     name="hyperbolic sine"),
849     OPERATOR(nickname="cosh",\
850     rng=[-5.,5.],
851     test_expr="math.cosh(%a1%)",
852     numarray_expr="numarray.cosh(%a1%)",
853     diff="sinh(%a1%)",
854     name="hyperbolic cosine"),
855     OPERATOR(nickname="tanh",\
856     rng=[-5.,5.],
857     test_expr="math.tanh(%a1%)",
858     numarray_expr="numarray.tanh(%a1%)",
859     diff="1./cosh(%a1%)**2",
860     name="hyperbolic tangent"),
861     OPERATOR(nickname="asinh",\
862     rng=[-100.,100.], \
863     test_expr="numarray.arcsinh(%a1%)",
864     math_expr="numarray.arcsinh(%a1%)",
865     numarray_expr="numarray.arcsinh(%a1%)",
866     diff="1./sqrt(%a1%**2+1)",
867     name="inverse hyperbolic sine"),
868     OPERATOR(nickname="acosh",\
869     rng=[1.001,100.],\
870     test_expr="numarray.arccosh(%a1%)",
871     math_expr="numarray.arccosh(%a1%)",
872     numarray_expr="numarray.arccosh(%a1%)",
873     diff="1./sqrt(%a1%**2-1)",
874     name="inverse hyperolic cosine"),
875     OPERATOR(nickname="atanh",\
876     rng=[-0.99,0.99], \
877     test_expr="numarray.arctanh(%a1%)",
878     math_expr="numarray.arctanh(%a1%)",
879     numarray_expr="numarray.arctanh(%a1%)",
880     diff="1./(1.-%a1%**2)",
881     name="inverse hyperbolic tangent"),
882     OPERATOR(nickname="exp",\
883     rng=[-5.,5.],
884     test_expr="math.exp(%a1%)",
885     numarray_expr="numarray.exp(%a1%)",
886     diff="self",
887     name="exponential"),
888     OPERATOR(nickname="sqrt",\
889     rng=[1.e-3,100.],\
890     test_expr="math.sqrt(%a1%)",
891     numarray_expr="numarray.sqrt(%a1%)",
892     diff="0.5/self",
893     name="square root"),
894     OPERATOR(nickname="log", \
895     rng=[1.e-3,100.],\
896     test_expr="math.log(%a1%)",
897     numarray_expr="numarray.log(%a1%)",
898     diff="1./arg",
899     name="natural logarithm"),
900     OPERATOR(nickname="sign",\
901     rng=[-100.,100.], \
902     math_expr="if %a1%>0:\n return 1.\nelif %a1%<0:\n return -1.\nelse:\n return 0.",
903     test_expr="wherepos(%a1%)-wherepos(-%a1%)",
904     numarray_expr="numarray.sign(%a1%)",
905     symbol_expr="wherePositive(%a1%)-whereNegative(%a1%)",\
906     name="sign"),
907     OPERATOR(nickname="abs",\
908     rng=[-100.,100.], \
909     math_expr="if %a1%>0:\n return %a1% \nelif %a1%<0:\n return -(%a1%)\nelse:\n return 0.",
910     test_expr="wherepos(%a1%)*(%a1%)-wherepos(-%a1%)*(%a1%)",
911     numarray_expr="abs(%a1%)",
912     diff="sign(%a1%)",
913     name="absolute value")
914    
915     ]
916     for f in func:
917     symbol_name=f.nickname[0].upper()+f.nickname[1:]
918     if f.nickname!="abs":
919     u_prog+="def %s(arg):\n"%f.nickname
920     u_prog+=" \"\"\"\n"
921     u_prog+=" returns %s of argument arg\n\n"%f.name
922     u_prog+=" @param arg: argument\n"
923     u_prog+=" @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.\n"
924     u_prog+=" @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.\n"
925     u_prog+=" @raises TypeError: if the type of the argument is not expected.\n"
926     u_prog+=" \"\"\"\n"
927     u_prog+=" if isinstance(arg,numarray.NumArray):\n"
928     u_prog+=mkCode(f.numarray_expr,["arg"],2*" ")
929     u_prog+=" elif isinstance(arg,escript.Data):\n"
930     u_prog+=mkCode("arg._%s()"%f.nickname,[],2*" ")
931     u_prog+=" elif isinstance(arg,float):\n"
932     u_prog+=mkCode(f.math_expr,["arg"],2*" ")
933     u_prog+=" elif isinstance(arg,int):\n"
934     u_prog+=mkCode(f.math_expr,["float(arg)"],2*" ")
935     u_prog+=" elif isinstance(arg,Symbol):\n"
936     if f.symbol_expr==None:
937     u_prog+=mkCode("%s_Symbol(arg)"%symbol_name,[],2*" ")
938     else:
939     u_prog+=mkCode(f.symbol_expr,["arg"],2*" ")
940     u_prog+=" else:\n"
941     u_prog+=" raise TypeError,\"%s: Unknown argument type.\"\n\n"%f.nickname
942     if f.symbol_expr==None:
943     u_prog+="class %s_Symbol(DependendSymbol):\n"%symbol_name
944     u_prog+=" \"\"\"\n"
945     u_prog+=" L{Symbol} representing the result of the %s function\n"%f.name
946     u_prog+=" \"\"\"\n"
947     u_prog+=" def __init__(self,arg):\n"
948     u_prog+=" \"\"\"\n"
949     u_prog+=" initialization of %s L{Symbol} with argument arg\n"%f.nickname
950     u_prog+=" @param arg: argument of function\n"
951     u_prog+=" @type arg: typically L{Symbol}.\n"
952     u_prog+=" \"\"\"\n"
953     u_prog+=" DependendSymbol.__init__(self,args=[arg],shape=arg.getShape(),dim=arg.getDim())\n"
954     u_prog+="\n"
955    
956     u_prog+=" def getMyCode(self,argstrs,format=\"escript\"):\n"
957     u_prog+=" \"\"\"\n"
958     u_prog+=" returns a program code that can be used to evaluate the symbol.\n\n"
959 jgs 154
960 gross 157 u_prog+=" @param argstrs: gives for each argument a string representing the argument for the evaluation.\n"
961     u_prog+=" @type argstrs: C{str} or a C{list} of length 1 of C{str}.\n"
962     u_prog+=" @param format: specifies the format to be used. At the moment only \"escript\" ,\"text\" and \"str\" are supported.\n"
963     u_prog+=" @type format: C{str}\n"
964     u_prog+=" @return: a piece of program code which can be used to evaluate the expression assuming the values for the arguments are available.\n"
965     u_prog+=" @rtype: C{str}\n"
966     u_prog+=" @raise: NotImplementedError: if the requested format is not available\n"
967     u_prog+=" \"\"\"\n"
968     u_prog+=" if isinstance(argstrs,list):\n"
969     u_prog+=" argstrs=argstrs[0]\n"
970     u_prog+=" if format==\"escript\" or format==\"str\" or format==\"text\":\n"
971     u_prog+=" return \"%s(%%s)\"%%argstrs\n"%f.nickname
972     u_prog+=" else:\n"
973     u_prog+=" raise NotImplementedError,\"%s_Symbol does not provide program code for format %%s.\"%%format\n"%symbol_name
974     u_prog+="\n"
975 jgs 154
976 gross 157 u_prog+=" def substitute(self,argvals):\n"
977     u_prog+=" \"\"\"\n"
978     u_prog+=" assigns new values to symbols in the definition of the symbol.\n"
979     u_prog+=" The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.\n"
980     u_prog+="\n"
981     u_prog+=" @param argvals: new values assigned to symbols\n"
982     u_prog+=" @type argvals: C{dict} with keywords of type L{Symbol}.\n"
983     u_prog+=" @return: result of the substitution process. Operations are executed as much as possible.\n"
984     u_prog+=" @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution\n"
985     u_prog+=" @raise TypeError: if a value for a L{Symbol} cannot be substituted.\n"
986     u_prog+=" \"\"\"\n"
987     u_prog+=" if argvals.has_key(self):\n"
988     u_prog+=" arg=argvals[self]\n"
989     u_prog+=" if self.isAppropriateValue(arg):\n"
990     u_prog+=" return arg\n"
991     u_prog+=" else:\n"
992     u_prog+=" raise TypeError,\"%s: new value is not appropriate.\"%str(self)\n"
993     u_prog+=" else:\n"
994     u_prog+=" arg=self.getSubstitutedArguments(argvals)[0]\n"
995     u_prog+=" return %s(arg)\n\n"%f.nickname
996     if not f.diff==None:
997     u_prog+=" def diff(self,arg):\n"
998     u_prog+=" \"\"\"\n"
999     u_prog+=" differential of this object\n"
1000     u_prog+="\n"
1001     u_prog+=" @param arg: the derivative is calculated with respect to arg\n"
1002     u_prog+=" @type arg: L{escript.Symbol}\n"
1003     u_prog+=" @return: derivative with respect to C{arg}\n"
1004     u_prog+=" @rtype: typically L{Symbol} but other types such as C{float}, L{escript.Data}, L{numarray.NumArray} are possible.\n"
1005     u_prog+=" \"\"\"\n"
1006     u_prog+=" if arg==self:\n"
1007     u_prog+=" return identity(self.getShape())\n"
1008     u_prog+=" else:\n"
1009     u_prog+=" myarg=self.getArgument()[0]\n"
1010     u_prog+=" val=matchShape(%s,self.getDifferentiatedArguments(arg)[0])\n"%f.diff.replace("%a1%","myarg")
1011     u_prog+=" return val[0]*val[1]\n\n"
1012 jgs 154
1013 gross 157 for case in case_set:
1014     for sh in shape_set:
1015     if not case=="float" or len(sh)==0:
1016     text=""
1017     text+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1018     tname="def test_%s_%s_rank%s"%(f.nickname,case,len(sh))
1019     text+=" %s(self):\n"%tname
1020     a=makeArray(sh,f.rng)
1021     a1=makeArray(sh,f.rng)
1022     r1=makeResult(a1,f.test_expr)
1023     r=makeResult(a,f.test_expr)
1024    
1025     text+=mkText(case,"arg",a,a1)
1026     text+=" res=%s(arg)\n"%f.nickname
1027     if case=="Symbol":
1028     text+=mkText("array","s",a,a1)
1029     text+=" sub=res.substitute({arg:s})\n"
1030     text+=mkText("array","ref",r,r1)
1031     res="sub"
1032     else:
1033     text+=mkText(case,"ref",r,r1)
1034     res="res"
1035     text+=mkTypeAndShapeTest(case,sh,"res")
1036     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1037     if case == "taggedData":
1038     t_prog_with_tags+=text
1039     else:
1040     t_prog+=text
1041    
1042     #=========== END OF GOOD CODE +++++++++++++++++++++++++++
1043 jgs 154
1044 gross 157 1/0
1045 jgs 154
1046 gross 157 def X():
1047     if args=="float":
1048     a=makeArray(sh,f[RANGE])
1049 jgs 154 r=makeResult(a,f)
1050 gross 157 t_prog+=" arg=%s\n"%a[0]
1051     t_prog+=" ref=%s\n"%r[0]
1052     t_prog+=" res=%s(%a1%)\n"%f.nickname
1053     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1054     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1055     elif args == "array":
1056     a=makeArray(sh,f[RANGE])
1057     r=makeResult(a,f)
1058 jgs 154 if len(sh)==0:
1059 gross 157 t_prog+=" arg=numarray.array(%s)\n"%a[0]
1060     t_prog+=" ref=numarray.array(%s)\n"%r[0]
1061 jgs 154 else:
1062     t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
1063     t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
1064 gross 157 t_prog+=" res=%s(%a1%)\n"%f.nickname
1065     t_prog+=" self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh)
1066     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1067 jgs 154 elif args== "constData":
1068 gross 157 a=makeArray(sh,f[RANGE])
1069 jgs 154 r=makeResult(a,f)
1070     if len(sh)==0:
1071     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1072     t_prog+=" ref=%s\n"%r
1073     else:
1074     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1075     t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
1076 gross 157 t_prog+=" res=%s(%a1%)\n"%f.nickname
1077 jgs 154 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)
1078     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1079     elif args in [ "taggedData","expandedData"]:
1080 gross 157 a=makeArray(sh,f[RANGE])
1081 jgs 154 r=makeResult(a,f)
1082 gross 157 a1=makeArray(sh,f[RANGE])
1083 jgs 154 r1=makeResult(a1,f)
1084     if len(sh)==0:
1085     if args=="expandedData":
1086     t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a)
1087     t_prog+=" ref=Data(%s,self.functionspace,True)\n"%(r)
1088     else:
1089     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1090     t_prog+=" ref=Data(%s,self.functionspace)\n"%(r)
1091     t_prog+=" arg.setTaggedValue(1,%s)\n"%a
1092     t_prog+=" ref.setTaggedValue(1,%s)\n"%r1
1093     else:
1094     if args=="expandedData":
1095     t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())
1096     t_prog+=" ref=Data(numarray.array(%s),self.functionspace,True)\n"%(r.tolist())
1097     else:
1098     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1099     t_prog+=" ref=Data(numarray.array(%s),self.functionspace)\n"%(r.tolist())
1100     t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist()
1101     t_prog+=" ref.setTaggedValue(1,%s)\n"%r1.tolist()
1102 gross 157 t_prog+=" res=%s(%a1%)\n"%f.nickname
1103 jgs 154 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)
1104     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1105     elif args=="Symbol":
1106     t_prog+=" arg=Symbol(shape=%s)\n"%str(sh)
1107 gross 157 t_prog+=" v=%s(%a1%)\n"%f.nickname
1108 jgs 154 t_prog+=" self.failUnlessRaises(ValueError,v.substitute,Symbol(shape=(1,1)),\"illegal shape of substitute not identified.\")\n"
1109 gross 157 a=makeArray(sh,f[RANGE])
1110 jgs 154 r=makeResult(a,f)
1111     if len(sh)==0:
1112     t_prog+=" res=v.substitute({arg : %s})\n"%a
1113     t_prog+=" ref=%s\n"%r
1114     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1115     else:
1116     t_prog+=" res=v.substitute({arg : numarray.array(%s)})\n"%a.tolist()
1117     t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
1118     t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of substitution result.\")\n"%str(sh)
1119     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1120    
1121     if len(sh)==0:
1122     t_prog+=" # test derivative with respect to itself:\n"
1123     t_prog+=" dvdv=v.diff(v)\n"
1124     t_prog+=" self.failUnlessEqual(dvdv,1.,\"derivative with respect to self is not 1.\")\n"
1125     elif len(sh)==1:
1126     t_prog+=" # test derivative with respect to itself:\n"
1127     t_prog+=" dvdv=v.diff(v)\n"
1128     t_prog+=" self.failUnlessEqual(dvdv.shape,%s,\"shape of derivative with respect is wrong\")\n"%str(sh+sh)
1129     for i0_l in range(sh[0]):
1130     for i0_r in range(sh[0]):
1131     if i0_l == i0_r:
1132     v=1.
1133     else:
1134     v=0.
1135     t_prog+=" self.failUnlessEqual(dvdv[%s,%s],%s,\"derivative with respect to self: [%s,%s] is not %s\")\n"%(i0_l,i0_r,v,i0_l,i0_r,v)
1136     elif len(sh)==2:
1137     t_prog+=" # test derivative with respect to itself:\n"
1138     t_prog+=" dvdv=v.diff(v)\n"
1139     t_prog+=" self.failUnlessEqual(dvdv.shape,%s,\"shape of derivative with respect is wrong\")\n"%str(sh+sh)
1140     for i0_l in range(sh[0]):
1141     for i0_r in range(sh[0]):
1142     for i1_l in range(sh[1]):
1143     for i1_r in range(sh[1]):
1144     if i0_l == i0_r and i1_l == i1_r:
1145     v=1.
1146     else:
1147     v=0.
1148     t_prog+=" self.failUnlessEqual(dvdv[%s,%s,%s,%s],%s,\"derivative with respect to self: [%s,%s,%s,%s] is not %s\")\n"%(i0_l,i1_l,i0_r,i1_r,v,i0_l,i1_l,i0_r,i1_r,v)
1149    
1150     for sh_in in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1151     if len(sh_in)+len(sh)<=4:
1152    
1153     t_prog+=" # test derivative with shape %s as argument\n"%str(sh_in)
1154     trafo=makeArray(sh+sh_in,[0,1.])
1155 gross 157 a_in=makeArray(sh_in,f[RANGE])
1156 jgs 154 t_prog+=" arg_in=Symbol(shape=%s)\n"%str(sh_in)
1157     t_prog+=" arg2=Symbol(shape=%s)\n"%str(sh)
1158    
1159     if len(sh)==0:
1160     t_prog+=" arg2="
1161     if len(sh_in)==0:
1162     t_prog+="%s*arg_in\n"%trafo
1163     elif len(sh_in)==1:
1164     for i0 in range(sh_in[0]):
1165     if i0>0: t_prog+="+"
1166     t_prog+="%s*arg_in[%s]"%(trafo[i0],i0)
1167     t_prog+="\n"
1168     elif len(sh_in)==2:
1169     for i0 in range(sh_in[0]):
1170     for i1 in range(sh_in[1]):
1171     if i0+i1>0: t_prog+="+"
1172     t_prog+="%s*arg_in[%s,%s]"%(trafo[i0,i1],i0,i1)
1173    
1174     elif len(sh_in)==3:
1175     for i0 in range(sh_in[0]):
1176     for i1 in range(sh_in[1]):
1177     for i2 in range(sh_in[2]):
1178     if i0+i1+i2>0: t_prog+="+"
1179     t_prog+="%s*arg_in[%s,%s,%s]"%(trafo[i0,i1,i2],i0,i1,i2)
1180     elif len(sh_in)==4:
1181     for i0 in range(sh_in[0]):
1182     for i1 in range(sh_in[1]):
1183     for i2 in range(sh_in[2]):
1184     for i3 in range(sh_in[3]):
1185     if i0+i1+i2+i3>0: t_prog+="+"
1186     t_prog+="%s*arg_in[%s,%s,%s,%s]"%(trafo[i0,i1,i2,i3],i0,i1,i2,i3)
1187     t_prog+="\n"
1188     elif len(sh)==1:
1189     for j0 in range(sh[0]):
1190     t_prog+=" arg2[%s]="%j0
1191     if len(sh_in)==0:
1192     t_prog+="%s*arg_in"%trafo[j0]
1193     elif len(sh_in)==1:
1194     for i0 in range(sh_in[0]):
1195     if i0>0: t_prog+="+"
1196     t_prog+="%s*arg_in[%s]"%(trafo[j0,i0],i0)
1197     elif len(sh_in)==2:
1198     for i0 in range(sh_in[0]):
1199     for i1 in range(sh_in[1]):
1200     if i0+i1>0: t_prog+="+"
1201     t_prog+="%s*arg_in[%s,%s]"%(trafo[j0,i0,i1],i0,i1)
1202     elif len(sh_in)==3:
1203     for i0 in range(sh_in[0]):
1204     for i1 in range(sh_in[1]):
1205     for i2 in range(sh_in[2]):
1206     if i0+i1+i2>0: t_prog+="+"
1207     t_prog+="%s*arg_in[%s,%s,%s]"%(trafo[j0,i0,i1,i2],i0,i1,i2)
1208     t_prog+="\n"
1209     elif len(sh)==2:
1210     for j0 in range(sh[0]):
1211     for j1 in range(sh[1]):
1212     t_prog+=" arg2[%s,%s]="%(j0,j1)
1213     if len(sh_in)==0:
1214     t_prog+="%s*arg_in"%trafo[j0,j1]
1215     elif len(sh_in)==1:
1216     for i0 in range(sh_in[0]):
1217     if i0>0: t_prog+="+"
1218     t_prog+="%s*arg_in[%s]"%(trafo[j0,j1,i0],i0)
1219     elif len(sh_in)==2:
1220     for i0 in range(sh_in[0]):
1221     for i1 in range(sh_in[1]):
1222     if i0+i1>0: t_prog+="+"
1223     t_prog+="%s*arg_in[%s,%s]"%(trafo[j0,j1,i0,i1],i0,i1)
1224     t_prog+="\n"
1225     elif len(sh)==3:
1226     for j0 in range(sh[0]):
1227     for j1 in range(sh[1]):
1228     for j2 in range(sh[2]):
1229     t_prog+=" arg2[%s,%s,%s]="%(j0,j1,j2)
1230     if len(sh_in)==0:
1231     t_prog+="%s*arg_in"%trafo[j0,j1,j2]
1232     elif len(sh_in)==1:
1233     for i0 in range(sh_in[0]):
1234     if i0>0: t_prog+="+"
1235     t_prog+="%s*arg_in[%s]"%(trafo[j0,j1,j2,i0],i0)
1236     t_prog+="\n"
1237     elif len(sh)==4:
1238     for j0 in range(sh[0]):
1239     for j1 in range(sh[1]):
1240     for j2 in range(sh[2]):
1241     for j3 in range(sh[3]):
1242     t_prog+=" arg2[%s,%s,%s,%s]="%(j0,j1,j2,j3)
1243     if len(sh_in)==0:
1244     t_prog+="%s*arg_in"%trafo[j0,j1,j2,j3]
1245     t_prog+="\n"
1246     t_prog+=" dvdin=v.substitute({arg : arg2}).diff(arg_in)\n"
1247     if len(sh_in)==0:
1248     t_prog+=" res_in=dvdin.substitute({arg_in : %s})\n"%a_in
1249     else:
1250     t_prog+=" res_in=dvdin.substitute({arg : numarray.array(%s)})\n"%a_in.tolist()
1251    
1252     if len(sh)==0:
1253     if len(sh_in)==0:
1254     ref_diff=(makeResult(trafo*a_in+finc,f)-makeResult(trafo*a_in,f))/finc
1255     t_prog+=" self.failUnlessAlmostEqual(dvdin,%s,self.places,\"%s-derivative: wrong derivative\")\n"%(ref_diff,str(sh_in))
1256     elif len(sh_in)==1:
1257     s=0
1258     for k0 in range(sh_in[0]):
1259     s+=trafo[k0]*a_in[k0]
1260     for i0 in range(sh_in[0]):
1261     ref_diff=(makeResult(s+trafo[i0]*finc,f)-makeResult(s,f))/finc
1262     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,ref_diff,str(sh_in),str(i0))
1263     elif len(sh_in)==2:
1264     s=0
1265     for k0 in range(sh_in[0]):
1266     for k1 in range(sh_in[1]):
1267     s+=trafo[k0,k1]*a_in[k0,k1]
1268     for i0 in range(sh_in[0]):
1269     for i1 in range(sh_in[1]):
1270     ref_diff=(makeResult(s+trafo[i0,i1]*finc,f)-makeResult(s,f))/finc
1271     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,i1,ref_diff,str(sh_in),str((i0,i1)))
1272    
1273     elif len(sh_in)==3:
1274     s=0
1275     for k0 in range(sh_in[0]):
1276     for k1 in range(sh_in[1]):
1277     for k2 in range(sh_in[2]):
1278     s+=trafo[k0,k1,k2]*a_in[k0,k1,k2]
1279     for i0 in range(sh_in[0]):
1280     for i1 in range(sh_in[1]):
1281     for i2 in range(sh_in[2]):
1282     ref_diff=(makeResult(s+trafo[i0,i1,i2]*finc,f)-makeResult(s,f))/finc
1283     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,i1,i2,ref_diff,str(sh_in),str((i0,i1,i2)))
1284     elif len(sh_in)==4:
1285     s=0
1286     for k0 in range(sh_in[0]):
1287     for k1 in range(sh_in[1]):
1288     for k2 in range(sh_in[2]):
1289     for k3 in range(sh_in[3]):
1290     s+=trafo[k0,k1,k2,k3]*a_in[k0,k1,k2,k3]
1291     for i0 in range(sh_in[0]):
1292     for i1 in range(sh_in[1]):
1293     for i2 in range(sh_in[2]):
1294     for i3 in range(sh_in[3]):
1295     ref_diff=(makeResult(s+trafo[i0,i1,i2,i3]*finc,f)-makeResult(s,f))/finc
1296     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative with respect of %s\")\n"%(i0,i1,i2,i3,ref_diff,str(sh_in),str((i0,i1,i2,i3)))
1297     elif len(sh)==1:
1298     for j0 in range(sh[0]):
1299     if len(sh_in)==0:
1300     ref_diff=(makeResult(trafo[j0]*a_in+finc,f)-makeResult(trafo[j0]*a_in,f))/finc
1301     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,ref_diff,str(sh_in),j0)
1302     elif len(sh_in)==1:
1303     s=0
1304     for k0 in range(sh_in[0]):
1305     s+=trafo[j0,k0]*a_in[k0]
1306     for i0 in range(sh_in[0]):
1307     ref_diff=(makeResult(s+trafo[j0,i0]*finc,f)-makeResult(s,f))/finc
1308     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,i0,ref_diff,str(sh_in),str(j0),str(i0))
1309     elif len(sh_in)==2:
1310     s=0
1311     for k0 in range(sh_in[0]):
1312     for k1 in range(sh_in[1]):
1313     s+=trafo[j0,k0,k1]*a_in[k0,k1]
1314     for i0 in range(sh_in[0]):
1315     for i1 in range(sh_in[1]):
1316     ref_diff=(makeResult(s+trafo[j0,i0,i1]*finc,f)-makeResult(s,f))/finc
1317     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,i0,i1,ref_diff,str(sh_in),str(j0),str((i0,i1)))
1318    
1319     elif len(sh_in)==3:
1320    
1321     s=0
1322     for k0 in range(sh_in[0]):
1323     for k1 in range(sh_in[1]):
1324     for k2 in range(sh_in[2]):
1325     s+=trafo[j0,k0,k1,k2]*a_in[k0,k1,k2]
1326    
1327     for i0 in range(sh_in[0]):
1328     for i1 in range(sh_in[1]):
1329     for i2 in range(sh_in[2]):
1330     ref_diff=(makeResult(s+trafo[j0,i0,i1,i2]*finc,f)-makeResult(s,f))/finc
1331     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,i0,i1,i2,ref_diff,str(sh_in),str(j0),str((i0,i1,i2)))
1332     elif len(sh)==2:
1333     for j0 in range(sh[0]):
1334     for j1 in range(sh[1]):
1335     if len(sh_in)==0:
1336     ref_diff=(makeResult(trafo[j0,j1]*a_in+finc,f)-makeResult(trafo[j0,j1]*a_in,f))/finc
1337     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,j1,ref_diff,str(sh_in),str((j0,j1)))
1338     elif len(sh_in)==1:
1339     s=0
1340     for k0 in range(sh_in[0]):
1341     s+=trafo[j0,j1,k0]*a_in[k0]
1342     for i0 in range(sh_in[0]):
1343     ref_diff=(makeResult(s+trafo[j0,j1,i0]*finc,f)-makeResult(s,f))/finc
1344     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,j1,i0,ref_diff,str(sh_in),str((j0,j1)),str(i0))
1345    
1346     elif len(sh_in)==2:
1347     s=0
1348     for k0 in range(sh_in[0]):
1349     for k1 in range(sh_in[1]):
1350     s+=trafo[j0,j1,k0,k1]*a_in[k0,k1]
1351     for i0 in range(sh_in[0]):
1352     for i1 in range(sh_in[1]):
1353     ref_diff=(makeResult(s+trafo[j0,j1,i0,i1]*finc,f)-makeResult(s,f))/finc
1354     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,j1,i0,i1,ref_diff,str(sh_in),str((j0,j1)),str((i0,i1)))
1355     elif len(sh)==3:
1356     for j0 in range(sh[0]):
1357     for j1 in range(sh[1]):
1358     for j2 in range(sh[2]):
1359     if len(sh_in)==0:
1360     ref_diff=(makeResult(trafo[j0,j1,j2]*a_in+finc,f)-makeResult(trafo[j0,j1,j2]*a_in,f))/finc
1361     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,j1,j2,ref_diff,str(sh_in),str((j0,j1,j2)))
1362     elif len(sh_in)==1:
1363     s=0
1364     for k0 in range(sh_in[0]):
1365     s+=trafo[j0,j1,j2,k0]*a_in[k0]
1366     for i0 in range(sh_in[0]):
1367     ref_diff=(makeResult(s+trafo[j0,j1,j2,i0]*finc,f)-makeResult(s,f))/finc
1368     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s with respect of %s\")\n"%(j0,j1,j2,i0,ref_diff,str(sh_in),str((j0,j1,j2)),i0)
1369     elif len(sh)==4:
1370     for j0 in range(sh[0]):
1371     for j1 in range(sh[1]):
1372     for j2 in range(sh[2]):
1373     for j3 in range(sh[3]):
1374     if len(sh_in)==0:
1375     ref_diff=(makeResult(trafo[j0,j1,j2,j3]*a_in+finc,f)-makeResult(trafo[j0,j1,j2,j3]*a_in,f))/finc
1376     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s,%s,%s,%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,j1,j2,j3,ref_diff,str(sh_in),str((j0,j1,j2,j3)))
1377 gross 157
1378     #==================
1379     case="inner"
1380     for arg0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1381     for arg1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1382     for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1383     sh1=sh0
1384     if (len(sh0)==0 or not arg0=="float") and (len(sh1)==0 or not arg1=="float"):
1385     tname="test_%s_%s_rank%s_%s_rank%s"%(case,arg0,len(sh0),arg1,len(sh1))
1386     t_prog+=" def %s(self):\n"%tname
1387     a0=makeArray(sh0,[-1,1])
1388     a0_1=makeArray(sh0,[-1,1])
1389     a1=makeArray(sh1,[-1,1])
1390     a1_1=makeArray(sh1,[-1,1])
1391     t_prog+=mkText(arg0,"arg0",a0,a0_1)
1392     t_prog+=mkText(arg1,"arg1",a1,a1_1)
1393     t_prog+=" res=%s(arg0,arg1)\n"%case
1394    
1395     print t_prog
1396     1/0
1397    
1398     #==================
1399     cases=["Scalar","Vector","Tensor", "Tensor3","Tensor4"]
1400    
1401     for case in range(len(cases)):
1402     for d in [ None , "d", 1, 2 , 3]:
1403     if not d==None or cases[case]=="Scalar":
1404     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1405     tname="test_Symbol_%s_d%s"%(cases[case],d)
1406     t_prog+=" def %s(self):\n"%tname
1407     if d=="d":
1408     t_prog+=" s=%sSymbol(dim=self.functionspace)\n"%(cases[case])
1409     t_prog+=" d=self.functionspace.getDim()\n"
1410     sh="("
1411     for i in range(case):
1412     if i==0:
1413     sh+=d
1414     else:
1415     sh+=","+d
1416     sh+=")"
1417     else:
1418     t_prog+=" s=%sSymbol(dim=%s)\n"%(cases[case],d)
1419     sh=()
1420     for i in range(case): sh=sh+(d,)
1421     t_prog+=" self.failUnlessEqual(s.getRank(),%s,\"wrong rank.\")\n"%case
1422     t_prog+=" self.failUnlessEqual(s.getShape(),%s,\"wrong shape.\")\n"%str(sh)
1423     t_prog+=" self.failUnlessEqual(s.getDim(),%s,\"wrong spatial dimension.\")\n"%d
1424     t_prog+=" self.failUnlessEqual(s.getArgument(),[],\"wrong arguments.\")\n"
1425    
1426     print t_prog
1427     1/0
1428     for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1429     for d in [ None , "domain", 1, 2 , 3]:
1430     for args in [ [], ["s2"], [1,-1.] ]:
1431     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1432     tname="def test_Symbol_rank%s_d%s_nargs%s"%(len(sh),d,len(args))
1433     t_prog+=" %s(self):\n"%tname
1434     t_prog+=" s2=ScalarSymbol()\n"
1435     if args==["s2"]:
1436     a="[s2]"
1437     else:
1438     a=str(args)
1439     if d=="domain":
1440     t_prog+=" s=Symbol(shape=%s,dim=self.functionspace.getDim(),args=%s)\n"%(str(sh),a)
1441     d2="self.functionspace.getDim()"
1442     else:
1443     t_prog+=" s=Symbol(shape=%s,dim=%s,args=%s)\n"%(sh,d,a)
1444     d2=str(d)
1445    
1446     t_prog+=" self.failUnlessEqual(s.getRank(),%s,\"wrong rank.\")\n"%len(sh)
1447     t_prog+=" self.failUnlessEqual(s.getShape(),%s,\"wrong shape.\")\n"%str(sh)
1448     t_prog+=" self.failUnlessEqual(s.getDim(),%s,\"wrong spatial dimension.\")\n"%d2
1449     t_prog+=" self.failUnlessEqual(s.getArgument(),%s,\"wrong arguments.\")\n\n"%a
1450     t_prog+=" ss=s.substitute({s:numarray.zeros(%s)})\n"%str(sh)
1451     t_prog+=" self.failUnless(isinstance(ss,numarray.NumArray),\"value after substitution is not numarray.\")\n"
1452     t_prog+=" self.failUnlessEqual(ss.shape,%s,\"value after substitution has not expected shape\")\n"%str(sh)
1453    
1454     t_prog+=" try:\n s.substitute({s:numarray.zeros((5,))})\n fail(\"illegal substition was successful\")\n"
1455     t_prog+=" except TypeError:\n pass\n\n"
1456    
1457     ###
1458     for sh2 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1459     if len(sh+sh2)<5:
1460     t_prog+=" dsdarg=s.diff(Symbol(shape=%s))\n"%str(sh2)
1461     if len(sh+sh2)==0:
1462     t_prog+=" self.failUnless(isinstance(dsdarg,float),\"ds/ds() has wrong type.\")\n"
1463     else:
1464     t_prog+=" self.failUnless(isinstance(dsdarg,numarray.NumArray),\"ds/ds%s has wrong type.\")\n"%str(sh2)
1465    
1466     t_prog+=" self.failUnlessEqual(dsdarg.shape,%s,\"ds/ds%s has wrong shape.\")\n"%(str(sh+sh2),str(sh2))
1467     t_prog+=" self.failIf(Lsup(dsdarg)>0.,\"ds/ds%s has wrong value.\")\n"%str(sh2)
1468     if len(sh)<3:
1469     t_prog+="\n dsds=s.diff(s)\n"
1470     if len(sh)==0:
1471     t_prog+=" self.failUnless(isinstance(dsds,float),\"ds/ds has wrong type.\")\n"
1472     t_prog+=" self.failUnlessEqual(dsds,1.,\"ds/ds has wrong value.\")\n"
1473     else:
1474     t_prog+=" self.failUnless(isinstance(dsds,numarray.NumArray),\"ds/ds has wrong type.\")\n"
1475     t_prog+=" self.failUnlessEqual(dsds.shape,%s,\"ds/ds has wrong shape.\")\n"%str(sh+sh)
1476     if len(sh)==1:
1477     for i0 in range(sh[0]):
1478     for i2 in range(sh[0]):
1479     if i0==i2:
1480     v=1.
1481     else:
1482     v=0.
1483     t_prog+=" self.failUnlessEqual(dsds[%s,%s],%s,\"ds/ds has wrong value at (%s,%s).\")\n"%(i0,i2,v,i0,i2)
1484     else:
1485     for i0 in range(sh[0]):
1486     for i1 in range(sh[1]):
1487     for i2 in range(sh[0]):
1488     for i3 in range(sh[1]):
1489     if i0==i2 and i1==i3:
1490     v=1.
1491     else:
1492     v=0.
1493     t_prog+=" self.failUnlessEqual(dsds[%s,%s,%s,%s],%s,\"ds/ds has wrong value at (%s,%s,%s,%s).\")\n"%(i0,i1,i2,i3,v,i0,i1,i2,i3)
1494    
1495     ###
1496     t_prog+="\n"
1497     for i in range(len(args)):
1498     t_prog+=" self.failUnlessEqual(s.getArgument(%s),%s,\"wrong argument %s.\")\n"%(i,str(args[i]),i)
1499     t_prog+=" sa=s.getSubstitutedArguments({s2:-10})\n"
1500     t_prog+=" self.failUnlessEqual(len(sa),%s,\"wrong number of substituted arguments\")\n"%len(args)
1501     if args==["s2"]:
1502     t_prog+=" self.failUnlessEqual(sa[0],-10,\"wrongly substituted argument 0.\")\n"
1503     else:
1504     for i in range(len(args)):
1505     t_prog+=" self.failUnlessEqual(sa[%s],%s,\"wrongly substituted argument %s.\")\n"%(i,str(args[i]),i)
1506    
1507     t_prog+="\n"
1508     for arg in ["10.", "10", "SymbolMatch", "SymbolMisMatch", \
1509     "DataMatch","DataMisMatch", "NumArrayMatch", "NumArrayMisMatch"]:
1510     if arg in ["10.", "10"]:
1511     a=str(arg)
1512     if len(sh)==0:
1513     t_prog+=" self.failUnless(s.isAppropriateValue(%s),\"%s is appropriate substitute\")\n"%(a,arg)
1514     else:
1515     t_prog+=" self.failIf(s.isAppropriateValue(%s),\" %s is not appropriate substitute\")\n"%(a,arg)
1516     elif arg in ["SymbolMatch", "SymbolMisMatch"]:
1517     if arg=="SymbolMatch":
1518     t_prog+=" self.failUnless(s.isAppropriateValue(Symbol(shape=%s,dim=%s)),\"Symbol is appropriate substitute\")\n"%(str(sh),d)
1519     else:
1520     if isinstance(d,int):
1521     t_prog+=" self.failIf(s.isAppropriateValue(Symbol(shape=%s,dim=%s)),\"Symbol is not appropriate substitute (dim)\")\n"%(str(sh),d+1)
1522     else:
1523     t_prog+=" self.failIf(s.isAppropriateValue(Symbol(shape=%s)),\"Symbol is not appropriate substitute (shape)\")\n"%((5,))
1524    
1525     elif arg in ["DataMatch","DataMisMatch"]:
1526     if arg=="DataMatch" and d=="domain":
1527     t_prog+=" self.failUnless(s.isAppropriateValue(escript.Data(0.,%s,self.functionspace)),\"Data is appropriate substitute\")\n"%str(sh)
1528     elif arg=="DataMisMatch":
1529     t_prog+=" self.failIf(s.isAppropriateValue(escript.Data(0.,%s,self.functionspace)),\"Data is not appropriate substitute (shape)\")\n"%(str((5,)))
1530     else:
1531     if arg=="NumArrayMatch":
1532     t_prog+=" self.failUnless(s.isAppropriateValue(numarray.zeros(%s)),\"NumArray is appropriate substitute\")\n"%str(sh)
1533     else:
1534     t_prog+=" self.failIf(s.isAppropriateValue(numarray.zeros(%s)),\"NumArray is not appropriate substitute (shape)\")\n"%(str((5,)))
1535 jgs 154 print t_prog
1536 gross 157 1/0
1537    
1538    
1539     for case in ["Lsup", "sup", "inf"]:
1540     for args in ["float","array","constData","taggedData","expandedData"]:
1541     for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1542     if not args=="float" or len(sh)==0:
1543     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1544     tname="def test_%s_%s_rank%s"%(case,args,len(sh))
1545     t_prog+=" %s(self):\n"%tname
1546     if args in ["float","array" ]:
1547     a=makeArray(sh,[-1,1])
1548     r=makeResult2(a,case)
1549     if len(sh)==0:
1550     t_prog+=" arg=%s\n"%a
1551     else:
1552     t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
1553     t_prog+=" ref=%s\n"%r
1554     t_prog+=" res=%s(%a1%)\n"%case
1555     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1556     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1557     elif args== "constData":
1558     a=makeArray(sh,[-1,1])
1559     r=makeResult2(a,case)
1560     if len(sh)==0:
1561     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1562     else:
1563     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1564     t_prog+=" ref=%s\n"%r
1565     t_prog+=" res=%s(%a1%)\n"%case
1566     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1567     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1568     elif args in [ "taggedData","expandedData"]:
1569     a=makeArray(sh,[-1,1])
1570     r=makeResult2(a,case)
1571     a1=makeArray(sh,[-1,1])
1572     r1=makeResult2(a1,case)
1573     if case in ["Lsup","sup"]:
1574     r=max(r,r1)
1575     else:
1576     r=min(r,r1)
1577     if len(sh)==0:
1578     if args=="expandedData":
1579     t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a)
1580     else:
1581     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1582     t_prog+=" arg.setTaggedValue(1,%s)\n"%a
1583     else:
1584     if args=="expandedData":
1585     t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())
1586     else:
1587     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1588     t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist()
1589     t_prog+=" res=%s(%a1%)\n"%case
1590     t_prog+=" ref=%s\n"%r
1591     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1592     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1593    
1594     print t_prog
1595    
1596     1/0
1597    
1598    
1599     for case in ["Lsup", "sup", "inf"]:
1600     for args in ["float","array","constData","taggedData","expandedData"]:
1601     for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1602     if not args=="float" or len(sh)==0:
1603     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1604     tname="def test_%s_%s_rank%s"%(case,args,len(sh))
1605     t_prog+=" %s(self):\n"%tname
1606     if args in ["float","array" ]:
1607     a=makeArray(sh,[-1,1])
1608     r=makeResult2(a,case)
1609     if len(sh)==0:
1610     t_prog+=" arg=%s\n"%a
1611     else:
1612     t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
1613     t_prog+=" ref=%s\n"%r
1614     t_prog+=" res=%s(%a1%)\n"%case
1615     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1616     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1617     elif args== "constData":
1618     a=makeArray(sh,[-1,1])
1619     r=makeResult2(a,case)
1620     if len(sh)==0:
1621     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1622     else:
1623     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1624     t_prog+=" ref=%s\n"%r
1625     t_prog+=" res=%s(%a1%)\n"%case
1626     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1627     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1628     elif args in [ "taggedData","expandedData"]:
1629     a=makeArray(sh,[-1,1])
1630     r=makeResult2(a,case)
1631     a1=makeArray(sh,[-1,1])
1632     r1=makeResult2(a1,case)
1633     if case in ["Lsup","sup"]:
1634     r=max(r,r1)
1635     else:
1636     r=min(r,r1)
1637     if len(sh)==0:
1638     if args=="expandedData":
1639     t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a)
1640     else:
1641     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1642     t_prog+=" arg.setTaggedValue(1,%s)\n"%a
1643     else:
1644     if args=="expandedData":
1645     t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())
1646     else:
1647     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1648     t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist()
1649     t_prog+=" res=%s(%a1%)\n"%case
1650     t_prog+=" ref=%s\n"%r
1651     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1652     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1653    
1654     print t_prog
1655    
1656     1/0

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26