/[escript]/trunk/esys2/escript/test/python/DataTest.py
ViewVC logotype

Annotation of /trunk/esys2/escript/test/python/DataTest.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (hide annotations)
Tue Dec 14 05:39:33 2004 UTC (15 years, 6 months ago) by jgs
File MIME type: text/x-python
File size: 25549 byte(s)
*** empty log message ***

1 jgs 97 import sys
2     import unittest
3     import os
4    
5     esys_root=os.getenv('ESYS_ROOT')
6     sys.path.append(esys_root+'/finley/lib')
7     sys.path.append(esys_root+'/escript/lib')
8     sys.path.append(esys_root+'/escript/py_src')
9    
10     from escript import *
11     import finley
12     import numarray
13     from util import *
14 jgs 82
15     """
16     tests arithmetic operations on Data:
17    
18     +arg1
19     -arg1
20     arg1+arg2
21     arg1-arg2
22     arg1*arg2
23     arg1/arg2
24     arg1**arg2
25     arg1+=arg2
26     arg1-=arg2
27     arg1*=arg2
28     arg1/=arg2
29    
30     various values for arg1 and arg2 are tested on various atoms.
31    
32     moreover various slicing operations are tested.
33    
34     the results are compared to reference result. if the relative difference is
35     bigger than tol an exception is raised.
36    
37     by Lutz Gross, ACcESS, University of Queensland, Australia, 2003.
38    
39     Version $Id$
40    
41     """
42    
43     from numarray import array,Float64,ones,greater
44    
45     Tag1=10
46     Tag2=11
47    
48     tol=1.E-15
49    
50     #
51     # list of arguments: a list item has the form [a0,a1,a2]
52 jgs 97 # what a0 is the default value and a1 is used for tag Tag1
53 jgs 82 # and a2 for tag2. a0,a1,a2 are converted into numarrays.
54     #
55     # binary operations are tested on all pairs from arglist
56     #
57     # each item in the arglist are used to construct the following 5 argument
58     # types arg for arithmetic operations:
59     #
60     # 1) arg is a numarray/list a0
61     # 2) arg is a Data with default value a0
62     # 3) arg is an DataArray with constant value a0
63     # 4) arg is a Data object with constant value a0 and value a1 for tag Tag1
64     # 5) arg is a Data object with constant value a0 and value a1 for tag Tag1
65     # and value a2 for Tag2.
66     #
67     # i.e for a single binary arithmetic operation (len(arglist)*5)**2
68     # test are performed.
69     #
70    
71 jgs 97 arglist = [ \
72     [ [3,4], [-5,6.], [2,3] ], \
73     [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
74     [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
75     [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
76     [ 3.0, 6.0, 3 ] \
77 jgs 82 ]
78    
79 jgs 97 # these are used to test slicing:
80     a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
81     a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
82 jgs 82 a_r4=[ \
83 jgs 97 [ [ [[ 1,2,3],[11,12,13]], [[21,22,23],[31,32,33]], [[41,42,43],[51,52,53]] ], [ [[101,102,103],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[201,202,203],[211,212,213]], [[221,222,223],[231,232,233]], [[241,242,243],[251,252,253]] ] ], \
84     [ [ [[ -1,-2,-3],[-11,-12,-13]], [[-21,-22,-23],[-31,-32,-33]], [[-41,-42,-43],[-51,-52,-53]] ], [ [[-101,-102,-103],[-111,-112,-113]], [[-121,-122,-123],[-131,-132,-133]], [[-141,-142,-143],[-151,-152,-153]] ], [ [[-201,-202,-203],[-211,-212,-213]], [[-221,-222,-223],[-231,-232,-233]], [[-241,-242,-243],[-251,-252,-253]] ] ], \
85     [ [[[ 11,12,13],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[1101,1102,1103],[1111,1112,1113]], [[1121,1122,1123],[1131,1132,1133]], [[1141,1142,1143],[1151,1152,1153]] ], [ [[1201,1202,1203],[1211,1212,1213]], [[1221,1222,1223],[1231,1232,1233]], [[1241,1242,1243],[1251,1252,1253]] ] ] ]
86 jgs 82 a_r4_in=[ \
87 jgs 97 [ [ [[ 1./1,1./2,1./3],[1./11,1./12,1./13]], [[1./21,1./22,1./23],[1./31,1./32,1./33]], [[1./41,1./42,1./43],[1./51,1./52,1./53]] ], [ [[1./101,1./102,1./103],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./201,1./202,1./203],[1./211,1./212,1./213]], [[1./221,1./222,1./223],[1./231,1./232,1./233]], [[1./241,1./242,1./243],[1./251,1./252,1./253]] ] ], \
88     [ [ [[ -1./1,-1./2,-1./3],[-1./11,-1./12,-1./13]], [[-1./21,-1./22,-1./23],[-1./31,-1./32,-1./33]], [[-1./41,-1./42,-1./43],[-1./51,-1./52,-1./53]] ], [ [[-1./101,-1./102,-1./103],[-1./111,-1./112,-1./113]], [[-1./121,-1./122,-1./123],[-1./131,-1./132,-1./133]], [[-1./141,-1./142,-1./143],[1./-151,-1./152,-1./153]] ], [ [[-1./201,-1./202,-1./203],[-1./211,-1./212,-1./213]], [[-1./221,-1./222,-1./223],[-1./231,-1./232,-1./233]], [[-1./241,-1./242,-1./243],[-1./251,-1./252,-1./253]] ] ], \
89     [ [[[ 1./11,1./12,1./13],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./1101,1./1102,1./1103],[1./1111,1./1112,1./1113]], [[1./1121,1./1122,1./1123],[1./1131,1./1132,1./1133]], [[1./1141,1./1142,1./1143],[1./1151,1./1152,1./1153]] ], [ [[1./1201,1./1202,1./1203],[1./1211,1./1212,1./1213]], [[1./1221,1./1222,1./1223],[1./1231,1./1232,1./1233]], [[1./1241,1./1242,1./1243],[1./1251,1./1252,1./1253]] ] ] ]
90 jgs 82
91     def turnToArray(val,tagged):
92     if tagged=="Tagged1":
93     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
94     elif tagged=="Tagged2":
95     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
96     else:
97     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
98     return out
99    
100     def prepareArg(val,ex,wh):
101     if ex=="Array":
102     out=val[0]
103     else:
104     if ex=="Expanded":
105 jgs 97 exx=True
106 jgs 82 else:
107 jgs 97 exx=False
108     out=Data(val[0],what=wh,expand=exx)
109 jgs 82 if ex=="Tagged1":
110 jgs 97 out.setTaggedValue(Tag1,val[1])
111 jgs 82 elif ex=="Tagged2":
112 jgs 97 out.setTaggedValue(Tag1,val[1])
113     out.setTaggedValue(Tag2,val[2])
114 jgs 82 return out
115    
116     def checkResult(text,res,val0,val1,val2,wh):
117 jgs 97 ref=Data(val0,what=wh,expand=False)
118     ref.setTaggedValue(Tag1,val1)
119     ref.setTaggedValue(Tag2,val2)
120     norm=Lsup(ref)+tol
121     error=Lsup(ref-res)/norm
122     print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
123 jgs 82 if error>tol:
124 jgs 97 #raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
125     print "**** %s: error is too large"%(text)
126 jgs 82
127     def getRank(arg):
128     if isinstance(arg,Data):
129     return arg.getRank()
130     else:
131     g=array(arg)
132     if g.rank==0:
133     return 1
134     else:
135     return g.rank
136    
137     def isScalar(arg):
138     if isinstance(arg,Data):
139     if arg.getRank()==1 and arg.getShape()[0]==1:
140     return not None
141     else:
142     return None
143     else:
144     g=array(arg)
145     if g.rank==0:
146     return not None
147     else:
148     if g.rank==1 and g.shape[0]==1:
149     return not None
150     else:
151     return None
152    
153 jgs 97 msh=finley.Rectangle(20,6,1)
154     for wh in [ContinuousFunction(msh),Function(msh)]:
155    
156 jgs 82 #
157     # ==============================================================
158     #
159 jgs 97 # test slicing:
160 jgs 82 #
161 jgs 97
162     for ex1 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
163    
164     print "Slice getting: ", ex1, ":"
165    
166     # slice getting
167    
168     # rank 1
169 jgs 82 arg=prepareArg(a_r1,ex1,wh)
170     arrays=turnToArray(a_r1,ex1)
171     if isinstance(arg,Data):
172 jgs 97 checkResult("slicing: rank=1 [:] "+ex1,arg[:], \
173 jgs 82 arrays[0][:], \
174     arrays[1][:], \
175     arrays[2][:], \
176     wh)
177 jgs 97 checkResult("slicing: rank=1 [1] "+ex1,arg[1], \
178 jgs 82 arrays[0][1], \
179     arrays[1][1], \
180     arrays[2][1], \
181     wh)
182 jgs 97 checkResult("slicing: rank=1 [1:3] "+ex1,arg[1:3], \
183 jgs 82 arrays[0][1:3], \
184     arrays[1][1:3], \
185     arrays[2][1:3], \
186     wh)
187 jgs 97
188 jgs 82 # rank 4
189     arg=prepareArg(a_r4,ex1,wh)
190     arrays=turnToArray(a_r4,ex1)
191     if isinstance(arg,Data):
192 jgs 97 checkResult("slicing: rank=4 [:] "+ex1,arg[:], \
193 jgs 82 arrays[0][:], \
194     arrays[1][:], \
195     arrays[2][:], \
196     wh)
197 jgs 97 checkResult("slicing: rank=4 [1] "+ex1,arg[1], \
198 jgs 82 arrays[0][1], \
199     arrays[1][1], \
200     arrays[2][1], \
201     wh)
202 jgs 97 checkResult("slicing: rank=4 [1:3] "+ex1,arg[1:3], \
203 jgs 82 arrays[0][1:3], \
204     arrays[1][1:3], \
205     arrays[2][1:3], \
206     wh)
207 jgs 97 checkResult("slicing: rank=4 [:,:] "+ex1,arg[:,:], \
208 jgs 82 arrays[0][:,:], \
209     arrays[1][:,:], \
210     arrays[2][:,:], \
211     wh)
212 jgs 97 checkResult("slicing: rank=4 [:,1] "+ex1,arg[:,1], \
213 jgs 82 arrays[0][:,1], \
214     arrays[1][:,1], \
215     arrays[2][:,1], \
216     wh)
217 jgs 97 checkResult("slicing: rank=4 [:,1:3] "+ex1,arg[:,1:3], \
218 jgs 82 arrays[0][:,1:3], \
219     arrays[1][:,1:3], \
220     arrays[2][:,1:3], \
221     wh)
222 jgs 97 checkResult("slicing: rank=4 [1:2,1:3] "+ex1,arg[1:2,1:3], \
223 jgs 82 arrays[0][1:2,1:3], \
224     arrays[1][1:2,1:3], \
225     arrays[2][1:2,1:3], \
226     wh)
227 jgs 97 checkResult("slicing: rank=4 [1:2,1:3,1,1] "+ex1,arg[1:2,1:3,1,1], \
228 jgs 82 arrays[0][1:2,1:3,1,1], \
229     arrays[1][1:2,1:3,1,1], \
230     arrays[2][1:2,1:3,1,1], \
231     wh)
232 jgs 97 checkResult("slicing: rank=4 [1:2,1:3,:,1] "+ex1,arg[1:2,1:3,:,1], \
233 jgs 82 arrays[0][1:2,1:3,:,1], \
234     arrays[1][1:2,1:3,:,1], \
235     arrays[2][1:2,1:3,:,1], \
236     wh)
237 jgs 97 checkResult("slicing: rank=4 [1:2,1:3,0,:] "+ex1,arg[1:2,1:3,0,:], \
238 jgs 82 arrays[0][1:2,1:3,0,:], \
239     arrays[1][1:2,1:3,0,:], \
240     arrays[2][1:2,1:3,0,:], \
241     wh)
242 jgs 97 checkResult("slicing: rank=4 [1:2,1:3,0,1] "+ex1,arg[1:2,1:3,0,1], \
243 jgs 82 arrays[0][1:2,1:3,0,1], \
244     arrays[1][1:2,1:3,0,1], \
245     arrays[2][1:2,1:3,0,1], \
246     wh)
247 jgs 97 checkResult("slicing: rank=4 [1,1,0,1] "+ex1,arg[1,1,0,1], \
248 jgs 82 arrays[0][1,1,0,1], \
249     arrays[1][1,1,0,1], \
250     arrays[2][1,1,0,1], \
251     wh)
252 jgs 97
253     # slice setting
254    
255     #for ex2 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
256     for ex2 in ["Expanded"]:
257    
258     print "Slice setting: ", ex1, ",", ex2, ":"
259    
260     # rank 1
261    
262 jgs 82 arrays_in=turnToArray(a_r1_in,ex2)
263    
264     arg2=prepareArg(a_r1,ex1,wh)
265     arrays2=turnToArray(a_r1,ex1)
266     a_in=[arrays_in[0][:],arrays_in[1][:],arrays_in[2][:]]
267     arg2[:]=prepareArg(a_in,ex2,wh)
268     arrays2[0][:]=a_in[0]
269     arrays2[1][:]=a_in[1]
270     arrays2[2][:]=a_in[2]
271 jgs 97 checkResult("slicing, set: rank=1 [:] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
272 jgs 82
273     arg2=prepareArg(a_r1,ex1,wh)
274     arrays2=turnToArray(a_r1,ex1)
275     a_in=[arrays_in[0][1],arrays_in[1][1],arrays_in[2][1]]
276     arg2[1]=prepareArg(a_in,ex2,wh)
277     arrays2[0][1]=a_in[0]
278     arrays2[1][1]=a_in[1]
279     arrays2[2][1]=a_in[2]
280 jgs 97 checkResult("slicing, set: rank=1 [1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
281 jgs 82
282     arg2=prepareArg(a_r1,ex1,wh)
283     arrays2=turnToArray(a_r1,ex1)
284     a_in=[arrays_in[0][1:3],arrays_in[1][1:3],arrays_in[2][1:3]]
285     arg2[1:3]=prepareArg(a_in,ex2,wh)
286     arrays2[0][1:3]=a_in[0]
287     arrays2[1][1:3]=a_in[1]
288     arrays2[2][1:3]=a_in[2]
289 jgs 97 checkResult("slicing, set: rank=1 [1:3] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
290 jgs 82
291 jgs 97 # rank 4
292 jgs 82
293     arrays_in=turnToArray(a_r4_in,ex2)
294    
295     arg2=prepareArg(a_r4,ex1,wh)
296     arrays2=turnToArray(a_r4,ex1)
297     a_in=[arrays_in[0][:],arrays_in[1][:],arrays_in[2][:]]
298     arg2[:]=prepareArg(a_in,ex2,wh)
299     arrays2[0][:]=a_in[0]
300     arrays2[1][:]=a_in[1]
301     arrays2[2][:]=a_in[2]
302 jgs 97 checkResult("slicing, set: rank=4 [:] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
303 jgs 82
304     arg2=prepareArg(a_r4,ex1,wh)
305     arrays2=turnToArray(a_r4,ex1)
306     a_in=[arrays_in[0][1],arrays_in[1][1],arrays_in[2][1]]
307     arg2[1]=prepareArg(a_in,ex2,wh)
308     arrays2[0][1]=a_in[0]
309     arrays2[1][1]=a_in[1]
310     arrays2[2][1]=a_in[2]
311 jgs 97 checkResult("slicing, set: rank=4 [1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
312 jgs 82
313     arg2=prepareArg(a_r4,ex1,wh)
314     arrays2=turnToArray(a_r4,ex1)
315     a_in=[arrays_in[0][1:3],arrays_in[1][1:3],arrays_in[2][1:3]]
316     arg2[1:3]=prepareArg(a_in,ex2,wh)
317     arrays2[0][1:3]=a_in[0]
318     arrays2[1][1:3]=a_in[1]
319     arrays2[2][1:3]=a_in[2]
320 jgs 97 checkResult("slicing, set: rank=4 [1:3] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
321 jgs 82
322     arg2=prepareArg(a_r4,ex1,wh)
323     arrays2=turnToArray(a_r4,ex1)
324     a_in=[arrays_in[0][:,:],arrays_in[1][:,:],arrays_in[2][:,:]]
325     arg2[:,:]=prepareArg(a_in,ex2,wh)
326     arrays2[0][:,:]=a_in[0]
327     arrays2[1][:,:]=a_in[1]
328     arrays2[2][:,:]=a_in[2]
329 jgs 97 checkResult("slicing, set: rank=4 [:,:] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
330 jgs 82
331     arg2=prepareArg(a_r4,ex1,wh)
332     arrays2=turnToArray(a_r4,ex1)
333     a_in=[arrays_in[0][:,1],arrays_in[1][:,1],arrays_in[2][:,1]]
334     arg2[:,1]=prepareArg(a_in,ex2,wh)
335     arrays2[0][:,1]=a_in[0]
336     arrays2[1][:,1]=a_in[1]
337     arrays2[2][:,1]=a_in[2]
338 jgs 97 checkResult("slicing, set: rank=4 [:,1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
339 jgs 82
340     arg2=prepareArg(a_r4,ex1,wh)
341     arrays2=turnToArray(a_r4,ex1)
342     a_in=[arrays_in[0][:,1:3],arrays_in[1][:,1:3],arrays_in[2][:,1:3]]
343     arg2[:,1:3]=prepareArg(a_in,ex2,wh)
344     arrays2[0][:,1:3]=a_in[0]
345     arrays2[1][:,1:3]=a_in[1]
346     arrays2[2][:,1:3]=a_in[2]
347 jgs 97 checkResult("slicing, set: rank=4 [:,1:3] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
348 jgs 82
349     arg2=prepareArg(a_r4,ex1,wh)
350     arrays2=turnToArray(a_r4,ex1)
351     a_in=[arrays_in[0][1:2,1:3],arrays_in[1][1:2,1:3],arrays_in[2][1:2,1:3]]
352     arg2[1:2,1:3]=prepareArg(a_in,ex2,wh)
353     arrays2[0][1:2,1:3]=a_in[0]
354     arrays2[1][1:2,1:3]=a_in[1]
355     arrays2[2][1:2,1:3]=a_in[2]
356 jgs 97 checkResult("slicing, set: rank=4 [1:2,1:3] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
357 jgs 82
358     arg2=prepareArg(a_r4,ex1,wh)
359     arrays2=turnToArray(a_r4,ex1)
360     a_in=[arrays_in[0][1:2,1:3,1,1],arrays_in[1][1:2,1:3,1,1],arrays_in[2][1:2,1:3,1,1]]
361     arg2[1:2,1:3,1,1]=prepareArg(a_in,ex2,wh)
362     arrays2[0][1:2,1:3,1,1]=a_in[0]
363     arrays2[1][1:2,1:3,1,1]=a_in[1]
364     arrays2[2][1:2,1:3,1,1]=a_in[2]
365 jgs 97 checkResult("slicing, set: rank=4 [1:2,1:3,1,1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
366 jgs 82
367     arg2=prepareArg(a_r4,ex1,wh)
368     arrays2=turnToArray(a_r4,ex1)
369     a_in=[arrays_in[0][1:2,1:3,:,1],arrays_in[1][1:2,1:3,:,1],arrays_in[2][1:2,1:3,:,1]]
370     arg2[1:2,1:3,:,1]=prepareArg(a_in,ex2,wh)
371     arrays2[0][1:2,1:3,:,1]=a_in[0]
372     arrays2[1][1:2,1:3,:,1]=a_in[1]
373     arrays2[2][1:2,1:3,:,1]=a_in[2]
374 jgs 97 checkResult("slicing, set: rank=4 [1:2,1:3,:,1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
375 jgs 82
376     arg2=prepareArg(a_r4,ex1,wh)
377     arrays2=turnToArray(a_r4,ex1)
378     a_in=[arrays_in[0][1:2,1:3,0,:],arrays_in[1][1:2,1:3,0,:],arrays_in[2][1:2,1:3,0,:]]
379     arg2[1:2,1:3,0,:]=prepareArg(a_in,ex2,wh)
380     arrays2[0][1:2,1:3,0,:]=a_in[0]
381     arrays2[1][1:2,1:3,0,:]=a_in[1]
382     arrays2[2][1:2,1:3,0,:]=a_in[2]
383 jgs 97 checkResult("slicing, set: rank=4 [1:2,1:3,0,:] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
384 jgs 82
385     arg2=prepareArg(a_r4,ex1,wh)
386     arrays2=turnToArray(a_r4,ex1)
387     a_in=[arrays_in[0][1:2,1:3,0,1],arrays_in[1][1:2,1:3,0,1],arrays_in[2][1:2,1:3,0,1]]
388     arg2[1:2,1:3,0,1]=prepareArg(a_in,ex2,wh)
389     arrays2[0][1:2,1:3,0,1]=a_in[0]
390     arrays2[1][1:2,1:3,0,1]=a_in[1]
391     arrays2[2][1:2,1:3,0,1]=a_in[2]
392 jgs 97 checkResult("slicing, set: rank=4 [1:2,1:3,0,1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
393 jgs 82
394     arg2=prepareArg(a_r4,ex1,wh)
395     arrays2=turnToArray(a_r4,ex1)
396     a_in=[arrays_in[0][1,1,0,1],arrays_in[1][1,1,0,1],arrays_in[2][1,1,0,1]]
397     arg2[1,1,0,1]=prepareArg(a_in,ex2,wh)
398     arrays2[0][1,1,0,1]=a_in[0]
399     arrays2[1][1,1,0,1]=a_in[1]
400     arrays2[2][1,1,0,1]=a_in[2]
401 jgs 97 checkResult("slicing, set: rank=4 [1,1,0,1] "+ex1+","+ex2,arg2,arrays2[0],arrays2[1],arrays2[2],wh)
402    
403 jgs 82 #
404     # ==============================================================
405     #
406 jgs 97 # test operators:
407 jgs 82 #
408    
409 jgs 97 #for ex1 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
410     for ex1 in ["Array","Constant","Expanded"]:
411 jgs 82
412 jgs 97 for a1 in arglist:
413 jgs 82
414     arg1=prepareArg(a1,ex1,wh)
415     arrays1=turnToArray(a1,ex1)
416     if isScalar(arg1):
417     t1="(scalar)"
418     else:
419     t1=""
420 jgs 97
421 jgs 82 #
422 jgs 97 # unary operations:
423 jgs 82 #
424 jgs 97
425     print "Unary ops: ", ex1, ":"
426    
427 jgs 82 if isinstance(arg1,Data):
428 jgs 97
429 jgs 82 # pos:
430 jgs 97 #ref=checkResult("+"+ex1,+arg1, \
431     # +arrays1[0], \
432     # +arrays1[1], \
433     # +arrays1[2], \
434     # wh)
435 jgs 82 # neg:
436 jgs 97 #ref=checkResult("-"+ex1,-arg1, \
437     # -arrays1[0], \
438     # -arrays1[1], \
439     # -arrays1[2], \
440     # wh)
441 jgs 82 # pos:
442     ref=checkResult("where positive("+ex1+")",(arg1-3).wherePositive(), \
443     numarray.greater(arrays1[0],3.), \
444     numarray.greater(arrays1[1],3.), \
445     numarray.greater(arrays1[2],3.), \
446     wh)
447     # negative:
448     ref=checkResult("where negative("+ex1+")",(arg1-3).whereNegative(), \
449     numarray.greater(3.,arrays1[0]), \
450     numarray.greater(3.,arrays1[1]), \
451     numarray.greater(3.,arrays1[2]), \
452     wh)
453 jgs 97 # non-negative:
454     ref=checkResult("where nonnegative("+ex1+")",(arg1-3).whereNonNegative(), \
455     numarray.greater_equal(arrays1[0],3.), \
456     numarray.greater_equal(arrays1[1],3.), \
457     numarray.greater_equal(arrays1[2],3.), \
458     wh)
459     # non-positive:
460     ref=checkResult("where nonpositive("+ex1+")",(arg1-3).whereNonPositive(), \
461     numarray.greater_equal(3.,arrays1[0]), \
462     numarray.greater_equal(3.,arrays1[1]), \
463     numarray.greater_equal(3.,arrays1[2]), \
464     wh)
465 jgs 82 # zero:
466     ref=checkResult("where zero("+ex1+")",(arg1-3).whereZero(), \
467     numarray.less_equal(numarray.abs(arrays1[0]-3.),EPSILON), \
468     numarray.less_equal(numarray.abs(arrays1[1]-3.),EPSILON), \
469     numarray.less_equal(numarray.abs(arrays1[2]-3.),EPSILON), \
470     wh)
471     # non-zero:
472 jgs 97 ref=checkResult("where nonzero("+ex1+")",(arg1-3).whereNonZero(), \
473 jgs 82 numarray.greater(numarray.abs(arrays1[0]-3.),EPSILON), \
474     numarray.greater(numarray.abs(arrays1[1]-3.),EPSILON), \
475     numarray.greater(numarray.abs(arrays1[2]-3.),EPSILON), \
476     wh)
477    
478     #
479     # binary operations
480     #
481 jgs 97
482     for ex2 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
483    
484     print "Binary ops: ", ex1, ",", ex2, ":"
485    
486 jgs 82 for a2 in arglist:
487 jgs 97
488 jgs 82 arg2=prepareArg(a2,ex2,wh)
489     arrays2=turnToArray(a2,ex2)
490     if isScalar(arg2):
491     t2="(scalar)"
492     else:
493     t2=""
494 jgs 97
495 jgs 82 if isinstance(arg1,Data) or isinstance(arg2,Data):
496 jgs 97
497 jgs 82 # the shape must match or at least one argument is sclar:
498 jgs 97 if (getRank(arg1)==getRank(arg2)) or isScalar(arg1) or isScalar(arg2):
499    
500 jgs 82 # sum
501     checkResult(ex1+t1+"+"+ex2+t2,arg1+arg2, \
502     arrays1[0]+arrays2[0], \
503     arrays1[1]+arrays2[1], \
504     arrays1[2]+arrays2[2], \
505     wh)
506     # sub
507     checkResult(ex1+t1+"-"+ex2+t2,arg1-arg2, \
508 jgs 97 arrays1[0]-arrays2[0], \
509     arrays1[1]-arrays2[1], \
510     arrays1[2]-arrays2[2], \
511     wh)
512 jgs 82 # mul
513     checkResult(ex1+t1+"*"+ex2+t2,arg1*arg2, \
514     arrays1[0]*arrays2[0], \
515     arrays1[1]*arrays2[1], \
516     arrays1[2]*arrays2[2], \
517     wh)
518     # div
519     checkResult(ex1+t1+"/"+ex2+t2,arg1/arg2, \
520     arrays1[0]/arrays2[0], \
521     arrays1[1]/arrays2[1], \
522     arrays1[2]/arrays2[2], \
523 jgs 97 wh)
524 jgs 82 # pow
525     if isinstance(arg1,Data):
526 jgs 97 a=arg1
527 jgs 82 else:
528 jgs 97 a=Data(value=arg1,what=arg2.getFunctionSpace())
529 jgs 82 checkResult(ex1+t1+"^"+ex2+t2,a**arg2, \
530 jgs 97 arrays1[0]**arrays2[0], \
531     arrays1[1]**arrays2[1], \
532     arrays1[2]**arrays2[2], \
533     wh)
534    
535 jgs 82 # inplace for arg2 which must be of class Data
536     # if arg1 is expanded arg2 must be expanded
537 jgs 97 if isinstance(arg2,Data):
538    
539 jgs 82 # if arg2 is scalar arg1 must be scalar:
540 jgs 97 #if not (isScalar(arg2) and not isScalar(arg1)):
541     if not(isScalar(arg2)) and not(isScalar(arg1)):
542    
543 jgs 82 # inplace add:
544     arrays2[0]+=arrays1[0]
545     arrays2[1]+=arrays1[1]
546     arrays2[2]+=arrays1[2]
547     arg2+=arg1
548     checkResult(ex2+t2+"+="+ex1+t1,arg2, \
549     arrays2[0], \
550     arrays2[1], \
551     arrays2[2], \
552     wh)
553     # inplace sub:
554     arrays2[0]-=arrays1[0]
555     arrays2[1]-=arrays1[1]
556     arrays2[2]-=arrays1[2]
557     arg2-=arg1
558     checkResult(ex2+t2+"-="+ex1+t1,arg2, \
559     arrays2[0], \
560     arrays2[1], \
561     arrays2[2], \
562     wh)
563     # inplace mul:
564     arrays2[0]*=arrays1[0]
565     arrays2[1]*=arrays1[1]
566     arrays2[2]*=arrays1[2]
567     arg2*=arg1
568     checkResult(ex2+t2+"*="+ex1+t1,arg2, \
569     arrays2[0], \
570     arrays2[1], \
571     arrays2[2], \
572     wh)
573     # inplace div:
574     arrays2[0]/=arrays1[0]
575     arrays2[1]/=arrays1[1]
576     arrays2[2]/=arrays1[2]
577     arg2/=arg1
578     checkResult(ex2+t2+"/="+ex1+t1,arg2, \
579     arrays2[0], \
580     arrays2[1], \
581     arrays2[2], \
582     wh)
583    
584 jgs 97 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26