# Diff of /trunk/escript/py_src/generateutil

revision 155 by jgs, Wed Nov 9 02:02:19 2005 UTC revision 157 by gross, Wed Nov 9 10:01:06 2005 UTC
# Line 1  Line 1
1  #!/usr/bin/python  #!/usr/bin/python
2  # \$Id\$  # \$Id:\$
3
4  """  """
5  program generates parts of the util.py and the test_util.py script  program generates parts of the util.py and the test_util.py script
6  """  """
7  u_prog ="import esys.escript\n"  test_header=""
8  u_prog+="import numarray\n"  test_header+="import unittest\n"
9  u_prog+="import math\n"  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  t_prog=""  t_prog=""
26    t_prog_with_tags=""
27    t_prog_failing=""
28    u_prog=""
29
30    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
func= [ ["atan", [-100.,100.], "math.atan(%a%)"],
["log", [1.e-8,10.], "math.log(%a%)"]
]
51  import random  import random
52  import numarray  import numarray
53  import math  import math
54  finc=1.e-6  finc=1.e-6
55
56    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
151
152  def makeArray(shape,rng):  def makeArray(shape,rng):
153     l=rng[1]-rng[0]     l=rng[1]-rng[0]
154     out=numarray.zeros(shape,numarray.Float64)     out=numarray.zeros(shape,numarray.Float64)
155     if len(shape)==0:     if len(shape)==0:
156         out=random.random()         out=l*random.random()+rng[0]
157     elif len(shape)==1:     elif len(shape)==1:
158         for i0 in range(shape[0]):         for i0 in range(shape[0]):
159                     out[i0]=l*random.random()+rng[0]                     out[i0]=l*random.random()+rng[0]
# Line 45  def makeArray(shape,rng): Line 176  def makeArray(shape,rng):
176         raise SystemError,"rank is restricted to 4"         raise SystemError,"rank is restricted to 4"
177     return out             return out
178
def makeResult2(val,case):
if isinstance(val,float):
out=val
elif len(val.shape)==0:
out=val[0]
elif len(val.shape)==1:
out=val[0]
for i0 in range(val.shape[0]):
if case == "Lsup":
out=max(out,abs(val[i0]))
elif case == "sup":
out=max(out,val[i0])
else:
out=min(out,val[i0])
elif len(val.shape)==2:
out=val[0,0]
for i0 in range(val.shape[0]):
for i1 in range(val.shape[1]):
if case == "Lsup":
out=max(out,abs(val[i0,i1]))
elif case == "sup":
out=max(out,val[i0,i1])
else:
out=min(out,val[i0,i1])
elif len(val.shape)==3:
out=val[0,0,0]
for i0 in range(val.shape[0]):
for i1 in range(val.shape[1]):
for i2 in range(val.shape[2]):
if case == "Lsup":
out=max(out,abs(val[i0,i1,i2]))
elif case == "sup":
out=max(out,val[i0,i1,i2])
else:
out=min(out,val[i0,i1,i2])
elif len(val.shape)==4:
out=val[0,0,0,0]
for i0 in range(val.shape[0]):
for i1 in range(val.shape[1]):
for i2 in range(val.shape[2]):
for i3 in range(val.shape[3]):
if case == "Lsup":
out=max(out,abs(val[i0,i1,i2,i3]))
elif case == "sup":
out=max(out,val[i0,i1,i2,i3])
else:
out=min(out,val[i0,i1,i2,i3])
else:
raise SystemError,"rank is restricted to 4"
return out
179
180  def makeResult(val,f):  def makeResult(val,test_expr):
181     if isinstance(val,float):     if isinstance(val,float):
182         out=eval(f[2].replace("%a%","val"))         out=eval(test_expr.replace("%a1%","val"))
183     elif len(val.shape)==0:     elif len(val.shape)==0:
184         out=eval(f[2].replace("%a%","val"))         out=eval(test_expr.replace("%a1%","val"))
185     elif len(val.shape)==1:     elif len(val.shape)==1:
186         out=numarray.zeros(val.shape,numarray.Float64)         out=numarray.zeros(val.shape,numarray.Float64)
187         for i0 in range(val.shape[0]):         for i0 in range(val.shape[0]):
188                     out[i0]=eval(f[2].replace("%a%","val[i0]"))                     out[i0]=eval(test_expr.replace("%a1%","val[i0]"))
189     elif len(val.shape)==2:     elif len(val.shape)==2:
190         out=numarray.zeros(val.shape,numarray.Float64)         out=numarray.zeros(val.shape,numarray.Float64)
191         for i0 in range(val.shape[0]):         for i0 in range(val.shape[0]):
192            for i1 in range(val.shape[1]):            for i1 in range(val.shape[1]):
193                     out[i0,i1]=eval(f[2].replace("%a%","val[i0,i1]"))                     out[i0,i1]=eval(test_expr.replace("%a1%","val[i0,i1]"))
194     elif len(val.shape)==3:     elif len(val.shape)==3:
195         out=numarray.zeros(val.shape,numarray.Float64)         out=numarray.zeros(val.shape,numarray.Float64)
196         for i0 in range(val.shape[0]):         for i0 in range(val.shape[0]):
197            for i1 in range(val.shape[1]):            for i1 in range(val.shape[1]):
198               for i2 in range(val.shape[2]):               for i2 in range(val.shape[2]):
199                     out[i0,i1,i2]=eval(f[2].replace("%a%","val[i0,i1,i2]"))                     out[i0,i1,i2]=eval(test_expr.replace("%a1%","val[i0,i1,i2]"))
200     elif len(val.shape)==4:     elif len(val.shape)==4:
201         out=numarray.zeros(val.shape,numarray.Float64)         out=numarray.zeros(val.shape,numarray.Float64)
202         for i0 in range(val.shape[0]):         for i0 in range(val.shape[0]):
203            for i1 in range(val.shape[1]):            for i1 in range(val.shape[1]):
204               for i2 in range(val.shape[2]):               for i2 in range(val.shape[2]):
205                  for i3 in range(val.shape[3]):                  for i3 in range(val.shape[3]):
206                     out[i0,i1,i2,i3]=eval(f[2].replace("%a%","val[i0,i1,i2,i3]"))                     out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val[i0,i1,i2,i3]"))
207     else:     else:
208         raise SystemError,"rank is restricted to 4"         raise SystemError,"rank is restricted to 4"
209     return out             return out
210
211    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
428  def mkText(case,name,a,a1=None):
429    def mkText(case,name,a,a1=None,use_tagging_for_expanded_data=False):
430           t_out=""           t_out=""
431           if case in ["float","array" ]:           if case=="float":
432             if isinstance(a,float):             if isinstance(a,float):
433                  t_out+="      %s=%s\n"%(name,a)                  t_out+="      %s=%s\n"%(name,a)
434             elif len(a)==1:             elif len(a)==1:
435                  t_out+="      %s=%s\n"%(name,a)                  t_out+="      %s=%s\n"%(name,a)
436             else:             else:
437                  t_out+="      %s=numarray.array(%s)\n"%(name,a.tolist())                  t_out+="      %s=numarray.array(%s)\n"%(name,a.tolist())
438             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           elif case=="constData":           elif case=="constData":
446             if isinstance(a,float):             if isinstance(a,float):
447                t_out+="      %s=Data(%s,self.functionspace)\n"%(name,a)                t_out+="      %s=Data(%s,self.functionspace)\n"%(name,a)
# Line 153  def mkText(case,name,a,a1=None): Line 458  def mkText(case,name,a,a1=None):
458                t_out+="      %s.setTaggedValue(1,%s)\n"%(name,a1)                t_out+="      %s.setTaggedValue(1,%s)\n"%(name,a1)
459             else:             else:
460                t_out+="      %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())                t_out+="      %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
461                t_out+="      %s.setTaggedValue(1,%s)\n"%(name,a1.tolist())                t_out+="      %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())
462           elif case=="expandedData":           elif case=="expandedData":
463             t_out+="      msk_%s=whereNegative(self.functionspace.getX()[0]-0.5)\n"%name             if use_tagging_for_expanded_data:
464             if isinstance(a,float):                if isinstance(a,float):
465                t_out+="      %s=msk_%s*%s+(1.-msk_%s)*%s\n"%(name,name,a,name,a1)                   t_out+="      %s=Data(%s,self.functionspace)\n"%(name,a)
466             elif len(a)==1:                   t_out+="      %s.setTaggedValue(1,%s)\n"%(name,a1)
467                t_out+="      %s=msk_%s*%s+(1.-msk_%s)*%s\n"%(name,name,a,name,a1)                elif len(a)==1:
468             else:                   t_out+="      %s=Data(%s,self.functionspace)\n"%(name,a)
469                t_out+="      %s=msk_%s*%s+(1.-msk_%s)*%s\n"%(name,name,a.tolist(),name,a1.tolist())                   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           elif case=="Symbol":           elif case=="Symbol":
483             if isinstance(a,float):             if isinstance(a,float):
484                t_out+="      %s=Symbol(shape=())\n"%(name)                t_out+="      %s=Symbol(shape=())\n"%(name)
# Line 171  def mkText(case,name,a,a1=None): Line 488  def mkText(case,name,a,a1=None):
488                t_out+="      %s=Symbol(shape=%s)\n"%(name,str(a.shape))                t_out+="      %s=Symbol(shape=%s)\n"%(name,str(a.shape))
489
490           return t_out           return t_out
#==================
case="inner"
for arg0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
for arg1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
sh1=sh0
if (len(sh0)==0 or not arg0=="float") and (len(sh1)==0 or not arg1=="float"):
tname="test_%s_%s_rank%s_%s_rank%s"%(case,arg0,len(sh0),arg1,len(sh1))
t_prog+="   def %s(self):\n"%tname
a0=makeArray(sh0,[-1,1])
a0_1=makeArray(sh0,[-1,1])
a1=makeArray(sh1,[-1,1])
a1_1=makeArray(sh1,[-1,1])
t_prog+=mkText(arg0,"arg0",a0,a0_1)
t_prog+=mkText(arg1,"arg1",a1,a1_1)
t_prog+="      res=%s(arg0,arg1)\n"%case
491
492  print t_prog              def mkTypeAndShapeTest(case,sh,argstr):
493  1/0      text=""
494        if case=="float":
495  #==================           text+="      self.failUnless(isinstance(%s,float),\"wrong type of result.\")\n"%argstr
496  cases=["Scalar","Vector","Tensor", "Tensor3","Tensor4"]      elif case=="array":
497             text+="      self.failUnless(isinstance(%s,numarray.NumArray),\"wrong type of result.\")\n"%argstr
498  for case in range(len(cases)):           text+="      self.failUnlessEqual(%s.shape,%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
499    for d in [ None , "d", 1, 2 , 3]:      elif case in ["constData","taggedData","expandedData"]:
500           if not d==None or cases[case]=="Scalar":           text+="      self.failUnless(isinstance(%s,Data),\"wrong type of result.\")\n"%argstr
501              t_prog+="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"           text+="      self.failUnlessEqual(%s.getShape(),%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
502              tname="test_Symbol_%s_d%s"%(cases[case],d)      elif case=="Symbol":
503              t_prog+="   def %s(self):\n"%tname           text+="      self.failUnless(isinstance(%s,Symbol),\"wrong type of result.\")\n"%argstr
504              if d=="d":           text+="      self.failUnlessEqual(%s.getShape(),%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
505                 t_prog+="      s=%sSymbol(dim=self.functionspace)\n"%(cases[case])      return text
506                 t_prog+="      d=self.functionspace.getDim()\n"
507                 sh="("  def mkCode(txt,args=[],intend=""):
508                 for i in range(case):      s=txt.split("\n")
509                   if i==0:      if len(s)>1:
510                    sh+=d         out=""
511                   else:         for l in s:
512                    sh+=","+d                        out+=intend+l+"\n"
513                 sh+=")"      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
520    #=======================================================================================================
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              else:              else:
541                 t_prog+="      s=%sSymbol(dim=%s)\n"%(cases[case],d)                 sh1=sh
542                 sh=()                 sh0=sh+sh_p
for i in range(case): sh=sh+(d,)
t_prog+="      self.failUnlessEqual(s.getRank(),%s,\"wrong rank.\")\n"%case
t_prog+="      self.failUnlessEqual(s.getShape(),%s,\"wrong shape.\")\n"%str(sh)
t_prog+="      self.failUnlessEqual(s.getDim(),%s,\"wrong spatial dimension.\")\n"%d
t_prog+="      self.failUnlessEqual(s.getArgument(),[],\"wrong arguments.\")\n"

