/[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 155 - (show annotations)
Wed Nov 9 02:02:19 2005 UTC (13 years, 11 months ago) by jgs
File MIME type: text/x-python
File size: 10538 byte(s)
move all directories from trunk/esys2 into trunk and remove esys2

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26