/[escript]/trunk/escriptcore/test/python/run_symbolic.py
ViewVC logotype

Contents of /trunk/escriptcore/test/python/run_symbolic.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3493 - (show annotations)
Mon Apr 4 04:37:02 2011 UTC (8 years, 5 months ago) by caltinay
Original Path: branches/symbolic_from_3470/escript/test/python/run_symbolic.py
File MIME type: text/x-python
File size: 18822 byte(s)
Removed escript's legacy Symbol implementation and replaced by a sympy-based
variant. Added more symbolic versions of escript functions and the relevant
unit tests.
This removes nearly 150000 (!) lines of code mainly from the old test suite for
symbols...wow!

1
2 ########################################################
3 #
4 # Copyright (c) 2003-2010 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="https://launchpad.net/escript-finley"
21
22 """
23 Test suite for the escript.symbolic module
24
25 :var __author__: name of author
26 :var __copyright__: copyrights
27 :var __license__: licence agreement
28 :var __url__: url entry point on documentation
29 :var __version__: version
30 :var __date__: date of the version
31 """
32
33 __author__="Cihan Altinay"
34
35 from esys.escript import *
36 import unittest
37
38 class SymbolicTestCase(unittest.TestCase):
39
40 # number of digits that have to match for results to be considered equal
41 TOL_DIGITS=8
42
43 def test_Evaluator(self):
44 e=Evaluator()
45 self.assertEqual(len(e), 0, "empty evaluator returns wrong length")
46 self.assertEqual(e.evaluate(), (), "result of evaluate() not empty")
47
48 x=Symbol('x')
49 e=Evaluator(x*x, x**3)
50 self.assertEqual(len(e), 2, "evaluator returns wrong length")
51 self.assertEqual(e[0], x*x, "first expression wrong")
52 self.assertEqual(e[1], x**3, "second expression wrong")
53
54 f=e.addExpression(x**4)
55 self.assertEqual(len(e), 3, "wrong length after addExpression()")
56 self.assertEqual(e, f, "addExpression() did not return self")
57 self.assertEqual(e[2], x**4, "third expression wrong")
58
59 e+=x**5
60 self.assertEqual(len(e), 4, "wrong length after += operator")
61 self.assertEqual(e[3], x**5, "fourth expression wrong")
62
63 self.assertRaises(RuntimeError, e.evaluate)
64 f=e.subs(x=2)
65 self.assertEqual(e, f, "subs() did not return self")
66 self.assertEqual(e.evaluate(), (4,8,16,32), "wrong result after subs()")
67 self.assertEqual(e(x=3), (9,27,81,243), "wrong result after __call__")
68
69 xx=RandomData((4,), FunctionSpace())
70 ref=[d.toListOfTuples() for d in (xx**2, xx**3, xx**4, xx**5)]
71 res=e(x=xx)
72 for d in res:
73 self.assertTrue(isinstance(d, Data), "substituted expression not a Data object")
74 res=[x.toListOfTuples() for x in res]
75 self.assertEqual(res, ref, "wrong result after substitution with Data object")
76
77 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78 def test_wherePositive_Symbol(self):
79 x=Symbol('x')
80 y=wherePositive(x)
81 self.assertTrue(isinstance(y, Basic), "wrong type of result")
82 xx=RandomData((4,4), FunctionSpace())
83 ref=wherePositive(xx)
84 res=Evaluator(y)(x=xx)
85 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
86 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87 def test_whereNonPositive_Symbol(self):
88 x=Symbol('x')
89 y=whereNonPositive(x)
90 self.assertTrue(isinstance(y, Basic), "wrong type of result")
91 xx=RandomData((4,4), FunctionSpace())
92 ref=whereNonPositive(xx)
93 res=Evaluator(y)(x=xx)
94 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
95 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
96 def test_whereNegative_Symbol(self):
97 x=Symbol('x')
98 y=whereNegative(x)
99 self.assertTrue(isinstance(y, Basic), "wrong type of result")
100 xx=RandomData((4,4), FunctionSpace())
101 ref=whereNegative(xx)
102 res=Evaluator(y)(x=xx)
103 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
104 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105 def test_whereNonNegative_Symbol(self):
106 x=Symbol('x')
107 y=whereNonNegative(x)
108 self.assertTrue(isinstance(y, Basic), "wrong type of result")
109 xx=RandomData((4,4), FunctionSpace())
110 ref=whereNonNegative(xx)
111 res=Evaluator(y)(x=xx)
112 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
113 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114 def test_whereZero_Symbol(self):
115 x=Symbol('x')
116 y=whereZero(x)
117 self.assertTrue(isinstance(y, Basic), "wrong type of result")
118 xx=RandomData((4,4), FunctionSpace())
119 ref=whereZero(xx)
120 res=Evaluator(y)(x=xx)
121 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
122 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123 def test_whereNonZero_Symbol(self):
124 x=Symbol('x')
125 y=whereNonZero(x)
126 self.assertTrue(isinstance(y, Basic), "wrong type of result")
127 xx=RandomData((4,4), FunctionSpace())
128 ref=whereNonZero(xx)
129 res=Evaluator(y)(x=xx)
130 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
131 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
132 def test_log10_Symbol(self):
133 x=Symbol('x')
134 y=log10(x)
135 self.assertTrue(isinstance(y, Basic), "wrong type of result")
136 xx=RandomData((4,4), FunctionSpace())
137 ref=log10(xx)
138 res=Evaluator(y)(x=xx)
139 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
140 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
141 def test_inverse_Symbol(self):
142 x=Symbol('x')
143 y=inverse(x)
144 self.assertTrue(isinstance(y, Basic), "wrong type of result")
145 xx=RandomData((4,4), FunctionSpace())
146 ref=inverse(xx)
147 res=Evaluator(y)(x=xx)
148 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
149 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
150 def test_minval_Symbol(self):
151 x=Symbol('x')
152 y=minval(x)
153 self.assertTrue(isinstance(y, Basic), "wrong type of result")
154 xx=RandomData((4,4), FunctionSpace())
155 ref=minval(xx)
156 res=Evaluator(y)(x=xx)
157 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
158 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
159 def test_maxval_Symbol(self):
160 x=Symbol('x')
161 y=maxval(x)
162 self.assertTrue(isinstance(y, Basic), "wrong type of result")
163 xx=RandomData((4,4), FunctionSpace())
164 ref=maxval(xx)
165 res=Evaluator(y)(x=xx)
166 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
167 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
168 def test_trace_Symbol(self):
169 x=Symbol('x')
170 y=trace(x)
171 self.assertTrue(isinstance(y, Basic), "wrong type of result")
172 xx=RandomData((4,4), FunctionSpace())
173 ref=trace(xx)
174 res=Evaluator(y)(x=xx)
175 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
176 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
177 def test_transpose_Symbol(self):
178 x=Symbol('x')
179 y=transpose(x)
180 self.assertTrue(isinstance(y, Basic), "wrong type of result")
181 xx=RandomData((4,4), FunctionSpace())
182 ref=transpose(xx)
183 res=Evaluator(y)(x=xx)
184 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
185 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
186 def test_symmetric_Symbol(self):
187 x=Symbol('x')
188 y=symmetric(x)
189 self.assertTrue(isinstance(y, Basic), "wrong type of result")
190 xx=RandomData((4,4), FunctionSpace())
191 ref=symmetric(xx)
192 res=Evaluator(y)(x=xx)
193 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
194 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
195 def test_nonsymmetric_Symbol(self):
196 x=Symbol('x')
197 y=nonsymmetric(x)
198 self.assertTrue(isinstance(y, Basic), "wrong type of result")
199 xx=RandomData((4,4), FunctionSpace())
200 ref=nonsymmetric(xx)
201 res=Evaluator(y)(x=xx)
202 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
203 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
204 def test_swapaxes_Symbol(self):
205 x=Symbol('x')
206 y=swap_axes(x, 1, 0)
207 self.assertTrue(isinstance(y, Basic), "wrong type of result")
208 xx=RandomData((4,4), FunctionSpace())
209 ref=swap_axes(xx, 1, 0)
210 res=Evaluator(y)(x=xx)
211 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
212 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
213 def test_sin_Symbol(self):
214 x=Symbol('x')
215 y=sin(x)
216 self.assertTrue(isinstance(y, Basic), "wrong type of result")
217 xx=RandomData((4,4), FunctionSpace())
218 ref=sin(xx)
219 res=Evaluator(y)(x=xx)
220 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
221 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
222 def test_cos_Symbol(self):
223 x=Symbol('x')
224 y=cos(x)
225 self.assertTrue(isinstance(y, Basic), "wrong type of result")
226 xx=RandomData((4,4), FunctionSpace())
227 ref=cos(xx)
228 res=Evaluator(y)(x=xx)
229 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
230 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
231 def test_tan_Symbol(self):
232 x=Symbol('x')
233 y=tan(x)
234 self.assertTrue(isinstance(y, Basic), "wrong type of result")
235 xx=RandomData((4,4), FunctionSpace())
236 ref=tan(xx)
237 res=Evaluator(y)(x=xx)
238 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
239 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
240 def test_asin_Symbol(self):
241 x=Symbol('x')
242 y=asin(x)
243 self.assertTrue(isinstance(y, Basic), "wrong type of result")
244 xx=RandomData((4,4), FunctionSpace())
245 ref=asin(xx)
246 res=Evaluator(y)(x=xx)
247 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
248 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
249 def test_acos_Symbol(self):
250 x=Symbol('x')
251 y=acos(x)
252 self.assertTrue(isinstance(y, Basic), "wrong type of result")
253 xx=RandomData((4,4), FunctionSpace())
254 ref=acos(xx)
255 res=Evaluator(y)(x=xx)
256 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
257 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
258 def test_atan_Symbol(self):
259 x=Symbol('x')
260 y=atan(x)
261 self.assertTrue(isinstance(y, Basic), "wrong type of result")
262 xx=RandomData((4,4), FunctionSpace())
263 ref=atan(xx)
264 res=Evaluator(y)(x=xx)
265 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
266 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
267 def test_sinh_Symbol(self):
268 x=Symbol('x')
269 y=sinh(x)
270 self.assertTrue(isinstance(y, Basic), "wrong type of result")
271 xx=RandomData((4,4), FunctionSpace())
272 ref=sinh(xx)
273 res=Evaluator(y)(x=xx)
274 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
275 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
276 def test_cosh_Symbol(self):
277 x=Symbol('x')
278 y=cosh(x)
279 self.assertTrue(isinstance(y, Basic), "wrong type of result")
280 xx=RandomData((4,4), FunctionSpace())
281 ref=cosh(xx)
282 res=Evaluator(y)(x=xx)
283 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
284 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
285 def test_tanh_Symbol(self):
286 x=Symbol('x')
287 y=tanh(x)
288 self.assertTrue(isinstance(y, Basic), "wrong type of result")
289 xx=RandomData((4,4), FunctionSpace())
290 ref=tanh(xx)
291 res=Evaluator(y)(x=xx)
292 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
293 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
294 def test_asinh_Symbol(self):
295 x=Symbol('x')
296 y=asinh(x)
297 self.assertTrue(isinstance(y, Basic), "wrong type of result")
298 xx=RandomData((4,4), FunctionSpace())
299 ref=asinh(xx)
300 res=Evaluator(y)(x=xx)
301 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
302 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
303 def test_acosh_Symbol(self):
304 x=Symbol('x')
305 y=acosh(x)
306 self.assertTrue(isinstance(y, Basic), "wrong type of result")
307 xx=RandomData((4,4), FunctionSpace())
308 ref=acosh(xx)
309 res=Evaluator(y)(x=xx)
310 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
311 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
312 def test_atanh_Symbol(self):
313 x=Symbol('x')
314 y=atanh(x)
315 self.assertTrue(isinstance(y, Basic), "wrong type of result")
316 xx=RandomData((4,4), FunctionSpace())
317 ref=atanh(xx)
318 res=Evaluator(y)(x=xx)
319 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
320 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
321 def test_exp_Symbol(self):
322 x=Symbol('x')
323 y=exp(x)
324 self.assertTrue(isinstance(y, Basic), "wrong type of result")
325 xx=RandomData((4,4), FunctionSpace())
326 ref=exp(xx)
327 res=Evaluator(y)(x=xx)
328 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
329 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
330 def test_sqrt_Symbol(self):
331 x=Symbol('x')
332 y=sqrt(x)
333 self.assertTrue(isinstance(y, Basic), "wrong type of result")
334 xx=RandomData((4,4), FunctionSpace())
335 ref=sqrt(xx)
336 res=Evaluator(y)(x=xx)
337 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
338 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
339 def test_log_Symbol(self):
340 x=Symbol('x')
341 y=log(x)
342 self.assertTrue(isinstance(y, Basic), "wrong type of result")
343 xx=RandomData((4,4), FunctionSpace())
344 ref=log(xx)
345 res=Evaluator(y)(x=xx)
346 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
347 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
348 def test_sign_Symbol(self):
349 x=Symbol('x')
350 y=sign(x)
351 self.assertTrue(isinstance(y, Basic), "wrong type of result")
352 xx=RandomData((4,4), FunctionSpace())
353 ref=sign(xx)
354 res=Evaluator(y)(x=xx)
355 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
356 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
357 def test_abs_Symbol(self):
358 x=Symbol('x')
359 y=abs(x)
360 self.assertTrue(isinstance(y, Basic), "wrong type of result")
361 xx=RandomData((4,4), FunctionSpace())
362 ref=abs(xx)
363 res=Evaluator(y)(x=xx)
364 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
365 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
366 def test_inner_Symbol(self):
367 x,y=symbols('xy')
368 z=inner(x,y)
369 self.assertTrue(isinstance(z, Basic), "wrong type of result")
370 xx=RandomData((4,4), FunctionSpace())
371 yy=RandomData((4,4), FunctionSpace())
372 ref=inner(xx,yy)
373 res=Evaluator(z)(x=xx, y=yy)
374 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
375 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
376 def test_outer_Symbol(self):
377 x,y=symbols('xy')
378 z=outer(x,y)
379 self.assertTrue(isinstance(z, Basic), "wrong type of result")
380 xx=RandomData((4,4), FunctionSpace())
381 yy=RandomData((4,4), FunctionSpace())
382 ref=outer(xx,yy)
383 res=Evaluator(z)(x=xx, y=yy)
384 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
385 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
386 def test_clip_Symbol(self):
387 x=Symbol('x')
388 y=clip(x, 0.4, 0.6)
389 self.assertTrue(isinstance(y, Basic), "wrong type of result")
390 xx=RandomData((4,4), FunctionSpace())
391 ref=clip(xx, 0.4, 0.6)
392 res=Evaluator(y)(x=xx)
393 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
394 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
395 def test_length_Symbol(self):
396 x=Symbol('x')
397 y=length(x)
398 self.assertTrue(isinstance(y, Basic), "wrong type of result")
399 xx=RandomData((4,4), FunctionSpace())
400 ref=length(xx)
401 res=Evaluator(y)(x=xx)
402 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
403 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
404 def test_maximum_Symbol(self):
405 x,y=symbols('xy')
406 z=maximum(x,y)
407 self.assertTrue(isinstance(z, Basic), "wrong type of result")
408 xx=RandomData((4,4), FunctionSpace())
409 yy=RandomData((4,4), FunctionSpace())
410 ref=maximum(xx,yy)
411 res=Evaluator(z)(x=xx, y=yy)
412 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
413 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
414 def test_minimum_Symbol(self):
415 x,y=symbols('xy')
416 z=minimum(x,y)
417 self.assertTrue(isinstance(z, Basic), "wrong type of result")
418 xx=RandomData((4,4), FunctionSpace())
419 yy=RandomData((4,4), FunctionSpace())
420 ref=minimum(xx,yy)
421 res=Evaluator(z)(x=xx, y=yy)
422 self.assertAlmostEqual(Lsup(res-ref), 0.0, self.TOL_DIGITS, "wrong result")
423
424
425 if __name__ == "__main__":
426 import sys
427 suite = unittest.TestSuite()
428 suite.addTest(unittest.makeSuite(SymbolicTestCase))
429 s=unittest.TextTestRunner(verbosity=2).run(suite)
430 if not s.wasSuccessful(): sys.exit(1)
431

  ViewVC Help
Powered by ViewVC 1.1.26