print t_prog
1/0
for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
for d in [ None , "domain", 1, 2 , 3]:
for args in [ [], ["s2"], [1,-1.] ]:
t_prog+="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
tname="def test_Symbol_rank%s_d%s_nargs%s"%(len(sh),d,len(args))
t_prog+="   %s(self):\n"%tname
t_prog+="      s2=ScalarSymbol()\n"
if args==["s2"]:
a="[s2]"
else:
a=str(args)
if d=="domain":
t_prog+="      s=Symbol(shape=%s,dim=self.functionspace.getDim(),args=%s)\n"%(str(sh),a)
d2="self.functionspace.getDim()"
else:
t_prog+="      s=Symbol(shape=%s,dim=%s,args=%s)\n"%(sh,d,a)
d2=str(d)

t_prog+="      self.failUnlessEqual(s.getRank(),%s,\"wrong rank.\")\n"%len(sh)
t_prog+="      self.failUnlessEqual(s.getShape(),%s,\"wrong shape.\")\n"%str(sh)
t_prog+="      self.failUnlessEqual(s.getDim(),%s,\"wrong spatial dimension.\")\n"%d2
t_prog+="      self.failUnlessEqual(s.getArgument(),%s,\"wrong arguments.\")\n\n"%a
t_prog+="      ss=s.substitute({s:numarray.zeros(%s)})\n"%str(sh)
t_prog+="      self.failUnless(isinstance(ss,numarray.NumArray),\"value after substitution is not numarray.\")\n"
t_prog+="      self.failUnlessEqual(ss.shape,%s,\"value after substitution has not expected shape\")\n"%str(sh)

