/[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 108 - (show annotations)
Thu Jan 27 06:21:59 2005 UTC (14 years, 9 months ago) by jgs
Original Path: trunk/esys2/escript/test/python/UnaryOps.py
File MIME type: text/x-python
File size: 11435 byte(s)
*** empty log message ***

1 import sys
2 import unittest
3 import os
4
5 from esys.escript import *
6 from esys import finley
7
8 import numarray
9
10 """
11
12 Test unary ops on Data objects.
13
14 Version $Id$
15
16 """
17
18 from numarray import array,Float64,ones,greater
19
20 Tag1=10
21 Tag2=11
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 # these are used to test slicing:
40 a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
41 a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
42 a_r4=[ \
43 [ [ [[ 1,2,3],[11,12,13]], [[21,22,23],[31,32,33]], [[41,42,43],[51,52,53]] ], [ [[101,102,103],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[201,202,203],[211,212,213]], [[221,222,223],[231,232,233]], [[241,242,243],[251,252,253]] ] ], \
44 [ [ [[ -1,-2,-3],[-11,-12,-13]], [[-21,-22,-23],[-31,-32,-33]], [[-41,-42,-43],[-51,-52,-53]] ], [ [[-101,-102,-103],[-111,-112,-113]], [[-121,-122,-123],[-131,-132,-133]], [[-141,-142,-143],[-151,-152,-153]] ], [ [[-201,-202,-203],[-211,-212,-213]], [[-221,-222,-223],[-231,-232,-233]], [[-241,-242,-243],[-251,-252,-253]] ] ], \
45 [ [[[ 11,12,13],[111,112,113]], [[121,122,123],[131,132,133]], [[141,142,143],[151,152,153]] ], [ [[1101,1102,1103],[1111,1112,1113]], [[1121,1122,1123],[1131,1132,1133]], [[1141,1142,1143],[1151,1152,1153]] ], [ [[1201,1202,1203],[1211,1212,1213]], [[1221,1222,1223],[1231,1232,1233]], [[1241,1242,1243],[1251,1252,1253]] ] ] ]
46 a_r4_in=[ \
47 [ [ [[ 1./1,1./2,1./3],[1./11,1./12,1./13]], [[1./21,1./22,1./23],[1./31,1./32,1./33]], [[1./41,1./42,1./43],[1./51,1./52,1./53]] ], [ [[1./101,1./102,1./103],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./201,1./202,1./203],[1./211,1./212,1./213]], [[1./221,1./222,1./223],[1./231,1./232,1./233]], [[1./241,1./242,1./243],[1./251,1./252,1./253]] ] ], \
48 [ [ [[ -1./1,-1./2,-1./3],[-1./11,-1./12,-1./13]], [[-1./21,-1./22,-1./23],[-1./31,-1./32,-1./33]], [[-1./41,-1./42,-1./43],[-1./51,-1./52,-1./53]] ], [ [[-1./101,-1./102,-1./103],[-1./111,-1./112,-1./113]], [[-1./121,-1./122,-1./123],[-1./131,-1./132,-1./133]], [[-1./141,-1./142,-1./143],[1./-151,-1./152,-1./153]] ], [ [[-1./201,-1./202,-1./203],[-1./211,-1./212,-1./213]], [[-1./221,-1./222,-1./223],[-1./231,-1./232,-1./233]], [[-1./241,-1./242,-1./243],[-1./251,-1./252,-1./253]] ] ], \
49 [ [[[ 1./11,1./12,1./13],[1./111,1./112,1./113]], [[1./121,1./122,1./123],[1./131,1./132,1./133]], [[1./141,1./142,1./143],[1./151,1./152,1./153]] ], [ [[1./1101,1./1102,1./1103],[1./1111,1./1112,1./1113]], [[1./1121,1./1122,1./1123],[1./1131,1./1132,1./1133]], [[1./1141,1./1142,1./1143],[1./1151,1./1152,1./1153]] ], [ [[1./1201,1./1202,1./1203],[1./1211,1./1212,1./1213]], [[1./1221,1./1222,1./1223],[1./1231,1./1232,1./1233]], [[1./1241,1./1242,1./1243],[1./1251,1./1252,1./1253]] ] ] ]
50
51 def turnToArray(val,tagged):
52 if tagged=="Tagged1":
53 out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
54 elif tagged=="Tagged2":
55 out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
56 else:
57 out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
58 return out
59
60 def prepareArg(val,ex,wh):
61 if ex=="Array":
62 out=val[0]
63 else:
64 if ex=="Expanded":
65 exx=True
66 else:
67 exx=False
68 out=Data(val[0],what=wh,expand=exx)
69 if ex=="Tagged1":
70 out.setTaggedValue(Tag1,val[1])
71 elif ex=="Tagged2":
72 out.setTaggedValue(Tag1,val[1])
73 out.setTaggedValue(Tag2,val[2])
74 return out
75
76 def checkResult(text,res,val0,val1,val2,wh):
77 ref=Data(val0,what=wh,expand=False)
78 ref.setTaggedValue(Tag1,val1)
79 ref.setTaggedValue(Tag2,val2)
80 norm=Lsup(ref)+tol
81 error=Lsup(ref-res)/norm
82 print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
83 if error>tol:
84 #raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
85 print "**** %s: error is too large"%(text)
86
87 def getRank(arg):
88 if isinstance(arg,Data):
89 return arg.getRank()
90 else:
91 g=array(arg)
92 if g.rank==0:
93 return 1
94 else:
95 return g.rank
96
97 def isScalar(arg):
98 if isinstance(arg,Data):
99 if arg.getRank()==1 and arg.getShape()[0]==1:
100 return not None
101 else:
102 return None
103 else:
104 g=array(arg)
105 if g.rank==0:
106 return not None
107 else:
108 if g.rank==1 and g.shape[0]==1:
109 return not None
110 else:
111 return None
112
113 #
114 # ==============================================================
115 #
116 # test unary operators:
117 #
118
119 msh=finley.Rectangle(20,6,1)
120 for wh in [ContinuousFunction(msh),Function(msh)]:
121
122 print wh
123
124 #for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]:
125 for ex1 in ["Constant","Expanded"]:
126
127 print "Unary Ops:", ex1
128
129 for a1 in arglist:
130
131 arg1=prepareArg(a1,ex1,wh)
132 arrays1=turnToArray(a1,ex1)
133 if isScalar(arg1):
134 t1="(scalar)"
135 else:
136 t1=""
137
138 # + identity:
139 ref=checkResult("+"+ex1, \
140 +arg1, \
141 +arrays1[0], \
142 +arrays1[1], \
143 +arrays1[2], \
144 wh)
145
146 # - negation:
147 ref=checkResult("-"+ex1, \
148 -arg1, \
149 -arrays1[0], \
150 -arrays1[1], \
151 -arrays1[2], \
152 wh)
153
154 # where positive:
155 ref=checkResult("where positive("+ex1+")", \
156 (arg1-3).wherePositive(), \
157 numarray.greater(arrays1[0],3.), \
158 numarray.greater(arrays1[1],3.), \
159 numarray.greater(arrays1[2],3.), \
160 wh)
161
162 # where negative:
163 ref=checkResult("where negative("+ex1+")", \
164 (arg1-3).whereNegative(), \
165 numarray.greater(3.,arrays1[0]), \
166 numarray.greater(3.,arrays1[1]), \
167 numarray.greater(3.,arrays1[2]), \
168 wh)
169
170 # where non-negative:
171 ref=checkResult("where nonnegative("+ex1+")", \
172 (arg1-3).whereNonNegative(), \
173 numarray.greater_equal(arrays1[0],3.), \
174 numarray.greater_equal(arrays1[1],3.), \
175 numarray.greater_equal(arrays1[2],3.), \
176 wh)
177
178 # where non-positive:
179 ref=checkResult("where nonpositive("+ex1+")", \
180 (arg1-3).whereNonPositive(), \
181 numarray.greater_equal(3.,arrays1[0]), \
182 numarray.greater_equal(3.,arrays1[1]), \
183 numarray.greater_equal(3.,arrays1[2]), \
184 wh)
185
186 # where zero:
187 ref=checkResult("where zero("+ex1+")", \
188 (arg1-3).whereZero(), \
189 numarray.less_equal(numarray.abs(arrays1[0]-3.),EPSILON), \
190 numarray.less_equal(numarray.abs(arrays1[1]-3.),EPSILON), \
191 numarray.less_equal(numarray.abs(arrays1[2]-3.),EPSILON), \
192 wh)
193
194 # where non-zero:
195 ref=checkResult("where nonzero("+ex1+")", \
196 (arg1-3).whereNonZero(), \
197 numarray.greater(numarray.abs(arrays1[0]-3.),EPSILON), \
198 numarray.greater(numarray.abs(arrays1[1]-3.),EPSILON), \
199 numarray.greater(numarray.abs(arrays1[2]-3.),EPSILON), \
200 wh)
201
202 # exponential function:
203 ref=checkResult("exp("+ex1+")", \
204 arg1.exp(), \
205 numarray.exp(arrays1[0]), \
206 numarray.exp(arrays1[1]), \
207 numarray.exp(arrays1[2]), \
208 wh)
209
210 # sqrt
211 #ref=checkResult("sqrt("+ex1+")", \
212 # arg1.abs().sqrt(), \
213 # numarray.sqrt(numarray.abs(arrays1[0])), \
214 # numarray.sqrt(numarray.abs(arrays1[1])), \
215 # numarray.sqrt(numarray.abs(arrays1[2])), \
216 # wh)
217
218 # sin:
219 ref=checkResult("sin("+ex1+")", \
220 arg1.sin(), \
221 numarray.sin(arrays1[0]), \
222 numarray.sin(arrays1[1]), \
223 numarray.sin(arrays1[2]), \
224 wh)
225
226 # cos:
227 ref=checkResult("cos("+ex1+")", \
228 arg1.cos(), \
229 numarray.cos(arrays1[0]), \
230 numarray.cos(arrays1[1]), \
231 numarray.cos(arrays1[2]), \
232 wh)
233
234 # get the maximum value at each data point
235 #ref=checkResult("maxval("+ex1+")", \
236 # arg1.maxval(), \
237 # arrays1[0].max(), \
238 # arrays1[1].max(), \
239 # arrays1[2].max(), \
240 # wh)
241
242 # get the minimum value at each data point
243 #ref=checkResult("minval("+ex1+")", \
244 # arg1.minval(), \
245 # arrays1[0].min(), \
246 # arrays1[1].min(), \
247 # arrays1[2].min(), \
248 # wh)
249
250 # get the length at each data point = sqrt(sum_{i,j,k,l} A[i,j,k,l]^2)
251 #ref=checkResult("length("+ex1+")", \
252 # arg1.length(), \
253 # numarray.sqrt((arrays1[0]**2).sum()), \
254 # numarray.sqrt((arrays1[1]**2).sum()), \
255 # numarray.sqrt((arrays1[2]**2).sum()), \
256 # wh)
257
258 # trace:
259 #ref=checkResult("trace("+ex1+")", \
260 # arg1.trace(), \
261 # numarray.trace(arrays1[0]), \
262 # numarray.trace(arrays1[1]), \
263 # numarray.trace(arrays1[2]), \
264 # wh)
265
266 # transpose:
267 #axis=arrays1[0]/2
268 #ref=checkResult("transpose("+ex1+")", \
269 # arg1.transpose(), \
270 # numarray.transpose(arrays1[0],axis), \
271 # numarray.transpose(arrays1[1],axis), \
272 # numarray.transpose(arrays1[2],axis), \
273 # wh)
274
275 # get the signs of the values:
276 ref=checkResult("sign("+ex1+")", \
277 arg1.sign(), \
278 numarray.greater(arrays1[0],numarray.zeros(arrays1[0].shape)) \
279 -numarray.less(arrays1[0],numarray.zeros(arrays1[0].shape)),\
280 numarray.greater(arrays1[1],numarray.zeros(arrays1[1].shape)) \
281 -numarray.less(arrays1[1],numarray.zeros(arrays1[1].shape)),\
282 numarray.greater(arrays1[2],numarray.zeros(arrays1[2].shape)) \
283 -numarray.less(arrays1[2],numarray.zeros(arrays1[2].shape)),\
284 wh)
285
286 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26