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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 614 - (show annotations)
Wed Mar 22 01:37:07 2006 UTC (13 years, 4 months ago) by elspeth
File MIME type: text/x-python
File size: 12097 byte(s)
Corrected spelling of 'license' in url so that the link actually points to the license.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26