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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 564 - (show annotations)
Thu Feb 23 05:22:31 2006 UTC (14 years ago) by jgs
File MIME type: text/x-python
File size: 7964 byte(s)
extend testing to include DataTagged objects with no
tags, only default values

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26