t_prog+="      try:\n        s.substitute({s:numarray.zeros((5,))})\n        fail(\"illegal substition was successful\")\n"
t_prog+="      except TypeError:\n        pass\n\n"

###
for sh2 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
if len(sh+sh2)<5:
t_prog+="      dsdarg=s.diff(Symbol(shape=%s))\n"%str(sh2)
if  len(sh+sh2)==0:
t_prog+="      self.failUnless(isinstance(dsdarg,float),\"ds/ds() has wrong type.\")\n"
else:
t_prog+="      self.failUnless(isinstance(dsdarg,numarray.NumArray),\"ds/ds%s has wrong type.\")\n"%str(sh2)

t_prog+="      self.failUnlessEqual(dsdarg.shape,%s,\"ds/ds%s has wrong shape.\")\n"%(str(sh+sh2),str(sh2))
t_prog+="      self.failIf(Lsup(dsdarg)>0.,\"ds/ds%s has wrong value.\")\n"%str(sh2)
if len(sh)<3:
t_prog+="\n      dsds=s.diff(s)\n"
if  len(sh)==0:
t_prog+="      self.failUnless(isinstance(dsds,float),\"ds/ds has wrong type.\")\n"
t_prog+="      self.failUnlessEqual(dsds,1.,\"ds/ds has wrong value.\")\n"
else:
t_prog+="      self.failUnless(isinstance(dsds,numarray.NumArray),\"ds/ds has wrong type.\")\n"
t_prog+="      self.failUnlessEqual(dsds.shape,%s,\"ds/ds has wrong shape.\")\n"%str(sh+sh)
if len(sh)==1:
for i0 in range(sh[0]):
for i2 in range(sh[0]):
if i0==i2:
v=1.
else:
v=0.
t_prog+="      self.failUnlessEqual(dsds[%s,%s],%s,\"ds/ds has wrong value at (%s,%s).\")\n"%(i0,i2,v,i0,i2)
else:
for i0 in range(sh[0]):
for i1 in range(sh[1]):
for i2 in range(sh[0]):
for i3 in range(sh[1]):
if i0==i2 and i1==i3:
v=1.
else:
v=0.
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)
543
544           ###                  if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
545           t_prog+="\n"                     len(sh0)<5 and len(sh1)<5:
546           for i in range(len(args)):                use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
547               t_prog+="      self.failUnlessEqual(s.getArgument(%s),%s,\"wrong argument %s.\")\n"%(i,str(args[i]),i)                text="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
548           t_prog+="      sa=s.getSubstitutedArguments({s2:-10})\n"                tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
549           t_prog+="      self.failUnlessEqual(len(sa),%s,\"wrong number of substituted arguments\")\n"%len(args)                text+="   def %s(self):\n"%tname
550           if args==["s2"]:                a_0=makeArray(sh0,oper[2])
551                  t_prog+="      self.failUnlessEqual(sa[0],-10,\"wrongly substituted argument 0.\")\n"                if case0 in ["taggedData", "expandedData"]:
552           else:                    a1_0=makeArray(sh0,oper[2])
553               for i in range(len(args)):                else:
554                  t_prog+="      self.failUnlessEqual(sa[%s],%s,\"wrongly substituted argument %s.\")\n"%(i,str(args[i]),i)                    a1_0=a_0

