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

Annotation of /trunk/escript/py_src/generateutil

Parent Directory Parent Directory | Revision Log Revision Log


Revision 291 - (hide annotations)
Fri Dec 2 03:10:06 2005 UTC (13 years, 10 months ago) by gross
File size: 88538 byte(s)
just my private test generator
1 jgs 154 #!/usr/bin/python
2 gross 283 # $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 gross 291 elif a.rank==0:
435 jgs 154 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 gross 291 elif a.rank==0:
442 gross 157 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 gross 291 elif a.rank==0:
449 jgs 154 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 gross 291 elif a.rank==0:
457 jgs 154 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 gross 291 elif a.rank==0:
468 gross 157 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 gross 291 elif a.rank==0:
479 gross 157 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 gross 291 elif a.rank==0:
486 jgs 154 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 291 def innerTEST(arg0,arg1):
521     if isinstance(arg0,float):
522     out=numarray.array(arg0*arg1)
523     else:
524     out=(arg0*arg1).sum()
525     return out
526    
527     def outerTEST(arg0,arg1):
528     if isinstance(arg0,float):
529     out=numarray.array(arg0*arg1)
530     elif isinstance(arg1,float):
531     out=numarray.array(arg0*arg1)
532     else:
533     out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
534     return out
535    
536     def tensorProductTest(arg0,arg1,sh_s):
537     if isinstance(arg0,float):
538     out=numarray.array(arg0*arg1)
539     elif isinstance(arg1,float):
540     out=numarray.array(arg0*arg1)
541     elif len(sh_s)==0:
542     out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
543     else:
544     l=len(sh_s)
545     sh0=arg0.shape[:arg0.rank-l]
546     sh1=arg1.shape[l:]
547     ls,l0,l1=1,1,1
548     for i in sh_s: ls*=i
549     for i in sh0: l0*=i
550     for i in sh1: l1*=i
551     out1=numarray.outerproduct(arg0,arg1).resize((l0,ls,ls,l1))
552     out2=numarray.zeros((l0,l1),numarray.Float)
553     for i0 in range(l0):
554     for i1 in range(l1):
555     for i in range(ls): out2[i0,i1]+=out1[i0,i,i,i1]
556     out=out2.resize(sh0+sh1)
557     return out
558    
559     def testMatrixMult(arg0,arg1,sh_s):
560     return numarray.matrixmultiply(arg0,arg1)
561    
562    
563     def testTensorMult(arg0,arg1,sh_s):
564     if len(arg0)==2:
565     return numarray.matrixmultiply(arg0,arg1)
566     else:
567     if arg1.rank==4:
568     out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[2],arg1.shape[3]),numarray.Float)
569     for i0 in range(arg0.shape[0]):
570     for i1 in range(arg0.shape[1]):
571     for i2 in range(arg0.shape[2]):
572     for i3 in range(arg0.shape[3]):
573     for j2 in range(arg1.shape[2]):
574     for j3 in range(arg1.shape[3]):
575     out[i0,i1,j2,j3]+=arg0[i0,i1,i2,i3]*arg1[i2,i3,j2,j3]
576     elif arg1.rank==3:
577     out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[2]),numarray.Float)
578     for i0 in range(arg0.shape[0]):
579     for i1 in range(arg0.shape[1]):
580     for i2 in range(arg0.shape[2]):
581     for i3 in range(arg0.shape[3]):
582     for j2 in range(arg1.shape[2]):
583     out[i0,i1,j2]+=arg0[i0,i1,i2,i3]*arg1[i2,i3,j2]
584     elif arg1.rank==2:
585     out=numarray.zeros((arg0.shape[0],arg0.shape[1]),numarray.Float)
586     for i0 in range(arg0.shape[0]):
587     for i1 in range(arg0.shape[1]):
588     for i2 in range(arg0.shape[2]):
589     for i3 in range(arg0.shape[3]):
590     out[i0,i1]+=arg0[i0,i1,i2,i3]*arg1[i2,i3]
591     return out
592 gross 157 #=======================================================================================================
593 gross 291 # tensor multiply
594 gross 157 #=======================================================================================================
595 gross 291 # oper=["generalTensorProduct",tensorProductTest]
596     # oper=["matrixmult",testMatrixMult]
597     oper=["tensormult",testTensorMult]
598    
599     for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
600     for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
601     for case1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
602     for sh1 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
603     for sh_s in [ (),(3,), (2,3), (2,4,3),(4,2,3,2)]:
604     if (len(sh0+sh_s)==0 or not case0=="float") and (len(sh1+sh_s)==0 or not case1=="float") \
605     and len(sh0+sh1)<5 and len(sh0+sh_s)<5 and len(sh1+sh_s)<5:
606     # if len(sh_s)==1 and len(sh0+sh_s)==2 and (len(sh_s+sh1)==1 or len(sh_s+sh1)==2)): # test for matrixmult
607     if ( len(sh_s)==1 and len(sh0+sh_s)==2 and ( len(sh1+sh_s)==2 or len(sh1+sh_s)==1 )) or (len(sh_s)==2 and len(sh0+sh_s)==4 and (len(sh1+sh_s)==2 or len(sh1+sh_s)==3 or len(sh1+sh_s)==4)): # test for tensormult
608     case=getResultCaseForBin(case0,case1)
609 gross 157 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
610     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
611 gross 291 # tname="test_generalTensorProduct_%s_rank%s_%s_rank%s_offset%s"%(case0,len(sh0+sh_s),case1,len(sh_s+sh1),len(sh_s))
612     #tname="test_matrixmult_%s_rank%s_%s_rank%s"%(case0,len(sh0+sh_s),case1,len(sh_s+sh1))
613     tname="test_tensormult_%s_rank%s_%s_rank%s"%(case0,len(sh0+sh_s),case1,len(sh_s+sh1))
614     # if tname=="test_generalTensorProduct_array_rank1_array_rank2_offset1":
615     # print tnametest_generalTensorProduct_Symbol_rank1_Symbol_rank3_offset1
616     text+=" def %s(self):\n"%tname
617     a_0=makeArray(sh0+sh_s,[-1.,1])
618     if case0 in ["taggedData", "expandedData"]:
619     a1_0=makeArray(sh0+sh_s,[-1.,1])
620     else:
621     a1_0=a_0
622    
623     a_1=makeArray(sh_s+sh1,[-1.,1])
624     if case1 in ["taggedData", "expandedData"]:
625     a1_1=makeArray(sh_s+sh1,[-1.,1])
626     else:
627     a1_1=a_1
628     r=oper[1](a_0,a_1,sh_s)
629     r1=oper[1](a1_0,a1_1,sh_s)
630     text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
631     text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
632     #text+=" res=matrixmult(arg0,arg1)\n"
633     text+=" res=tensormult(arg0,arg1)\n"
634     #text+=" res=generalTensorProduct(arg0,arg1,offset=%s)\n"%(len(sh_s))
635     if case=="Symbol":
636     c0_res,c1_res=case0,case1
637     subs="{"
638     if case0=="Symbol":
639     text+=mkText("array","s0",a_0,a1_0)
640     subs+="arg0:s0"
641     c0_res="array"
642     if case1=="Symbol":
643     text+=mkText("array","s1",a_1,a1_1)
644     if not subs.endswith("{"): subs+=","
645     subs+="arg1:s1"
646     c1_res="array"
647     subs+="}"
648     text+=" sub=res.substitute(%s)\n"%subs
649     res="sub"
650     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
651     else:
652     res="res"
653     text+=mkText(case,"ref",r,r1)
654     text+=mkTypeAndShapeTest(case,sh0+sh1,"res")
655     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
656     if case0 == "taggedData" or case1 == "taggedData":
657     t_prog_with_tags+=text
658     else:
659     t_prog+=text
660     print test_header
661     # print t_prog
662     print t_prog_with_tags
663     print test_tail
664     1/0
665     #=======================================================================================================
666     # outer/inner
667     #=======================================================================================================
668     oper=["inner",innerTEST]
669     # oper=["outer",outerTEST]
670     for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
671     for sh1 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
672     for case1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
673     for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
674     if (len(sh0)==0 or not case0=="float") and (len(sh1)==0 or not case1=="float") \
675     and len(sh0+sh1)<5:
676     use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
677    
678     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
679 gross 157 tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
680     text+=" def %s(self):\n"%tname
681 gross 291 a_0=makeArray(sh0,[-1.,1])
682 gross 157 if case0 in ["taggedData", "expandedData"]:
683 gross 291 a1_0=makeArray(sh0,[-1.,1])
684 gross 157 else:
685     a1_0=a_0
686 jgs 154
687 gross 291 a_1=makeArray(sh1,[-1.,1])
688 gross 157 if case1 in ["taggedData", "expandedData"]:
689 gross 291 a1_1=makeArray(sh1,[-1.,1])
690 gross 157 else:
691     a1_1=a_1
692 gross 291 r=oper[1](a_0,a_1)
693     r1=oper[1](a1_0,a1_1)
694 gross 157 text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
695     text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
696     text+=" res=%s(arg0,arg1)\n"%oper[0]
697     case=getResultCaseForBin(case0,case1)
698     if case=="Symbol":
699     c0_res,c1_res=case0,case1
700     subs="{"
701     if case0=="Symbol":
702     text+=mkText("array","s0",a_0,a1_0)
703     subs+="arg0:s0"
704     c0_res="array"
705     if case1=="Symbol":
706     text+=mkText("array","s1",a_1,a1_1)
707     if not subs.endswith("{"): subs+=","
708     subs+="arg1:s1"
709     c1_res="array"
710     subs+="}"
711     text+=" sub=res.substitute(%s)\n"%subs
712     res="sub"
713     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
714     else:
715     res="res"
716     text+=mkText(case,"ref",r,r1)
717 gross 291 text+=mkTypeAndShapeTest(case,sh0+sh1,"res")
718 gross 157 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
719    
720     if case0 == "taggedData" or case1 == "taggedData":
721     t_prog_with_tags+=text
722     else:
723     t_prog+=text
724 gross 291
725 gross 157 print test_header
726     # print t_prog
727     print t_prog_with_tags
728 gross 291 print test_tail
729 jgs 154 1/0
730 gross 157 #=======================================================================================================
731     # basic binary operation overloading (tests only!)
732     #=======================================================================================================
733     oper_range=[-5.,5.]
734     for oper in [["add" ,"+",[-5.,5.]],
735     ["sub" ,"-",[-5.,5.]],
736     ["mult","*",[-5.,5.]],
737     ["div" ,"/",[-5.,5.]],
738     ["pow" ,"**",[0.01,5.]]]:
739     for case0 in case_set:
740     for sh0 in shape_set:
741     for case1 in case_set:
742     for sh1 in shape_set:
743 gross 291 if not case0=="array" and \
744     (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
745 gross 157 (sh0==() or sh1==() or sh1==sh0) and \
746     not (case0 in ["float","array"] and case1 in ["float","array"]):
747 gross 291 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
748 gross 157 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
749     tname="test_%s_overloaded_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
750     text+=" def %s(self):\n"%tname
751     a_0=makeArray(sh0,oper[2])
752     if case0 in ["taggedData", "expandedData"]:
753     a1_0=makeArray(sh0,oper[2])
754     else:
755     a1_0=a_0
756 jgs 154
757 gross 157 a_1=makeArray(sh1,oper[2])
758     if case1 in ["taggedData", "expandedData"]:
759     a1_1=makeArray(sh1,oper[2])
760 jgs 154 else:
761 gross 157 a1_1=a_1
762     r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
763     r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
764     text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
765     text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
766     text+=" res=arg0%sarg1\n"%oper[1]
767    
768     case=getResultCaseForBin(case0,case1)
769     if case=="Symbol":
770     c0_res,c1_res=case0,case1
771     subs="{"
772     if case0=="Symbol":
773     text+=mkText("array","s0",a_0,a1_0)
774     subs+="arg0:s0"
775     c0_res="array"
776     if case1=="Symbol":
777     text+=mkText("array","s1",a_1,a1_1)
778     if not subs.endswith("{"): subs+=","
779     subs+="arg1:s1"
780     c1_res="array"
781     subs+="}"
782     text+=" sub=res.substitute(%s)\n"%subs
783     res="sub"
784     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
785 jgs 154 else:
786 gross 157 res="res"
787     text+=mkText(case,"ref",r,r1)
788     if isinstance(r,float):
789     text+=mkTypeAndShapeTest(case,(),"res")
790     else:
791     text+=mkTypeAndShapeTest(case,r.shape,"res")
792     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
793    
794     if case0 in [ "constData","taggedData","expandedData"] and case1 == "Symbol":
795     t_prog_failing+=text
796     else:
797     if case0 == "taggedData" or case1 == "taggedData":
798     t_prog_with_tags+=text
799     else:
800     t_prog+=text
801 jgs 154
802 gross 157
803     print test_header
804 gross 291 # print t_prog
805     # print t_prog_with_tags
806     print t_prog_failing
807     print test_tail
808 jgs 154 1/0
809 gross 291 #=======================================================================================================
810     # basic binary operations (tests only!)
811     #=======================================================================================================
812     oper_range=[-5.,5.]
813     for oper in [["add" ,"+",[-5.,5.]],
814     ["mult","*",[-5.,5.]],
815     ["quotient" ,"/",[-5.,5.]],
816     ["power" ,"**",[0.01,5.]]]:
817     for case0 in case_set:
818     for case1 in case_set:
819     for sh in shape_set:
820     for sh_p in shape_set:
821     if len(sh_p)>0:
822     resource=[-1,1]
823     else:
824     resource=[1]
825     for sh_d in resource:
826     if sh_d>0:
827     sh0=sh
828     sh1=sh+sh_p
829     else:
830     sh1=sh
831     sh0=sh+sh_p
832    
833     if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
834     len(sh0)<5 and len(sh1)<5:
835     use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
836     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
837     tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
838     text+=" def %s(self):\n"%tname
839     a_0=makeArray(sh0,oper[2])
840     if case0 in ["taggedData", "expandedData"]:
841     a1_0=makeArray(sh0,oper[2])
842     else:
843     a1_0=a_0
844    
845     a_1=makeArray(sh1,oper[2])
846     if case1 in ["taggedData", "expandedData"]:
847     a1_1=makeArray(sh1,oper[2])
848     else:
849     a1_1=a_1
850     r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
851     r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
852     text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
853     text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
854     text+=" res=%s(arg0,arg1)\n"%oper[0]
855    
856     case=getResultCaseForBin(case0,case1)
857     if case=="Symbol":
858     c0_res,c1_res=case0,case1
859     subs="{"
860     if case0=="Symbol":
861     text+=mkText("array","s0",a_0,a1_0)
862     subs+="arg0:s0"
863     c0_res="array"
864     if case1=="Symbol":
865     text+=mkText("array","s1",a_1,a1_1)
866     if not subs.endswith("{"): subs+=","
867     subs+="arg1:s1"
868     c1_res="array"
869     subs+="}"
870     text+=" sub=res.substitute(%s)\n"%subs
871     res="sub"
872     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
873     else:
874     res="res"
875     text+=mkText(case,"ref",r,r1)
876     if isinstance(r,float):
877     text+=mkTypeAndShapeTest(case,(),"res")
878     else:
879     text+=mkTypeAndShapeTest(case,r.shape,"res")
880     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
881    
882     if case0 == "taggedData" or case1 == "taggedData":
883     t_prog_with_tags+=text
884     else:
885     t_prog+=text
886     print test_header
887     # print t_prog
888     print t_prog_with_tags
889     print test_tail
890     1/0
891    
892 gross 157 # print t_prog_with_tagsoper_range=[-5.,5.]
893     for oper in [["add" ,"+",[-5.,5.]],
894     ["sub" ,"-",[-5.,5.]],
895     ["mult","*",[-5.,5.]],
896     ["div" ,"/",[-5.,5.]],
897     ["pow" ,"**",[0.01,5.]]]:
898     for case0 in case_set:
899     for sh0 in shape_set:
900     for case1 in case_set:
901     for sh1 in shape_set:
902     if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
903     (sh0==() or sh1==() or sh1==sh0) and \
904     not (case0 in ["float","array"] and case1 in ["float","array"]):
905     text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
906     tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
907     text+=" def %s(self):\n"%tname
908     a_0=makeArray(sh0,oper[2])
909     if case0 in ["taggedData", "expandedData"]:
910     a1_0=makeArray(sh0,oper[2])
911     else:
912     a1_0=a_0
913 jgs 154
914 gross 157 a_1=makeArray(sh1,oper[2])
915     if case1 in ["taggedData", "expandedData"]:
916     a1_1=makeArray(sh1,oper[2])
917 jgs 154 else:
918 gross 157 a1_1=a_1
919     r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
920     r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
921     text+=mkText(case0,"arg0",a_0,a1_0)
922     text+=mkText(case1,"arg1",a_1,a1_1)
923     text+=" res=arg0%sarg1\n"%oper[1]
924    
925     case=getResultCaseForBin(case0,case1)
926     if case=="Symbol":
927     c0_res,c1_res=case0,case1
928     subs="{"
929     if case0=="Symbol":
930     text+=mkText("array","s0",a_0,a1_0)
931     subs+="arg0:s0"
932     c0_res="array"
933     if case1=="Symbol":
934     text+=mkText("array","s1",a_1,a1_1)
935     if not subs.endswith("{"): subs+=","
936     subs+="arg1:s1"
937     c1_res="array"
938     subs+="}"
939     text+=" sub=res.substitute(%s)\n"%subs
940     res="sub"
941     text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
942 jgs 154 else:
943 gross 157 res="res"
944     text+=mkText(case,"ref",r,r1)
945     if isinstance(r,float):
946     text+=mkTypeAndShapeTest(case,(),"res")
947     else:
948     text+=mkTypeAndShapeTest(case,r.shape,"res")
949     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
950    
951     if case0 in [ "constData","taggedData","expandedData"] and case1 == "Symbol":
952     t_prog_failing+=text
953     else:
954     if case0 == "taggedData" or case1 == "taggedData":
955     t_prog_with_tags+=text
956     else:
957     t_prog+=text
958 jgs 154
959 gross 157
960     # print u_prog
961     # 1/0
962     print test_header
963 jgs 154 print t_prog
964 gross 157 # print t_prog_with_tags
965     # print t_prog_failing
966     print test_tail
967     # print t_prog_failing
968     print test_tail
969 jgs 154
970 gross 157 #=======================================================================================================
971     # unary operations:
972     #=======================================================================================================
973     func= [
974     OPERATOR(nickname="log10",\
975     rng=[1.e-3,100.],\
976     test_expr="math.log10(%a1%)",\
977     math_expr="math.log10(%a1%)",\
978     numarray_expr="numarray.log10(%a1%)",\
979     symbol_expr="log(%a1%)/log(10.)",\
980     name="base-10 logarithm"),
981     OPERATOR(nickname="wherePositive",\
982     rng=[-100.,100.],\
983     test_expr="wherepos(%a1%)",\
984     math_expr="if arg>0:\n return 1.\nelse:\n return 0.",
985     numarray_expr="numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))",\
986     name="mask of positive values"),
987     OPERATOR(nickname="whereNegative",\
988     rng=[-100.,100.],\
989     test_expr="wherepos(-%a1%)",\
990     math_expr="if arg<0:\n return 1.\nelse:\n return 0.",
991     numarray_expr="numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))",\
992     name="mask of positive values"),
993     OPERATOR(nickname="whereNonNegative",\
994     rng=[-100.,100.],\
995     test_expr="1-wherepos(-%a1%)", \
996     math_expr="if arg<0:\n return 0.\nelse:\n return 1.",
997     numarray_expr="numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))",\
998     symbol_expr="1-wherePositive(%a1%)",\
999     name="mask of non-negative values"),
1000     OPERATOR(nickname="whereNonPositive",\
1001     rng=[-100.,100.],\
1002     test_expr="1-wherepos(%a1%)",\
1003     math_expr="if arg>0:\n return 0.\nelse:\n return 1.",
1004     numarray_expr="numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))",\
1005     symbol_expr="1-whereNegative(%a1%)",\
1006     name="mask of non-positive values"),
1007     OPERATOR(nickname="whereZero",\
1008     rng=[-100.,100.],\
1009     test_expr="1-wherepos(%a1%)-wherepos(-%a1%)",\
1010     math_expr="if abs(%a1%)<=tol:\n return 1.\nelse:\n return 0.",
1011     numarray_expr="numarray.less_equal(abs(%a1%)-tol,numarray.zeros(arg.shape,numarray.Float))",\
1012     name="mask of zero entries"),
1013     OPERATOR(nickname="whereNonZero",\
1014     rng=[-100.,100.],\
1015     test_expr="wherepos(%a1%)+wherepos(-%a1%)",\
1016     math_expr="if abs(%a1%)>tol:\n return 1.\nelse:\n return 0.",\
1017     numarray_expr="numarray.greater(abs(%a1%)-tol,numarray.zeros(arg.shape,numarray.Float))",\
1018     symbol_expr="1-whereZero(arg,tol)",\
1019     name="mask of values different from zero"),
1020     OPERATOR(nickname="sin",\
1021     rng=[-100.,100.],\
1022     test_expr="math.sin(%a1%)",
1023     numarray_expr="numarray.sin(%a1%)",\
1024     diff="cos(%a1%)",\
1025     name="sine"),
1026     OPERATOR(nickname="cos",\
1027     rng=[-100.,100.],\
1028     test_expr="math.cos(%a1%)",
1029     numarray_expr="numarray.cos(%a1%)",\
1030     diff="-sin(%a1%)",
1031     name="cosine"),
1032     OPERATOR(nickname="tan",\
1033     rng=[-100.,100.],\
1034     test_expr="math.tan(%a1%)",
1035     numarray_expr="numarray.tan(%a1%)",\
1036     diff="1./cos(%a1%)**2",
1037     name="tangent"),
1038     OPERATOR(nickname="asin",\
1039     rng=[-0.99,0.99],\
1040     test_expr="math.asin(%a1%)",
1041     numarray_expr="numarray.arcsin(%a1%)",
1042     diff="1./sqrt(1.-%a1%**2)",
1043     name="inverse sine"),
1044     OPERATOR(nickname="acos",\
1045     rng=[-0.99,0.99],\
1046     test_expr="math.acos(%a1%)",
1047     numarray_expr="numarray.arccos(%a1%)",
1048     diff="-1./sqrt(1.-%a1%**2)",
1049     name="inverse cosine"),
1050     OPERATOR(nickname="atan",\
1051     rng=[-100.,100.],\
1052     test_expr="math.atan(%a1%)",
1053     numarray_expr="numarray.arctan(%a1%)",
1054     diff="1./(1+%a1%**2)",
1055     name="inverse tangent"),
1056     OPERATOR(nickname="sinh",\
1057     rng=[-5,5],\
1058     test_expr="math.sinh(%a1%)",
1059     numarray_expr="numarray.sinh(%a1%)",
1060     diff="cosh(%a1%)",
1061     name="hyperbolic sine"),
1062     OPERATOR(nickname="cosh",\
1063     rng=[-5.,5.],
1064     test_expr="math.cosh(%a1%)",
1065     numarray_expr="numarray.cosh(%a1%)",
1066     diff="sinh(%a1%)",
1067     name="hyperbolic cosine"),
1068     OPERATOR(nickname="tanh",\
1069     rng=[-5.,5.],
1070     test_expr="math.tanh(%a1%)",
1071     numarray_expr="numarray.tanh(%a1%)",
1072     diff="1./cosh(%a1%)**2",
1073     name="hyperbolic tangent"),
1074     OPERATOR(nickname="asinh",\
1075     rng=[-100.,100.], \
1076     test_expr="numarray.arcsinh(%a1%)",
1077     math_expr="numarray.arcsinh(%a1%)",
1078     numarray_expr="numarray.arcsinh(%a1%)",
1079     diff="1./sqrt(%a1%**2+1)",
1080     name="inverse hyperbolic sine"),
1081     OPERATOR(nickname="acosh",\
1082     rng=[1.001,100.],\
1083     test_expr="numarray.arccosh(%a1%)",
1084     math_expr="numarray.arccosh(%a1%)",
1085     numarray_expr="numarray.arccosh(%a1%)",
1086     diff="1./sqrt(%a1%**2-1)",
1087     name="inverse hyperolic cosine"),
1088     OPERATOR(nickname="atanh",\
1089     rng=[-0.99,0.99], \
1090     test_expr="numarray.arctanh(%a1%)",
1091     math_expr="numarray.arctanh(%a1%)",
1092     numarray_expr="numarray.arctanh(%a1%)",
1093     diff="1./(1.-%a1%**2)",
1094     name="inverse hyperbolic tangent"),
1095     OPERATOR(nickname="exp",\
1096     rng=[-5.,5.],
1097     test_expr="math.exp(%a1%)",
1098     numarray_expr="numarray.exp(%a1%)",
1099     diff="self",
1100     name="exponential"),
1101     OPERATOR(nickname="sqrt",\
1102     rng=[1.e-3,100.],\
1103     test_expr="math.sqrt(%a1%)",
1104     numarray_expr="numarray.sqrt(%a1%)",
1105     diff="0.5/self",
1106     name="square root"),
1107     OPERATOR(nickname="log", \
1108     rng=[1.e-3,100.],\
1109     test_expr="math.log(%a1%)",
1110     numarray_expr="numarray.log(%a1%)",
1111     diff="1./arg",
1112     name="natural logarithm"),
1113     OPERATOR(nickname="sign",\
1114     rng=[-100.,100.], \
1115     math_expr="if %a1%>0:\n return 1.\nelif %a1%<0:\n return -1.\nelse:\n return 0.",
1116     test_expr="wherepos(%a1%)-wherepos(-%a1%)",
1117     numarray_expr="numarray.sign(%a1%)",
1118     symbol_expr="wherePositive(%a1%)-whereNegative(%a1%)",\
1119     name="sign"),
1120     OPERATOR(nickname="abs",\
1121     rng=[-100.,100.], \
1122     math_expr="if %a1%>0:\n return %a1% \nelif %a1%<0:\n return -(%a1%)\nelse:\n return 0.",
1123     test_expr="wherepos(%a1%)*(%a1%)-wherepos(-%a1%)*(%a1%)",
1124     numarray_expr="abs(%a1%)",
1125     diff="sign(%a1%)",
1126     name="absolute value")
1127    
1128     ]
1129     for f in func:
1130     symbol_name=f.nickname[0].upper()+f.nickname[1:]
1131     if f.nickname!="abs":
1132     u_prog+="def %s(arg):\n"%f.nickname
1133     u_prog+=" \"\"\"\n"
1134     u_prog+=" returns %s of argument arg\n\n"%f.name
1135     u_prog+=" @param arg: argument\n"
1136     u_prog+=" @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.\n"
1137     u_prog+=" @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.\n"
1138     u_prog+=" @raises TypeError: if the type of the argument is not expected.\n"
1139     u_prog+=" \"\"\"\n"
1140     u_prog+=" if isinstance(arg,numarray.NumArray):\n"
1141     u_prog+=mkCode(f.numarray_expr,["arg"],2*" ")
1142     u_prog+=" elif isinstance(arg,escript.Data):\n"
1143     u_prog+=mkCode("arg._%s()"%f.nickname,[],2*" ")
1144     u_prog+=" elif isinstance(arg,float):\n"
1145     u_prog+=mkCode(f.math_expr,["arg"],2*" ")
1146     u_prog+=" elif isinstance(arg,int):\n"
1147     u_prog+=mkCode(f.math_expr,["float(arg)"],2*" ")
1148     u_prog+=" elif isinstance(arg,Symbol):\n"
1149     if f.symbol_expr==None:
1150     u_prog+=mkCode("%s_Symbol(arg)"%symbol_name,[],2*" ")
1151     else:
1152     u_prog+=mkCode(f.symbol_expr,["arg"],2*" ")
1153     u_prog+=" else:\n"
1154     u_prog+=" raise TypeError,\"%s: Unknown argument type.\"\n\n"%f.nickname
1155     if f.symbol_expr==None:
1156     u_prog+="class %s_Symbol(DependendSymbol):\n"%symbol_name
1157     u_prog+=" \"\"\"\n"
1158     u_prog+=" L{Symbol} representing the result of the %s function\n"%f.name
1159     u_prog+=" \"\"\"\n"
1160     u_prog+=" def __init__(self,arg):\n"
1161     u_prog+=" \"\"\"\n"
1162     u_prog+=" initialization of %s L{Symbol} with argument arg\n"%f.nickname
1163     u_prog+=" @param arg: argument of function\n"
1164     u_prog+=" @type arg: typically L{Symbol}.\n"
1165     u_prog+=" \"\"\"\n"
1166     u_prog+=" DependendSymbol.__init__(self,args=[arg],shape=arg.getShape(),dim=arg.getDim())\n"
1167     u_prog+="\n"
1168    
1169     u_prog+=" def getMyCode(self,argstrs,format=\"escript\"):\n"
1170     u_prog+=" \"\"\"\n"
1171     u_prog+=" returns a program code that can be used to evaluate the symbol.\n\n"
1172 jgs 154
1173 gross 157 u_prog+=" @param argstrs: gives for each argument a string representing the argument for the evaluation.\n"
1174     u_prog+=" @type argstrs: C{str} or a C{list} of length 1 of C{str}.\n"
1175     u_prog+=" @param format: specifies the format to be used. At the moment only \"escript\" ,\"text\" and \"str\" are supported.\n"
1176     u_prog+=" @type format: C{str}\n"
1177     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"
1178     u_prog+=" @rtype: C{str}\n"
1179     u_prog+=" @raise: NotImplementedError: if the requested format is not available\n"
1180     u_prog+=" \"\"\"\n"
1181     u_prog+=" if isinstance(argstrs,list):\n"
1182     u_prog+=" argstrs=argstrs[0]\n"
1183     u_prog+=" if format==\"escript\" or format==\"str\" or format==\"text\":\n"
1184     u_prog+=" return \"%s(%%s)\"%%argstrs\n"%f.nickname
1185     u_prog+=" else:\n"
1186     u_prog+=" raise NotImplementedError,\"%s_Symbol does not provide program code for format %%s.\"%%format\n"%symbol_name
1187     u_prog+="\n"
1188 jgs 154
1189 gross 157 u_prog+=" def substitute(self,argvals):\n"
1190     u_prog+=" \"\"\"\n"
1191     u_prog+=" assigns new values to symbols in the definition of the symbol.\n"
1192     u_prog+=" The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.\n"
1193     u_prog+="\n"
1194     u_prog+=" @param argvals: new values assigned to symbols\n"
1195     u_prog+=" @type argvals: C{dict} with keywords of type L{Symbol}.\n"
1196     u_prog+=" @return: result of the substitution process. Operations are executed as much as possible.\n"
1197     u_prog+=" @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution\n"
1198     u_prog+=" @raise TypeError: if a value for a L{Symbol} cannot be substituted.\n"
1199     u_prog+=" \"\"\"\n"
1200     u_prog+=" if argvals.has_key(self):\n"
1201     u_prog+=" arg=argvals[self]\n"
1202     u_prog+=" if self.isAppropriateValue(arg):\n"
1203     u_prog+=" return arg\n"
1204     u_prog+=" else:\n"
1205     u_prog+=" raise TypeError,\"%s: new value is not appropriate.\"%str(self)\n"
1206     u_prog+=" else:\n"
1207     u_prog+=" arg=self.getSubstitutedArguments(argvals)[0]\n"
1208     u_prog+=" return %s(arg)\n\n"%f.nickname
1209     if not f.diff==None:
1210     u_prog+=" def diff(self,arg):\n"
1211     u_prog+=" \"\"\"\n"
1212     u_prog+=" differential of this object\n"
1213     u_prog+="\n"
1214     u_prog+=" @param arg: the derivative is calculated with respect to arg\n"
1215     u_prog+=" @type arg: L{escript.Symbol}\n"
1216     u_prog+=" @return: derivative with respect to C{arg}\n"
1217     u_prog+=" @rtype: typically L{Symbol} but other types such as C{float}, L{escript.Data}, L{numarray.NumArray} are possible.\n"
1218     u_prog+=" \"\"\"\n"
1219     u_prog+=" if arg==self:\n"
1220     u_prog+=" return identity(self.getShape())\n"
1221     u_prog+=" else:\n"
1222     u_prog+=" myarg=self.getArgument()[0]\n"
1223     u_prog+=" val=matchShape(%s,self.getDifferentiatedArguments(arg)[0])\n"%f.diff.replace("%a1%","myarg")
1224     u_prog+=" return val[0]*val[1]\n\n"
1225 jgs 154
1226 gross 157 for case in case_set:
1227     for sh in shape_set:
1228     if not case=="float" or len(sh)==0:
1229     text=""
1230     text+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1231     tname="def test_%s_%s_rank%s"%(f.nickname,case,len(sh))
1232     text+=" %s(self):\n"%tname
1233     a=makeArray(sh,f.rng)
1234     a1=makeArray(sh,f.rng)
1235     r1=makeResult(a1,f.test_expr)
1236     r=makeResult(a,f.test_expr)
1237    
1238     text+=mkText(case,"arg",a,a1)
1239     text+=" res=%s(arg)\n"%f.nickname
1240     if case=="Symbol":
1241     text+=mkText("array","s",a,a1)
1242     text+=" sub=res.substitute({arg:s})\n"
1243     text+=mkText("array","ref",r,r1)
1244     res="sub"
1245     else:
1246     text+=mkText(case,"ref",r,r1)
1247     res="res"
1248     text+=mkTypeAndShapeTest(case,sh,"res")
1249     text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1250     if case == "taggedData":
1251     t_prog_with_tags+=text
1252     else:
1253     t_prog+=text
1254    
1255     #=========== END OF GOOD CODE +++++++++++++++++++++++++++
1256 jgs 154
1257 gross 157 1/0
1258 jgs 154
1259 gross 157 def X():
1260     if args=="float":
1261     a=makeArray(sh,f[RANGE])
1262 jgs 154 r=makeResult(a,f)
1263 gross 157 t_prog+=" arg=%s\n"%a[0]
1264     t_prog+=" ref=%s\n"%r[0]
1265     t_prog+=" res=%s(%a1%)\n"%f.nickname
1266     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1267     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1268     elif args == "array":
1269     a=makeArray(sh,f[RANGE])
1270     r=makeResult(a,f)
1271 jgs 154 if len(sh)==0:
1272 gross 157 t_prog+=" arg=numarray.array(%s)\n"%a[0]
1273     t_prog+=" ref=numarray.array(%s)\n"%r[0]
1274 jgs 154 else:
1275     t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
1276     t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
1277 gross 157 t_prog+=" res=%s(%a1%)\n"%f.nickname
1278     t_prog+=" self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh)
1279     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1280 jgs 154 elif args== "constData":
1281 gross 157 a=makeArray(sh,f[RANGE])
1282 jgs 154 r=makeResult(a,f)
1283     if len(sh)==0:
1284     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1285     t_prog+=" ref=%s\n"%r
1286     else:
1287     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1288     t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
1289 gross 157 t_prog+=" res=%s(%a1%)\n"%f.nickname
1290 jgs 154 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)
1291     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1292     elif args in [ "taggedData","expandedData"]:
1293 gross 157 a=makeArray(sh,f[RANGE])
1294 jgs 154 r=makeResult(a,f)
1295 gross 157 a1=makeArray(sh,f[RANGE])
1296 jgs 154 r1=makeResult(a1,f)
1297     if len(sh)==0:
1298     if args=="expandedData":
1299     t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a)
1300     t_prog+=" ref=Data(%s,self.functionspace,True)\n"%(r)
1301     else:
1302     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1303     t_prog+=" ref=Data(%s,self.functionspace)\n"%(r)
1304     t_prog+=" arg.setTaggedValue(1,%s)\n"%a
1305     t_prog+=" ref.setTaggedValue(1,%s)\n"%r1
1306     else:
1307     if args=="expandedData":
1308     t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())
1309     t_prog+=" ref=Data(numarray.array(%s),self.functionspace,True)\n"%(r.tolist())
1310     else:
1311     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1312     t_prog+=" ref=Data(numarray.array(%s),self.functionspace)\n"%(r.tolist())
1313     t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist()
1314     t_prog+=" ref.setTaggedValue(1,%s)\n"%r1.tolist()
1315 gross 157 t_prog+=" res=%s(%a1%)\n"%f.nickname
1316 jgs 154 t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)
1317     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1318     elif args=="Symbol":
1319     t_prog+=" arg=Symbol(shape=%s)\n"%str(sh)
1320 gross 157 t_prog+=" v=%s(%a1%)\n"%f.nickname
1321 jgs 154 t_prog+=" self.failUnlessRaises(ValueError,v.substitute,Symbol(shape=(1,1)),\"illegal shape of substitute not identified.\")\n"
1322 gross 157 a=makeArray(sh,f[RANGE])
1323 jgs 154 r=makeResult(a,f)
1324     if len(sh)==0:
1325     t_prog+=" res=v.substitute({arg : %s})\n"%a
1326     t_prog+=" ref=%s\n"%r
1327     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1328     else:
1329     t_prog+=" res=v.substitute({arg : numarray.array(%s)})\n"%a.tolist()
1330     t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
1331     t_prog+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of substitution result.\")\n"%str(sh)
1332     t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1333    
1334     if len(sh)==0:
1335     t_prog+=" # test derivative with respect to itself:\n"
1336     t_prog+=" dvdv=v.diff(v)\n"
1337     t_prog+=" self.failUnlessEqual(dvdv,1.,\"derivative with respect to self is not 1.\")\n"
1338     elif len(sh)==1:
1339     t_prog+=" # test derivative with respect to itself:\n"
1340     t_prog+=" dvdv=v.diff(v)\n"
1341     t_prog+=" self.failUnlessEqual(dvdv.shape,%s,\"shape of derivative with respect is wrong\")\n"%str(sh+sh)
1342     for i0_l in range(sh[0]):
1343     for i0_r in range(sh[0]):
1344     if i0_l == i0_r:
1345     v=1.
1346     else:
1347     v=0.
1348     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)
1349     elif len(sh)==2:
1350     t_prog+=" # test derivative with respect to itself:\n"
1351     t_prog+=" dvdv=v.diff(v)\n"
1352     t_prog+=" self.failUnlessEqual(dvdv.shape,%s,\"shape of derivative with respect is wrong\")\n"%str(sh+sh)
1353     for i0_l in range(sh[0]):
1354     for i0_r in range(sh[0]):
1355     for i1_l in range(sh[1]):
1356     for i1_r in range(sh[1]):
1357     if i0_l == i0_r and i1_l == i1_r:
1358     v=1.
1359     else:
1360     v=0.
1361     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)
1362    
1363     for sh_in in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1364     if len(sh_in)+len(sh)<=4:
1365    
1366     t_prog+=" # test derivative with shape %s as argument\n"%str(sh_in)
1367     trafo=makeArray(sh+sh_in,[0,1.])
1368 gross 157 a_in=makeArray(sh_in,f[RANGE])
1369 jgs 154 t_prog+=" arg_in=Symbol(shape=%s)\n"%str(sh_in)
1370     t_prog+=" arg2=Symbol(shape=%s)\n"%str(sh)
1371    
1372     if len(sh)==0:
1373     t_prog+=" arg2="
1374     if len(sh_in)==0:
1375     t_prog+="%s*arg_in\n"%trafo
1376     elif len(sh_in)==1:
1377     for i0 in range(sh_in[0]):
1378     if i0>0: t_prog+="+"
1379     t_prog+="%s*arg_in[%s]"%(trafo[i0],i0)
1380     t_prog+="\n"
1381     elif len(sh_in)==2:
1382     for i0 in range(sh_in[0]):
1383     for i1 in range(sh_in[1]):
1384     if i0+i1>0: t_prog+="+"
1385     t_prog+="%s*arg_in[%s,%s]"%(trafo[i0,i1],i0,i1)
1386    
1387     elif len(sh_in)==3:
1388     for i0 in range(sh_in[0]):
1389     for i1 in range(sh_in[1]):
1390     for i2 in range(sh_in[2]):
1391     if i0+i1+i2>0: t_prog+="+"
1392     t_prog+="%s*arg_in[%s,%s,%s]"%(trafo[i0,i1,i2],i0,i1,i2)
1393     elif len(sh_in)==4:
1394     for i0 in range(sh_in[0]):
1395     for i1 in range(sh_in[1]):
1396     for i2 in range(sh_in[2]):
1397     for i3 in range(sh_in[3]):
1398     if i0+i1+i2+i3>0: t_prog+="+"
1399     t_prog+="%s*arg_in[%s,%s,%s,%s]"%(trafo[i0,i1,i2,i3],i0,i1,i2,i3)
1400     t_prog+="\n"
1401     elif len(sh)==1:
1402     for j0 in range(sh[0]):
1403     t_prog+=" arg2[%s]="%j0
1404     if len(sh_in)==0:
1405     t_prog+="%s*arg_in"%trafo[j0]
1406     elif len(sh_in)==1:
1407     for i0 in range(sh_in[0]):
1408     if i0>0: t_prog+="+"
1409     t_prog+="%s*arg_in[%s]"%(trafo[j0,i0],i0)
1410     elif len(sh_in)==2:
1411     for i0 in range(sh_in[0]):
1412     for i1 in range(sh_in[1]):
1413     if i0+i1>0: t_prog+="+"
1414     t_prog+="%s*arg_in[%s,%s]"%(trafo[j0,i0,i1],i0,i1)
1415     elif len(sh_in)==3:
1416     for i0 in range(sh_in[0]):
1417     for i1 in range(sh_in[1]):
1418     for i2 in range(sh_in[2]):
1419     if i0+i1+i2>0: t_prog+="+"
1420     t_prog+="%s*arg_in[%s,%s,%s]"%(trafo[j0,i0,i1,i2],i0,i1,i2)
1421     t_prog+="\n"
1422     elif len(sh)==2:
1423     for j0 in range(sh[0]):
1424     for j1 in range(sh[1]):
1425     t_prog+=" arg2[%s,%s]="%(j0,j1)
1426     if len(sh_in)==0:
1427     t_prog+="%s*arg_in"%trafo[j0,j1]
1428     elif len(sh_in)==1:
1429     for i0 in range(sh_in[0]):
1430     if i0>0: t_prog+="+"
1431     t_prog+="%s*arg_in[%s]"%(trafo[j0,j1,i0],i0)
1432     elif len(sh_in)==2:
1433     for i0 in range(sh_in[0]):
1434     for i1 in range(sh_in[1]):
1435     if i0+i1>0: t_prog+="+"
1436     t_prog+="%s*arg_in[%s,%s]"%(trafo[j0,j1,i0,i1],i0,i1)
1437     t_prog+="\n"
1438     elif len(sh)==3:
1439     for j0 in range(sh[0]):
1440     for j1 in range(sh[1]):
1441     for j2 in range(sh[2]):
1442     t_prog+=" arg2[%s,%s,%s]="%(j0,j1,j2)
1443     if len(sh_in)==0:
1444     t_prog+="%s*arg_in"%trafo[j0,j1,j2]
1445     elif len(sh_in)==1:
1446     for i0 in range(sh_in[0]):
1447     if i0>0: t_prog+="+"
1448     t_prog+="%s*arg_in[%s]"%(trafo[j0,j1,j2,i0],i0)
1449     t_prog+="\n"
1450     elif len(sh)==4:
1451     for j0 in range(sh[0]):
1452     for j1 in range(sh[1]):
1453     for j2 in range(sh[2]):
1454     for j3 in range(sh[3]):
1455     t_prog+=" arg2[%s,%s,%s,%s]="%(j0,j1,j2,j3)
1456     if len(sh_in)==0:
1457     t_prog+="%s*arg_in"%trafo[j0,j1,j2,j3]
1458     t_prog+="\n"
1459     t_prog+=" dvdin=v.substitute({arg : arg2}).diff(arg_in)\n"
1460     if len(sh_in)==0:
1461     t_prog+=" res_in=dvdin.substitute({arg_in : %s})\n"%a_in
1462     else:
1463     t_prog+=" res_in=dvdin.substitute({arg : numarray.array(%s)})\n"%a_in.tolist()
1464    
1465     if len(sh)==0:
1466     if len(sh_in)==0:
1467     ref_diff=(makeResult(trafo*a_in+finc,f)-makeResult(trafo*a_in,f))/finc
1468     t_prog+=" self.failUnlessAlmostEqual(dvdin,%s,self.places,\"%s-derivative: wrong derivative\")\n"%(ref_diff,str(sh_in))
1469     elif len(sh_in)==1:
1470     s=0
1471     for k0 in range(sh_in[0]):
1472     s+=trafo[k0]*a_in[k0]
1473     for i0 in range(sh_in[0]):
1474     ref_diff=(makeResult(s+trafo[i0]*finc,f)-makeResult(s,f))/finc
1475     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))
1476     elif len(sh_in)==2:
1477     s=0
1478     for k0 in range(sh_in[0]):
1479     for k1 in range(sh_in[1]):
1480     s+=trafo[k0,k1]*a_in[k0,k1]
1481     for i0 in range(sh_in[0]):
1482     for i1 in range(sh_in[1]):
1483     ref_diff=(makeResult(s+trafo[i0,i1]*finc,f)-makeResult(s,f))/finc
1484     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)))
1485    
1486     elif len(sh_in)==3:
1487     s=0
1488     for k0 in range(sh_in[0]):
1489     for k1 in range(sh_in[1]):
1490     for k2 in range(sh_in[2]):
1491     s+=trafo[k0,k1,k2]*a_in[k0,k1,k2]
1492     for i0 in range(sh_in[0]):
1493     for i1 in range(sh_in[1]):
1494     for i2 in range(sh_in[2]):
1495     ref_diff=(makeResult(s+trafo[i0,i1,i2]*finc,f)-makeResult(s,f))/finc
1496     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)))
1497     elif len(sh_in)==4:
1498     s=0
1499     for k0 in range(sh_in[0]):
1500     for k1 in range(sh_in[1]):
1501     for k2 in range(sh_in[2]):
1502     for k3 in range(sh_in[3]):
1503     s+=trafo[k0,k1,k2,k3]*a_in[k0,k1,k2,k3]
1504     for i0 in range(sh_in[0]):
1505     for i1 in range(sh_in[1]):
1506     for i2 in range(sh_in[2]):
1507     for i3 in range(sh_in[3]):
1508     ref_diff=(makeResult(s+trafo[i0,i1,i2,i3]*finc,f)-makeResult(s,f))/finc
1509     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)))
1510     elif len(sh)==1:
1511     for j0 in range(sh[0]):
1512     if len(sh_in)==0:
1513     ref_diff=(makeResult(trafo[j0]*a_in+finc,f)-makeResult(trafo[j0]*a_in,f))/finc
1514     t_prog+=" self.failUnlessAlmostEqual(dvdin[%s],%s,self.places,\"%s-derivative: wrong derivative of %s\")\n"%(j0,ref_diff,str(sh_in),j0)
1515     elif len(sh_in)==1:
1516     s=0
1517     for k0 in range(sh_in[0]):
1518     s+=trafo[j0,k0]*a_in[k0]
1519     for i0 in range(sh_in[0]):
1520     ref_diff=(makeResult(s+trafo[j0,i0]*finc,f)-makeResult(s,f))/finc
1521     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))
1522     elif len(sh_in)==2:
1523     s=0
1524     for k0 in range(sh_in[0]):
1525     for k1 in range(sh_in[1]):
1526     s+=trafo[j0,k0,k1]*a_in[k0,k1]
1527     for i0 in range(sh_in[0]):
1528     for i1 in range(sh_in[1]):
1529     ref_diff=(makeResult(s+trafo[j0,i0,i1]*finc,f)-makeResult(s,f))/finc
1530     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)))
1531    
1532     elif len(sh_in)==3:
1533    
1534     s=0
1535     for k0 in range(sh_in[0]):
1536     for k1 in range(sh_in[1]):
1537     for k2 in range(sh_in[2]):
1538     s+=trafo[j0,k0,k1,k2]*a_in[k0,k1,k2]
1539    
1540     for i0 in range(sh_in[0]):
1541     for i1 in range(sh_in[1]):
1542     for i2 in range(sh_in[2]):
1543     ref_diff=(makeResult(s+trafo[j0,i0,i1,i2]*finc,f)-makeResult(s,f))/finc
1544     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)))
1545     elif len(sh)==2:
1546     for j0 in range(sh[0]):
1547     for j1 in range(sh[1]):
1548     if len(sh_in)==0:
1549     ref_diff=(makeResult(trafo[j0,j1]*a_in+finc,f)-makeResult(trafo[j0,j1]*a_in,f))/finc
1550     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)))
1551     elif len(sh_in)==1:
1552     s=0
1553     for k0 in range(sh_in[0]):
1554     s+=trafo[j0,j1,k0]*a_in[k0]
1555     for i0 in range(sh_in[0]):
1556     ref_diff=(makeResult(s+trafo[j0,j1,i0]*finc,f)-makeResult(s,f))/finc
1557     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))
1558    
1559     elif len(sh_in)==2:
1560     s=0
1561     for k0 in range(sh_in[0]):
1562     for k1 in range(sh_in[1]):
1563     s+=trafo[j0,j1,k0,k1]*a_in[k0,k1]
1564     for i0 in range(sh_in[0]):
1565     for i1 in range(sh_in[1]):
1566     ref_diff=(makeResult(s+trafo[j0,j1,i0,i1]*finc,f)-makeResult(s,f))/finc
1567     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)))
1568     elif len(sh)==3:
1569     for j0 in range(sh[0]):
1570     for j1 in range(sh[1]):
1571     for j2 in range(sh[2]):
1572     if len(sh_in)==0:
1573     ref_diff=(makeResult(trafo[j0,j1,j2]*a_in+finc,f)-makeResult(trafo[j0,j1,j2]*a_in,f))/finc
1574     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)))
1575     elif len(sh_in)==1:
1576     s=0
1577     for k0 in range(sh_in[0]):
1578     s+=trafo[j0,j1,j2,k0]*a_in[k0]
1579     for i0 in range(sh_in[0]):
1580     ref_diff=(makeResult(s+trafo[j0,j1,j2,i0]*finc,f)-makeResult(s,f))/finc
1581     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)
1582     elif len(sh)==4:
1583     for j0 in range(sh[0]):
1584     for j1 in range(sh[1]):
1585     for j2 in range(sh[2]):
1586     for j3 in range(sh[3]):
1587     if len(sh_in)==0:
1588     ref_diff=(makeResult(trafo[j0,j1,j2,j3]*a_in+finc,f)-makeResult(trafo[j0,j1,j2,j3]*a_in,f))/finc
1589     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)))
1590 gross 157
1591 gross 291 #
1592 gross 157
1593     #==================
1594     cases=["Scalar","Vector","Tensor", "Tensor3","Tensor4"]
1595    
1596     for case in range(len(cases)):
1597     for d in [ None , "d", 1, 2 , 3]:
1598     if not d==None or cases[case]=="Scalar":
1599     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1600     tname="test_Symbol_%s_d%s"%(cases[case],d)
1601     t_prog+=" def %s(self):\n"%tname
1602     if d=="d":
1603     t_prog+=" s=%sSymbol(dim=self.functionspace)\n"%(cases[case])
1604     t_prog+=" d=self.functionspace.getDim()\n"
1605     sh="("
1606     for i in range(case):
1607     if i==0:
1608     sh+=d
1609     else:
1610     sh+=","+d
1611     sh+=")"
1612     else:
1613     t_prog+=" s=%sSymbol(dim=%s)\n"%(cases[case],d)
1614     sh=()
1615     for i in range(case): sh=sh+(d,)
1616     t_prog+=" self.failUnlessEqual(s.getRank(),%s,\"wrong rank.\")\n"%case
1617     t_prog+=" self.failUnlessEqual(s.getShape(),%s,\"wrong shape.\")\n"%str(sh)
1618     t_prog+=" self.failUnlessEqual(s.getDim(),%s,\"wrong spatial dimension.\")\n"%d
1619     t_prog+=" self.failUnlessEqual(s.getArgument(),[],\"wrong arguments.\")\n"
1620    
1621     print t_prog
1622     1/0
1623     for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1624     for d in [ None , "domain", 1, 2 , 3]:
1625     for args in [ [], ["s2"], [1,-1.] ]:
1626     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1627     tname="def test_Symbol_rank%s_d%s_nargs%s"%(len(sh),d,len(args))
1628     t_prog+=" %s(self):\n"%tname
1629     t_prog+=" s2=ScalarSymbol()\n"
1630     if args==["s2"]:
1631     a="[s2]"
1632     else:
1633     a=str(args)
1634     if d=="domain":
1635     t_prog+=" s=Symbol(shape=%s,dim=self.functionspace.getDim(),args=%s)\n"%(str(sh),a)
1636     d2="self.functionspace.getDim()"
1637     else:
1638     t_prog+=" s=Symbol(shape=%s,dim=%s,args=%s)\n"%(sh,d,a)
1639     d2=str(d)
1640    
1641     t_prog+=" self.failUnlessEqual(s.getRank(),%s,\"wrong rank.\")\n"%len(sh)
1642     t_prog+=" self.failUnlessEqual(s.getShape(),%s,\"wrong shape.\")\n"%str(sh)
1643     t_prog+=" self.failUnlessEqual(s.getDim(),%s,\"wrong spatial dimension.\")\n"%d2
1644     t_prog+=" self.failUnlessEqual(s.getArgument(),%s,\"wrong arguments.\")\n\n"%a
1645     t_prog+=" ss=s.substitute({s:numarray.zeros(%s)})\n"%str(sh)
1646     t_prog+=" self.failUnless(isinstance(ss,numarray.NumArray),\"value after substitution is not numarray.\")\n"
1647     t_prog+=" self.failUnlessEqual(ss.shape,%s,\"value after substitution has not expected shape\")\n"%str(sh)
1648    
1649     t_prog+=" try:\n s.substitute({s:numarray.zeros((5,))})\n fail(\"illegal substition was successful\")\n"
1650     t_prog+=" except TypeError:\n pass\n\n"
1651    
1652     ###
1653     for sh2 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1654     if len(sh+sh2)<5:
1655     t_prog+=" dsdarg=s.diff(Symbol(shape=%s))\n"%str(sh2)
1656     if len(sh+sh2)==0:
1657     t_prog+=" self.failUnless(isinstance(dsdarg,float),\"ds/ds() has wrong type.\")\n"
1658     else:
1659     t_prog+=" self.failUnless(isinstance(dsdarg,numarray.NumArray),\"ds/ds%s has wrong type.\")\n"%str(sh2)
1660    
1661     t_prog+=" self.failUnlessEqual(dsdarg.shape,%s,\"ds/ds%s has wrong shape.\")\n"%(str(sh+sh2),str(sh2))
1662     t_prog+=" self.failIf(Lsup(dsdarg)>0.,\"ds/ds%s has wrong value.\")\n"%str(sh2)
1663     if len(sh)<3:
1664     t_prog+="\n dsds=s.diff(s)\n"
1665     if len(sh)==0:
1666     t_prog+=" self.failUnless(isinstance(dsds,float),\"ds/ds has wrong type.\")\n"
1667     t_prog+=" self.failUnlessEqual(dsds,1.,\"ds/ds has wrong value.\")\n"
1668     else:
1669     t_prog+=" self.failUnless(isinstance(dsds,numarray.NumArray),\"ds/ds has wrong type.\")\n"
1670     t_prog+=" self.failUnlessEqual(dsds.shape,%s,\"ds/ds has wrong shape.\")\n"%str(sh+sh)
1671     if len(sh)==1:
1672     for i0 in range(sh[0]):
1673     for i2 in range(sh[0]):
1674     if i0==i2:
1675     v=1.
1676     else:
1677     v=0.
1678     t_prog+=" self.failUnlessEqual(dsds[%s,%s],%s,\"ds/ds has wrong value at (%s,%s).\")\n"%(i0,i2,v,i0,i2)
1679     else:
1680     for i0 in range(sh[0]):
1681     for i1 in range(sh[1]):
1682     for i2 in range(sh[0]):
1683     for i3 in range(sh[1]):
1684     if i0==i2 and i1==i3:
1685     v=1.
1686     else:
1687     v=0.
1688     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)
1689    
1690     ###
1691     t_prog+="\n"
1692     for i in range(len(args)):
1693     t_prog+=" self.failUnlessEqual(s.getArgument(%s),%s,\"wrong argument %s.\")\n"%(i,str(args[i]),i)
1694     t_prog+=" sa=s.getSubstitutedArguments({s2:-10})\n"
1695     t_prog+=" self.failUnlessEqual(len(sa),%s,\"wrong number of substituted arguments\")\n"%len(args)
1696     if args==["s2"]:
1697     t_prog+=" self.failUnlessEqual(sa[0],-10,\"wrongly substituted argument 0.\")\n"
1698     else:
1699     for i in range(len(args)):
1700     t_prog+=" self.failUnlessEqual(sa[%s],%s,\"wrongly substituted argument %s.\")\n"%(i,str(args[i]),i)
1701    
1702     t_prog+="\n"
1703     for arg in ["10.", "10", "SymbolMatch", "SymbolMisMatch", \
1704     "DataMatch","DataMisMatch", "NumArrayMatch", "NumArrayMisMatch"]:
1705     if arg in ["10.", "10"]:
1706     a=str(arg)
1707     if len(sh)==0:
1708     t_prog+=" self.failUnless(s.isAppropriateValue(%s),\"%s is appropriate substitute\")\n"%(a,arg)
1709     else:
1710     t_prog+=" self.failIf(s.isAppropriateValue(%s),\" %s is not appropriate substitute\")\n"%(a,arg)
1711     elif arg in ["SymbolMatch", "SymbolMisMatch"]:
1712     if arg=="SymbolMatch":
1713     t_prog+=" self.failUnless(s.isAppropriateValue(Symbol(shape=%s,dim=%s)),\"Symbol is appropriate substitute\")\n"%(str(sh),d)
1714     else:
1715     if isinstance(d,int):
1716     t_prog+=" self.failIf(s.isAppropriateValue(Symbol(shape=%s,dim=%s)),\"Symbol is not appropriate substitute (dim)\")\n"%(str(sh),d+1)
1717     else:
1718     t_prog+=" self.failIf(s.isAppropriateValue(Symbol(shape=%s)),\"Symbol is not appropriate substitute (shape)\")\n"%((5,))
1719    
1720     elif arg in ["DataMatch","DataMisMatch"]:
1721     if arg=="DataMatch" and d=="domain":
1722     t_prog+=" self.failUnless(s.isAppropriateValue(escript.Data(0.,%s,self.functionspace)),\"Data is appropriate substitute\")\n"%str(sh)
1723     elif arg=="DataMisMatch":
1724     t_prog+=" self.failIf(s.isAppropriateValue(escript.Data(0.,%s,self.functionspace)),\"Data is not appropriate substitute (shape)\")\n"%(str((5,)))
1725     else:
1726     if arg=="NumArrayMatch":
1727     t_prog+=" self.failUnless(s.isAppropriateValue(numarray.zeros(%s)),\"NumArray is appropriate substitute\")\n"%str(sh)
1728     else:
1729     t_prog+=" self.failIf(s.isAppropriateValue(numarray.zeros(%s)),\"NumArray is not appropriate substitute (shape)\")\n"%(str((5,)))
1730 jgs 154 print t_prog
1731 gross 157 1/0
1732    
1733    
1734     for case in ["Lsup", "sup", "inf"]:
1735     for args in ["float","array","constData","taggedData","expandedData"]:
1736     for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1737     if not args=="float" or len(sh)==0:
1738     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1739     tname="def test_%s_%s_rank%s"%(case,args,len(sh))
1740     t_prog+=" %s(self):\n"%tname
1741     if args in ["float","array" ]:
1742     a=makeArray(sh,[-1,1])
1743     r=makeResult2(a,case)
1744     if len(sh)==0:
1745     t_prog+=" arg=%s\n"%a
1746     else:
1747     t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
1748     t_prog+=" ref=%s\n"%r
1749     t_prog+=" res=%s(%a1%)\n"%case
1750     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1751     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1752     elif args== "constData":
1753     a=makeArray(sh,[-1,1])
1754     r=makeResult2(a,case)
1755     if len(sh)==0:
1756     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1757     else:
1758     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1759     t_prog+=" ref=%s\n"%r
1760     t_prog+=" res=%s(%a1%)\n"%case
1761     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1762     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1763     elif args in [ "taggedData","expandedData"]:
1764     a=makeArray(sh,[-1,1])
1765     r=makeResult2(a,case)
1766     a1=makeArray(sh,[-1,1])
1767     r1=makeResult2(a1,case)
1768     if case in ["Lsup","sup"]:
1769     r=max(r,r1)
1770     else:
1771     r=min(r,r1)
1772     if len(sh)==0:
1773     if args=="expandedData":
1774     t_prog+=" arg=Data(%s,self.functionspace,True)\n"%(a)
1775     else:
1776     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1777     t_prog+=" arg.setTaggedValue(1,%s)\n"%a
1778     else:
1779     if args=="expandedData":
1780     t_prog+=" arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())
1781     else:
1782     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1783     t_prog+=" arg.setTaggedValue(1,%s)\n"%a1.tolist()
1784     t_prog+=" res=%s(%a1%)\n"%case
1785     t_prog+=" ref=%s\n"%r
1786     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1787     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1788    
1789     print t_prog
1790    
1791     1/0
1792    
1793    
1794     for case in ["Lsup", "sup", "inf"]:
1795     for args in ["float","array","constData","taggedData","expandedData"]:
1796     for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1797     if not args=="float" or len(sh)==0:
1798     t_prog+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1799     tname="def test_%s_%s_rank%s"%(case,args,len(sh))
1800     t_prog+=" %s(self):\n"%tname
1801     if args in ["float","array" ]:
1802     a=makeArray(sh,[-1,1])
1803     r=makeResult2(a,case)
1804     if len(sh)==0:
1805     t_prog+=" arg=%s\n"%a
1806     else:
1807     t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
1808     t_prog+=" ref=%s\n"%r
1809     t_prog+=" res=%s(%a1%)\n"%case
1810     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1811     t_prog+=" self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
1812     elif args== "constData":
1813     a=makeArray(sh,[-1,1])
1814     r=makeResult2(a,case)
1815     if len(sh)==0:
1816     t_prog+=" arg=Data(%s,self.functionspace)\n"%(a)
1817     else:
1818     t_prog+=" arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1819     t_prog+=" ref=%s\n"%r
1820     t_prog+=" res=%s(%a1%)\n"%case
1821     t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1822     t_prog