/[escript]/trunk/escript/test/python/BinaryOps.py
ViewVC logotype

Annotation of /trunk/escript/test/python/BinaryOps.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 556 - (hide annotations)
Wed Feb 22 04:45:15 2006 UTC (14 years, 7 months ago) by jgs
File MIME type: text/x-python
File size: 7894 byte(s)
reinstate testing using DataTagged objects

1 jgs 545 """
2    
3     Test binary operations.
4    
5     Version $Id$
6    
7     """
8    
9 jgs 102 import sys
10     import unittest
11     import os
12    
13 jgs 149 from esys.escript import *
14 jgs 153 from esys import bruce
15 jgs 102
16     import numarray
17    
18     from numarray import array,Float64,ones,greater
19    
20     Tag1=10
21 jgs 545 Tag2=20
22 jgs 102
23     tol=1.E-15
24    
25     #
26     # list of arguments: a list item has the form [a0,a1,a2]
27     # what a0 is the default value and a1 is used for tag Tag1
28     # and a2 for tag2. a0,a1,a2 are converted into numarrays.
29 jgs 556 #
30 jgs 102 # binary operations are tested on all pairs from arglist
31     #
32     # each item in the arglist are used to construct the following 5 argument
33     # types arg for arithmetic operations:
34     #
35     # 1) arg is a numarray/list a0
36     # 2) arg is a Data with default value a0
37     # 3) arg is an DataArray with constant value a0
38     # 4) arg is a Data object with constant value a0 and value a1 for tag Tag1
39     # 5) arg is a Data object with constant value a0 and value a1 for tag Tag1
40     # and value a2 for Tag2.
41     #
42     # i.e for a single binary arithmetic operation (len(arglist)*5)**2
43     # test are performed.
44 jgs 556 #
45 jgs 102
46     arglist = [ \
47     [ [3,4], [-5,6.], [2,3] ], \
48     [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
49     [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
50     [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
51     [ 3.0, 6.0, 3 ] \
52     ]
53    
54     def prepareArg(val,ex,wh):
55 jgs 545 if ex=="Array":
56 jgs 102 out=val[0]
57 jgs 545 else:
58 jgs 102 if ex=="Expanded":
59 jgs 545 exx=True
60 jgs 102 else:
61 jgs 545 exx=False
62 jgs 102 out=Data(val[0],what=wh,expand=exx)
63     if ex=="Tagged1":
64 jgs 545 out.setTaggedValue(Tag1,val[1])
65 jgs 102 elif ex=="Tagged2":
66 jgs 545 out.setTaggedValue(Tag1,val[1])
67     out.setTaggedValue(Tag2,val[2])
68     return out
69 jgs 102
70 jgs 545 def turnToArray(val,tagged):
71     if tagged=="Tagged1":
72     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
73     elif tagged=="Tagged2":
74     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
75     else:
76     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
77     return out
78    
79 jgs 102 def checkResult(text,res,val0,val1,val2,wh):
80     ref=Data(val0,what=wh,expand=False)
81     ref.setTaggedValue(Tag1,val1)
82     ref.setTaggedValue(Tag2,val2)
83     norm=Lsup(ref)+tol
84     error=Lsup(ref-res)/norm
85     print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
86     if error>tol:
87 jgs 545 print "**** %s : error is too large"%(text)
88     raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
89     sys.exit(1)
90 jgs 102
91     def getRank(arg):
92     if isinstance(arg,Data):
93     return arg.getRank()
94     else:
95     g=array(arg)
96     if g.rank==0:
97     return 1
98     else:
99     return g.rank
100    
101     def isScalar(arg):
102     if isinstance(arg,Data):
103     if arg.getRank()==1 and arg.getShape()[0]==1:
104     return not None
105     else:
106     return None
107     else:
108     g=array(arg)
109     if g.rank==0:
110     return not None
111     else:
112     if g.rank==1 and g.shape[0]==1:
113     return not None
114     else:
115     return None
116    
117     #
118     # ==============================================================
119     #
120     # test binary operators:
121     #
122    
123 jgs 153 msh=bruce.Rectangle(20,6)
124 jgs 102 for wh in [ContinuousFunction(msh),Function(msh)]:
125    
126     print wh
127    
128 jgs 556 for ex1 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
129 jgs 102
130 jgs 556 for ex2 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
131 jgs 102
132     if ex1=="Array" and ex2=="Array":
133     continue
134    
135     print "Binary ops: ", ex1, ",", ex2, ":"
136    
137     for a1 in arglist:
138    
139     arg1=prepareArg(a1,ex1,wh)
140     arrays1=turnToArray(a1,ex1)
141     if isScalar(arg1):
142     t1="(scalar)"
143     else:
144     t1=""
145    
146     for a2 in arglist:
147    
148     arg2=prepareArg(a2,ex2,wh)
149     arrays2=turnToArray(a2,ex2)
150     if isScalar(arg2):
151     t2="(scalar)"
152     else:
153     t2=""
154    
155 jgs 556 # the shape must match or at least one argument is scalar:
156 jgs 102 if (getRank(arg1)==getRank(arg2)) or isScalar(arg1) or isScalar(arg2):
157    
158     # sum
159     checkResult(ex1+t1+"+"+ex2+t2, \
160     arg1+arg2, \
161     arrays1[0]+arrays2[0], \
162     arrays1[1]+arrays2[1], \
163     arrays1[2]+arrays2[2], \
164     wh)
165    
166     # sub
167     checkResult(ex1+t1+"-"+ex2+t2, \
168     arg1-arg2, \
169     arrays1[0]-arrays2[0], \
170     arrays1[1]-arrays2[1], \
171     arrays1[2]-arrays2[2], \
172     wh)
173    
174     # mul
175     checkResult(ex1+t1+"*"+ex2+t2, \
176     arg1*arg2, \
177     arrays1[0]*arrays2[0], \
178     arrays1[1]*arrays2[1], \
179     arrays1[2]*arrays2[2], \
180     wh)
181    
182     # div
183     checkResult(ex1+t1+"/"+ex2+t2, \
184     arg1/arg2, \
185     arrays1[0]/arrays2[0], \
186     arrays1[1]/arrays2[1], \
187     arrays1[2]/arrays2[2], \
188     wh)
189    
190     # pow
191     if isinstance(arg1,Data):
192     a=arg1
193     else:
194     a=Data(value=arg1,what=arg2.getFunctionSpace())
195     checkResult(ex1+t1+"^"+ex2+t2, \
196     a**arg2, \
197     arrays1[0]**arrays2[0], \
198     arrays1[1]**arrays2[1], \
199     arrays1[2]**arrays2[2], \
200     wh)
201    
202     # test inplace operations
203    
204     # arg2 must be class Data
205     if isinstance(arg2,Data):
206    
207     # if arg1 is expanded arg2 must be expanded
208     if not (not ex2=="Expanded" and ex1=="Expanded") :
209    
210     # if arg2 is scalar arg1 must be scalar:
211     if not (isScalar(arg2) and not isScalar(arg1)):
212    
213     # inplace add:
214     arrays2[0]+=arrays1[0]
215     arrays2[1]+=arrays1[1]
216     arrays2[2]+=arrays1[2]
217     arg2+=arg1
218     checkResult(ex2+t2+"+="+ex1+t1, \
219     arg2, \
220     arrays2[0], \
221     arrays2[1], \
222     arrays2[2], \
223     wh)
224    
225     # inplace sub:
226     arrays2[0]-=arrays1[0]
227     arrays2[1]-=arrays1[1]
228     arrays2[2]-=arrays1[2]
229     arg2-=arg1
230     checkResult(ex2+t2+"-="+ex1+t1, \
231     arg2, \
232     arrays2[0], \
233     arrays2[1], \
234     arrays2[2], \
235     wh)
236    
237     # inplace mul:
238     arrays2[0]*=arrays1[0]
239     arrays2[1]*=arrays1[1]
240     arrays2[2]*=arrays1[2]
241     arg2*=arg1
242     checkResult(ex2+t2+"*="+ex1+t1, \
243     arg2, \
244     arrays2[0], \
245     arrays2[1], \
246     arrays2[2], \
247     wh)
248    
249     # inplace div:
250     arrays2[0]/=arrays1[0]
251     arrays2[1]/=arrays1[1]
252     arrays2[2]/=arrays1[2]
253     arg2/=arg1
254     checkResult(ex2+t2+"/="+ex1+t1, \
255     arg2, \
256     arrays2[0], \
257     arrays2[1], \
258     arrays2[2], \
259     wh)
260    
261 jgs 147 sys.exit(0)
262 jgs 102 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26