/[escript]/trunk/escript/test/python/SliceGetting.py
ViewVC logotype

Contents of /trunk/escript/test/python/SliceGetting.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 141 - (show annotations)
Mon Jul 25 05:15:41 2005 UTC (14 years, 3 months ago) by jgs
Original Path: trunk/esys2/escript/test/python/SliceGetting.py
File MIME type: text/x-python
File size: 10394 byte(s)
fixed to work with new python source/install directory structure

1 """
2
3 Tests slice getting for Data objects.
4
5 Version $Id$
6
7 """
8
9 import sys
10 import unittest
11 import os
12
13 from escript.escript import *
14 from finley import finley
15
16 import numarray
17 from numarray import array,Float64,ones,greater
18
19 Tag1=10
20 Tag2=11
21
22 tol=1.E-15
23
24 #
25 # list of arguments: a list item has the form [a0,a1,a2]
26 # what a0 is the default value and a1 is used for tag Tag1
27 # and a2 for tag2. a0,a1,a2 are converted into numarrays.
28 #
29
30 arglist = [ \
31 [ [3,4], [-5,6.], [2,3] ], \
32 [ [[1,2],[3,4]], [[5,6],[7,8]], [[-5,-6],[7,8]] ], \
33 [ [[15,8],[12,8]], [[-9,9],[13,8]], [[7,34],[19,7]] ], \
34 [ [[[15,8],[12,8]],[[-9,9],[13,8]]], [[[3,4],[-9,4]],[[1,-9],[7,4]]], [[[5,2],[6,2]],[[-6,4],[7,5]]] ], \
35 [ 3.0, 6.0, 3 ] \
36 ]
37
38 # these are used to test slicing:
39 a_r1=[ [1,2,3], [-1,-2,-3], [100,200,300] ]
40 a_r1_in=[ [1./1,2,3], [-1./1,-1./2,-1./3], [1./100,1./200,1./300] ]
41 a_r4=[ \
42 [ [ [[ 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]] ] ], \
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 [ [[[ 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]] ] ] ]
45 a_r4_in=[ \
46 [ [ [[ 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]] ] ], \
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./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]] ] ] ]
49
50 def turnToArray(val,tagged):
51 if tagged=="Tagged1":
52 out=[array(val[0],Float64),array(val[1],Float64),array(val[0],Float64)]
53 elif tagged=="Tagged2":
54 out=[array(val[0],Float64),array(val[1],Float64),array(val[2],Float64)]
55 else:
56 out=[array(val[0],Float64),array(val[0],Float64),array(val[0],Float64)]
57 return out
58
59 def prepareArg(val,ex,wh):
60 if ex=="Array":
61 out=val[0]
62 else:
63 if ex=="Expanded":
64 exx=True
65 else:
66 exx=False
67 out=Data(val[0],what=wh,expand=exx)
68 if ex=="Tagged1":
69 out.setTaggedValue(Tag1,val[1])
70 elif ex=="Tagged2":
71 out.setTaggedValue(Tag1,val[1])
72 out.setTaggedValue(Tag2,val[2])
73 return out
74
75 def checkResult(text,res,val0,val1,val2,wh):
76 ref=Data(val0,what=wh,expand=False)
77 ref.setTaggedValue(Tag1,val1)
78 ref.setTaggedValue(Tag2,val2)
79 norm=Lsup(ref)+tol
80 error=Lsup(ref-res)/norm
81 print "@@ %s, shape %s: error = %e"%(text,ref.getShape(),error)
82 if error>tol:
83 #raise SystemError,"@@ %s at %s: error is too large"%(text,wh)
84 print "**** %s: error is too large"%(text)
85
86 def getRank(arg):
87 if isinstance(arg,Data):
88 return arg.getRank()
89 else:
90 g=array(arg)
91 if g.rank==0:
92 return 1
93 else:
94 return g.rank
95
96 def isScalar(arg):
97 if isinstance(arg,Data):
98 if arg.getRank()==1 and arg.getShape()[0]==1:
99 return not None
100 else:
101 return None
102 else:
103 g=array(arg)
104 if g.rank==0:
105 return not None
106 else:
107 if g.rank==1 and g.shape[0]==1:
108 return not None
109 else:
110 return None
111
112 #
113 # ==============================================================
114 #
115 # test slice getting:
116 #
117
118 msh=finley.Rectangle(20,6,1)
119 for wh in [ContinuousFunction(msh),Function(msh)]:
120
121 print wh
122
123 for ex1 in ["Constant", "Expanded"]:
124
125 print "Slice getting: ", ex1, ":"
126
127 # rank 1
128
129 arg=prepareArg(a_r1,ex1,wh)
130 arrays=turnToArray(a_r1,ex1)
131
132 checkResult("slicing: rank=1 [:] "+ex1, \
133 arg[:], \
134 arrays[0][:], \
135 arrays[1][:], \
136 arrays[2][:], \
137 wh)
138
139 checkResult("slicing: rank=1 [1] "+ex1, \
140 arg[1], \
141 arrays[0][1], \
142 arrays[1][1], \
143 arrays[2][1], \
144 wh)
145
146 checkResult("slicing: rank=1 [1:3] "+ex1, \
147 arg[1:3], \
148 arrays[0][1:3], \
149 arrays[1][1:3], \
150 arrays[2][1:3], \
151 wh)
152
153 checkResult("slicing: rank=1 [2:] "+ex1, \
154 arg[2:], \
155 arrays[0][2:], \
156 arrays[1][2:], \
157 arrays[2][2:], \
158 wh)
159
160 checkResult("slicing: rank=1 [:1] "+ex1, \
161 arg[:1], \
162 arrays[0][:1], \
163 arrays[1][:1], \
164 arrays[2][:1], \
165 wh)
166
167 # rank 4
168
169 arg=prepareArg(a_r4,ex1,wh)
170 arrays=turnToArray(a_r4,ex1)
171
172 checkResult("slicing: rank=4 [:] "+ex1, \
173 arg[:], \
174 arrays[0][:], \
175 arrays[1][:], \
176 arrays[2][:], \
177 wh)
178
179 checkResult("slicing: rank=4 [1] "+ex1, \
180 arg[1], \
181 arrays[0][1], \
182 arrays[1][1], \
183 arrays[2][1], \
184 wh)
185
186 checkResult("slicing: rank=4 [1,1,1,1] "+ex1, \
187 arg[1,1,1,1], \
188 arrays[0][1,1,1,1], \
189 arrays[1][1,1,1,1], \
190 arrays[2][1,1,1,1], \
191 wh)
192
193 checkResult("slicing: rank=4 [1:3] "+ex1, \
194 arg[1:3], \
195 arrays[0][1:3], \
196 arrays[1][1:3], \
197 arrays[2][1:3], \
198 wh)
199
200 checkResult("slicing: rank=4 [:2] "+ex1, \
201 arg[:2], \
202 arrays[0][:2], \
203 arrays[1][:2], \
204 arrays[2][:2], \
205 wh)
206
207 checkResult("slicing: rank=4 [1:] "+ex1, \
208 arg[1:], \
209 arrays[0][1:], \
210 arrays[1][1:], \
211 arrays[2][1:], \
212 wh)
213
214 checkResult("slicing: rank=4 [:,:] "+ex1, \
215 arg[:,:], \
216 arrays[0][:,:], \
217 arrays[1][:,:], \
218 arrays[2][:,:], \
219 wh)
220
221 checkResult("slicing: rank=4 [:,1] "+ex1, \
222 arg[:,1], \
223 arrays[0][:,1], \
224 arrays[1][:,1], \
225 arrays[2][:,1], \
226 wh)
227
228 checkResult("slicing: rank=4 [:,1:3] "+ex1, \
229 arg[:,1:3], \
230 arrays[0][:,1:3], \
231 arrays[1][:,1:3], \
232 arrays[2][:,1:3], \
233 wh)
234
235 checkResult("slicing: rank=4 [1:2,1:3] "+ex1, \
236 arg[1:2,1:3], \
237 arrays[0][1:2,1:3], \
238 arrays[1][1:2,1:3], \
239 arrays[2][1:2,1:3], \
240 wh)
241
242 checkResult("slicing: rank=4 [:,1:2,1:2] "+ex1, \
243 arg[:,1:2,1:2], \
244 arrays[0][:,1:2,1:2], \
245 arrays[1][:,1:2,1:2], \
246 arrays[2][:,1:2,1:2], \
247 wh)
248
249 checkResult("slicing: rank=4 [:,:,1:2,1:3] "+ex1, \
250 arg[:,:,1:2,1:3], \
251 arrays[0][:,:,1:2,1:3], \
252 arrays[1][:,:,1:2,1:3], \
253 arrays[2][:,:,1:2,1:3], \
254 wh)
255
256 checkResult("slicing: rank=4 [1:2,1:3,1,1] "+ex1, \
257 arg[1:2,1:3,1,1], \
258 arrays[0][1:2,1:3,1,1], \
259 arrays[1][1:2,1:3,1,1], \
260 arrays[2][1:2,1:3,1,1], \
261 wh)
262
263 checkResult("slicing: rank=4 [1:2,1:3,:,1] "+ex1, \
264 arg[1:2,1:3,:,1], \
265 arrays[0][1:2,1:3,:,1], \
266 arrays[1][1:2,1:3,:,1], \
267 arrays[2][1:2,1:3,:,1], \
268 wh)
269
270 checkResult("slicing: rank=4 [1:2,1:3,0,:] "+ex1, \
271 arg[1:2,1:3,0,:], \
272 arrays[0][1:2,1:3,0,:], \
273 arrays[1][1:2,1:3,0,:], \
274 arrays[2][1:2,1:3,0,:], \
275 wh)
276
277 checkResult("slicing: rank=4 [1:2,1:3,0,1] "+ex1, \
278 arg[1:2,1:3,0,1], \
279 arrays[0][1:2,1:3,0,1], \
280 arrays[1][1:2,1:3,0,1], \
281 arrays[2][1:2,1:3,0,1], \
282 wh)
283
284 checkResult("slicing: rank=4 [1,1,0,1] "+ex1, \
285 arg[1,1,0,1], \
286 arrays[0][1,1,0,1], \
287 arrays[1][1,1,0,1], \
288 arrays[2][1,1,0,1], \
289 wh)
290
291 # end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26