1 |
import sys |
2 |
import unittest |
3 |
import os |
4 |
|
5 |
from esys.escript import * |
6 |
from esys import bruce |
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 |
print "**** %s: error is too large"%(text) |
85 |
raise SystemError,"@@ %s at %s: error is too large"%(text,wh) |
86 |
sys.exit(1) |
87 |
|
88 |
def getRank(arg): |
89 |
if isinstance(arg,Data): |
90 |
return arg.getRank() |
91 |
else: |
92 |
g=array(arg) |
93 |
if g.rank==0: |
94 |
return 1 |
95 |
else: |
96 |
return g.rank |
97 |
|
98 |
def isScalar(arg): |
99 |
if isinstance(arg,Data): |
100 |
if arg.getRank()==1 and arg.getShape()[0]==1: |
101 |
return not None |
102 |
else: |
103 |
return None |
104 |
else: |
105 |
g=array(arg) |
106 |
if g.rank==0: |
107 |
return not None |
108 |
else: |
109 |
if g.rank==1 and g.shape[0]==1: |
110 |
return not None |
111 |
else: |
112 |
return None |
113 |
|
114 |
# |
115 |
# ============================================================== |
116 |
# |
117 |
# test unary operators: |
118 |
# |
119 |
|
120 |
msh=bruce.Rectangle(20,6) |
121 |
for wh in [ContinuousFunction(msh),Function(msh)]: |
122 |
|
123 |
print wh |
124 |
|
125 |
#for ex1 in ["Constant","Expanded","Tagged1","Tagged2"]: |
126 |
for ex1 in ["Constant","Expanded"]: |
127 |
|
128 |
print "Unary Ops:", ex1 |
129 |
|
130 |
for a1 in arglist: |
131 |
|
132 |
arg1=prepareArg(a1,ex1,wh) |
133 |
arrays1=turnToArray(a1,ex1) |
134 |
if isScalar(arg1): |
135 |
t1="(scalar)" |
136 |
else: |
137 |
t1="" |
138 |
|
139 |
# + identity: |
140 |
ref=checkResult("+"+ex1, \ |
141 |
+arg1, \ |
142 |
arrays1[0], \ |
143 |
arrays1[1], \ |
144 |
arrays1[2], \ |
145 |
wh) |
146 |
|
147 |
# - negation: |
148 |
ref=checkResult("-"+ex1, \ |
149 |
-arg1, \ |
150 |
-arrays1[0], \ |
151 |
-arrays1[1], \ |
152 |
-arrays1[2], \ |
153 |
wh) |
154 |
|
155 |
# where positive: |
156 |
ref=checkResult("where positive("+ex1+")", \ |
157 |
(arg1-3)._wherePositive(), \ |
158 |
numarray.greater(arrays1[0],3.), \ |
159 |
numarray.greater(arrays1[1],3.), \ |
160 |
numarray.greater(arrays1[2],3.), \ |
161 |
wh) |
162 |
|
163 |
# where negative: |
164 |
ref=checkResult("where negative("+ex1+")", \ |
165 |
(arg1-3)._whereNegative(), \ |
166 |
numarray.greater(3.,arrays1[0]), \ |
167 |
numarray.greater(3.,arrays1[1]), \ |
168 |
numarray.greater(3.,arrays1[2]), \ |
169 |
wh) |
170 |
|
171 |
# where non-negative: |
172 |
ref=checkResult("where nonnegative("+ex1+")", \ |
173 |
(arg1-3)._whereNonNegative(), \ |
174 |
numarray.greater_equal(arrays1[0],3.), \ |
175 |
numarray.greater_equal(arrays1[1],3.), \ |
176 |
numarray.greater_equal(arrays1[2],3.), \ |
177 |
wh) |
178 |
|
179 |
# where non-positive: |
180 |
ref=checkResult("where nonpositive("+ex1+")", \ |
181 |
(arg1-3)._whereNonPositive(), \ |
182 |
numarray.greater_equal(3.,arrays1[0]), \ |
183 |
numarray.greater_equal(3.,arrays1[1]), \ |
184 |
numarray.greater_equal(3.,arrays1[2]), \ |
185 |
wh) |
186 |
|
187 |
# where zero: |
188 |
ref=checkResult("where zero("+ex1+")", \ |
189 |
(arg1-3)._whereZero(), \ |
190 |
numarray.less_equal(numarray.abs(arrays1[0]-3.),0.0), \ |
191 |
numarray.less_equal(numarray.abs(arrays1[1]-3.),0.0), \ |
192 |
numarray.less_equal(numarray.abs(arrays1[2]-3.),0.0), \ |
193 |
wh) |
194 |
|
195 |
# where non-zero: |
196 |
ref=checkResult("where nonzero("+ex1+")", \ |
197 |
(arg1-3)._whereNonZero(), \ |
198 |
numarray.greater(numarray.abs(arrays1[0]-3.),0.0), \ |
199 |
numarray.greater(numarray.abs(arrays1[1]-3.),0.0), \ |
200 |
numarray.greater(numarray.abs(arrays1[2]-3.),0.0), \ |
201 |
wh) |
202 |
|
203 |
# exponential function: |
204 |
ref=checkResult("exp("+ex1+")", \ |
205 |
arg1._exp(), \ |
206 |
numarray.exp(arrays1[0]), \ |
207 |
numarray.exp(arrays1[1]), \ |
208 |
numarray.exp(arrays1[2]), \ |
209 |
wh) |
210 |
|
211 |
# sqrt |
212 |
#ref=checkResult("sqrt("+ex1+")", \ |
213 |
# arg1._abs()._sqrt(), \ |
214 |
# numarray.sqrt(numarray.abs(arrays1[0])), \ |
215 |
# numarray.sqrt(numarray.abs(arrays1[1])), \ |
216 |
# numarray.sqrt(numarray.abs(arrays1[2])), \ |
217 |
# wh) |
218 |
|
219 |
# sin: |
220 |
ref=checkResult("sin("+ex1+")", \ |
221 |
arg1._sin(), \ |
222 |
numarray.sin(arrays1[0]), \ |
223 |
numarray.sin(arrays1[1]), \ |
224 |
numarray.sin(arrays1[2]), \ |
225 |
wh) |
226 |
|
227 |
# cos: |
228 |
ref=checkResult("cos("+ex1+")", \ |
229 |
arg1._cos(), \ |
230 |
numarray.cos(arrays1[0]), \ |
231 |
numarray.cos(arrays1[1]), \ |
232 |
numarray.cos(arrays1[2]), \ |
233 |
wh) |
234 |
|
235 |
# tan: |
236 |
ref=checkResult("tan("+ex1+")", \ |
237 |
arg1._tan(), \ |
238 |
numarray.tan(arrays1[0]), \ |
239 |
numarray.tan(arrays1[1]), \ |
240 |
numarray.tan(arrays1[2]), \ |
241 |
wh) |
242 |
|
243 |
# asin: |
244 |
#ref=checkResult("asin("+ex1+")", \ |
245 |
# arg1.asin(), \ |
246 |
# numarray.asin(arrays1[0]), \ |
247 |
# numarray.asin(arrays1[1]), \ |
248 |
# numarray.asin(arrays1[2]), \ |
249 |
# wh) |
250 |
|
251 |
# acos: |
252 |
#ref=checkResult("acos("+ex1+")", \ |
253 |
# arg1.acos(), \ |
254 |
# numarray.acos(arrays1[0]), \ |
255 |
# numarray.acos(arrays1[1]), \ |
256 |
# numarray.acos(arrays1[2]), \ |
257 |
# wh) |
258 |
|
259 |
# atan: |
260 |
#ref=checkResult("atan("+ex1+")", \ |
261 |
# arg1.atan(), \ |
262 |
# numarray.atan(arrays1[0]), \ |
263 |
# numarray.atan(arrays1[1]), \ |
264 |
# numarray.atan(arrays1[2]), \ |
265 |
# wh) |
266 |
|
267 |
# sinh: |
268 |
ref=checkResult("sinh("+ex1+")", \ |
269 |
arg1._sinh(), \ |
270 |
numarray.sinh(arrays1[0]), \ |
271 |
numarray.sinh(arrays1[1]), \ |
272 |
numarray.sinh(arrays1[2]), \ |
273 |
wh) |
274 |
|
275 |
# cosh: |
276 |
ref=checkResult("cosh("+ex1+")", \ |
277 |
arg1._cosh(), \ |
278 |
numarray.cosh(arrays1[0]), \ |
279 |
numarray.cosh(arrays1[1]), \ |
280 |
numarray.cosh(arrays1[2]), \ |
281 |
wh) |
282 |
|
283 |
# tanh: |
284 |
ref=checkResult("tanh("+ex1+")", \ |
285 |
arg1._tanh(), \ |
286 |
numarray.tanh(arrays1[0]), \ |
287 |
numarray.tanh(arrays1[1]), \ |
288 |
numarray.tanh(arrays1[2]), \ |
289 |
wh) |
290 |
|
291 |
# asinh: |
292 |
#ref=checkResult("asinh("+ex1+")", \ |
293 |
# arg1.asinh(), \ |
294 |
# numarray.asinh(arrays1[0]), \ |
295 |
# numarray.asinh(arrays1[1]), \ |
296 |
# numarray.asinh(arrays1[2]), \ |
297 |
# wh) |
298 |
|
299 |
# acosh: |
300 |
#ref=checkResult("acosh("+ex1+")", \ |
301 |
# arg1.acosh(), \ |
302 |
# numarray.acosh(arrays1[0]), \ |
303 |
# numarray.acosh(arrays1[1]), \ |
304 |
# numarray.acosh(arrays1[2]), \ |
305 |
# wh) |
306 |
|
307 |
# atanh: |
308 |
#ref=checkResult("atanh("+ex1+")", \ |
309 |
# arg1.atanh(), \ |
310 |
# numarray.atanh(arrays1[0]), \ |
311 |
# numarray.atanh(arrays1[1]), \ |
312 |
# numarray.atanh(arrays1[2]), \ |
313 |
# wh) |
314 |
|
315 |
# get the maximum value at each data point |
316 |
#ref=checkResult("maxval("+ex1+")", \ |
317 |
# arg1.maxval(), \ |
318 |
# arrays1[0].max(), \ |
319 |
# arrays1[1].max(), \ |
320 |
# arrays1[2].max(), \ |
321 |
# wh) |
322 |
|
323 |
# get the minimum value at each data point |
324 |
#ref=checkResult("minval("+ex1+")", \ |
325 |
# arg1.minval(), \ |
326 |
# arrays1[0].min(), \ |
327 |
# arrays1[1].min(), \ |
328 |
# arrays1[2].min(), \ |
329 |
# wh) |
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 |