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

Contents of /trunk/escript/py_src/generatediff

Parent Directory Parent Directory | Revision Log Revision Log


Revision 443 - (show annotations)
Fri Jan 20 06:22:38 2006 UTC (13 years, 8 months ago) by gross
Original Path: trunk/escript/py_src/generateutil
File size: 128530 byte(s)
L2 function added
1 #!/usr/bin/python
2 # $Id$
3
4 """
5 program generates parts of the util.py and the test_util.py script
6 """
7 test_header=""
8 test_header+="import unittest\n"
9 test_header+="import numarray\n"
10 test_header+="from esys.escript import *\n"
11 test_header+="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=""
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
37 class OPERATOR:
38 def __init__(self,nickname,rng=[-1000.,1000],test_expr="",math_expr=None,
39 numarray_expr="",symbol_expr=None,diff=None,name=""):
40 self.nickname=nickname
41 self.rng=rng
42 self.test_expr=test_expr
43 if math_expr==None:
44 self.math_expr=test_expr.replace("%a1%","arg")
45 else:
46 self.math_expr=math_expr
47 self.numarray_expr=numarray_expr
48 self.symbol_expr=symbol_expr
49 self.diff=diff
50 self.name=name
51
52 import random
53 import numarray
54 import math
55 finc=1.e-6
56
57 def getResultCaseForBin(case0,case1):
58 if case0=="float":
59 if case1=="float":
60 case="float"
61 elif case1=="array":
62 case="array"
63 elif case1=="constData":
64 case="constData"
65 elif case1=="taggedData":
66 case="taggedData"
67 elif case1=="expandedData":
68 case="expandedData"
69 elif case1=="Symbol":
70 case="Symbol"
71 else:
72 raise ValueError,"unknown case1=%s"%case1
73 elif case0=="array":
74 if case1=="float":
75 case="array"
76 elif case1=="array":
77 case="array"
78 elif case1=="constData":
79 case="constData"
80 elif case1=="taggedData":
81 case="taggedData"
82 elif case1=="expandedData":
83 case="expandedData"
84 elif case1=="Symbol":
85 case="Symbol"
86 else:
87 raise ValueError,"unknown case1=%s"%case1
88 elif case0=="constData":
89 if case1=="float":
90 case="constData"
91 elif case1=="array":
92 case="constData"
93 elif case1=="constData":
94 case="constData"
95 elif case1=="taggedData":
96 case="taggedData"
97 elif case1=="expandedData":
98 case="expandedData"
99 elif case1=="Symbol":
100 case="Symbol"
101 else:
102 raise ValueError,"unknown case1=%s"%case1
103 elif case0=="taggedData":
104 if case1=="float":
105 case="taggedData"
106 elif case1=="array":
107 case="taggedData"
108 elif case1=="constData":
109 case="taggedData"
110 elif case1=="taggedData":
111 case="taggedData"
112 elif case1=="expandedData":
113 case="expandedData"
114 elif case1=="Symbol":
115 case="Symbol"
116 else:
117 raise ValueError,"unknown case1=%s"%case1
118 elif case0=="expandedData":
119 if case1=="float":
120 case="expandedData"
121 elif case1=="array":
122 case="expandedData"
123 elif case1=="constData":
124 case="expandedData"
125 elif case1=="taggedData":
126 case="expandedData"
127 elif case1=="expandedData":
128 case="expandedData"
129 elif case1=="Symbol":
130 case="Symbol"
131 else:
132 raise ValueError,"unknown case1=%s"%case1
133 elif case0=="Symbol":
134 if case1=="float":
135 case="Symbol"
136 elif case1=="array":
137 case="Symbol"
138 elif case1=="constData":
139 case="Symbol"
140 elif case1=="taggedData":
141 case="Symbol"
142 elif case1=="expandedData":
143 case="Symbol"
144 elif case1=="Symbol":
145 case="Symbol"
146 else:
147 raise ValueError,"unknown case1=%s"%case1
148 else:
149 raise ValueError,"unknown case0=%s"%case0
150 return case
151
152
153 def makeArray(shape,rng):
154 l=rng[1]-rng[0]
155 out=numarray.zeros(shape,numarray.Float64)
156 if len(shape)==0:
157 out=l*random.random()+rng[0]
158 elif len(shape)==1:
159 for i0 in range(shape[0]):
160 out[i0]=l*random.random()+rng[0]
161 elif len(shape)==2:
162 for i0 in range(shape[0]):
163 for i1 in range(shape[1]):
164 out[i0,i1]=l*random.random()+rng[0]
165 elif len(shape)==3:
166 for i0 in range(shape[0]):
167 for i1 in range(shape[1]):
168 for i2 in range(shape[2]):
169 out[i0,i1,i2]=l*random.random()+rng[0]
170 elif len(shape)==4:
171 for i0 in range(shape[0]):
172 for i1 in range(shape[1]):
173 for i2 in range(shape[2]):
174 for i3 in range(shape[3]):
175 out[i0,i1,i2,i3]=l*random.random()+rng[0]
176 elif len(shape)==5:
177 for i0 in range(shape[0]):
178 for i1 in range(shape[1]):
179 for i2 in range(shape[2]):
180 for i3 in range(shape[3]):
181 for i4 in range(shape[4]):
182 out[i0,i1,i2,i3,i4]=l*random.random()+rng[0]
183 else:
184 raise SystemError,"rank is restricted to 5"
185 return out
186
187
188 def makeResult(val,test_expr):
189 if isinstance(val,float):
190 out=eval(test_expr.replace("%a1%","val"))
191 elif len(val.shape)==0:
192 out=eval(test_expr.replace("%a1%","val"))
193 elif len(val.shape)==1:
194 out=numarray.zeros(val.shape,numarray.Float64)
195 for i0 in range(val.shape[0]):
196 out[i0]=eval(test_expr.replace("%a1%","val[i0]"))
197 elif len(val.shape)==2:
198 out=numarray.zeros(val.shape,numarray.Float64)
199 for i0 in range(val.shape[0]):
200 for i1 in range(val.shape[1]):
201 out[i0,i1]=eval(test_expr.replace("%a1%","val[i0,i1]"))
202 elif len(val.shape)==3:
203 out=numarray.zeros(val.shape,numarray.Float64)
204 for i0 in range(val.shape[0]):
205 for i1 in range(val.shape[1]):
206 for i2 in range(val.shape[2]):
207 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val[i0,i1,i2]"))
208 elif len(val.shape)==4:
209 out=numarray.zeros(val.shape,numarray.Float64)
210 for i0 in range(val.shape[0]):
211 for i1 in range(val.shape[1]):
212 for i2 in range(val.shape[2]):
213 for i3 in range(val.shape[3]):
214 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val[i0,i1,i2,i3]"))
215 else:
216 raise SystemError,"rank is restricted to 4"
217 return out
218
219 def makeResult2(val0,val1,test_expr):
220 if isinstance(val0,float):
221 if isinstance(val1,float):
222 out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
223 elif len(val1.shape)==0:
224 out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
225 elif len(val1.shape)==1:
226 out=numarray.zeros(val1.shape,numarray.Float64)
227 for i0 in range(val1.shape[0]):
228 out[i0]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0]"))
229 elif len(val1.shape)==2:
230 out=numarray.zeros(val1.shape,numarray.Float64)
231 for i0 in range(val1.shape[0]):
232 for i1 in range(val1.shape[1]):
233 out[i0,i1]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1]"))
234 elif len(val1.shape)==3:
235 out=numarray.zeros(val1.shape,numarray.Float64)
236 for i0 in range(val1.shape[0]):
237 for i1 in range(val1.shape[1]):
238 for i2 in range(val1.shape[2]):
239 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2]"))
240 elif len(val1.shape)==4:
241 out=numarray.zeros(val1.shape,numarray.Float64)
242 for i0 in range(val1.shape[0]):
243 for i1 in range(val1.shape[1]):
244 for i2 in range(val1.shape[2]):
245 for i3 in range(val1.shape[3]):
246 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2,i3]"))
247 else:
248 raise SystemError,"rank of val1 is restricted to 4"
249 elif len(val0.shape)==0:
250 if isinstance(val1,float):
251 out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
252 elif len(val1.shape)==0:
253 out=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1"))
254 elif len(val1.shape)==1:
255 out=numarray.zeros(val1.shape,numarray.Float64)
256 for i0 in range(val1.shape[0]):
257 out[i0]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0]"))
258 elif len(val1.shape)==2:
259 out=numarray.zeros(val1.shape,numarray.Float64)
260 for i0 in range(val1.shape[0]):
261 for i1 in range(val1.shape[1]):
262 out[i0,i1]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1]"))
263 elif len(val1.shape)==3:
264 out=numarray.zeros(val1.shape,numarray.Float64)
265 for i0 in range(val1.shape[0]):
266 for i1 in range(val1.shape[1]):
267 for i2 in range(val1.shape[2]):
268 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2]"))
269 elif len(val1.shape)==4:
270 out=numarray.zeros(val1.shape,numarray.Float64)
271 for i0 in range(val1.shape[0]):
272 for i1 in range(val1.shape[1]):
273 for i2 in range(val1.shape[2]):
274 for i3 in range(val1.shape[3]):
275 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0").replace("%a2%","val1[i0,i1,i2,i3]"))
276 else:
277 raise SystemError,"rank of val1 is restricted to 4"
278 elif len(val0.shape)==1:
279 if isinstance(val1,float):
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"))
283 elif len(val1.shape)==0:
284 out=numarray.zeros(val0.shape,numarray.Float64)
285 for i0 in range(val0.shape[0]):
286 out[i0]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1"))
287 elif len(val1.shape)==1:
288 out=numarray.zeros(val0.shape,numarray.Float64)
289 for i0 in range(val0.shape[0]):
290 out[i0]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0]"))
291 elif len(val1.shape)==2:
292 out=numarray.zeros(val0.shape+val1.shape[1:],numarray.Float64)
293 for i0 in range(val0.shape[0]):
294 for j1 in range(val1.shape[1]):
295 out[i0,j1]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0,j1]"))
296 elif len(val1.shape)==3:
297 out=numarray.zeros(val0.shape+val1.shape[1:],numarray.Float64)
298 for i0 in range(val0.shape[0]):
299 for j1 in range(val1.shape[1]):
300 for j2 in range(val1.shape[2]):
301 out[i0,j1,j2]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0,j1,j2]"))
302 elif len(val1.shape)==4:
303 out=numarray.zeros(val0.shape+val1.shape[1:],numarray.Float64)
304 for i0 in range(val0.shape[0]):
305 for j1 in range(val1.shape[1]):
306 for j2 in range(val1.shape[2]):
307 for j3 in range(val1.shape[3]):
308 out[i0,j1,j2,j3]=eval(test_expr.replace("%a1%","val0[i0]").replace("%a2%","val1[i0,j1,j2,j3]"))
309 else:
310 raise SystemError,"rank of val1 is restricted to 4"
311 elif len(val0.shape)==2:
312 if isinstance(val1,float):
313 out=numarray.zeros(val0.shape,numarray.Float64)
314 for i0 in range(val0.shape[0]):
315 for i1 in range(val0.shape[1]):
316 out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1"))
317 elif len(val1.shape)==0:
318 out=numarray.zeros(val0.shape,numarray.Float64)
319 for i0 in range(val0.shape[0]):
320 for i1 in range(val0.shape[1]):
321 out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1"))
322 elif len(val1.shape)==1:
323 out=numarray.zeros(val0.shape,numarray.Float64)
324 for i0 in range(val0.shape[0]):
325 for i1 in range(val0.shape[1]):
326 out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0]"))
327 elif len(val1.shape)==2:
328 out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
329 for i0 in range(val0.shape[0]):
330 for i1 in range(val0.shape[1]):
331 out[i0,i1]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0,i1]"))
332 elif len(val1.shape)==3:
333 out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
334 for i0 in range(val0.shape[0]):
335 for i1 in range(val0.shape[1]):
336 for j2 in range(val1.shape[2]):
337 out[i0,i1,j2]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0,i1,j2]"))
338 elif len(val1.shape)==4:
339 out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
340 for i0 in range(val0.shape[0]):
341 for i1 in range(val0.shape[1]):
342 for j2 in range(val1.shape[2]):
343 for j3 in range(val1.shape[3]):
344 out[i0,i1,j2,j3]=eval(test_expr.replace("%a1%","val0[i0,i1]").replace("%a2%","val1[i0,i1,j2,j3]"))
345 else:
346 raise SystemError,"rank of val1 is restricted to 4"
347 elif len(val0.shape)==3:
348 if isinstance(val1,float):
349 out=numarray.zeros(val0.shape,numarray.Float64)
350 for i0 in range(val0.shape[0]):
351 for i1 in range(val0.shape[1]):
352 for i2 in range(val0.shape[2]):
353 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1"))
354 elif len(val1.shape)==0:
355 out=numarray.zeros(val0.shape,numarray.Float64)
356 for i0 in range(val0.shape[0]):
357 for i1 in range(val0.shape[1]):
358 for i2 in range(val0.shape[2]):
359 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1"))
360 elif len(val1.shape)==1:
361 out=numarray.zeros(val0.shape,numarray.Float64)
362 for i0 in range(val0.shape[0]):
363 for i1 in range(val0.shape[1]):
364 for i2 in range(val0.shape[2]):
365 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0]"))
366 elif len(val1.shape)==2:
367 out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
368 for i0 in range(val0.shape[0]):
369 for i1 in range(val0.shape[1]):
370 for i2 in range(val0.shape[2]):
371 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0,i1]"))
372 elif len(val1.shape)==3:
373 out=numarray.zeros(val0.shape,numarray.Float64)
374 for i0 in range(val0.shape[0]):
375 for i1 in range(val0.shape[1]):
376 for i2 in range(val0.shape[2]):
377 out[i0,i1,i2]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0,i1,i2]"))
378 elif len(val1.shape)==4:
379 out=numarray.zeros(val0.shape+val1.shape[3:],numarray.Float64)
380 for i0 in range(val0.shape[0]):
381 for i1 in range(val0.shape[1]):
382 for i2 in range(val0.shape[2]):
383 for j3 in range(val1.shape[3]):
384 out[i0,i1,i2,j3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2]").replace("%a2%","val1[i0,i1,i2,j3]"))
385 else:
386 raise SystemError,"rank of val1 is rargs[1]estricted to 4"
387 elif len(val0.shape)==4:
388 if isinstance(val1,float):
389 out=numarray.zeros(val0.shape,numarray.Float64)
390 for i0 in range(val0.shape[0]):
391 for i1 in range(val0.shape[1]):
392 for i2 in range(val0.shape[2]):
393 for i3 in range(val0.shape[3]):
394 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1"))
395 elif len(val1.shape)==0:
396 out=numarray.zeros(val0.shape,numarray.Float64)
397 for i0 in range(val0.shape[0]):
398 for i1 in range(val0.shape[1]):
399 for i2 in range(val0.shape[2]):
400 for i3 in range(val0.shape[3]):
401 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1"))
402 elif len(val1.shape)==1:
403 out=numarray.zeros(val0.shape,numarray.Float64)
404 for i0 in range(val0.shape[0]):
405 for i1 in range(val0.shape[1]):
406 for i2 in range(val0.shape[2]):
407 for i3 in range(val0.shape[3]):
408 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0]"))
409 elif len(val1.shape)==2:
410 out=numarray.zeros(val0.shape+val1.shape[2:],numarray.Float64)
411 for i0 in range(val0.shape[0]):
412 for i1 in range(val0.shape[1]):
413 for i2 in range(val0.shape[2]):
414 for i3 in range(val0.shape[3]):
415 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0,i1]"))
416 elif len(val1.shape)==3:
417 out=numarray.zeros(val0.shape,numarray.Float64)
418 for i0 in range(val0.shape[0]):
419 for i1 in range(val0.shape[1]):
420 for i2 in range(val0.shape[2]):
421 for i3 in range(val0.shape[3]):
422 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0,i1,i2]"))
423 elif len(val1.shape)==4:
424 out=numarray.zeros(val0.shape,numarray.Float64)
425 for i0 in range(val0.shape[0]):
426 for i1 in range(val0.shape[1]):
427 for i2 in range(val0.shape[2]):
428 for i3 in range(val0.shape[3]):
429 out[i0,i1,i2,i3]=eval(test_expr.replace("%a1%","val0[i0,i1,i2,i3]").replace("%a2%","val1[i0,i1,i2,i3]"))
430 else:
431 raise SystemError,"rank of val1 is restricted to 4"
432 else:
433 raise SystemError,"rank is restricted to 4"
434 return out
435
436
437 def mkText(case,name,a,a1=None,use_tagging_for_expanded_data=False):
438 t_out=""
439 if case=="float":
440 if isinstance(a,float):
441 t_out+=" %s=%s\n"%(name,a)
442 elif a.rank==0:
443 t_out+=" %s=%s\n"%(name,a)
444 else:
445 t_out+=" %s=numarray.array(%s)\n"%(name,a.tolist())
446 elif case=="array":
447 if isinstance(a,float):
448 t_out+=" %s=numarray.array(%s)\n"%(name,a)
449 elif a.rank==0:
450 t_out+=" %s=numarray.array(%s)\n"%(name,a)
451 else:
452 t_out+=" %s=numarray.array(%s)\n"%(name,a.tolist())
453 elif case=="constData":
454 if isinstance(a,float):
455 t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
456 elif a.rank==0:
457 t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
458 else:
459 t_out+=" %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
460 elif case=="taggedData":
461 if isinstance(a,float):
462 t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
463 t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
464 elif a.rank==0:
465 t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
466 t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
467 else:
468 t_out+=" %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
469 t_out+=" %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())
470 elif case=="expandedData":
471 if use_tagging_for_expanded_data:
472 if isinstance(a,float):
473 t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
474 t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
475 elif a.rank==0:
476 t_out+=" %s=Data(%s,self.functionspace)\n"%(name,a)
477 t_out+=" %s.setTaggedValue(1,%s)\n"%(name,a1)
478 else:
479 t_out+=" %s=Data(numarray.array(%s),self.functionspace)\n"%(name,a.tolist())
480 t_out+=" %s.setTaggedValue(1,numarray.array(%s))\n"%(name,a1.tolist())
481 t_out+=" %s.expand()\n"%name
482 else:
483 t_out+=" msk_%s=whereNegative(self.functionspace.getX()[0]-0.5)\n"%name
484 if isinstance(a,float):
485 t_out+=" %s=msk_%s*(%s)+(1.-msk_%s)*(%s)\n"%(name,name,a,name,a1)
486 elif a.rank==0:
487 t_out+=" %s=msk_%s*numarray.array(%s)+(1.-msk_%s)*numarray.array(%s)\n"%(name,name,a,name,a1)
488 else:
489 t_out+=" %s=msk_%s*numarray.array(%s)+(1.-msk_%s)*numarray.array(%s)\n"%(name,name,a.tolist(),name,a1.tolist())
490 elif case=="Symbol":
491 if isinstance(a,float):
492 t_out+=" %s=Symbol(shape=())\n"%(name)
493 elif a.rank==0:
494 t_out+=" %s=Symbol(shape=())\n"%(name)
495 else:
496 t_out+=" %s=Symbol(shape=%s)\n"%(name,str(a.shape))
497
498 return t_out
499
500 def mkTypeAndShapeTest(case,sh,argstr):
501 text=""
502 if case=="float":
503 text+=" self.failUnless(isinstance(%s,float),\"wrong type of result.\")\n"%argstr
504 elif case=="array":
505 text+=" self.failUnless(isinstance(%s,numarray.NumArray),\"wrong type of result.\")\n"%argstr
506 text+=" self.failUnlessEqual(%s.shape,%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
507 elif case in ["constData","taggedData","expandedData"]:
508 text+=" self.failUnless(isinstance(%s,Data),\"wrong type of result.\")\n"%argstr
509 text+=" self.failUnlessEqual(%s.getShape(),%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
510 elif case=="Symbol":
511 text+=" self.failUnless(isinstance(%s,Symbol),\"wrong type of result.\")\n"%argstr
512 text+=" self.failUnlessEqual(%s.getShape(),%s,\"wrong shape of result.\")\n"%(argstr,str(sh))
513 return text
514
515 def mkCode(txt,args=[],intend=""):
516 s=txt.split("\n")
517 if len(s)>1:
518 out=""
519 for l in s:
520 out+=intend+l+"\n"
521 else:
522 out="%sreturn %s\n"%(intend,txt)
523 c=1
524 for r in args:
525 out=out.replace("%%a%s%%"%c,r)
526 return out
527
528 def innerTEST(arg0,arg1):
529 if isinstance(arg0,float):
530 out=numarray.array(arg0*arg1)
531 else:
532 out=(arg0*arg1).sum()
533 return out
534
535 def outerTEST(arg0,arg1):
536 if isinstance(arg0,float):
537 out=numarray.array(arg0*arg1)
538 elif isinstance(arg1,float):
539 out=numarray.array(arg0*arg1)
540 else:
541 out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
542 return out
543
544 def tensorProductTest(arg0,arg1,sh_s):
545 if isinstance(arg0,float):
546 out=numarray.array(arg0*arg1)
547 elif isinstance(arg1,float):
548 out=numarray.array(arg0*arg1)
549 elif len(sh_s)==0:
550 out=numarray.outerproduct(arg0,arg1).resize(arg0.shape+arg1.shape)
551 else:
552 l=len(sh_s)
553 sh0=arg0.shape[:arg0.rank-l]
554 sh1=arg1.shape[l:]
555 ls,l0,l1=1,1,1
556 for i in sh_s: ls*=i
557 for i in sh0: l0*=i
558 for i in sh1: l1*=i
559 out1=numarray.outerproduct(arg0,arg1).resize((l0,ls,ls,l1))
560 out2=numarray.zeros((l0,l1),numarray.Float)
561 for i0 in range(l0):
562 for i1 in range(l1):
563 for i in range(ls): out2[i0,i1]+=out1[i0,i,i,i1]
564 out=out2.resize(sh0+sh1)
565 return out
566
567 def testMatrixMult(arg0,arg1,sh_s):
568 return numarray.matrixmultiply(arg0,arg1)
569
570
571 def testTensorMult(arg0,arg1,sh_s):
572 if len(arg0)==2:
573 return numarray.matrixmultiply(arg0,arg1)
574 else:
575 if arg1.rank==4:
576 out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[2],arg1.shape[3]),numarray.Float)
577 for i0 in range(arg0.shape[0]):
578 for i1 in range(arg0.shape[1]):
579 for i2 in range(arg0.shape[2]):
580 for i3 in range(arg0.shape[3]):
581 for j2 in range(arg1.shape[2]):
582 for j3 in range(arg1.shape[3]):
583 out[i0,i1,j2,j3]+=arg0[i0,i1,i2,i3]*arg1[i2,i3,j2,j3]
584 elif arg1.rank==3:
585 out=numarray.zeros((arg0.shape[0],arg0.shape[1],arg1.shape[2]),numarray.Float)
586 for i0 in range(arg0.shape[0]):
587 for i1 in range(arg0.shape[1]):
588 for i2 in range(arg0.shape[2]):
589 for i3 in range(arg0.shape[3]):
590 for j2 in range(arg1.shape[2]):
591 out[i0,i1,j2]+=arg0[i0,i1,i2,i3]*arg1[i2,i3,j2]
592 elif arg1.rank==2:
593 out=numarray.zeros((arg0.shape[0],arg0.shape[1]),numarray.Float)
594 for i0 in range(arg0.shape[0]):
595 for i1 in range(arg0.shape[1]):
596 for i2 in range(arg0.shape[2]):
597 for i3 in range(arg0.shape[3]):
598 out[i0,i1]+=arg0[i0,i1,i2,i3]*arg1[i2,i3]
599 return out
600
601 def testReduce(arg0,init_val,test_expr,post_expr):
602 out=init_val
603 if isinstance(arg0,float):
604 out=eval(test_expr.replace("%a1%","arg0"))
605 elif arg0.rank==0:
606 out=eval(test_expr.replace("%a1%","arg0"))
607 elif arg0.rank==1:
608 for i0 in range(arg0.shape[0]):
609 out=eval(test_expr.replace("%a1%","arg0[i0]"))
610 elif arg0.rank==2:
611 for i0 in range(arg0.shape[0]):
612 for i1 in range(arg0.shape[1]):
613 out=eval(test_expr.replace("%a1%","arg0[i0,i1]"))
614 elif arg0.rank==3:
615 for i0 in range(arg0.shape[0]):
616 for i1 in range(arg0.shape[1]):
617 for i2 in range(arg0.shape[2]):
618 out=eval(test_expr.replace("%a1%","arg0[i0,i1,i2]"))
619 elif arg0.rank==4:
620 for i0 in range(arg0.shape[0]):
621 for i1 in range(arg0.shape[1]):
622 for i2 in range(arg0.shape[2]):
623 for i3 in range(arg0.shape[3]):
624 out=eval(test_expr.replace("%a1%","arg0[i0,i1,i2,i3]"))
625 return eval(post_expr)
626
627 def clipTEST(arg0,mn,mx):
628 if isinstance(arg0,float):
629 return max(min(arg0,mx),mn)
630 out=numarray.zeros(arg0.shape,numarray.Float64)
631 if arg0.rank==1:
632 for i0 in range(arg0.shape[0]):
633 out[i0]=max(min(arg0[i0],mx),mn)
634 elif arg0.rank==2:
635 for i0 in range(arg0.shape[0]):
636 for i1 in range(arg0.shape[1]):
637 out[i0,i1]=max(min(arg0[i0,i1],mx),mn)
638 elif arg0.rank==3:
639 for i0 in range(arg0.shape[0]):
640 for i1 in range(arg0.shape[1]):
641 for i2 in range(arg0.shape[2]):
642 out[i0,i1,i2]=max(min(arg0[i0,i1,i2],mx),mn)
643 elif arg0.rank==4:
644 for i0 in range(arg0.shape[0]):
645 for i1 in range(arg0.shape[1]):
646 for i2 in range(arg0.shape[2]):
647 for i3 in range(arg0.shape[3]):
648 out[i0,i1,i2,i3]=max(min(arg0[i0,i1,i2,i3],mx),mn)
649 return out
650 def minimumTEST(arg0,arg1):
651 if isinstance(arg0,float):
652 if isinstance(arg1,float):
653 if arg0>arg1:
654 return arg1
655 else:
656 return arg0
657 else:
658 arg0=numarray.ones(arg1.shape)*arg0
659 else:
660 if isinstance(arg1,float):
661 arg1=numarray.ones(arg0.shape)*arg1
662 out=numarray.zeros(arg0.shape,numarray.Float64)
663 if arg0.rank==0:
664 if arg0>arg1:
665 out=arg1
666 else:
667 out=arg0
668 elif arg0.rank==1:
669 for i0 in range(arg0.shape[0]):
670 if arg0[i0]>arg1[i0]:
671 out[i0]=arg1[i0]
672 else:
673 out[i0]=arg0[i0]
674 elif arg0.rank==2:
675 for i0 in range(arg0.shape[0]):
676 for i1 in range(arg0.shape[1]):
677 if arg0[i0,i1]>arg1[i0,i1]:
678 out[i0,i1]=arg1[i0,i1]
679 else:
680 out[i0,i1]=arg0[i0,i1]
681 elif arg0.rank==3:
682 for i0 in range(arg0.shape[0]):
683 for i1 in range(arg0.shape[1]):
684 for i2 in range(arg0.shape[2]):
685 if arg0[i0,i1,i2]>arg1[i0,i1,i2]:
686 out[i0,i1,i2]=arg1[i0,i1,i2]
687 else:
688 out[i0,i1,i2]=arg0[i0,i1,i2]
689 elif arg0.rank==4:
690 for i0 in range(arg0.shape[0]):
691 for i1 in range(arg0.shape[1]):
692 for i2 in range(arg0.shape[2]):
693 for i3 in range(arg0.shape[3]):
694 if arg0[i0,i1,i2,i3]>arg1[i0,i1,i2,i3]:
695 out[i0,i1,i2,i3]=arg1[i0,i1,i2,i3]
696 else:
697 out[i0,i1,i2,i3]=arg0[i0,i1,i2,i3]
698 return out
699
700 def unrollLoops(a,b,o,arg,tap="",x="x"):
701 out=""
702 if a.rank==1:
703 z=""
704 for i99 in range(a.shape[0]):
705 if not z=="": z+="+"
706 if o=="1":
707 z+="(%s)*%s[%s]"%(a[i99]+b[i99],x,i99)
708 else:
709 z+="(%s)*%s[%s]**o+(%s)*%s[%s]"%(a[i99],x,i99,b[i99],x,i99)
710
711 out+=tap+"%s=%s\n"%(arg,z)
712
713 elif a.rank==2:
714 for i0 in range(a.shape[0]):
715 z=""
716 for i99 in range(a.shape[1]):
717 if not z=="": z+="+"
718 if o=="1":
719 z+="(%s)*x[%s]"%(a[i0,i99]+b[i0,i99],i99)
720 else:
721 z+="(%s)*%s[%s]**o+(%s)*%s[%s]"%(a[i0,i99],x,i99,b[i0,i99],x,i99)
722
723 out+=tap+"%s[%s]=%s\n"%(arg,i0,z)
724 elif a.rank==3:
725 for i0 in range(a.shape[0]):
726 for i1 in range(a.shape[1]):
727 z=""
728 for i99 in range(a.shape[2]):
729 if not z=="": z+="+"
730 if o=="1":
731 z+="(%s)*%s[%s]"%(a[i0,i1,i99]+b[i0,i1,i99],x,i99)
732 else:
733 z+="(%s)*%s[%s]**o+(%s)*%s[%s]"%(a[i0,i1,i99],x,i99,b[i0,i1,i99],x,i99)
734
735 out+=tap+"%s[%s,%s]=%s\n"%(arg,i0,i1,z)
736 elif a.rank==4:
737 for i0 in range(a.shape[0]):
738 for i1 in range(a.shape[1]):
739 for i2 in range(a.shape[2]):
740 z=""
741 for i99 in range(a.shape[3]):
742 if not z=="": z+="+"
743 if o=="1":
744 z+="(%s)*%s[%s]"%(a[i0,i1,i2,i99]+b[i0,i1,i2,i99],x,i99)
745 else:
746 z+="(%s)*%s[%s]**o+(%s)*%s[%s]"%(a[i0,i1,i2,i99],x,i99,b[i0,i1,i2,i99],x,i99)
747
748 out+=tap+"%s[%s,%s,%s]=%s\n"%(arg,i0,i1,i2,z)
749 elif a.rank==5:
750 for i0 in range(a.shape[0]):
751 for i1 in range(a.shape[1]):
752 for i2 in range(a.shape[2]):
753 for i3 in range(a.shape[3]):
754 z=""
755 for i99 in range(a.shape[4]):
756 if not z=="": z+="+"
757 if o=="1":
758 z+="(%s)*%s[%s]"%(a[i0,i1,i2,i3,i99]+b[i0,i1,i2,i3,i99],x,i99)
759 else:
760 z+="(%s)*%s[%s]**o+(%s)*%s[%s]"%(a[i0,i1,i2,i3,i99],x,i99,b[i0,i1,i2,i3,i99],x,i99)
761
762 out+=tap+"%s[%s,%s,%s,%s]=%s\n"%(arg,i0,i1,i2,i3,z)
763 return out
764
765 def unrollLoopsOfGrad(a,b,o,arg,tap=""):
766 out=""
767 if a.rank==1:
768 for i99 in range(a.shape[0]):
769 if o=="1":
770 out+=tap+"%s[%s]=(%s)\n"%(arg,i99,a[i99]+b[i99])
771 else:
772 out+=tap+"%s[%s]=o*(%s)*x_ref[%s]**(o-1)+(%s)\n"%(arg,i99,a[i99],i99,b[i99])
773
774 elif a.rank==2:
775 for i0 in range(a.shape[0]):
776 for i99 in range(a.shape[1]):
777 if o=="1":
778 out+=tap+"%s[%s,%s]=(%s)\n"%(arg,i0,i99,a[i0,i99]+b[i0,i99])
779 else:
780 out+=tap+"%s[%s,%s]=o*(%s)*x_ref[%s]**(o-1)+(%s)\n"%(arg,i0,i99,a[i0,i99],i99,b[i0,i99])
781
782 elif a.rank==3:
783 for i0 in range(a.shape[0]):
784 for i1 in range(a.shape[1]):
785 for i99 in range(a.shape[2]):
786 if o=="1":
787 out+=tap+"%s[%s,%s,%s]=(%s)\n"%(arg,i0,i1,i99,a[i0,i1,i99]+b[i0,i1,i99])
788 else:
789 out+=tap+"%s[%s,%s,%s]=o*(%s)*x_ref[%s]**(o-1)+(%s)\n"%(arg,i0,i1,i99,a[i0,i1,i99],i99,b[i0,i1,i99])
790
791 elif a.rank==4:
792 for i0 in range(a.shape[0]):
793 for i1 in range(a.shape[1]):
794 for i2 in range(a.shape[2]):
795 for i99 in range(a.shape[3]):
796 if o=="1":
797 out+=tap+"%s[%s,%s,%s,%s]=(%s)\n"%(arg,i0,i1,i2,i99,a[i0,i1,i2,i99]+b[i0,i1,i2,i99])
798 else:
799 out+=tap+"%s[%s,%s,%s,%s]=o*(%s)*x_ref[%s]**(o-1)+(%s)\n"%(arg,i0,i1,i2,i99,a[i0,i1,i2,i99],i99,b[i0,i1,i2,i99])
800 return out
801 def unrollLoopsOfDiv(a,b,o,arg,tap=""):
802 out=tap+arg+"="
803 if o=="1":
804 z=0.
805 for i99 in range(a.shape[0]):
806 z+=b[i99,i99]+a[i99,i99]
807 out+="(%s)"%z
808 else:
809 z=0.
810 for i99 in range(a.shape[0]):
811 z+=b[i99,i99]
812 if i99>0: out+="+"
813 out+="o*(%s)*x_ref[%s]**(o-1)"%(a[i99,i99],i99)
814 out+="+(%s)"%z
815 return out
816
817 def unrollLoopsOfInteriorIntegral(a,b,where,arg,tap=""):
818 if where=="Function":
819 xfac_o=1.
820 xfac_op=0.
821 z_fac=1./2.
822 z_fac_s=""
823 zo_fac_s="/(o+1.)"
824 elif where=="FunctionOnBoundary":
825 xfac_o=1.
826 xfac_op=0.
827 z_fac=1.
828 z_fac_s="*dim"
829 zo_fac_s="*(1+2.*(dim-1.)/(o+1.))"
830 elif where in ["FunctionOnContactZero","FunctionOnContactOne"]:
831 xfac_o=0.
832 xfac_op=1.
833 z_fac=1./2.
834 z_fac_s=""
835 zo_fac_s="/(o+1.)"
836 out=""
837 if a.rank==1:
838 zo=0.
839 zop=0.
840 z=0.
841 for i99 in range(a.shape[0]):
842 if i99==0:
843 zo+= xfac_o*a[i99]
844 zop+= xfac_op*a[i99]
845 else:
846 zo+=a[i99]
847 z+=b[i99]
848
849 out+=tap+"%s=(%s)%s+(%s)%s"%(arg,zo,zo_fac_s,z*z_fac,z_fac_s)
850 if zop==0.:
851 out+="\n"
852 else:
853 out+="+(%s)*0.5**o\n"%zop
854 elif a.rank==2:
855 for i0 in range(a.shape[0]):
856 zo=0.
857 zop=0.
858 z=0.
859 for i99 in range(a.shape[1]):
860 if i99==0:
861 zo+= xfac_o*a[i0,i99]
862 zop+= xfac_op*a[i0,i99]
863 else:
864 zo+=a[i0,i99]
865 z+=b[i0,i99]
866
867 out+=tap+"%s[%s]=(%s)%s+(%s)%s"%(arg,i0,zo,zo_fac_s,z*z_fac,z_fac_s)
868 if zop==0.:
869 out+="\n"
870 else:
871 out+="+(%s)*0.5**o\n"%zop
872 elif a.rank==3:
873 for i0 in range(a.shape[0]):
874 for i1 in range(a.shape[1]):
875 zo=0.
876 zop=0.
877 z=0.
878 for i99 in range(a.shape[2]):
879 if i99==0:
880 zo+= xfac_o*a[i0,i1,i99]
881 zop+= xfac_op*a[i0,i1,i99]
882 else:
883 zo+=a[i0,i1,i99]
884 z+=b[i0,i1,i99]
885
886 out+=tap+"%s[%s,%s]=(%s)%s+(%s)%s"%(arg,i0,i1,zo,zo_fac_s,z*z_fac,z_fac_s)
887 if zop==0.:
888 out+="\n"
889 else:
890 out+="+(%s)*0.5**o\n"%zop
891 elif a.rank==4:
892 for i0 in range(a.shape[0]):
893 for i1 in range(a.shape[1]):
894 for i2 in range(a.shape[2]):
895 zo=0.
896 zop=0.
897 z=0.
898 for i99 in range(a.shape[3]):
899 if i99==0:
900 zo+= xfac_o*a[i0,i1,i2,i99]
901 zop+= xfac_op*a[i0,i1,i2,i99]
902
903 else:
904 zo+=a[i0,i1,i2,i99]
905 z+=b[i0,i1,i2,i99]
906
907 out+=tap+"%s[%s,%s,%s]=(%s)%s+(%s)%s"%(arg,i0,i1,i2,zo,zo_fac_s,z*z_fac,z_fac_s)
908 if zop==0.:
909 out+="\n"
910 else:
911 out+="+(%s)*0.5**o\n"%zop
912
913 elif a.rank==5:
914 for i0 in range(a.shape[0]):
915 for i1 in range(a.shape[1]):
916 for i2 in range(a.shape[2]):
917 for i3 in range(a.shape[3]):
918 zo=0.
919 zop=0.
920 z=0.
921 for i99 in range(a.shape[4]):
922 if i99==0:
923 zo+= xfac_o*a[i0,i1,i2,i3,i99]
924 zop+= xfac_op*a[i0,i1,i2,i3,i99]
925
926 else:
927 zo+=a[i0,i1,i2,i3,i99]
928 z+=b[i0,i1,i2,i3,i99]
929 out+=tap+"%s[%s,%s,%s,%s]=(%s)%s+(%s)%s"%(arg,i0,i1,i2,i3,zo,zo_fac_s,z*z_fac,z_fac_s)
930 if zop==0.:
931 out+="\n"
932 else:
933 out+="+(%s)*0.5**o\n"%zop
934
935 return out
936 def unrollLoopsSimplified(b,arg,tap=""):
937 out=""
938 if isinstance(b,float) or b.rank==0:
939 out+=tap+"%s=(%s)*x[0]\n"%(arg,str(b))
940
941 elif b.rank==1:
942 for i0 in range(b.shape[0]):
943 out+=tap+"%s[%s]=(%s)*x[%s]\n"%(arg,i0,b[i0],i0)
944 elif b.rank==2:
945 for i0 in range(b.shape[0]):
946 for i1 in range(b.shape[1]):
947 out+=tap+"%s[%s,%s]=(%s)*x[%s]\n"%(arg,i0,i1,b[i0,i1],i1)
948 elif b.rank==3:
949 for i0 in range(b.shape[0]):
950 for i1 in range(b.shape[1]):
951 for i2 in range(b.shape[2]):
952 out+=tap+"%s[%s,%s,%s]=(%s)*x[%s]\n"%(arg,i0,i1,i2,b[i0,i1,i2],i2)
953 elif b.rank==4:
954 for i0 in range(b.shape[0]):
955 for i1 in range(b.shape[1]):
956 for i2 in range(b.shape[2]):
957 for i3 in range(b.shape[3]):
958 out+=tap+"%s[%s,%s,%s,%s]=(%s)*x[%s]\n"%(arg,i0,i1,i2,i3,b[i0,i1,i2,i3],i3)
959 return out
960
961 def unrollLoopsOfL2(b,where,arg,tap=""):
962 out=""
963 z=[]
964 if isinstance(b,float) or b.rank==0:
965 z.append(b**2)
966 elif b.rank==1:
967 for i0 in range(b.shape[0]):
968 z.append(b[i0]**2)
969 elif b.rank==2:
970 for i1 in range(b.shape[1]):
971 s=0
972 for i0 in range(b.shape[0]):
973 s+=b[i0,i1]**2
974 z.append(s)
975 elif b.rank==3:
976 for i2 in range(b.shape[2]):
977 s=0
978 for i0 in range(b.shape[0]):
979 for i1 in range(b.shape[1]):
980 s+=b[i0,i1,i2]**2
981 z.append(s)
982
983 elif b.rank==4:
984 for i3 in range(b.shape[3]):
985 s=0
986 for i0 in range(b.shape[0]):
987 for i1 in range(b.shape[1]):
988 for i2 in range(b.shape[2]):
989 s+=b[i0,i1,i2,i3]**2
990 z.append(s)
991 if where=="Function":
992 xfac_o=1.
993 xfac_op=0.
994 z_fac_s=""
995 zo_fac_s=""
996 zo_fac=1./3.
997 elif where=="FunctionOnBoundary":
998 xfac_o=1.
999 xfac_op=0.
1000 z_fac_s="*dim"
1001 zo_fac_s="*(2.*dim+1.)/3."
1002 zo_fac=1.
1003 elif where in ["FunctionOnContactZero","FunctionOnContactOne"]:
1004 xfac_o=0.
1005 xfac_op=1.
1006 z_fac_s=""
1007 zo_fac_s=""
1008 zo_fac=1./3.
1009 zo=0.
1010 zop=0.
1011 for i99 in range(len(z)):
1012 if i99==0:
1013 zo+=xfac_o*z[i99]
1014 zop+=xfac_op*z[i99]
1015 else:
1016 zo+=z[i99]
1017 out+=tap+"%s=sqrt((%s)%s"%(arg,zo*zo_fac,zo_fac_s)
1018 if zop==0.:
1019 out+=")\n"
1020 else:
1021 out+="+(%s))\n"%(zop*0.5**2)
1022 return out
1023
1024 #=======================================================================================================
1025 # L2
1026 #=======================================================================================================
1027 for where in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]:
1028 for data in ["Data","Symbol"]:
1029 for sh in [ (),(2,), (4,5), (6,2,2),(4,5,3,2)]:
1030 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1031 tname="test_L2_on%s_from%s_rank%s"%(where,data,len(sh))
1032 text+=" def %s(self):\n"%tname
1033 text+=" \"\"\"\n"
1034 text+=" tests L2-norm of %s on the %s\n\n"%(data,where)
1035 text+=" assumptions: self.domain supports integration on %s\n"%where
1036 text+=" \"\"\"\n"
1037 text+=" dim=self.domain.getDim()\n"
1038 text+=" w=%s(self.domain)\n"%where
1039 text+=" x=w.getX()\n"
1040 o="1"
1041 if len(sh)>0:
1042 sh_2=sh[:len(sh)-1]+(2,)
1043 sh_3=sh[:len(sh)-1]+(3,)
1044 b_2=makeArray(sh[:len(sh)-1]+(2,),[-1.,1])
1045 b_3=makeArray(sh[:len(sh)-1]+(3,),[-1.,1])
1046 else:
1047 sh_2=()
1048 sh_3=()
1049 b_2=makeArray(sh,[-1.,1])
1050 b_3=makeArray(sh,[-1.,1])
1051
1052 if data=="Symbol":
1053 val="s"
1054 res="sub"
1055 else:
1056 val="arg"
1057 res="res"
1058 text+=" if dim==2:\n"
1059 if data=="Symbol":
1060 text+=" arg=Symbol(shape=%s,dim=dim)\n"%str(sh_2)
1061
1062 text+=" %s=Data(0,%s,w)\n"%(val,sh_2)
1063 text+=unrollLoopsSimplified(b_2,val,tap=" ")
1064 text+=unrollLoopsOfL2(b_2,where,"ref",tap=" ")
1065 text+="\n else:\n"
1066 if data=="Symbol":
1067 text+=" arg=Symbol(shape=%s,dim=dim)\n"%str(sh_3)
1068 text+=" %s=Data(0,%s,w)\n"%(val,sh_3)
1069 text+=unrollLoopsSimplified(b_3,val,tap=" ")
1070 text+=unrollLoopsOfL2(b_3,where,"ref",tap=" ")
1071 text+="\n res=L2(arg)\n"
1072 if data=="Symbol":
1073 text+=" sub=res.substitute({arg:s})\n"
1074 text+=" self.failUnless(isinstance(res,Symbol),\"wrong type of result.\")\n"
1075 text+=" self.failUnlessEqual(res.getShape(),(),\"wrong shape of result.\")\n"
1076 else:
1077 text+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
1078 text+=" self.failUnlessAlmostEqual(%s,ref,int(-log10(self.RES_TOL)),\"wrong result\")\n"%res
1079 t_prog+=text
1080 print t_prog
1081 1/0
1082
1083 #=======================================================================================================
1084 # div
1085 #=======================================================================================================
1086 for where in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]:
1087 for data in ["Data","Symbol"]:
1088 for case in ["ContinuousFunction","Solution","ReducedSolution"]:
1089 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1090 tname="test_div_on%s_from%s_%s"%(where,data,case)
1091 text+=" def %s(self):\n"%tname
1092 text+=" \"\"\"\n"
1093 text+=" tests divergence of %s on the %s\n\n"%(data,where)
1094 text+=" assumptions: %s(self.domain) exists\n"%case
1095 text+=" self.domain supports div on %s\n"%where
1096 text+=" \"\"\"\n"
1097 if case=="ReducedSolution":
1098 text+=" o=1\n"
1099 o="1"
1100 else:
1101 text+=" o=self.order\n"
1102 o="o"
1103 text+=" dim=self.domain.getDim()\n"
1104 text+=" w_ref=%s(self.domain)\n"%where
1105 text+=" x_ref=w_ref.getX()\n"
1106 text+=" w=%s(self.domain)\n"%case
1107 text+=" x=w.getX()\n"
1108 a_2=makeArray((2,2),[-1.,1])
1109 b_2=makeArray((2,2),[-1.,1])
1110 a_3=makeArray((3,3),[-1.,1])
1111 b_3=makeArray((3,3),[-1.,1])
1112 if data=="Symbol":
1113 text+=" arg=Symbol(shape=(dim,),dim=dim)\n"
1114 val="s"
1115 res="sub"
1116 else:
1117 val="arg"
1118 res="res"
1119 text+=" %s=Vector(0,w)\n"%val
1120 text+=" if dim==2:\n"
1121 text+=unrollLoops(a_2,b_2,o,val,tap=" ")
1122 text+=unrollLoopsOfDiv(a_2,b_2,o,"ref",tap=" ")
1123 text+="\n else:\n"
1124
1125 text+=unrollLoops(a_3,b_3,o,val,tap=" ")
1126 text+=unrollLoopsOfDiv(a_3,b_3,o,"ref",tap=" ")
1127 text+="\n res=div(arg,where=w_ref)\n"
1128 if data=="Symbol":
1129 text+=" sub=res.substitute({arg:s})\n"
1130 text+=" self.failUnless(isinstance(res,%s),\"wrong type of result.\")\n"%data
1131 text+=" self.failUnlessEqual(res.getShape(),(),\"wrong shape of result.\")\n"
1132 text+=" self.failUnlessEqual(%s.getFunctionSpace(),w_ref,\"wrong function space of result.\")\n"%res
1133 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1134
1135
1136 t_prog+=text
1137 print t_prog
1138 1/0
1139
1140 #=======================================================================================================
1141 # interpolation
1142 #=======================================================================================================
1143 for where in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne","Solution","ReducedSolution"]:
1144 for data in ["Data","Symbol"]:
1145 for case in ["ContinuousFunction","Solution","ReducedSolution","Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]:
1146 for sh in [ (),(2,), (4,5), (6,2,2),(4,5,3,2)]:
1147 if where==case or \
1148 ( case in ["ContinuousFunction","Solution","ReducedSolution"] and where in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"] ) or \
1149 ( case in ["FunctionOnContactZero","FunctionOnContactOne"] and where in ["FunctionOnContactZero","FunctionOnContactOne"] ) or \
1150 (case=="ContinuousFunction" and where in ["Solution","ReducedSolution"]) or \
1151 (case=="Solution" and where=="ReducedSolution") :
1152
1153
1154 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1155 tname="test_interpolation_on%s_from%s_%s_rank%s"%(where,data,case,len(sh))
1156 text+=" def %s(self):\n"%tname
1157 text+=" \"\"\"\n"
1158 text+=" tests interpolation for rank %s %s onto the %s\n\n"%(len(sh),data,where)
1159 text+=" assumptions: self.domain supports inpterpolation from %s onto %s\n"%(case,where)
1160 text+=" \"\"\"\n"
1161 if case=="ReducedSolution" or where=="ReducedSolution":
1162 text+=" o=1\n"
1163 o="1"
1164 else:
1165 text+=" o=self.order\n"
1166 o="o"
1167 text+=" dim=self.domain.getDim()\n"
1168 text+=" w_ref=%s(self.domain)\n"%where
1169 text+=" x_ref=w_ref.getX()\n"
1170 text+=" w=%s(self.domain)\n"%case
1171 text+=" x=w.getX()\n"
1172 a_2=makeArray(sh+(2,),[-1.,1])
1173 b_2=makeArray(sh+(2,),[-1.,1])
1174 a_3=makeArray(sh+(3,),[-1.,1])
1175 b_3=makeArray(sh+(3,),[-1.,1])
1176 if data=="Symbol":
1177 text+=" arg=Symbol(shape=%s,dim=dim)\n"%str(sh)
1178 val="s"
1179 res="sub"
1180 else:
1181 val="arg"
1182 res="res"
1183 text+=" %s=Data(0,%s,w)\n"%(val,str(sh))
1184 text+=" ref=Data(0,%s,w_ref)\n"%str(sh)
1185 text+=" if dim==2:\n"
1186 text+=unrollLoops(a_2,b_2,o,val,tap=" ")
1187 text+=unrollLoops(a_2,b_2,o,"ref",tap=" ",x="x_ref")
1188 text+=" else:\n"
1189
1190 text+=unrollLoops(a_3,b_3,o,val,tap=" ")
1191 text+=unrollLoops(a_3,b_3,o,"ref",tap=" ",x="x_ref")
1192 text+=" res=interpolate(arg,where=w_ref)\n"
1193 if data=="Symbol":
1194 text+=" sub=res.substitute({arg:s})\n"
1195 text+=" self.failUnless(isinstance(res,%s),\"wrong type of result.\")\n"%data
1196 text+=" self.failUnlessEqual(%s.getFunctionSpace(),w_ref,\"wrong functionspace of result.\")\n"%res
1197 text+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)
1198 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1199 t_prog+=text
1200 print test_header
1201 print t_prog
1202 print test_tail
1203 1/0
1204
1205 #=======================================================================================================
1206 # grad
1207 #=======================================================================================================
1208 for where in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]:
1209 for data in ["Data","Symbol"]:
1210 for case in ["ContinuousFunction","Solution","ReducedSolution"]:
1211 for sh in [ (),(2,), (4,5), (6,2,2)]:
1212 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1213 tname="test_grad_on%s_from%s_%s_rank%s"%(where,data,case,len(sh))
1214 text+=" def %s(self):\n"%tname
1215 text+=" \"\"\"\n"
1216 text+=" tests gradient for rank %s %s on the %s\n\n"%(len(sh),data,where)
1217 text+=" assumptions: %s(self.domain) exists\n"%case
1218 text+=" self.domain supports gardient on %s\n"%where
1219 text+=" \"\"\"\n"
1220 if case=="ReducedSolution":
1221 text+=" o=1\n"
1222 o="1"
1223 else:
1224 text+=" o=self.order\n"
1225 o="o"
1226 text+=" dim=self.domain.getDim()\n"
1227 text+=" w_ref=%s(self.domain)\n"%where
1228 text+=" x_ref=w_ref.getX()\n"
1229 text+=" w=%s(self.domain)\n"%case
1230 text+=" x=w.getX()\n"
1231 a_2=makeArray(sh+(2,),[-1.,1])
1232 b_2=makeArray(sh+(2,),[-1.,1])
1233 a_3=makeArray(sh+(3,),[-1.,1])
1234 b_3=makeArray(sh+(3,),[-1.,1])
1235 if data=="Symbol":
1236 text+=" arg=Symbol(shape=%s,dim=dim)\n"%str(sh)
1237 val="s"
1238 res="sub"
1239 else:
1240 val="arg"
1241 res="res"
1242 text+=" %s=Data(0,%s,w)\n"%(val,str(sh))
1243 text+=" ref=Data(0,%s+(dim,),w_ref)\n"%str(sh)
1244 text+=" if dim==2:\n"
1245 text+=unrollLoops(a_2,b_2,o,val,tap=" ")
1246 text+=unrollLoopsOfGrad(a_2,b_2,o,"ref",tap=" ")
1247 text+=" else:\n"
1248
1249 text+=unrollLoops(a_3,b_3,o,val,tap=" ")
1250 text+=unrollLoopsOfGrad(a_3,b_3,o,"ref",tap=" ")
1251 text+=" res=grad(arg,where=w_ref)\n"
1252 if data=="Symbol":
1253 text+=" sub=res.substitute({arg:s})\n"
1254 text+=" self.failUnless(isinstance(res,%s),\"wrong type of result.\")\n"%data
1255 text+=" self.failUnlessEqual(res.getShape(),%s+(dim,),\"wrong shape of result.\")\n"%str(sh)
1256 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1257
1258
1259 t_prog+=text
1260 print test_header
1261 print t_prog
1262 print test_tail
1263 1/0
1264
1265
1266 #=======================================================================================================
1267 # integrate
1268 #=======================================================================================================
1269 for where in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]:
1270 for data in ["Data","Symbol"]:
1271 for case in ["ContinuousFunction","Solution","ReducedSolution","Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]:
1272 for sh in [ (),(2,), (4,5), (6,2,2),(4,5,3,2)]:
1273 if (not case in ["Function","FunctionOnBoundary","FunctionOnContactZero","FunctionOnContactOne"]) or where==case:
1274 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1275 tname="test_integrate_on%s_from%s_%s_rank%s"%(where,data,case,len(sh))
1276 text+=" def %s(self):\n"%tname
1277 text+=" \"\"\"\n"
1278 text+=" tests integral of rank %s %s on the %s\n\n"%(len(sh),data,where)
1279 text+=" assumptions: %s(self.domain) exists\n"%case
1280 text+=" self.domain supports integral on %s\n"%where
1281
1282 text+=" \"\"\"\n"
1283 if case=="ReducedSolution":
1284 text+=" o=1\n"
1285 o="1"
1286 else:
1287 text+=" o=self.order\n"
1288 o="o"
1289 text+=" dim=self.domain.getDim()\n"
1290 text+=" w_ref=%s(self.domain)\n"%where
1291 text+=" w=%s(self.domain)\n"%case
1292 text+=" x=w.getX()\n"
1293 a_2=makeArray(sh+(2,),[-1.,1])
1294 b_2=makeArray(sh+(2,),[-1.,1])
1295 a_3=makeArray(sh+(3,),[-1.,1])
1296 b_3=makeArray(sh+(3,),[-1.,1])
1297 if data=="Symbol":
1298 text+=" arg=Symbol(shape=%s)\n"%str(sh)
1299 val="s"
1300 res="sub"
1301 else:
1302 val="arg"
1303 res="res"
1304
1305 text+=" %s=Data(0,%s,w)\n"%(val,str(sh))
1306 if not len(sh)==0:
1307 text+=" ref=numarray.zeros(%s,numarray.Float)\n"%str(sh)
1308 text+=" if dim==2:\n"
1309 text+=unrollLoops(a_2,b_2,o,val,tap=" ")
1310 text+=unrollLoopsOfInteriorIntegral(a_2,b_2,where,"ref",tap=" ")
1311 text+=" else:\n"
1312
1313 text+=unrollLoops(a_3,b_3,o,val,tap=" ")
1314 text+=unrollLoopsOfInteriorIntegral(a_3,b_3,where,"ref",tap=" ")
1315 if case in ["ContinuousFunction","Solution","ReducedSolution"]:
1316 text+=" res=integrate(arg,where=w_ref)\n"
1317 else:
1318 text+=" res=integrate(arg)\n"
1319
1320 if data=="Symbol":
1321 text+=" sub=res.substitute({arg:s})\n"
1322 if len(sh)==0 and data=="Data":
1323 text+=" self.failUnless(isinstance(%s,float),\"wrong type of result.\")\n"%res
1324 else:
1325 if data=="Symbol":
1326 text+=" self.failUnless(isinstance(res,Symbol),\"wrong type of result.\")\n"
1327 text+=" self.failUnlessEqual(res.getShape(),%s,\"wrong shape of result.\")\n"%str(sh)
1328 else:
1329 text+=" self.failUnless(isinstance(res,numarray.NumArray),\"wrong type of result.\")\n"
1330 text+=" self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh)
1331 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1332
1333
1334 t_prog+=text
1335 print test_header
1336 print t_prog
1337 print test_tail
1338 1/0
1339 #=======================================================================================================
1340 # inverse
1341 #=======================================================================================================
1342 name="inverse"
1343 for case0 in ["array","Symbol","constData","taggedData","expandedData"]:
1344 for sh0 in [ (1,1), (2,2), (3,3)]:
1345 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1346 tname="test_%s_%s_dim%s"%(name,case0,sh0[0])
1347 text+=" def %s(self):\n"%tname
1348 a_0=makeArray(sh0,[-1.,1])
1349 for i in range(sh0[0]): a_0[i,i]+=2
1350 if case0 in ["taggedData", "expandedData"]:
1351 a1_0=makeArray(sh0,[-1.,1])
1352 for i in range(sh0[0]): a1_0[i,i]+=3
1353 else:
1354 a1_0=a_0
1355
1356 text+=mkText(case0,"arg",a_0,a1_0)
1357 text+=" res=%s(arg)\n"%name
1358 if case0=="Symbol":
1359 text+=mkText("array","s",a_0,a1_0)
1360 text+=" sub=res.substitute({arg:s})\n"
1361 res="sub"
1362 ref="s"
1363 else:
1364 ref="arg"
1365 res="res"
1366 text+=mkTypeAndShapeTest(case0,sh0,"res")
1367 text+=" self.failUnless(Lsup(matrixmult(%s,%s)-kronecker(%s))<=self.RES_TOL,\"wrong result\")\n"%(res,ref,sh0[0])
1368
1369 if case0 == "taggedData" :
1370 t_prog_with_tags+=text
1371 else:
1372 t_prog+=text
1373
1374 print test_header
1375 # print t_prog
1376 print t_prog_with_tags
1377 print test_tail
1378 1/0
1379
1380 #=======================================================================================================
1381 # trace
1382 #=======================================================================================================
1383 def traceTest(r,offset):
1384 sh=r.shape
1385 r1=1
1386 for i in range(offset): r1*=sh[i]
1387 r2=1
1388 for i in range(offset+2,len(sh)): r2*=sh[i]
1389 r_s=numarray.reshape(r,(r1,sh[offset],sh[offset],r2))
1390 s=numarray.zeros([r1,r2],numarray.Float)
1391 for i1 in range(r1):
1392 for i2 in range(r2):
1393 for j in range(sh[offset]): s[i1,i2]+=r_s[i1,j,j,i2]
1394 return s.resize(sh[:offset]+sh[offset+2:])
1395 name,tt="trace",traceTest
1396 for case0 in ["array","Symbol","constData","taggedData","expandedData"]:
1397 for sh0 in [ (4,5), (6,2,2),(3,2,3,4)]:
1398 for offset in range(len(sh0)-1):
1399 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1400 tname="test_%s_%s_rank%s_offset%s"%(name,case0,len(sh0),offset)
1401 text+=" def %s(self):\n"%tname
1402 sh_t=list(sh0)
1403 sh_t[offset+1]=sh_t[offset]
1404 sh_t=tuple(sh_t)
1405 sh_r=[]
1406 for i in range(offset): sh_r.append(sh0[i])
1407 for i in range(offset+2,len(sh0)): sh_r.append(sh0[i])
1408 sh_r=tuple(sh_r)
1409 a_0=makeArray(sh_t,[-1.,1])
1410 if case0 in ["taggedData", "expandedData"]:
1411 a1_0=makeArray(sh_t,[-1.,1])
1412 else:
1413 a1_0=a_0
1414 r=tt(a_0,offset)
1415 r1=tt(a1_0,offset)
1416 text+=mkText(case0,"arg",a_0,a1_0)
1417 text+=" res=%s(arg,%s)\n"%(name,offset)
1418 if case0=="Symbol":
1419 text+=mkText("array","s",a_0,a1_0)
1420 text+=" sub=res.substitute({arg:s})\n"
1421 res="sub"
1422 text+=mkText("array","ref",r,r1)
1423 else:
1424 res="res"
1425 text+=mkText(case0,"ref",r,r1)
1426 text+=mkTypeAndShapeTest(case0,sh_r,"res")
1427 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1428
1429 if case0 == "taggedData" :
1430 t_prog_with_tags+=text
1431 else:
1432 t_prog+=text
1433
1434 print test_header
1435 # print t_prog
1436 print t_prog_with_tags
1437 print test_tail
1438 1/0
1439
1440 #=======================================================================================================
1441 # clip
1442 #=======================================================================================================
1443 oper_L=[["clip",clipTEST]]
1444 for oper in oper_L:
1445 for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1446 for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1447 if len(sh0)==0 or not case0=="float":
1448 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1449 tname="test_%s_%s_rank%s"%(oper[0],case0,len(sh0))
1450 text+=" def %s(self):\n"%tname
1451 a_0=makeArray(sh0,[-1.,1])
1452 if case0 in ["taggedData", "expandedData"]:
1453 a1_0=makeArray(sh0,[-1.,1])
1454 else:
1455 a1_0=a_0
1456
1457 r=oper[1](a_0,-0.3,0.5)
1458 r1=oper[1](a1_0,-0.3,0.5)
1459 text+=mkText(case0,"arg",a_0,a1_0)
1460 text+=" res=%s(arg,-0.3,0.5)\n"%oper[0]
1461 if case0=="Symbol":
1462 text+=mkText("array","s",a_0,a1_0)
1463 text+=" sub=res.substitute({arg:s})\n"
1464 res="sub"
1465 text+=mkText("array","ref",r,r1)
1466 else:
1467 res="res"
1468 text+=mkText(case0,"ref",r,r1)
1469 text+=mkTypeAndShapeTest(case0,sh0,"res")
1470 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1471
1472 if case0 == "taggedData" :
1473 t_prog_with_tags+=text
1474 else:
1475 t_prog+=text
1476
1477 print test_header
1478 # print t_prog
1479 print t_prog_with_tags
1480 print test_tail
1481 1/0
1482
1483 #=======================================================================================================
1484 # maximum, minimum, clipping
1485 #=======================================================================================================
1486 oper_L=[ ["maximum",maximumTEST],
1487 ["minimum",minimumTEST]]
1488 for oper in oper_L:
1489 for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1490 for sh1 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1491 for case1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1492 for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1493 if (len(sh0)==0 or not case0=="float") and (len(sh1)==0 or not case1=="float") \
1494 and (sh0==sh1 or len(sh0)==0 or len(sh1)==0) :
1495 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
1496
1497 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1498 tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
1499 text+=" def %s(self):\n"%tname
1500 a_0=makeArray(sh0,[-1.,1])
1501 if case0 in ["taggedData", "expandedData"]:
1502 a1_0=makeArray(sh0,[-1.,1])
1503 else:
1504 a1_0=a_0
1505
1506 a_1=makeArray(sh1,[-1.,1])
1507 if case1 in ["taggedData", "expandedData"]:
1508 a1_1=makeArray(sh1,[-1.,1])
1509 else:
1510 a1_1=a_1
1511 r=oper[1](a_0,a_1)
1512 r1=oper[1](a1_0,a1_1)
1513 text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
1514 text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
1515 text+=" res=%s(arg0,arg1)\n"%oper[0]
1516 case=getResultCaseForBin(case0,case1)
1517 if case=="Symbol":
1518 c0_res,c1_res=case0,case1
1519 subs="{"
1520 if case0=="Symbol":
1521 text+=mkText("array","s0",a_0,a1_0)
1522 subs+="arg0:s0"
1523 c0_res="array"
1524 if case1=="Symbol":
1525 text+=mkText("array","s1",a_1,a1_1)
1526 if not subs.endswith("{"): subs+=","
1527 subs+="arg1:s1"
1528 c1_res="array"
1529 subs+="}"
1530 text+=" sub=res.substitute(%s)\n"%subs
1531 res="sub"
1532 text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
1533 else:
1534 res="res"
1535 text+=mkText(case,"ref",r,r1)
1536 if len(sh0)>len(sh1):
1537 text+=mkTypeAndShapeTest(case,sh0,"res")
1538 else:
1539 text+=mkTypeAndShapeTest(case,sh1,"res")
1540 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1541
1542 if case0 == "taggedData" or case1 == "taggedData":
1543 t_prog_with_tags+=text
1544 else:
1545 t_prog+=text
1546
1547 print test_header
1548 # print t_prog
1549 print t_prog_with_tags
1550 print test_tail
1551 1/0
1552
1553
1554 #=======================================================================================================
1555 # outer inner
1556 #=======================================================================================================
1557 oper=["outer",outerTEST]
1558 # oper=["inner",innerTEST]
1559 for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1560 for sh1 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1561 for case1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1562 for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1563 if (len(sh0)==0 or not case0=="float") and (len(sh1)==0 or not case1=="float") \
1564 and len(sh0+sh1)<5:
1565 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
1566
1567 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1568 tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
1569 text+=" def %s(self):\n"%tname
1570 a_0=makeArray(sh0,[-1.,1])
1571 if case0 in ["taggedData", "expandedData"]:
1572 a1_0=makeArray(sh0,[-1.,1])
1573 else:
1574 a1_0=a_0
1575
1576 a_1=makeArray(sh1,[-1.,1])
1577 if case1 in ["taggedData", "expandedData"]:
1578 a1_1=makeArray(sh1,[-1.,1])
1579 else:
1580 a1_1=a_1
1581 r=oper[1](a_0,a_1)
1582 r1=oper[1](a1_0,a1_1)
1583 text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
1584 text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
1585 text+=" res=%s(arg0,arg1)\n"%oper[0]
1586 case=getResultCaseForBin(case0,case1)
1587 if case=="Symbol":
1588 c0_res,c1_res=case0,case1
1589 subs="{"
1590 if case0=="Symbol":
1591 text+=mkText("array","s0",a_0,a1_0)
1592 subs+="arg0:s0"
1593 c0_res="array"
1594 if case1=="Symbol":
1595 text+=mkText("array","s1",a_1,a1_1)
1596 if not subs.endswith("{"): subs+=","
1597 subs+="arg1:s1"
1598 c1_res="array"
1599 subs+="}"
1600 text+=" sub=res.substitute(%s)\n"%subs
1601 res="sub"
1602 text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
1603 else:
1604 res="res"
1605 text+=mkText(case,"ref",r,r1)
1606 text+=mkTypeAndShapeTest(case,sh0+sh1,"res")
1607 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1608
1609 if case0 == "taggedData" or case1 == "taggedData":
1610 t_prog_with_tags+=text
1611 else:
1612 t_prog+=text
1613
1614 print test_header
1615 # print t_prog
1616 print t_prog_with_tags
1617 print test_tail
1618 1/0
1619
1620 #=======================================================================================================
1621 # local reduction
1622 #=======================================================================================================
1623 for oper in [["length",0.,"out+%a1%**2","math.sqrt(out)"],
1624 ["maxval",-1.e99,"max(out,%a1%)","out"],
1625 ["minval",1.e99,"min(out,%a1%)","out"] ]:
1626 for case in case_set:
1627 for sh in shape_set:
1628 if not case=="float" or len(sh)==0:
1629 text=""
1630 text+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1631 tname="def test_%s_%s_rank%s"%(oper[0],case,len(sh))
1632 text+=" %s(self):\n"%tname
1633 a=makeArray(sh,[-1.,1.])
1634 a1=makeArray(sh,[-1.,1.])
1635 r1=testReduce(a1,oper[1],oper[2],oper[3])
1636 r=testReduce(a,oper[1],oper[2],oper[3])
1637
1638 text+=mkText(case,"arg",a,a1)
1639 text+=" res=%s(arg)\n"%oper[0]
1640 if case=="Symbol":
1641 text+=mkText("array","s",a,a1)
1642 text+=" sub=res.substitute({arg:s})\n"
1643 text+=mkText("array","ref",r,r1)
1644 res="sub"
1645 else:
1646 text+=mkText(case,"ref",r,r1)
1647 res="res"
1648 if oper[0]=="length":
1649 text+=mkTypeAndShapeTest(case,(),"res")
1650 else:
1651 if case=="float" or case=="array":
1652 text+=mkTypeAndShapeTest("float",(),"res")
1653 else:
1654 text+=mkTypeAndShapeTest(case,(),"res")
1655 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1656 if case == "taggedData":
1657 t_prog_with_tags+=text
1658 else:
1659 t_prog+=text
1660 print test_header
1661 # print t_prog
1662 print t_prog_with_tags
1663 print test_tail
1664 1/0
1665
1666 #=======================================================================================================
1667 # tensor multiply
1668 #=======================================================================================================
1669 # oper=["generalTensorProduct",tensorProductTest]
1670 # oper=["matrixmult",testMatrixMult]
1671 oper=["tensormult",testTensorMult]
1672
1673 for case0 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1674 for sh0 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1675 for case1 in ["float","array","Symbol","constData","taggedData","expandedData"]:
1676 for sh1 in [ (),(2,), (4,5), (6,2,2),(3,2,3,4)]:
1677 for sh_s in [ (),(3,), (2,3), (2,4,3),(4,2,3,2)]:
1678 if (len(sh0+sh_s)==0 or not case0=="float") and (len(sh1+sh_s)==0 or not case1=="float") \
1679 and len(sh0+sh1)<5 and len(sh0+sh_s)<5 and len(sh1+sh_s)<5:
1680 # if len(sh_s)==1 and len(sh0+sh_s)==2 and (len(sh_s+sh1)==1 or len(sh_s+sh1)==2)): # test for matrixmult
1681 if ( len(sh_s)==1 and len(sh0+sh_s)==2 and ( len(sh1+sh_s)==2 or len(sh1+sh_s)==1 )) or (len(sh_s)==2 and len(sh0+sh_s)==4 and (len(sh1+sh_s)==2 or len(sh1+sh_s)==3 or len(sh1+sh_s)==4)): # test for tensormult
1682 case=getResultCaseForBin(case0,case1)
1683 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
1684 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1685 # tname="test_generalTensorProduct_%s_rank%s_%s_rank%s_offset%s"%(case0,len(sh0+sh_s),case1,len(sh_s+sh1),len(sh_s))
1686 #tname="test_matrixmult_%s_rank%s_%s_rank%s"%(case0,len(sh0+sh_s),case1,len(sh_s+sh1))
1687 tname="test_tensormult_%s_rank%s_%s_rank%s"%(case0,len(sh0+sh_s),case1,len(sh_s+sh1))
1688 # if tname=="test_generalTensorProduct_array_rank1_array_rank2_offset1":
1689 # print tnametest_generalTensorProduct_Symbol_rank1_Symbol_rank3_offset1
1690 text+=" def %s(self):\n"%tname
1691 a_0=makeArray(sh0+sh_s,[-1.,1])
1692 if case0 in ["taggedData", "expandedData"]:
1693 a1_0=makeArray(sh0+sh_s,[-1.,1])
1694 else:
1695 a1_0=a_0
1696
1697 a_1=makeArray(sh_s+sh1,[-1.,1])
1698 if case1 in ["taggedData", "expandedData"]:
1699 a1_1=makeArray(sh_s+sh1,[-1.,1])
1700 else:
1701 a1_1=a_1
1702 r=oper[1](a_0,a_1,sh_s)
1703 r1=oper[1](a1_0,a1_1,sh_s)
1704 text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
1705 text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
1706 #text+=" res=matrixmult(arg0,arg1)\n"
1707 text+=" res=tensormult(arg0,arg1)\n"
1708 #text+=" res=generalTensorProduct(arg0,arg1,offset=%s)\n"%(len(sh_s))
1709 if case=="Symbol":
1710 c0_res,c1_res=case0,case1
1711 subs="{"
1712 if case0=="Symbol":
1713 text+=mkText("array","s0",a_0,a1_0)
1714 subs+="arg0:s0"
1715 c0_res="array"
1716 if case1=="Symbol":
1717 text+=mkText("array","s1",a_1,a1_1)
1718 if not subs.endswith("{"): subs+=","
1719 subs+="arg1:s1"
1720 c1_res="array"
1721 subs+="}"
1722 text+=" sub=res.substitute(%s)\n"%subs
1723 res="sub"
1724 text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
1725 else:
1726 res="res"
1727 text+=mkText(case,"ref",r,r1)
1728 text+=mkTypeAndShapeTest(case,sh0+sh1,"res")
1729 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1730 if case0 == "taggedData" or case1 == "taggedData":
1731 t_prog_with_tags+=text
1732 else:
1733 t_prog+=text
1734 print test_header
1735 # print t_prog
1736 print t_prog_with_tags
1737 print test_tail
1738 1/0
1739 #=======================================================================================================
1740 # basic binary operation overloading (tests only!)
1741 #=======================================================================================================
1742 oper_range=[-5.,5.]
1743 for oper in [["add" ,"+",[-5.,5.]],
1744 ["sub" ,"-",[-5.,5.]],
1745 ["mult","*",[-5.,5.]],
1746 ["div" ,"/",[-5.,5.]],
1747 ["pow" ,"**",[0.01,5.]]]:
1748 for case0 in case_set:
1749 for sh0 in shape_set:
1750 for case1 in case_set:
1751 for sh1 in shape_set:
1752 if not case0=="array" and \
1753 (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
1754 (sh0==() or sh1==() or sh1==sh0) and \
1755 not (case0 in ["float","array"] and case1 in ["float","array"]):
1756 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
1757 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1758 tname="test_%s_overloaded_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
1759 text+=" def %s(self):\n"%tname
1760 a_0=makeArray(sh0,oper[2])
1761 if case0 in ["taggedData", "expandedData"]:
1762 a1_0=makeArray(sh0,oper[2])
1763 else:
1764 a1_0=a_0
1765
1766 a_1=makeArray(sh1,oper[2])
1767 if case1 in ["taggedData", "expandedData"]:
1768 a1_1=makeArray(sh1,oper[2])
1769 else:
1770 a1_1=a_1
1771 r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
1772 r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
1773 text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
1774 text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
1775 text+=" res=arg0%sarg1\n"%oper[1]
1776
1777 case=getResultCaseForBin(case0,case1)
1778 if case=="Symbol":
1779 c0_res,c1_res=case0,case1
1780 subs="{"
1781 if case0=="Symbol":
1782 text+=mkText("array","s0",a_0,a1_0)
1783 subs+="arg0:s0"
1784 c0_res="array"
1785 if case1=="Symbol":
1786 text+=mkText("array","s1",a_1,a1_1)
1787 if not subs.endswith("{"): subs+=","
1788 subs+="arg1:s1"
1789 c1_res="array"
1790 subs+="}"
1791 text+=" sub=res.substitute(%s)\n"%subs
1792 res="sub"
1793 text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
1794 else:
1795 res="res"
1796 text+=mkText(case,"ref",r,r1)
1797 if isinstance(r,float):
1798 text+=mkTypeAndShapeTest(case,(),"res")
1799 else:
1800 text+=mkTypeAndShapeTest(case,r.shape,"res")
1801 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1802
1803 if case0 in [ "constData","taggedData","expandedData"] and case1 == "Symbol":
1804 t_prog_failing+=text
1805 else:
1806 if case0 == "taggedData" or case1 == "taggedData":
1807 t_prog_with_tags+=text
1808 else:
1809 t_prog+=text
1810
1811
1812 print test_header
1813 # print t_prog
1814 # print t_prog_with_tags
1815 print t_prog_failing
1816 print test_tail
1817 1/0
1818 #=======================================================================================================
1819 # basic binary operations (tests only!)
1820 #=======================================================================================================
1821 oper_range=[-5.,5.]
1822 for oper in [["add" ,"+",[-5.,5.]],
1823 ["mult","*",[-5.,5.]],
1824 ["quotient" ,"/",[-5.,5.]],
1825 ["power" ,"**",[0.01,5.]]]:
1826 for case0 in case_set:
1827 for case1 in case_set:
1828 for sh in shape_set:
1829 for sh_p in shape_set:
1830 if len(sh_p)>0:
1831 resource=[-1,1]
1832 else:
1833 resource=[1]
1834 for sh_d in resource:
1835 if sh_d>0:
1836 sh0=sh
1837 sh1=sh+sh_p
1838 else:
1839 sh1=sh
1840 sh0=sh+sh_p
1841
1842 if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
1843 len(sh0)<5 and len(sh1)<5:
1844 use_tagging_for_expanded_data= case0=="taggedData" or case1=="taggedData"
1845 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1846 tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
1847 text+=" def %s(self):\n"%tname
1848 a_0=makeArray(sh0,oper[2])
1849 if case0 in ["taggedData", "expandedData"]:
1850 a1_0=makeArray(sh0,oper[2])
1851 else:
1852 a1_0=a_0
1853
1854 a_1=makeArray(sh1,oper[2])
1855 if case1 in ["taggedData", "expandedData"]:
1856 a1_1=makeArray(sh1,oper[2])
1857 else:
1858 a1_1=a_1
1859 r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
1860 r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
1861 text+=mkText(case0,"arg0",a_0,a1_0,use_tagging_for_expanded_data)
1862 text+=mkText(case1,"arg1",a_1,a1_1,use_tagging_for_expanded_data)
1863 text+=" res=%s(arg0,arg1)\n"%oper[0]
1864
1865 case=getResultCaseForBin(case0,case1)
1866 if case=="Symbol":
1867 c0_res,c1_res=case0,case1
1868 subs="{"
1869 if case0=="Symbol":
1870 text+=mkText("array","s0",a_0,a1_0)
1871 subs+="arg0:s0"
1872 c0_res="array"
1873 if case1=="Symbol":
1874 text+=mkText("array","s1",a_1,a1_1)
1875 if not subs.endswith("{"): subs+=","
1876 subs+="arg1:s1"
1877 c1_res="array"
1878 subs+="}"
1879 text+=" sub=res.substitute(%s)\n"%subs
1880 res="sub"
1881 text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
1882 else:
1883 res="res"
1884 text+=mkText(case,"ref",r,r1)
1885 if isinstance(r,float):
1886 text+=mkTypeAndShapeTest(case,(),"res")
1887 else:
1888 text+=mkTypeAndShapeTest(case,r.shape,"res")
1889 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1890
1891 if case0 == "taggedData" or case1 == "taggedData":
1892 t_prog_with_tags+=text
1893 else:
1894 t_prog+=text
1895 print test_header
1896 # print t_prog
1897 print t_prog_with_tags
1898 print test_tail
1899 1/0
1900
1901 # print t_prog_with_tagsoper_range=[-5.,5.]
1902 for oper in [["add" ,"+",[-5.,5.]],
1903 ["sub" ,"-",[-5.,5.]],
1904 ["mult","*",[-5.,5.]],
1905 ["div" ,"/",[-5.,5.]],
1906 ["pow" ,"**",[0.01,5.]]]:
1907 for case0 in case_set:
1908 for sh0 in shape_set:
1909 for case1 in case_set:
1910 for sh1 in shape_set:
1911 if (not case0=="float" or len(sh0)==0) and (not case1=="float" or len(sh1)==0) and \
1912 (sh0==() or sh1==() or sh1==sh0) and \
1913 not (case0 in ["float","array"] and case1 in ["float","array"]):
1914 text=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
1915 tname="test_%s_%s_rank%s_%s_rank%s"%(oper[0],case0,len(sh0),case1,len(sh1))
1916 text+=" def %s(self):\n"%tname
1917 a_0=makeArray(sh0,oper[2])
1918 if case0 in ["taggedData", "expandedData"]:
1919 a1_0=makeArray(sh0,oper[2])
1920 else:
1921 a1_0=a_0
1922
1923 a_1=makeArray(sh1,oper[2])
1924 if case1 in ["taggedData", "expandedData"]:
1925 a1_1=makeArray(sh1,oper[2])
1926 else:
1927 a1_1=a_1
1928 r1=makeResult2(a1_0,a1_1,"%a1%"+oper[1]+"%a2%")
1929 r=makeResult2(a_0,a_1,"%a1%"+oper[1]+"%a2%")
1930 text+=mkText(case0,"arg0",a_0,a1_0)
1931 text+=mkText(case1,"arg1",a_1,a1_1)
1932 text+=" res=arg0%sarg1\n"%oper[1]
1933
1934 case=getResultCaseForBin(case0,case1)
1935 if case=="Symbol":
1936 c0_res,c1_res=case0,case1
1937 subs="{"
1938 if case0=="Symbol":
1939 text+=mkText("array","s0",a_0,a1_0)
1940 subs+="arg0:s0"
1941 c0_res="array"
1942 if case1=="Symbol":
1943 text+=mkText("array","s1",a_1,a1_1)
1944 if not subs.endswith("{"): subs+=","
1945 subs+="arg1:s1"
1946 c1_res="array"
1947 subs+="}"
1948 text+=" sub=res.substitute(%s)\n"%subs
1949 res="sub"
1950 text+=mkText(getResultCaseForBin(c0_res,c1_res),"ref",r,r1)
1951 else:
1952 res="res"
1953 text+=mkText(case,"ref",r,r1)
1954 if isinstance(r,float):
1955 text+=mkTypeAndShapeTest(case,(),"res")
1956 else:
1957 text+=mkTypeAndShapeTest(case,r.shape,"res")
1958 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
1959
1960 if case0 in [ "constData","taggedData","expandedData"] and case1 == "Symbol":
1961 t_prog_failing+=text
1962 else:
1963 if case0 == "taggedData" or case1 == "taggedData":
1964 t_prog_with_tags+=text
1965 else:
1966 t_prog+=text
1967
1968
1969 # print u_prog
1970 # 1/0
1971 print test_header
1972 print t_prog
1973 # print t_prog_with_tags
1974 # print t_prog_failing
1975 print test_tail
1976 # print t_prog_failing
1977 print test_tail
1978
1979 #=======================================================================================================
1980 # unary operations:
1981 #=======================================================================================================
1982 func= [
1983 OPERATOR(nickname="log10",\
1984 rng=[1.e-3,100.],\
1985 test_expr="math.log10(%a1%)",\
1986 math_expr="math.log10(%a1%)",\
1987 numarray_expr="numarray.log10(%a1%)",\
1988 symbol_expr="log(%a1%)/log(10.)",\
1989 name="base-10 logarithm"),
1990 OPERATOR(nickname="wherePositive",\
1991 rng=[-100.,100.],\
1992 test_expr="wherepos(%a1%)",\
1993 math_expr="if arg>0:\n return 1.\nelse:\n return 0.",
1994 numarray_expr="numarray.greater(arg,numarray.zeros(arg.shape,numarray.Float))",\
1995 name="mask of positive values"),
1996 OPERATOR(nickname="whereNegative",\
1997 rng=[-100.,100.],\
1998 test_expr="wherepos(-%a1%)",\
1999 math_expr="if arg<0:\n return 1.\nelse:\n return 0.",
2000 numarray_expr="numarray.less(arg,numarray.zeros(arg.shape,numarray.Float))",\
2001 name="mask of positive values"),
2002 OPERATOR(nickname="whereNonNegative",\
2003 rng=[-100.,100.],\
2004 test_expr="1-wherepos(-%a1%)", \
2005 math_expr="if arg<0:\n return 0.\nelse:\n return 1.",
2006 numarray_expr="numarray.greater_equal(arg,numarray.zeros(arg.shape,numarray.Float))",\
2007 symbol_expr="1-wherePositive(%a1%)",\
2008 name="mask of non-negative values"),
2009 OPERATOR(nickname="whereNonPositive",\
2010 rng=[-100.,100.],\
2011 test_expr="1-wherepos(%a1%)",\
2012 math_expr="if arg>0:\n return 0.\nelse:\n return 1.",
2013 numarray_expr="numarray.less_equal(arg,numarray.zeros(arg.shape,numarray.Float))",\
2014 symbol_expr="1-whereNegative(%a1%)",\
2015 name="mask of non-positive values"),
2016 OPERATOR(nickname="whereZero",\
2017 rng=[-100.,100.],\
2018 test_expr="1-wherepos(%a1%)-wherepos(-%a1%)",\
2019 math_expr="if abs(%a1%)<=tol:\n return 1.\nelse:\n return 0.",
2020 numarray_expr="numarray.less_equal(abs(%a1%)-tol,numarray.zeros(arg.shape,numarray.Float))",\
2021 name="mask of zero entries"),
2022 OPERATOR(nickname="whereNonZero",\
2023 rng=[-100.,100.],\
2024 test_expr="wherepos(%a1%)+wherepos(-%a1%)",\
2025 math_expr="if abs(%a1%)>tol:\n return 1.\nelse:\n return 0.",\
2026 numarray_expr="numarray.greater(abs(%a1%)-tol,numarray.zeros(arg.shape,numarray.Float))",\
2027 symbol_expr="1-whereZero(arg,tol)",\
2028 name="mask of values different from zero"),
2029 OPERATOR(nickname="sin",\
2030 rng=[-100.,100.],\
2031 test_expr="math.sin(%a1%)",
2032 numarray_expr="numarray.sin(%a1%)",\
2033 diff="cos(%a1%)",\
2034 name="sine"),
2035 OPERATOR(nickname="cos",\
2036 rng=[-100.,100.],\
2037 test_expr="math.cos(%a1%)",
2038 numarray_expr="numarray.cos(%a1%)",\
2039 diff="-sin(%a1%)",
2040 name="cosine"),
2041 OPERATOR(nickname="tan",\
2042 rng=[-100.,100.],\
2043 test_expr="math.tan(%a1%)",
2044 numarray_expr="numarray.tan(%a1%)",\
2045 diff="1./cos(%a1%)**2",
2046 name="tangent"),
2047 OPERATOR(nickname="asin",\
2048 rng=[-0.99,0.99],\
2049 test_expr="math.asin(%a1%)",
2050 numarray_expr="numarray.arcsin(%a1%)",
2051 diff="1./sqrt(1.-%a1%**2)",
2052 name="inverse sine"),
2053 OPERATOR(nickname="acos",\
2054 rng=[-0.99,0.99],\
2055 test_expr="math.acos(%a1%)",
2056 numarray_expr="numarray.arccos(%a1%)",
2057 diff="-1./sqrt(1.-%a1%**2)",
2058 name="inverse cosine"),
2059 OPERATOR(nickname="atan",\
2060 rng=[-100.,100.],\
2061 test_expr="math.atan(%a1%)",
2062 numarray_expr="numarray.arctan(%a1%)",
2063 diff="1./(1+%a1%**2)",
2064 name="inverse tangent"),
2065 OPERATOR(nickname="sinh",\
2066 rng=[-5,5],\
2067 test_expr="math.sinh(%a1%)",
2068 numarray_expr="numarray.sinh(%a1%)",
2069 diff="cosh(%a1%)",
2070 name="hyperbolic sine"),
2071 OPERATOR(nickname="cosh",\
2072 rng=[-5.,5.],
2073 test_expr="math.cosh(%a1%)",
2074 numarray_expr="numarray.cosh(%a1%)",
2075 diff="sinh(%a1%)",
2076 name="hyperbolic cosine"),
2077 OPERATOR(nickname="tanh",\
2078 rng=[-5.,5.],
2079 test_expr="math.tanh(%a1%)",
2080 numarray_expr="numarray.tanh(%a1%)",
2081 diff="1./cosh(%a1%)**2",
2082 name="hyperbolic tangent"),
2083 OPERATOR(nickname="asinh",\
2084 rng=[-100.,100.], \
2085 test_expr="numarray.arcsinh(%a1%)",
2086 math_expr="numarray.arcsinh(%a1%)",
2087 numarray_expr="numarray.arcsinh(%a1%)",
2088 diff="1./sqrt(%a1%**2+1)",
2089 name="inverse hyperbolic sine"),
2090 OPERATOR(nickname="acosh",\
2091 rng=[1.001,100.],\
2092 test_expr="numarray.arccosh(%a1%)",
2093 math_expr="numarray.arccosh(%a1%)",
2094 numarray_expr="numarray.arccosh(%a1%)",
2095 diff="1./sqrt(%a1%**2-1)",
2096 name="inverse hyperolic cosine"),
2097 OPERATOR(nickname="atanh",\
2098 rng=[-0.99,0.99], \
2099 test_expr="numarray.arctanh(%a1%)",
2100 math_expr="numarray.arctanh(%a1%)",
2101 numarray_expr="numarray.arctanh(%a1%)",
2102 diff="1./(1.-%a1%**2)",
2103 name="inverse hyperbolic tangent"),
2104 OPERATOR(nickname="exp",\
2105 rng=[-5.,5.],
2106 test_expr="math.exp(%a1%)",
2107 numarray_expr="numarray.exp(%a1%)",
2108 diff="self",
2109 name="exponential"),
2110 OPERATOR(nickname="sqrt",\
2111 rng=[1.e-3,100.],\
2112 test_expr="math.sqrt(%a1%)",
2113 numarray_expr="numarray.sqrt(%a1%)",
2114 diff="0.5/self",
2115 name="square root"),
2116 OPERATOR(nickname="log", \
2117 rng=[1.e-3,100.],\
2118 test_expr="math.log(%a1%)",
2119 numarray_expr="numarray.log(%a1%)",
2120 diff="1./arg",
2121 name="natural logarithm"),
2122 OPERATOR(nickname="sign",\
2123 rng=[-100.,100.], \
2124 math_expr="if %a1%>0:\n return 1.\nelif %a1%<0:\n return -1.\nelse:\n return 0.",
2125 test_expr="wherepos(%a1%)-wherepos(-%a1%)",
2126 numarray_expr="numarray.sign(%a1%)",
2127 symbol_expr="wherePositive(%a1%)-whereNegative(%a1%)",\
2128 name="sign"),
2129 OPERATOR(nickname="abs",\
2130 rng=[-100.,100.], \
2131 math_expr="if %a1%>0:\n return %a1% \nelif %a1%<0:\n return -(%a1%)\nelse:\n return 0.",
2132 test_expr="wherepos(%a1%)*(%a1%)-wherepos(-%a1%)*(%a1%)",
2133 numarray_expr="abs(%a1%)",
2134 diff="sign(%a1%)",
2135 name="absolute value")
2136
2137 ]
2138 for f in func:
2139 symbol_name=f.nickname[0].upper()+f.nickname[1:]
2140 if f.nickname!="abs":
2141 u_prog+="def %s(arg):\n"%f.nickname
2142 u_prog+=" \"\"\"\n"
2143 u_prog+=" returns %s of argument arg\n\n"%f.name
2144 u_prog+=" @param arg: argument\n"
2145 u_prog+=" @type arg: C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray}.\n"
2146 u_prog+=" @rtype:C{float}, L{escript.Data}, L{Symbol}, L{numarray.NumArray} depending on the type of arg.\n"
2147 u_prog+=" @raises TypeError: if the type of the argument is not expected.\n"
2148 u_prog+=" \"\"\"\n"
2149 u_prog+=" if isinstance(arg,numarray.NumArray):\n"
2150 u_prog+=mkCode(f.numarray_expr,["arg"],2*" ")
2151 u_prog+=" elif isinstance(arg,escript.Data):\n"
2152 u_prog+=mkCode("arg._%s()"%f.nickname,[],2*" ")
2153 u_prog+=" elif isinstance(arg,float):\n"
2154 u_prog+=mkCode(f.math_expr,["arg"],2*" ")
2155 u_prog+=" elif isinstance(arg,int):\n"
2156 u_prog+=mkCode(f.math_expr,["float(arg)"],2*" ")
2157 u_prog+=" elif isinstance(arg,Symbol):\n"
2158 if f.symbol_expr==None:
2159 u_prog+=mkCode("%s_Symbol(arg)"%symbol_name,[],2*" ")
2160 else:
2161 u_prog+=mkCode(f.symbol_expr,["arg"],2*" ")
2162 u_prog+=" else:\n"
2163 u_prog+=" raise TypeError,\"%s: Unknown argument type.\"\n\n"%f.nickname
2164 if f.symbol_expr==None:
2165 u_prog+="class %s_Symbol(DependendSymbol):\n"%symbol_name
2166 u_prog+=" \"\"\"\n"
2167 u_prog+=" L{Symbol} representing the result of the %s function\n"%f.name
2168 u_prog+=" \"\"\"\n"
2169 u_prog+=" def __init__(self,arg):\n"
2170 u_prog+=" \"\"\"\n"
2171 u_prog+=" initialization of %s L{Symbol} with argument arg\n"%f.nickname
2172 u_prog+=" @param arg: argument of function\n"
2173 u_prog+=" @type arg: typically L{Symbol}.\n"
2174 u_prog+=" \"\"\"\n"
2175 u_prog+=" DependendSymbol.__init__(self,args=[arg],shape=arg.getShape(),dim=arg.getDim())\n"
2176 u_prog+="\n"
2177
2178 u_prog+=" def getMyCode(self,argstrs,format=\"escript\"):\n"
2179 u_prog+=" \"\"\"\n"
2180 u_prog+=" returns a program code that can be used to evaluate the symbol.\n\n"
2181
2182 u_prog+=" @param argstrs: gives for each argument a string representing the argument for the evaluation.\n"
2183 u_prog+=" @type argstrs: C{str} or a C{list} of length 1 of C{str}.\n"
2184 u_prog+=" @param format: specifies the format to be used. At the moment only \"escript\" ,\"text\" and \"str\" are supported.\n"
2185 u_prog+=" @type format: C{str}\n"
2186 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"
2187 u_prog+=" @rtype: C{str}\n"
2188 u_prog+=" @raise: NotImplementedError: if the requested format is not available\n"
2189 u_prog+=" \"\"\"\n"
2190 u_prog+=" if isinstance(argstrs,list):\n"
2191 u_prog+=" argstrs=argstrs[0]\n"
2192 u_prog+=" if format==\"escript\" or format==\"str\" or format==\"text\":\n"
2193 u_prog+=" return \"%s(%%s)\"%%argstrs\n"%f.nickname
2194 u_prog+=" else:\n"
2195 u_prog+=" raise NotImplementedError,\"%s_Symbol does not provide program code for format %%s.\"%%format\n"%symbol_name
2196 u_prog+="\n"
2197
2198 u_prog+=" def substitute(self,argvals):\n"
2199 u_prog+=" \"\"\"\n"
2200 u_prog+=" assigns new values to symbols in the definition of the symbol.\n"
2201 u_prog+=" The method replaces the L{Symbol} u by argvals[u] in the expression defining this object.\n"
2202 u_prog+="\n"
2203 u_prog+=" @param argvals: new values assigned to symbols\n"
2204 u_prog+=" @type argvals: C{dict} with keywords of type L{Symbol}.\n"
2205 u_prog+=" @return: result of the substitution process. Operations are executed as much as possible.\n"
2206 u_prog+=" @rtype: L{escript.Symbol}, C{float}, L{escript.Data}, L{numarray.NumArray} depending on the degree of substitution\n"
2207 u_prog+=" @raise TypeError: if a value for a L{Symbol} cannot be substituted.\n"
2208 u_prog+=" \"\"\"\n"
2209 u_prog+=" if argvals.has_key(self):\n"
2210 u_prog+=" arg=argvals[self]\n"
2211 u_prog+=" if self.isAppropriateValue(arg):\n"
2212 u_prog+=" return arg\n"
2213 u_prog+=" else:\n"
2214 u_prog+=" raise TypeError,\"%s: new value is not appropriate.\"%str(self)\n"
2215 u_prog+=" else:\n"
2216 u_prog+=" arg=self.getSubstitutedArguments(argvals)[0]\n"
2217 u_prog+=" return %s(arg)\n\n"%f.nickname
2218 if not f.diff==None:
2219 u_prog+=" def diff(self,arg):\n"
2220 u_prog+=" \"\"\"\n"
2221 u_prog+=" differential of this object\n"
2222 u_prog+="\n"
2223 u_prog+=" @param arg: the derivative is calculated with respect to arg\n"
2224 u_prog+=" @type arg: L{escript.Symbol}\n"
2225 u_prog+=" @return: derivative with respect to C{arg}\n"
2226 u_prog+=" @rtype: typically L{Symbol} but other types such as C{float}, L{escript.Data}, L{numarray.NumArray} are possible.\n"
2227 u_prog+=" \"\"\"\n"
2228 u_prog+=" if arg==self:\n"
2229 u_prog+=" return identity(self.getShape())\n"
2230 u_prog+=" else:\n"
2231 u_prog+=" myarg=self.getArgument()[0]\n"
2232 u_prog+=" val=matchShape(%s,self.getDifferentiatedArguments(arg)[0])\n"%f.diff.replace("%a1%","myarg")
2233 u_prog+=" return val[0]*val[1]\n\n"
2234
2235 for case in case_set:
2236 for sh in shape_set:
2237 if not case=="float" or len(sh)==0:
2238 text=""
2239 text+=" #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
2240 tname="def test_%s_%s_rank%s"%(f.nickname,case,len(sh))
2241 text+=" %s(self):\n"%tname
2242 a=makeArray(sh,f.rng)
2243 a1=makeArray(sh,f.rng)
2244 r1=makeResult(a1,f.test_expr)
2245 r=makeResult(a,f.test_expr)
2246
2247 text+=mkText(case,"arg",a,a1)
2248 text+=" res=%s(arg)\n"%f.nickname
2249 if case=="Symbol":
2250 text+=mkText("array","s",a,a1)
2251 text+=" sub=res.substitute({arg:s})\n"
2252 text+=mkText("array","ref",r,r1)
2253 res="sub"
2254 else:
2255 text+=mkText(case,"ref",r,r1)
2256 res="res"
2257 text+=mkTypeAndShapeTest(case,sh,"res")
2258 text+=" self.failUnless(Lsup(%s-ref)<=self.RES_TOL*Lsup(ref),\"wrong result\")\n"%res
2259 if case == "taggedData":
2260 t_prog_with_tags+=text
2261 else:
2262 t_prog+=text
2263
2264 #=========== END OF GOOD CODE +++++++++++++++++++++++++++
2265
2266 1/0
2267
2268 def X():
2269 if args=="float":
2270 a=makeArray(sh,f[RANGE])
2271 r=makeResult(a,f)
2272 t_prog+=" arg=%s\n"%a[0]
2273 t_prog+=" ref=%s\n"%r[0]
2274 t_prog+=" res=%s(%a1%)\n"%f.nickname
2275 t_prog+=" self.failUnless(isinstance(res,float),\"wrong type of result.\")\n"
2276 t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
2277 elif args == "array":
2278 a=makeArray(sh,f[RANGE])
2279 r=makeResult(a,f)
2280 if len(sh)==0:
2281 t_prog+=" arg=numarray.array(%s)\n"%a[0]
2282 t_prog+=" ref=numarray.array(%s)\n"%r[0]
2283 else:
2284 t_prog+=" arg=numarray.array(%s)\n"%a.tolist()
2285 t_prog+=" ref=numarray.array(%s)\n"%r.tolist()
2286 t_prog+=" res=%s(%a1%)\n"%f.nickname
2287 t_prog+=" self.failUnlessEqual(res.shape,%s,\"wrong shape of result.\")\n"%str(sh)
2288 t_prog+=" self.failUnless(Lsup(res-ref)<=self.tol*Lsup(ref),\"wrong result\")\n"
2289 elif args== "constData":
2290 a=makeArray(sh,f[RANGE])
2291 r=makeResult(a,f)
2292 if len(sh)==0: