/[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 546 - (show annotations)
Tue Feb 21 01:59:07 2006 UTC (14 years, 7 months ago) by jgs
File MIME type: text/x-python
File size: 11666 byte(s)
reinstate testing of DataTagged objects

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. a0,a1,a2 are converted into numarrays.
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._wherePositive(), \
145 numarray.greater(arrays1[0],0.), \
146 numarray.greater(arrays1[1],0.), \
147 numarray.greater(arrays1[2],0.), \
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._abs()._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 # asin:
231 #ref=checkResult("asin("+ex1+")", \
232 # arg1.asin(), \
233 # numarray.asin(arrays1[0]), \
234 # numarray.asin(arrays1[1]), \
235 # numarray.asin(arrays1[2]), \
236 # wh)
237
238 # acos:
239 #ref=checkResult("acos("+ex1+")", \
240 # arg1.acos(), \
241 # numarray.acos(arrays1[0]), \
242 # numarray.acos(arrays1[1]), \
243 # numarray.acos(arrays1[2]), \
244 # wh)
245
246 # atan:
247 #ref=checkResult("atan("+ex1+")", \
248 # arg1.atan(), \
249 # numarray.atan(arrays1[0]), \
250 # numarray.atan(arrays1[1]), \
251 # numarray.atan(arrays1[2]), \
252 # wh)
253
254 # sinh:
255 ref=checkResult("sinh("+ex1+")", \
256 arg1._sinh(), \
257 numarray.sinh(arrays1[0]), \
258 numarray.sinh(arrays1[1]), \
259 numarray.sinh(arrays1[2]), \
260 wh)
261
262 # cosh:
263 ref=checkResult("cosh("+ex1+")", \
264 arg1._cosh(), \
265 numarray.cosh(arrays1[0]), \
266 numarray.cosh(arrays1[1]), \
267 numarray.cosh(arrays1[2]), \
268 wh)
269
270 # tanh:
271 ref=checkResult("tanh("+ex1+")", \
272 arg1._tanh(), \
273 numarray.tanh(arrays1[0]), \
274 numarray.tanh(arrays1[1]), \
275 numarray.tanh(arrays1[2]), \
276 wh)
277
278 # asinh:
279 #ref=checkResult("asinh("+ex1+")", \
280 # arg1.asinh(), \
281 # numarray.asinh(arrays1[0]), \
282 # numarray.asinh(arrays1[1]), \
283 # numarray.asinh(arrays1[2]), \
284 # wh)
285
286 # acosh:
287 #ref=checkResult("acosh("+ex1+")", \
288 # arg1.acosh(), \
289 # numarray.acosh(arrays1[0]), \
290 # numarray.acosh(arrays1[1]), \
291 # numarray.acosh(arrays1[2]), \
292 # wh)
293
294 # atanh:
295 #ref=checkResult("atanh("+ex1+")", \
296 # arg1.atanh(), \
297 # numarray.atanh(arrays1[0]), \
298 # numarray.atanh(arrays1[1]), \
299 # numarray.atanh(arrays1[2]), \
300 # wh)
301
302 # get the maximum value at each data point
303 #ref=checkResult("maxval("+ex1+")", \
304 # arg1.maxval(), \
305 # arrays1[0].max(), \
306 # arrays1[1].max(), \
307 # arrays1[2].max(), \
308 # wh)
309
310 # get the minimum value at each data point
311 #ref=checkResult("minval("+ex1+")", \
312 # arg1.minval(), \
313 # arrays1[0].min(), \
314 # arrays1[1].min(), \
315 # arrays1[2].min(), \
316 # wh)
317
318 # get the length at each data point = sqrt(sum_{i,j,k,l} A[i,j,k,l]^2)
319 #ref=checkResult("length("+ex1+")", \
320 # arg1.length(), \
321 # numarray.sqrt((arrays1[0]**2).sum()), \
322 # numarray.sqrt((arrays1[1]**2).sum()), \
323 # numarray.sqrt((arrays1[2]**2).sum()), \
324 # wh)
325
326 # trace:
327 #ref=checkResult("trace("+ex1+")", \
328 # arg1.trace(), \
329 # numarray.trace(arrays1[0]), \
330 # numarray.trace(arrays1[1]), \
331 # numarray.trace(arrays1[2]), \
332 # wh)
333
334 # transpose:
335 #axis=arrays1[0]/2
336 #ref=checkResult("transpose("+ex1+")", \
337 # arg1.transpose(), \
338 # numarray.transpose(arrays1[0],axis), \
339 # numarray.transpose(arrays1[1],axis), \
340 # numarray.transpose(arrays1[2],axis), \
341 # wh)
342
343 # get the signs of the values:
344 ref=checkResult("sign("+ex1+")", \
345 arg1._sign(), \
346 numarray.greater(arrays1[0],numarray.zeros(arrays1[0].shape)) \
347 -numarray.less(arrays1[0],numarray.zeros(arrays1[0].shape)),\
348 numarray.greater(arrays1[1],numarray.zeros(arrays1[1].shape)) \
349 -numarray.less(arrays1[1],numarray.zeros(arrays1[1].shape)),\
350 numarray.greater(arrays1[2],numarray.zeros(arrays1[2].shape)) \
351 -numarray.less(arrays1[2],numarray.zeros(arrays1[2].shape)),\
352 wh)
353
354 sys.exit(0)
355 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26