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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 147 - (hide annotations)
Fri Aug 12 01:45:47 2005 UTC (14 years, 3 months ago) by jgs
Original Path: trunk/esys2/escript/test/python/UnaryOps.py
File MIME type: text/x-python
File size: 11464 byte(s)
erge of development branch dev-02 back to main trunk on 2005-08-12

1 jgs 102 import sys
2     import unittest
3     import os
4    
5 jgs 141 from escript.escript import *
6     from finley import finley
7 jgs 102
8     import numarray
9    
10     """
11    
12     Test unary ops on Data objects.
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    
31     arglist = [ \
32     [ [3,4], [-5,6.], [2,3] ], \
33     [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
34     [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
35     [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
36     [ 3.0, 6.0, 3 ] \
37     ]
38    
39     # these are used to test slicing:
40     a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
41     a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
42     a_r4=[ \
43     [ [ [[ 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]] ] ], \
44     [ [ [[ -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]] ] ], \
45     [ [[[ 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]] ] ] ]
46     a_r4_in=[ \
47     [ [ [[ 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]] ] ], \
48     [ [ [[ -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]] ] ], \
49     [ [[[ 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]] ] ] ]
50    
51     def turnToArray(val,tagged):
52     if tagged=="Tagged1":
53     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
54     elif tagged=="Tagged2":
55     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
56     else:
57     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
58     return out
59    
60     def prepareArg(val,ex,wh):
61     if ex=="Array":
62     out=val[0]
63     else:
64     if ex=="Expanded":
65     exx=True
66     else:
67     exx=False
68     out=Data(val[0],what=wh,expand=exx)
69     if ex=="Tagged1":
70     out.setTaggedValue(Tag1,val[1])
71     elif ex=="Tagged2":
72     out.setTaggedValue(Tag1,val[1])
73     out.setTaggedValue(Tag2,val[2])
74     return out
75    
76     def checkResult(text,res,val0,val1,val2,wh):
77     ref=Data(val0,what=wh,expand=False)
78     ref.setTaggedValue(Tag1,val1)
79     ref.setTaggedValue(Tag2,val2)
80     norm=Lsup(ref)+tol
81     error=Lsup(ref-res)/norm
82     print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
83     if error>tol:
84     print "**** %s: error is too large"%(text)
85 jgs 147 raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
86     sys.exit(1)
87 jgs 102
88     def getRank(arg):
89     if isinstance(arg,Data):
90     return arg.getRank()
91     else:
92     g=array(arg)
93     if g.rank==0:
94     return 1
95     else:
96     return g.rank
97    
98     def isScalar(arg):
99     if isinstance(arg,Data):
100     if arg.getRank()==1 and arg.getShape()[0]==1:
101     return not None
102     else:
103     return None
104     else:
105     g=array(arg)
106     if g.rank==0:
107     return not None
108     else:
109     if g.rank==1 and g.shape[0]==1:
110     return not None
111     else:
112     return None
113    
114     #
115     # ==============================================================
116     #
117     # test unary operators:
118     #
119    
120     msh=finley.Rectangle(20,6,1)
121     for wh in [ContinuousFunction(msh),Function(msh)]:
122    
123     print wh
124    
125     #for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
126     for ex1 in ["Constant","Expanded"]:
127    
128     print "Unary Ops:", ex1
129    
130     for a1 in arglist:
131    
132     arg1=prepareArg(a1,ex1,wh)
133     arrays1=turnToArray(a1,ex1)
134     if isScalar(arg1):
135     t1="(scalar)"
136     else:
137     t1=""
138    
139     # + identity:
140     ref=checkResult("+"+ex1, \
141     +arg1, \
142     +arrays1[0], \
143     +arrays1[1], \
144     +arrays1[2], \
145     wh)
146    
147     # - negation:
148     ref=checkResult("-"+ex1, \
149     -arg1, \
150     -arrays1[0], \
151     -arrays1[1], \
152     -arrays1[2], \
153     wh)
154    
155     # where positive:
156     ref=checkResult("where positive("+ex1+")", \
157     (arg1-3).wherePositive(), \
158     numarray.greater(arrays1[0],3.), \
159     numarray.greater(arrays1[1],3.), \
160     numarray.greater(arrays1[2],3.), \
161     wh)
162    
163     # where negative:
164     ref=checkResult("where negative("+ex1+")", \
165     (arg1-3).whereNegative(), \
166     numarray.greater(3.,arrays1[0]), \
167     numarray.greater(3.,arrays1[1]), \
168     numarray.greater(3.,arrays1[2]), \
169     wh)
170    
171     # where non-negative:
172     ref=checkResult("where nonnegative("+ex1+")", \
173     (arg1-3).whereNonNegative(), \
174     numarray.greater_equal(arrays1[0],3.), \
175     numarray.greater_equal(arrays1[1],3.), \
176     numarray.greater_equal(arrays1[2],3.), \
177     wh)
178    
179     # where non-positive:
180     ref=checkResult("where nonpositive("+ex1+")", \
181     (arg1-3).whereNonPositive(), \
182     numarray.greater_equal(3.,arrays1[0]), \
183     numarray.greater_equal(3.,arrays1[1]), \
184     numarray.greater_equal(3.,arrays1[2]), \
185     wh)
186    
187     # where zero:
188     ref=checkResult("where zero("+ex1+")", \
189     (arg1-3).whereZero(), \
190     numarray.less_equal(numarray.abs(arrays1[0]-3.),EPSILON), \
191     numarray.less_equal(numarray.abs(arrays1[1]-3.),EPSILON), \
192     numarray.less_equal(numarray.abs(arrays1[2]-3.),EPSILON), \
193     wh)
194    
195     # where non-zero:
196     ref=checkResult("where nonzero("+ex1+")", \
197     (arg1-3).whereNonZero(), \
198     numarray.greater(numarray.abs(arrays1[0]-3.),EPSILON), \
199     numarray.greater(numarray.abs(arrays1[1]-3.),EPSILON), \
200     numarray.greater(numarray.abs(arrays1[2]-3.),EPSILON), \
201     wh)
202    
203     # exponential function:
204     ref=checkResult("exp("+ex1+")", \
205     arg1.exp(), \
206     numarray.exp(arrays1[0]), \
207     numarray.exp(arrays1[1]), \
208     numarray.exp(arrays1[2]), \
209     wh)
210    
211     # sqrt
212 jgs 147 ref=checkResult("sqrt("+ex1+")", \
213     arg1.abs().sqrt(), \
214     numarray.sqrt(numarray.abs(arrays1[0])), \
215     numarray.sqrt(numarray.abs(arrays1[1])), \
216     numarray.sqrt(numarray.abs(arrays1[2])), \
217     wh)
218 jgs 102
219     # sin:
220     ref=checkResult("sin("+ex1+")", \
221     arg1.sin(), \
222     numarray.sin(arrays1[0]), \
223     numarray.sin(arrays1[1]), \
224     numarray.sin(arrays1[2]), \
225     wh)
226    
227     # cos:
228     ref=checkResult("cos("+ex1+")", \
229     arg1.cos(), \
230     numarray.cos(arrays1[0]), \
231     numarray.cos(arrays1[1]), \
232     numarray.cos(arrays1[2]), \
233     wh)
234    
235     # get the maximum value at each data point
236     #ref=checkResult("maxval("+ex1+")", \
237     # arg1.maxval(), \
238     # arrays1[0].max(), \
239     # arrays1[1].max(), \
240     # arrays1[2].max(), \
241     # wh)
242    
243     # get the minimum value at each data point
244     #ref=checkResult("minval("+ex1+")", \
245     # arg1.minval(), \
246     # arrays1[0].min(), \
247     # arrays1[1].min(), \
248     # arrays1[2].min(), \
249     # wh)
250    
251     # get the length at each data point = sqrt(sum_{i,j,k,l} A[i,j,k,l]^2)
252     #ref=checkResult("length("+ex1+")", \
253     # arg1.length(), \
254     # numarray.sqrt((arrays1[0]**2).sum()), \
255     # numarray.sqrt((arrays1[1]**2).sum()), \
256     # numarray.sqrt((arrays1[2]**2).sum()), \
257     # wh)
258    
259 jgs 108 # trace:
260     #ref=checkResult("trace("+ex1+")", \
261     # arg1.trace(), \
262     # numarray.trace(arrays1[0]), \
263     # numarray.trace(arrays1[1]), \
264     # numarray.trace(arrays1[2]), \
265     # wh)
266    
267     # transpose:
268     #axis=arrays1[0]/2
269     #ref=checkResult("transpose("+ex1+")", \
270     # arg1.transpose(), \
271     # numarray.transpose(arrays1[0],axis), \
272     # numarray.transpose(arrays1[1],axis), \
273     # numarray.transpose(arrays1[2],axis), \
274     # wh)
275    
276 jgs 102 # get the signs of the values:
277     ref=checkResult("sign("+ex1+")", \
278     arg1.sign(), \
279     numarray.greater(arrays1[0],numarray.zeros(arrays1[0].shape)) \
280     -numarray.less(arrays1[0],numarray.zeros(arrays1[0].shape)),\
281     numarray.greater(arrays1[1],numarray.zeros(arrays1[1].shape)) \
282     -numarray.less(arrays1[1],numarray.zeros(arrays1[1].shape)),\
283     numarray.greater(arrays1[2],numarray.zeros(arrays1[2].shape)) \
284     -numarray.less(arrays1[2],numarray.zeros(arrays1[2].shape)),\
285     wh)
286    
287 jgs 147 sys.exit(0)
288 jgs 102 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26