/[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 564 - (hide annotations)
Thu Feb 23 05:22:31 2006 UTC (13 years, 8 months ago) by jgs
File MIME type: text/x-python
File size: 11809 byte(s)
extend testing to include DataTagged objects with no
tags, only default values

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26