/[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 100 - (hide annotations)
Wed Dec 15 03:48:48 2004 UTC (16 years, 9 months ago) by jgs
File MIME type: text/x-python
File size: 25218 byte(s)
*** empty log message ***

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26