/[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 564 - (show 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 """
2
3 Test unary ops on Data objects.
4
5 Version $Id$
6
7 """
8
9 import sys
10 import unittest
11 import os
12
13 from esys.escript import *
14 from esys import bruce
15
16 import numarray
17
18 from numarray import array,Float64,ones,greater
19
20 Tag1=10
21 Tag2=15
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.
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 def prepareArg(val,ex,wh):
40 if ex=="Array":
41 out=val[0]
42 else:
43 if ex=="Expanded":
44 exx=True
45 else:
46 exx=False
47 out=Data(val[0],what=wh,expand=exx)
48 if ex=="Tagged0":
49 out.tag()
50 elif ex=="Tagged1":
51 out.setTaggedValue(Tag1,val[1])
52 elif ex=="Tagged2":
53 out.setTaggedValue(Tag1,val[1])
54 out.setTaggedValue(Tag2,val[2])
55 return out
56
57 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 def checkResult(text,res,val0,val1,val2,wh):
67 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
78 def getRank(arg):
79 if isinstance(arg,Data):
80 return arg.getRank()
81 else:
82 g=array(arg)
83 if g.rank==0:
84 return 1
85 else:
86 return g.rank
87
88 def isScalar(arg):
89 if isinstance(arg,Data):
90 if arg.getRank()==1 and arg.getShape()[0]==1:
91 return not None
92 else:
93 return None
94 else:
95 g=array(arg)
96 if g.rank==0:
97 return not None
98 else:
99 if g.rank==1 and g.shape[0]==1:
100 return not None
101 else:
102 return None
103
104 #
105 # ==============================================================
106 #
107 # test unary operators:
108 #
109
110 msh=bruce.Rectangle(20,6)
111 for wh in [ContinuousFunction(msh),Function(msh)]:
112
113 print wh
114
115 for ex1 in ["Constant","Expanded","Tagged0","Tagged1","Tagged2"]:
116
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 arrays1[0], \
132 arrays1[1], \
133 arrays1[2], \
134 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 (arg1-3)._wherePositive(), \
147 numarray.greater(arrays1[0],3.), \
148 numarray.greater(arrays1[1],3.), \
149 numarray.greater(arrays1[2],3.), \
150 wh)
151
152 # where negative:
153 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
160 # where non-negative:
161 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
168 # where non-positive:
169 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
176 # where zero:
177 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
184 # where non-zero:
185 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
192 # exponential function:
193 ref=checkResult("exp("+ex1+")", \
194 arg1._exp(), \
195 numarray.exp(arrays1[0]), \
196 numarray.exp(arrays1[1]), \
197 numarray.exp(arrays1[2]), \
198 wh)
199
200 # sqrt
201 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
208 # sin:
209 ref=checkResult("sin("+ex1+")", \
210 arg1._sin(), \
211 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 arg1._cos(), \
219 numarray.cos(arrays1[0]), \
220 numarray.cos(arrays1[1]), \
221 numarray.cos(arrays1[2]), \
222 wh)
223
224 # tan:
225 ref=checkResult("tan("+ex1+")", \
226 arg1._tan(), \
227 numarray.tan(arrays1[0]), \
228 numarray.tan(arrays1[1]), \
229 numarray.tan(arrays1[2]), \
230 wh)
231
232 # numarray has no asin/acos/atan funcs
233
234 # asin:
235 #ref=checkResult("asin("+ex1+")", \
236 # arg1._asin(), \
237 # 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 # arg1._acos(), \
245 # 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 # arg1._atan(), \
253 # 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 arg1._sinh(), \
261 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 arg1._cosh(), \
269 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 arg1._tanh(), \
277 numarray.tanh(arrays1[0]), \
278 numarray.tanh(arrays1[1]), \
279 numarray.tanh(arrays1[2]), \
280 wh)
281
282 # numarray has no asinh/acosh/atanh funcs
283
284 # asinh:
285 #ref=checkResult("asinh("+ex1+")", \
286 # arg1._asinh(), \
287 # 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 # arg1._acosh(), \
295 # 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 # arg1._atanh(), \
303 # numarray.atanh(arrays1[0]), \
304 # numarray.atanh(arrays1[1]), \
305 # numarray.atanh(arrays1[2]), \
306 # wh)
307
308 # get the maximum value at each data point
309 ref=checkResult("maxval("+ex1+")", \
310 arg1._maxval(), \
311 arrays1[0].max(), \
312 arrays1[1].max(), \
313 arrays1[2].max(), \
314 wh)
315
316 # get the minimum value at each data point
317 ref=checkResult("minval("+ex1+")", \
318 arg1._minval(), \
319 arrays1[0].min(), \
320 arrays1[1].min(), \
321 arrays1[2].min(), \
322 wh)
323
324 # length/trace/transpose not yet implemented for Data
325
326 # 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 # arg1._length(), \
329 # numarray.sqrt((arrays1[0]**2).sum()), \
330 # numarray.sqrt((arrays1[1]**2).sum()), \
331 # numarray.sqrt((arrays1[2]**2).sum()), \
332 # wh)
333
334 # trace:
335 #ref=checkResult("trace("+ex1+")", \
336 # arg1._trace(), \
337 # 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 # arg1._transpose(), \
346 # numarray.transpose(arrays1[0],axis), \
347 # numarray.transpose(arrays1[1],axis), \
348 # numarray.transpose(arrays1[2],axis), \
349 # wh)
350
351 # get the signs of the values:
352 ref=checkResult("sign("+ex1+")", \
353 arg1._sign(), \
354 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 sys.exit(0)
363 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26