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

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=="Tagged1":
49 out.setTaggedValue(Tag1,val[1])
50 elif ex=="Tagged2":
51 out.setTaggedValue(Tag1,val[1])
52 out.setTaggedValue(Tag2,val[2])
53 return out
54
55 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 def checkResult(text,res,val0,val1,val2,wh):
65 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
76 def getRank(arg):
77 if isinstance(arg,Data):
78 return arg.getRank()
79 else:
80 g=array(arg)
81 if g.rank==0:
82 return 1
83 else:
84 return g.rank
85
86 def isScalar(arg):
87 if isinstance(arg,Data):
88 if arg.getRank()==1 and arg.getShape()[0]==1:
89 return not None
90 else:
91 return None
92 else:
93 g=array(arg)
94 if g.rank==0:
95 return not None
96 else:
97 if g.rank==1 and g.shape[0]==1:
98 return not None
99 else:
100 return None
101
102 #
103 # ==============================================================
104 #
105 # test unary operators:
106 #
107
108 msh=bruce.Rectangle(20,6)
109 for wh in [ContinuousFunction(msh),Function(msh)]:
110
111 print wh
112
113 for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
114
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 arrays1[0], \
130 arrays1[1], \
131 arrays1[2], \
132 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 (arg1-3)._wherePositive(), \
145 numarray.greater(arrays1[0],3.), \
146 numarray.greater(arrays1[1],3.), \
147 numarray.greater(arrays1[2],3.), \
148 wh)
149
150 # where negative:
151 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
158 # where non-negative:
159 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
166 # where non-positive:
167 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
174 # where zero:
175 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
182 # where non-zero:
183 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
190 # exponential function:
191 ref=checkResult("exp("+ex1+")", \
192 arg1._exp(), \
193 numarray.exp(arrays1[0]), \
194 numarray.exp(arrays1[1]), \
195 numarray.exp(arrays1[2]), \
196 wh)
197
198 # sqrt
199 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
206 # sin:
207 ref=checkResult("sin("+ex1+")", \
208 arg1._sin(), \
209 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 arg1._cos(), \
217 numarray.cos(arrays1[0]), \
218 numarray.cos(arrays1[1]), \
219 numarray.cos(arrays1[2]), \
220 wh)
221
222 # tan:
223 ref=checkResult("tan("+ex1+")", \
224 arg1._tan(), \
225 numarray.tan(arrays1[0]), \
226 numarray.tan(arrays1[1]), \
227 numarray.tan(arrays1[2]), \
228 wh)
229
230 # numarray has no asin/acos/atan funcs
231
232 # asin:
233 #ref=checkResult("asin("+ex1+")", \
234 # arg1._asin(), \
235 # 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 # arg1._acos(), \
243 # 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 # arg1._atan(), \
251 # 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 arg1._sinh(), \
259 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 arg1._cosh(), \
267 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 arg1._tanh(), \
275 numarray.tanh(arrays1[0]), \
276 numarray.tanh(arrays1[1]), \
277 numarray.tanh(arrays1[2]), \
278 wh)
279
280 # numarray has no asinh/acosh/atanh funcs
281
282 # asinh:
283 #ref=checkResult("asinh("+ex1+")", \
284 # arg1._asinh(), \
285 # 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 # arg1._acosh(), \
293 # 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 # arg1._atanh(), \
301 # numarray.atanh(arrays1[0]), \
302 # numarray.atanh(arrays1[1]), \
303 # numarray.atanh(arrays1[2]), \
304 # wh)
305
306 # get the maximum value at each data point
307 ref=checkResult("maxval("+ex1+")", \
308 arg1._maxval(), \
309 arrays1[0].max(), \
310 arrays1[1].max(), \
311 arrays1[2].max(), \
312 wh)
313
314 # get the minimum value at each data point
315 ref=checkResult("minval("+ex1+")", \
316 arg1._minval(), \
317 arrays1[0].min(), \
318 arrays1[1].min(), \
319 arrays1[2].min(), \
320 wh)
321
322 # length/trace/transpose not yet implemented for Data
323
324 # 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 # arg1._length(), \
327 # numarray.sqrt((arrays1[0]**2).sum()), \
328 # numarray.sqrt((arrays1[1]**2).sum()), \
329 # numarray.sqrt((arrays1[2]**2).sum()), \
330 # wh)
331
332 # trace:
333 #ref=checkResult("trace("+ex1+")", \
334 # arg1._trace(), \
335 # 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 # arg1._transpose(), \
344 # numarray.transpose(arrays1[0],axis), \
345 # numarray.transpose(arrays1[1],axis), \
346 # numarray.transpose(arrays1[2],axis), \
347 # wh)
348
349 # get the signs of the values:
350 ref=checkResult("sign("+ex1+")", \
351 arg1._sign(), \
352 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 sys.exit(0)
361 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26