t_prog+="\n"
for arg in ["10.", "10", "SymbolMatch", "SymbolMisMatch", \
"DataMatch","DataMisMatch", "NumArrayMatch", "NumArrayMisMatch"]:
if arg in ["10.", "10"]:
a=str(arg)
if len(sh)==0:
t_prog+="      self.failUnless(s.isAppropriateValue(%s),\"%s is appropriate substitute\")\n"%(a,arg)
else:
t_prog+="      self.failIf(s.isAppropriateValue(%s),\" %s is not appropriate substitute\")\n"%(a,arg)
elif arg in ["SymbolMatch", "SymbolMisMatch"]:
if arg=="SymbolMatch":
t_prog+="      self.failUnless(s.isAppropriateValue(Symbol(shape=%s,dim=%s)),\"Symbol is appropriate substitute\")\n"%(str(sh),d)
else:
if isinstance(d,int):
t_prog+="      self.failIf(s.isAppropriateValue(Symbol(shape=%s,dim=%s)),\"Symbol is not appropriate substitute (dim)\")\n"%(str(sh),d+1)
else:
t_prog+="      self.failIf(s.isAppropriateValue(Symbol(shape=%s)),\"Symbol is not appropriate substitute (shape)\")\n"%((5,))
555
556               elif arg in ["DataMatch","DataMisMatch"]:                a_1=makeArray(sh1,oper[2])
557                    if arg=="DataMatch" and d=="domain":                if case1 in ["taggedData", "expandedData"]:
558                     t_prog+="      self.failUnless(s.isAppropriateValue(escript.Data(0.,%s,self.functionspace)),\"Data is appropriate substitute\")\n"%str(sh)                    a1_1=makeArray(sh1,oper[2])
559                    elif arg=="DataMisMatch":                else:
560                        t_prog+="      self.failIf(s.isAppropriateValue(escript.Data(0.,%s,self.functionspace)),\"Data is not appropriate substitute (shape)\")\n"%(str((5,)))                    a1_1=a_1
561               else:                r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
562                    if arg=="NumArrayMatch":                r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
563                     t_prog+="      self.failUnless(s.isAppropriateValue(numarray.zeros(%s)),\"NumArray is appropriate substitute\")\n"%str(sh)                text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
564                    else:                text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
565                        t_prog+="      self.failIf(s.isAppropriateValue(numarray.zeros(%s)),\"NumArray is not appropriate substitute (shape)\")\n"%(str((5,)))                text+="      res=%s(arg0,arg1)\n"%oper[0]
566  print t_prog
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  1/0  1/0
602
603    #=======================================================================================================
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
628  for case in ["Lsup", "sup", "inf"]:                a_1=makeArray(sh1,oper[2])
629     for args in ["float","array","constData","taggedData","expandedData"]:                if case1 in ["taggedData", "expandedData"]:
630       for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:                    a1_1=makeArray(sh1,oper[2])
if not args=="float" or len(sh)==0:
t_prog+="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
tname="def test_%s_%s_rank%s"%(case,args,len(sh))
t_prog+="   %s(self):\n"%tname
if args in ["float","array" ]:
a=makeArray(sh,[-1,1])
r=makeResult2(a,case)
if len(sh)==0:
t_prog+="      arg=%s\n"%a
else:
t_prog+="      arg=numarray.array(%s)\n"%a.tolist()
t_prog+="      ref=%s\n"%r
t_prog+="      res=%s(arg)\n"%case
t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
t_prog+="      self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
elif args== "constData":
a=makeArray(sh,[-1,1])
r=makeResult2(a,case)
if len(sh)==0:
t_prog+="      arg=Data(%s,self.functionspace)\n"%(a)
else:
t_prog+="      arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
t_prog+="      ref=%s\n"%r
t_prog+="      res=%s(arg)\n"%case
t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
t_prog+="      self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
elif args in [ "taggedData","expandedData"]:
a=makeArray(sh,[-1,1])
r=makeResult2(a,case)
a1=makeArray(sh,[-1,1])
r1=makeResult2(a1,case)
if case in ["Lsup","sup"]:
r=max(r,r1)
else:
r=min(r,r1)
if len(sh)==0:
if args=="expandedData":
t_prog+="      arg=Data(%s,self.functionspace,True)\n"%(a)
631                else:                else:
632                   t_prog+="      arg=Data(%s,self.functionspace)\n"%(a)                    a1_1=a_1
633                t_prog+="      arg.setTaggedValue(1,%s)\n"%a                r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
634             else:                r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
635                if args=="expandedData":                text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
636                   t_prog+="      arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())                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                else:                else:
657                   t_prog+="      arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())                   res="res"
658                t_prog+="      arg.setTaggedValue(1,%s)\n"%a1.tolist()                   text+=mkText(case,"ref",r,r1)
659             t_prog+="      res=%s(arg)\n"%case                if isinstance(r,float):
660             t_prog+="      ref=%s\n"%r                   text+=mkTypeAndShapeTest(case,(),"res")
661             t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"                else:
662             t_prog+="      self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"                   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
673
674    # print u_prog
675    # 1/0
676    print test_header
677  print t_prog  print t_prog

