/[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 614 - (show annotations)
Wed Mar 22 01:37:07 2006 UTC (13 years, 4 months ago) by elspeth
File MIME type: text/x-python
File size: 8252 byte(s)
Corrected spelling of 'license' in url so that the link actually points to the license.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26