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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 102 - (hide annotations)
Wed Dec 15 07:08:39 2004 UTC (16 years, 10 months ago) by jgs
File MIME type: text/x-python
File size: 10512 byte(s)
*** empty log message ***

1 jgs 102 import sys
2     import unittest
3     import os
4    
5     from esys.escript import *
6     from esys import finley
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     #raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
100     print "**** %s : error is too large"%(text)
101    
102     def getRank(arg):
103     if isinstance(arg,Data):
104     return arg.getRank()
105     else:
106     g=array(arg)
107     if g.rank==0:
108     return 1
109     else:
110     return g.rank
111    
112     def isScalar(arg):
113     if isinstance(arg,Data):
114     if arg.getRank()==1 and arg.getShape()[0]==1:
115     return not None
116     else:
117     return None
118     else:
119     g=array(arg)
120     if g.rank==0:
121     return not None
122     else:
123     if g.rank==1 and g.shape[0]==1:
124     return not None
125     else:
126     return None
127    
128     #
129     # ==============================================================
130     #
131     # test binary operators:
132     #
133    
134     msh=finley.Rectangle(20,6,1)
135     for wh in [ContinuousFunction(msh),Function(msh)]:
136    
137     print wh
138    
139     #for ex1 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
140     for ex1 in ["Array","Constant","Expanded"]:
141    
142     #for ex2 in ["Array","Constant","Expanded","Tagged1","Tagged2"]:
143     for ex2 in ["Array","Constant","Expanded"]:
144    
145     if ex1=="Array" and ex2=="Array":
146     continue
147    
148     print "Binary ops: ", ex1, ",", ex2, ":"
149    
150     for a1 in arglist:
151    
152     arg1=prepareArg(a1,ex1,wh)
153     arrays1=turnToArray(a1,ex1)
154     if isScalar(arg1):
155     t1="(scalar)"
156     else:
157     t1=""
158    
159     for a2 in arglist:
160    
161     arg2=prepareArg(a2,ex2,wh)
162     arrays2=turnToArray(a2,ex2)
163     if isScalar(arg2):
164     t2="(scalar)"
165     else:
166     t2=""
167    
168     # the shape must match or at least one argument is sclar:
169     if (getRank(arg1)==getRank(arg2)) or isScalar(arg1) or isScalar(arg2):
170    
171     # sum
172     checkResult(ex1+t1+"+"+ex2+t2, \
173     arg1+arg2, \
174     arrays1[0]+arrays2[0], \
175     arrays1[1]+arrays2[1], \
176     arrays1[2]+arrays2[2], \
177     wh)
178    
179     # sub
180     checkResult(ex1+t1+"-"+ex2+t2, \
181     arg1-arg2, \
182     arrays1[0]-arrays2[0], \
183     arrays1[1]-arrays2[1], \
184     arrays1[2]-arrays2[2], \
185     wh)
186    
187     # mul
188     checkResult(ex1+t1+"*"+ex2+t2, \
189     arg1*arg2, \
190     arrays1[0]*arrays2[0], \
191     arrays1[1]*arrays2[1], \
192     arrays1[2]*arrays2[2], \
193     wh)
194    
195     # div
196     checkResult(ex1+t1+"/"+ex2+t2, \
197     arg1/arg2, \
198     arrays1[0]/arrays2[0], \
199     arrays1[1]/arrays2[1], \
200     arrays1[2]/arrays2[2], \
201     wh)
202    
203     # pow
204     if isinstance(arg1,Data):
205     a=arg1
206     else:
207     a=Data(value=arg1,what=arg2.getFunctionSpace())
208     checkResult(ex1+t1+"^"+ex2+t2, \
209     a**arg2, \
210     arrays1[0]**arrays2[0], \
211     arrays1[1]**arrays2[1], \
212     arrays1[2]**arrays2[2], \
213     wh)
214    
215     # test inplace operations
216    
217     # arg2 must be class Data
218     if isinstance(arg2,Data):
219    
220     # if arg1 is expanded arg2 must be expanded
221     if not (not ex2=="Expanded" and ex1=="Expanded") :
222    
223     # if arg2 is scalar arg1 must be scalar:
224     if not (isScalar(arg2) and not isScalar(arg1)):
225    
226     # inplace add:
227     arrays2[0]+=arrays1[0]
228     arrays2[1]+=arrays1[1]
229     arrays2[2]+=arrays1[2]
230     arg2+=arg1
231     checkResult(ex2+t2+"+="+ex1+t1, \
232     arg2, \
233     arrays2[0], \
234     arrays2[1], \
235     arrays2[2], \
236     wh)
237    
238     # inplace sub:
239     arrays2[0]-=arrays1[0]
240     arrays2[1]-=arrays1[1]
241     arrays2[2]-=arrays1[2]
242     arg2-=arg1
243     checkResult(ex2+t2+"-="+ex1+t1, \
244     arg2, \
245     arrays2[0], \
246     arrays2[1], \
247     arrays2[2], \
248     wh)
249    
250     # inplace mul:
251     arrays2[0]*=arrays1[0]
252     arrays2[1]*=arrays1[1]
253     arrays2[2]*=arrays1[2]
254     arg2*=arg1
255     checkResult(ex2+t2+"*="+ex1+t1, \
256     arg2, \
257     arrays2[0], \
258     arrays2[1], \
259     arrays2[2], \
260     wh)
261    
262     # inplace div:
263     arrays2[0]/=arrays1[0]
264     arrays2[1]/=arrays1[1]
265     arrays2[2]/=arrays1[2]
266     arg2/=arg1
267     checkResult(ex2+t2+"/="+ex1+t1, \
268     arg2, \
269     arrays2[0], \
270     arrays2[1], \
271     arrays2[2], \
272     wh)
273    
274     # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26