678  1/0  1/0
679    # 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
701                          a_1=makeArray(sh1,oper[2])
702  for case in ["Lsup", "sup", "inf"]:                if case1 in ["taggedData", "expandedData"]:
703     for args in ["float","array","constData","taggedData","expandedData"]:                    a1_1=makeArray(sh1,oper[2])
for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
if not args=="float" or len(sh)==0:
t_prog+="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
tname="def test_%s_%s_rank%s"%(case,args,len(sh))
t_prog+="   %s(self):\n"%tname
if args in ["float","array" ]:
a=makeArray(sh,[-1,1])
r=makeResult2(a,case)
if len(sh)==0:
t_prog+="      arg=%s\n"%a
else:
t_prog+="      arg=numarray.array(%s)\n"%a.tolist()
t_prog+="      ref=%s\n"%r
t_prog+="      res=%s(arg)\n"%case
t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
t_prog+="      self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
elif args== "constData":
a=makeArray(sh,[-1,1])
r=makeResult2(a,case)
if len(sh)==0:
t_prog+="      arg=Data(%s,self.functionspace)\n"%(a)
else:
t_prog+="      arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
t_prog+="      ref=%s\n"%r
t_prog+="      res=%s(arg)\n"%case
t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
t_prog+="      self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"
elif args in [ "taggedData","expandedData"]:
a=makeArray(sh,[-1,1])
r=makeResult2(a,case)
a1=makeArray(sh,[-1,1])
r1=makeResult2(a1,case)
if case in ["Lsup","sup"]:
r=max(r,r1)
else:
r=min(r,r1)
if len(sh)==0:
if args=="expandedData":
t_prog+="      arg=Data(%s,self.functionspace,True)\n"%(a)
704                else:                else:
705                   t_prog+="      arg=Data(%s,self.functionspace)\n"%(a)                    a1_1=a_1
706                t_prog+="      arg.setTaggedValue(1,%s)\n"%a                r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
707             else:                r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
708                if args=="expandedData":                text+=mkText(case0,"arg0",a_0,a1_0)
709                   t_prog+="      arg=Data(numarray.array(%s),self.functionspace,True)\n"%(a.tolist())                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                else:                else:
730                   t_prog+="      arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())                   res="res"
731                t_prog+="      arg.setTaggedValue(1,%s)\n"%a1.tolist()                   text+=mkText(case,"ref",r,r1)
732             t_prog+="      res=%s(arg)\n"%case                if isinstance(r,float):
733             t_prog+="      ref=%s\n"%r                   text+=mkTypeAndShapeTest(case,(),"res")
734             t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"                else:
735             t_prog+="      self.failUnless(abs(res-ref)<=self.tol*abs(ref),\"wrong result\")\n"                   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
746
747    # print u_prog
748    # 1/0
749    print test_header
750  print t_prog  print t_prog
751    # print t_prog_with_tags
752  1/0  # print t_prog_failing
753    print test_tail
754    # print t_prog_failing
755    print test_tail
756
757    #=======================================================================================================
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:  for f in func:
917     u_prog+="def %s(arg):\n"%f[0]     symbol_name=f.nickname[0].upper()+f.nickname[1:]
918     u_prog+="   \"\"\"\n"     if f.nickname!="abs":
919     u_prog+="   returns %s of argument arg\n\n"%f[0]        u_prog+="def %s(arg):\n"%f.nickname
920     u_prog+="   @param arg: argument\n"        u_prog+="   \"\"\"\n"
921     u_prog+="   @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.\n"        u_prog+="   returns %s of argument arg\n\n"%f.name
922     u_prog+="   @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.\n"        u_prog+="   @param arg: argument\n"
923     u_prog+="   @raises TypeError: if the type of the argument is not expected.\n"        u_prog+="   @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.\n"
924     u_prog+="   \"\"\"\n"        u_prog+="   @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.\n"
925     u_prog+="   if isinstance(arg,numarray.NumArray):\n"        u_prog+="   @raises TypeError: if the type of the argument is not expected.\n"
926     u_prog+="       return numarray.%s(arg)\n"%f[0]        u_prog+="   \"\"\"\n"
927     u_prog+="   elif isinstance(arg,escript.Data):\n"        u_prog+="   if isinstance(arg,numarray.NumArray):\n"
928     u_prog+="       return arg._%s(arg)\n"%f[0]        u_prog+=mkCode(f.numarray_expr,["arg"],2*"   ")
929     u_prog+="   elif isinstance(arg,float):\n"        u_prog+="   elif isinstance(arg,escript.Data):\n"
930     u_prog+="       return math.%s(arg)\n"%f[0]        u_prog+=mkCode("arg._%s()"%f.nickname,[],2*"   ")
931     u_prog+="   elif isinstance(arg,int):\n"        u_prog+="   elif isinstance(arg,float):\n"
932     u_prog+="       return math.%s(float(arg))\n"%f[0]        u_prog+=mkCode(f.math_expr,["arg"],2*"   ")
933     u_prog+="   elif isinstance(arg,Symbol):\n"        u_prog+="   elif isinstance(arg,int):\n"
934     u_prog+="       return Symbol_%s(arg)\n"%f[0]        u_prog+=mkCode(f.math_expr,["float(arg)"],2*"   ")
935     u_prog+="   else:\n"        u_prog+="   elif isinstance(arg,Symbol):\n"
936     u_prog+="       raise TypeError,\"%s: Unknown argument type.\"\n"%f[0]        if f.symbol_expr==None:
937     u_prog+="class Symbol_%s(DependendSymbol):\n"%f[0]           u_prog+=mkCode("%s_Symbol(arg)"%symbol_name,[],2*"   ")
938     u_prog+="   \"\"\"\n"        else:
939     u_prog+="   Symbol of the result of the %s function\n"%f[0]           u_prog+=mkCode(f.symbol_expr,["arg"],2*"   ")
940     u_prog+="   \"\"\"\n"        u_prog+="   else:\n"
941     u_prog+="   def __init__(self,arg):\n"        u_prog+="      raise TypeError,\"%s: Unknown argument type.\"\n\n"%f.nickname
942     u_prog+="      \"\"\"\n"     if f.symbol_expr==None:
943     u_prog+="      initialization %s function with argument arg\n"%f[0]        u_prog+="class %s_Symbol(DependendSymbol):\n"%symbol_name
944     u_prog+="      @param arg: argument of function\n"        u_prog+="   \"\"\"\n"
945     u_prog+="      @type arg: L{Symbol}.\n"        u_prog+="   L{Symbol} representing the result of the %s function\n"%f.name
946     u_prog+="      \"\"\"\n"        u_prog+="   \"\"\"\n"
947     u_prog+="      DependendSymbol.__init__(self,args=[arg],shape=arg.getShape(),dim=arg.getDim())\n"        u_prog+="   def __init__(self,arg):\n"
948     u_prog+="   def __str__(self):\n"        u_prog+="      \"\"\"\n"
949     u_prog+="      \"\"\"\n"        u_prog+="      initialization of %s L{Symbol} with argument arg\n"%f.nickname
950     u_prog+="      string representation of the object\n"        u_prog+="      @param arg: argument of function\n"
951     u_prog+="      @rtype: C{str}\n"        u_prog+="      @type arg: typically L{Symbol}.\n"
952     u_prog+="      \"\"\"\n"        u_prog+="      \"\"\"\n"
953     u_prog+="      return \"%s(%%s)\"%%str(self.getArgument(0))\n"%f[0]        u_prog+="      DependendSymbol.__init__(self,args=[arg],shape=arg.getShape(),dim=arg.getDim())\n"
954     u_prog+="   def substitute(self,argvals):\n"        u_prog+="\n"
955     u_prog+="      \"\"\"\n"
956     u_prog+="      assigns new values to symbols in the definition of the symbol\n      The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.\n\n"        u_prog+="   def getMyCode(self,argstrs,format=\"escript\"):\n"
957     u_prog+="      @param argvals: new values assigned to symbols\n"        u_prog+="      \"\"\"\n"
958     u_prog+="      @type argvals: C{dict} with keywords of type L{Symbol}.\n"        u_prog+="      returns a program code that can be used to evaluate the symbol.\n\n"
959     u_prog+="      @return: result of the substitution process. Operations are executed as much as possible.\""
960     u_prog+="      @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution\n"        u_prog+="      @param argstrs: gives for each argument a string representing the argument for the evaluation.\n"
961     u_prog+="      raise: TypeError: if a value for a L{Symbol} cannot be substituted.\n"        u_prog+="      @type argstrs: C{str} or a C{list} of length 1 of C{str}.\n"
962     u_prog+="      \"\"\"\n"        u_prog+="      @param format: specifies the format to be used. At the moment only \"escript\" ,\"text\" and \"str\" are supported.\n"
963     u_prog+="      if argval.has_key(self): \n"        u_prog+="      @type format: C{str}\n"
964     u_prog+="         if self.isAppropriateValue(arg):\n"        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+="            return argval[self] \n"        u_prog+="      @rtype: C{str}\n"
966     u_prog+="         else:\n"        u_prog+="      @raise: NotImplementedError: if the requested format is not available\n"
967     u_prog+="            raise TypeError,\"%s: new value is not appropriate.\"\n"        u_prog+="      \"\"\"\n"
968     u_prog+="      else:\n"        u_prog+="      if isinstance(argstrs,list):\n"
969     u_prog+="         arg=self.getSubstitutedArguments(argvals)[0]\n"        u_prog+="          argstrs=argstrs[0]\n"
970     u_prog+="         return %s(arg)\n"%f[0]        u_prog+="      if format==\"escript\" or format==\"str\"  or format==\"text\":\n"
971     u_prog+="   def getMyCode(self,argstrs,format=\"escript\"):\n"        u_prog+="         return \"%s(%%s)\"%%argstrs\n"%f.nickname
972     u_prog+="       \"\"\"\n"        u_prog+="      else:\n"
973     u_prog+="       returns a program code that can be used to evaluate the symbol.\n"        u_prog+="         raise NotImplementedError,\"%s_Symbol does not provide program code for format %%s.\"%%format\n"%symbol_name
974     u_prog+="\n"        u_prog+="\n"
975     u_prog+="       @param argstrs: gives for each argument a string representing of the argument for the evaluation.\n"
976     u_prog+="       @type argstrs: C{list} of C{str}.\n"        u_prog+="   def substitute(self,argvals):\n"
977     u_prog+="       @param format: specifies the format to be used. At the moment only \"escript\" is supported.\n"        u_prog+="      \"\"\"\n"
978     u_prog+="       @type format: C{str}\n"        u_prog+="      assigns new values to symbols in the definition of the symbol.\n"
979     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"        u_prog+="      The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.\n"
980     u_prog+="       @rtype: C{str}\n"        u_prog+="\n"
981     u_prog+="       @raises NotImplementedError: if no implementation for the given format is available\n"        u_prog+="      @param argvals: new values assigned to symbols\n"
982     u_prog+="       \"\"\"\n"        u_prog+="      @type argvals: C{dict} with keywords of type L{Symbol}.\n"
983     u_prog+="       if format==\"escript\":\n"        u_prog+="      @return: result of the substitution process. Operations are executed as much as possible.\n"
984     u_prog+="          return %s(%%s)%argstrs[0]\"\n"%f[0]        u_prog+="      @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution\n"
985     u_prog+="       else:\n"        u_prog+="      @raise TypeError: if a value for a L{Symbol} cannot be substituted.\n"
986     u_prog+="          raise NotImplementedError,\"%s does not provide program code for format %%s.\"%format\n"%f[0]        u_prog+="      \"\"\"\n"
987          u_prog+="      if argvals.has_key(self):\n"
988     u_prog+="   def diff(self,arg):\n"        u_prog+="         arg=argvals[self]\n"
989     u_prog+="      \"\"\"\n"        u_prog+="         if self.isAppropriateValue(arg):\n"
990     u_prog+="      returns the derivative of the symbol with respect to a symbol\n"        u_prog+="            return arg\n"
991     u_prog+="      @param arg: argument for derivation\n"        u_prog+="         else:\n"
992     u_prog+="      @type arg: L{Symbol}.\n"        u_prog+="            raise TypeError,\"%s: new value is not appropriate.\"%str(self)\n"
993     u_prog+="      @raises NotImplementedError: derivative not available.\n"        u_prog+="      else:\n"
994     u_prog+="      \"\"\"\n"        u_prog+="         arg=self.getSubstitutedArguments(argvals)[0]\n"
995     u_prog+="      if arg==self: \n"        u_prog+="         return %s(arg)\n\n"%f.nickname
996     u_prog+="         return identity(self.getShape())\n"        if not f.diff==None:
997     u_prog+="      else:\n"           u_prog+="   def diff(self,arg):\n"
998     u_prog+="        darg=self.getDifferentiatedArguments(arg)[0]\n"           u_prog+="      \"\"\"\n"
999     u_prog+="        raise NotImplementedError,\"%s: derivative is not available.\"\n"%f[0]           u_prog+="      differential of this object\n"
1000     u_prog+="        # dself=?\n"           u_prog+="\n"
1001     u_prog+="        return dself.matchShape(darg)*darg\n"           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
1013       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
1044    1/0
1045
1046     for args in ["float","array","constData","taggedData","expandedData","Symbol"]:  def X():
1047       for sh in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:           if args=="float":
1048         if not args=="float" or len(sh)==0:             a=makeArray(sh,f[RANGE])
1049           t_prog+="   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"             r=makeResult(a,f)
1050           tname="def test_%s_%s_rank%s"%(f[0],args,len(sh))             t_prog+="      arg=%s\n"%a[0]
1051           t_prog+="   %s(self):\n"%tname             t_prog+="      ref=%s\n"%r[0]
1052           if args in ["float","array" ]:             t_prog+="      res=%s(%a1%)\n"%f.nickname
1053             a=makeArray(sh,f[1])             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)             r=makeResult(a,f)
1058             if len(sh)==0:             if len(sh)==0:
1059                t_prog+="      arg=%s\n"%a                t_prog+="      arg=numarray.array(%s)\n"%a[0]
1060                t_prog+="      ref=%s\n"%r                t_prog+="      ref=numarray.array(%s)\n"%r[0]
1061             else:             else:
1062                t_prog+="      arg=numarray.array(%s)\n"%a.tolist()                t_prog+="      arg=numarray.array(%s)\n"%a.tolist()
1063                t_prog+="      ref=numarray.array(%s)\n"%r.tolist()                t_prog+="      ref=numarray.array(%s)\n"%r.tolist()
1064             t_prog+="      res=%s(arg)\n"%f[0]             t_prog+="      res=%s(%a1%)\n"%f.nickname
1065             if args=="float":             t_prog+="      self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh)
1066                t_prog+="      self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"             t_prog+="      self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
else:
t_prog+="      self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh)
t_prog+="      self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1067           elif args== "constData":           elif args== "constData":
1068             a=makeArray(sh,f[1])             a=makeArray(sh,f[RANGE])
1069             r=makeResult(a,f)             r=makeResult(a,f)
1070             if len(sh)==0:             if len(sh)==0:
1071                t_prog+="      arg=Data(%s,self.functionspace)\n"%(a)                t_prog+="      arg=Data(%s,self.functionspace)\n"%(a)
# Line 570  for f in func: Line 1073  for f in func:
1073             else:             else:
1074                t_prog+="      arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())                t_prog+="      arg=Data(numarray.array(%s),self.functionspace)\n"%(a.tolist())
1075                t_prog+="      ref=numarray.array(%s)\n"%r.tolist()                t_prog+="      ref=numarray.array(%s)\n"%r.tolist()
1076             t_prog+="      res=%s(arg)\n"%f[0]             t_prog+="      res=%s(%a1%)\n"%f.nickname
1077             t_prog+="      self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)             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"             t_prog+="      self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1079           elif args in [ "taggedData","expandedData"]:           elif args in [ "taggedData","expandedData"]:
1080             a=makeArray(sh,f[1])             a=makeArray(sh,f[RANGE])
1081             r=makeResult(a,f)             r=makeResult(a,f)
1082             a1=makeArray(sh,f[1])             a1=makeArray(sh,f[RANGE])
1083             r1=makeResult(a1,f)             r1=makeResult(a1,f)
1084             if len(sh)==0:             if len(sh)==0:
1085                if args=="expandedData":                if args=="expandedData":
# Line 596  for f in func: Line 1099  for f in func:
1099                   t_prog+="      ref=Data(numarray.array(%s),self.functionspace)\n"%(r.tolist())                   t_prog+="      ref=Data(numarray.array(%s),self.functionspace)\n"%(r.tolist())
1100                t_prog+="      arg.setTaggedValue(1,%s)\n"%a1.tolist()                t_prog+="      arg.setTaggedValue(1,%s)\n"%a1.tolist()
1101                t_prog+="      ref.setTaggedValue(1,%s)\n"%r1.tolist()                t_prog+="      ref.setTaggedValue(1,%s)\n"%r1.tolist()
1102             t_prog+="      res=%s(arg)\n"%f[0]             t_prog+="      res=%s(%a1%)\n"%f.nickname
1103             t_prog+="      self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)             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"             t_prog+="      self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
1105           elif args=="Symbol":           elif args=="Symbol":
1106             t_prog+="      arg=Symbol(shape=%s)\n"%str(sh)             t_prog+="      arg=Symbol(shape=%s)\n"%str(sh)
1107             t_prog+="      v=%s(arg)\n"%f[0]             t_prog+="      v=%s(%a1%)\n"%f.nickname
1108             t_prog+="      self.failUnlessRaises(ValueError,v.substitute,Symbol(shape=(1,1)),\"illegal shape of substitute not identified.\")\n"             t_prog+="      self.failUnlessRaises(ValueError,v.substitute,Symbol(shape=(1,1)),\"illegal shape of substitute not identified.\")\n"
1109             a=makeArray(sh,f[1])             a=makeArray(sh,f[RANGE])
1110             r=makeResult(a,f)             r=makeResult(a,f)
1111             if len(sh)==0:             if len(sh)==0:
1112                t_prog+="      res=v.substitute({arg : %s})\n"%a                t_prog+="      res=v.substitute({arg : %s})\n"%a
# Line 649  for f in func: Line 1152  for f in func:
1152
1153                 t_prog+="      # test derivative with shape %s as argument\n"%str(sh_in)                 t_prog+="      # test derivative with shape %s as argument\n"%str(sh_in)
1154                 trafo=makeArray(sh+sh_in,[0,1.])                 trafo=makeArray(sh+sh_in,[0,1.])
1155                 a_in=makeArray(sh_in,f[1])                 a_in=makeArray(sh_in,f[RANGE])
1156                 t_prog+="      arg_in=Symbol(shape=%s)\n"%str(sh_in)                 t_prog+="      arg_in=Symbol(shape=%s)\n"%str(sh_in)
1157                 t_prog+="      arg2=Symbol(shape=%s)\n"%str(sh)                 t_prog+="      arg2=Symbol(shape=%s)\n"%str(sh)
1158
# Line 871  for f in func: Line 1374  for f in func:
1374                             if len(sh_in)==0:                             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                                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)))                                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
1378  # print u_prog  #==================
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  print t_prog  print t_prog
1536    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

Legend:
 Removed from v.155 changed lines Added in v.157

 ViewVC Help Powered by ViewVC 1.1.26