/[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 560 - (hide annotations)
Thu Feb 23 01:58:35 2006 UTC (16 years, 5 months ago) by jgs
File MIME type: text/x-python
File size: 11749 byte(s)
reinstate testing of DataTagged object unary ops

1 jgs 546 """
2    
3     Test unary ops on Data objects.
4    
5     Version $Id$
6    
7     """
8    
9 jgs 102 import sys
10     import unittest
11     import os
12    
13 jgs 149 from esys.escript import *
14 jgs 153 from esys import bruce
15 jgs 102
16     import numarray
17    
18     from numarray import array,Float64,ones,greater
19    
20     Tag1=10
21 jgs 546 Tag2=15
22 jgs 102
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 jgs 560 # and a2 for Tag2.
29 jgs 102 #
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 jgs 560 [ [3.0], [6.0], [3] ] \
37 jgs 102 ]
38    
39     def prepareArg(val,ex,wh):
40 jgs 546 if ex=="Array":
41 jgs 102 out=val[0]
42 jgs 546 else:
43 jgs 102 if ex=="Expanded":
44     exx=True
45     else:
46     exx=False
47     out=Data(val[0],what=wh,expand=exx)
48     if ex=="Tagged1":
49 jgs 546 out.setTaggedValue(Tag1,val[1])
50 jgs 102 elif ex=="Tagged2":
51 jgs 546 out.setTaggedValue(Tag1,val[1])
52     out.setTaggedValue(Tag2,val[2])
53     return out
54 jgs 102
55 jgs 546 def turnToArray(val,tagged):
56     if tagged=="Tagged1":
57     out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
58     elif tagged=="Tagged2":
59     out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
60     else:
61     out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
62     return out
63    
64 jgs 102 def checkResult(text,res,val0,val1,val2,wh):
65 jgs 546 ref=Data(val0,what=wh,expand=False)
66     ref.setTaggedValue(Tag1,val1)
67     ref.setTaggedValue(Tag2,val2)
68     norm=Lsup(ref)+tol
69     error=Lsup(ref-res)/norm
70     print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
71     if error>tol:
72     print "**** %s: error is too large"%(text)
73     raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
74     sys.exit(1)
75 jgs 102
76     def getRank(arg):
77     if isinstance(arg,Data):
78 jgs 546 return arg.getRank()
79 jgs 102 else:
80     g=array(arg)
81     if g.rank==0:
82 jgs 546 return 1
83 jgs 102 else:
84 jgs 546 return g.rank
85 jgs 102
86     def isScalar(arg):
87     if isinstance(arg,Data):
88 jgs 546 if arg.getRank()==1 and arg.getShape()[0]==1:
89     return not None
90     else:
91     return None
92 jgs 102 else:
93     g=array(arg)
94     if g.rank==0:
95 jgs 546 return not None
96 jgs 102 else:
97 jgs 546 if g.rank==1 and g.shape[0]==1:
98     return not None
99     else:
100     return None
101 jgs 102
102     #
103     # ==============================================================
104     #
105     # test unary operators:
106     #
107    
108 jgs 153 msh=bruce.Rectangle(20,6)
109 jgs 102 for wh in [ContinuousFunction(msh),Function(msh)]:
110    
111     print wh
112    
113 jgs 546 for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
114 jgs 102
115     print "Unary Ops:", ex1
116    
117     for a1 in arglist:
118    
119     arg1=prepareArg(a1,ex1,wh)
120     arrays1=turnToArray(a1,ex1)
121     if isScalar(arg1):
122     t1="(scalar)"
123     else:
124     t1=""
125    
126     # + identity:
127     ref=checkResult("+"+ex1, \
128     +arg1, \
129 phornby 184 arrays1[0], \
130     arrays1[1], \
131     arrays1[2], \
132 jgs 102 wh)
133    
134     # - negation:
135     ref=checkResult("-"+ex1, \
136     -arg1, \
137     -arrays1[0], \
138     -arrays1[1], \
139     -arrays1[2], \
140     wh)
141    
142     # where positive:
143     ref=checkResult("where positive("+ex1+")", \
144 jgs 560 (arg1-3)._wherePositive(), \
145     numarray.greater(arrays1[0],3.), \
146     numarray.greater(arrays1[1],3.), \
147     numarray.greater(arrays1[2],3.), \
148 jgs 102 wh)
149    
150     # where negative:
151 jgs 560 ref=checkResult("where negative("+ex1+")", \
152     (arg1-3)._whereNegative(), \
153     numarray.greater(3.,arrays1[0]), \
154     numarray.greater(3.,arrays1[1]), \
155     numarray.greater(3.,arrays1[2]), \
156     wh)
157 jgs 102
158     # where non-negative:
159 jgs 560 ref=checkResult("where nonnegative("+ex1+")", \
160     (arg1-3)._whereNonNegative(), \
161     numarray.greater_equal(arrays1[0],3.), \
162     numarray.greater_equal(arrays1[1],3.), \
163     numarray.greater_equal(arrays1[2],3.), \
164     wh)
165 jgs 102
166     # where non-positive:
167 jgs 560 ref=checkResult("where nonpositive("+ex1+")", \
168     (arg1-3)._whereNonPositive(), \
169     numarray.greater_equal(3.,arrays1[0]), \
170     numarray.greater_equal(3.,arrays1[1]), \
171     numarray.greater_equal(3.,arrays1[2]), \
172     wh)
173 jgs 102
174     # where zero:
175 jgs 560 ref=checkResult("where zero("+ex1+")", \
176     (arg1-3)._whereZero(), \
177     numarray.less_equal(numarray.abs(arrays1[0]-3.),0.0), \
178     numarray.less_equal(numarray.abs(arrays1[1]-3.),0.0), \
179     numarray.less_equal(numarray.abs(arrays1[2]-3.),0.0), \
180     wh)
181 jgs 102
182     # where non-zero:
183 jgs 560 ref=checkResult("where nonzero("+ex1+")", \
184     (arg1-3)._whereNonZero(), \
185     numarray.greater(numarray.abs(arrays1[0]-3.),0.0), \
186     numarray.greater(numarray.abs(arrays1[1]-3.),0.0), \
187     numarray.greater(numarray.abs(arrays1[2]-3.),0.0), \
188     wh)
189 jgs 102
190     # exponential function:
191     ref=checkResult("exp("+ex1+")", \
192 jgs 314 arg1._exp(), \
193 jgs 102 numarray.exp(arrays1[0]), \
194     numarray.exp(arrays1[1]), \
195     numarray.exp(arrays1[2]), \
196     wh)
197    
198     # sqrt
199 jgs 560 ref=checkResult("sqrt("+ex1+")", \
200     arg1._sqrt(), \
201     numarray.sqrt(numarray.abs(arrays1[0])), \
202     numarray.sqrt(numarray.abs(arrays1[1])), \
203     numarray.sqrt(numarray.abs(arrays1[2])), \
204     wh)
205 jgs 102
206     # sin:
207     ref=checkResult("sin("+ex1+")", \
208 jgs 314 arg1._sin(), \
209 jgs 102 numarray.sin(arrays1[0]), \
210     numarray.sin(arrays1[1]), \
211     numarray.sin(arrays1[2]), \
212     wh)
213    
214     # cos:
215     ref=checkResult("cos("+ex1+")", \
216 jgs 314 arg1._cos(), \
217 jgs 102 numarray.cos(arrays1[0]), \
218     numarray.cos(arrays1[1]), \
219     numarray.cos(arrays1[2]), \
220     wh)
221    
222 jgs 150 # tan:
223     ref=checkResult("tan("+ex1+")", \
224 jgs 314 arg1._tan(), \
225 jgs 150 numarray.tan(arrays1[0]), \
226     numarray.tan(arrays1[1]), \
227     numarray.tan(arrays1[2]), \
228     wh)
229    
230 jgs 560 # numarray has no asin/acos/atan funcs
231    
232 jgs 150 # asin:
233     #ref=checkResult("asin("+ex1+")", \
234 jgs 560 # arg1._asin(), \
235 jgs 150 # numarray.asin(arrays1[0]), \
236     # numarray.asin(arrays1[1]), \
237     # numarray.asin(arrays1[2]), \
238     # wh)
239    
240     # acos:
241     #ref=checkResult("acos("+ex1+")", \
242 jgs 560 # arg1._acos(), \
243 jgs 150 # numarray.acos(arrays1[0]), \
244     # numarray.acos(arrays1[1]), \
245     # numarray.acos(arrays1[2]), \
246     # wh)
247    
248     # atan:
249     #ref=checkResult("atan("+ex1+")", \
250 jgs 560 # arg1._atan(), \
251 jgs 150 # numarray.atan(arrays1[0]), \
252     # numarray.atan(arrays1[1]), \
253     # numarray.atan(arrays1[2]), \
254     # wh)
255    
256     # sinh:
257     ref=checkResult("sinh("+ex1+")", \
258 jgs 314 arg1._sinh(), \
259 jgs 150 numarray.sinh(arrays1[0]), \
260     numarray.sinh(arrays1[1]), \
261     numarray.sinh(arrays1[2]), \
262     wh)
263    
264     # cosh:
265     ref=checkResult("cosh("+ex1+")", \
266 jgs 314 arg1._cosh(), \
267 jgs 150 numarray.cosh(arrays1[0]), \
268     numarray.cosh(arrays1[1]), \
269     numarray.cosh(arrays1[2]), \
270     wh)
271    
272     # tanh:
273     ref=checkResult("tanh("+ex1+")", \
274 jgs 314 arg1._tanh(), \
275 jgs 150 numarray.tanh(arrays1[0]), \
276     numarray.tanh(arrays1[1]), \
277     numarray.tanh(arrays1[2]), \
278     wh)
279    
280 jgs 560 # numarray has no asinh/acosh/atanh funcs
281    
282 jgs 150 # asinh:
283     #ref=checkResult("asinh("+ex1+")", \
284 jgs 560 # arg1._asinh(), \
285 jgs 150 # numarray.asinh(arrays1[0]), \
286     # numarray.asinh(arrays1[1]), \
287     # numarray.asinh(arrays1[2]), \
288     # wh)
289    
290     # acosh:
291     #ref=checkResult("acosh("+ex1+")", \
292 jgs 560 # arg1._acosh(), \
293 jgs 150 # numarray.acosh(arrays1[0]), \
294     # numarray.acosh(arrays1[1]), \
295     # numarray.acosh(arrays1[2]), \
296     # wh)
297    
298     # atanh:
299     #ref=checkResult("atanh("+ex1+")", \
300 jgs 560 # arg1._atanh(), \
301 jgs 150 # numarray.atanh(arrays1[0]), \
302     # numarray.atanh(arrays1[1]), \
303     # numarray.atanh(arrays1[2]), \
304     # wh)
305    
306 jgs 102 # get the maximum value at each data point
307 jgs 560 ref=checkResult("maxval("+ex1+")", \
308     arg1._maxval(), \
309     arrays1[0].max(), \
310     arrays1[1].max(), \
311     arrays1[2].max(), \
312     wh)
313 jgs 102
314     # get the minimum value at each data point
315 jgs 560 ref=checkResult("minval("+ex1+")", \
316     arg1._minval(), \
317     arrays1[0].min(), \
318     arrays1[1].min(), \
319     arrays1[2].min(), \
320     wh)
321 jgs 102
322 jgs 560 # length/trace/transpose not yet implemented for Data
323    
324 jgs 102 # get the length at each data point = sqrt(sum_{i,j,k,l} A[i,j,k,l]^2)
325     #ref=checkResult("length("+ex1+")", \
326 jgs 560 # arg1._length(), \
327 jgs 102 # numarray.sqrt((arrays1[0]**2).sum()), \
328     # numarray.sqrt((arrays1[1]**2).sum()), \
329     # numarray.sqrt((arrays1[2]**2).sum()), \
330     # wh)
331    
332 jgs 108 # trace:
333     #ref=checkResult("trace("+ex1+")", \
334 jgs 560 # arg1._trace(), \
335 jgs 108 # numarray.trace(arrays1[0]), \
336     # numarray.trace(arrays1[1]), \
337     # numarray.trace(arrays1[2]), \
338     # wh)
339    
340     # transpose:
341     #axis=arrays1[0]/2
342     #ref=checkResult("transpose("+ex1+")", \
343 jgs 560 # arg1._transpose(), \
344 jgs 108 # numarray.transpose(arrays1[0],axis), \
345     # numarray.transpose(arrays1[1],axis), \
346     # numarray.transpose(arrays1[2],axis), \
347     # wh)
348    
349 jgs 102 # get the signs of the values:
350     ref=checkResult("sign("+ex1+")", \
351 jgs 314 arg1._sign(), \
352 jgs 102 numarray.greater(arrays1[0],numarray.zeros(arrays1[0].shape)) \
353     -numarray.less(arrays1[0],numarray.zeros(arrays1[0].shape)),\
354     numarray.greater(arrays1[1],numarray.zeros(arrays1[1].shape)) \
355     -numarray.less(arrays1[1],numarray.zeros(arrays1[1].shape)),\
356     numarray.greater(arrays1[2],numarray.zeros(arrays1[2].shape)) \
357     -numarray.less(arrays1[2],numarray.zeros(arrays1[2].shape)),\
358     wh)
359    
360 jgs 147 sys.exit(0)
361 jgs 102 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26