Debian Codegraph @

test/unit/symbolics/test_product.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010 Kristian B. Oelgaard
3 #
4 # This file is part of FFC.
5 #
6 # FFC is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Lesser General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # FFC is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with FFC. If not, see <http://www.gnu.org/licenses/>.
18
19 import pytest
20 import time
21
22 # FFC modules
23 from ffc.quadrature.symbolics import *
24 from ffc.quadrature.cpp import format, set_float_formatting
25 from ffc.parameters import FFC_PARAMETERS
26 set_float_formatting(FFC_PARAMETERS['precision'])
27
28
29 def testProduct():
30 "Test simple product instance."
31
32 f_0 = format["float"](0)
33 f_1 = format["float"](1)
34 f0 = FloatValue(-2.0)
35 f1 = FloatValue(3.0)
36 f2 = FloatValue(0)
37 f3 = FloatValue(-1)
38 f4 = FloatValue(1)
39 f5 = FloatValue(-0.5)
40 f6 = FloatValue(2.0)
41 s0 = Symbol("x", BASIS)
42 s1 = Symbol("y", GEO)
43 s2 = Symbol("z", GEO)
44
45 p0 = Product([])
46 p1 = Product([s0])
47 p2 = Product([s0, s1])
48 p3 = Product([f1, s0, s1])
49 p4 = Product([s0, f2, s2])
50 p5 = Product([s0, f0, s1, f1, s2])
51 p6 = Product([s0, f3, s1])
52 p7 = Product([s0, f4, s1]).expand().reduce_ops()
53 p8 = Product([s0, f0, s2, f5])
54 p9 = Product([s0, s1])
55 p10 = Product([p0, p1])
56 p11 = Product([f5, f0])
57 p12 = Product([f6, f5])
58 p13 = Product([f6, f5]).expand()
59 p14 = Product([f1, f2])
60 p_tmp = Product([f1])
61 p_tmp.expand()
62 p15 = Product([p_tmp, s0])
63
64 assert repr(p0) == "Product([FloatValue(%s)])" % f_0
65 assert repr(p1) == "Product([Symbol('x', BASIS)])"
66 assert repr(p3) == "Product([FloatValue(%s), Symbol('x', BASIS), Symbol('y', GEO)])"\
67 % format["float"](3)
68 assert repr(p6) == "Product([FloatValue(-%s), Symbol('x', BASIS), Symbol('y', GEO)])" % f_1
69 assert repr(p7) == "Product([Symbol('x', BASIS), Symbol('y', GEO)])"
70 assert repr(p8) == "Product([Symbol('x', BASIS), Symbol('z', GEO)])"
71 assert str(p2) == 'x*y'
72 assert str(p4) == '%s' % f_0
73 assert str(p5) == '-%s*x*y*z' % format["float"](6)
74 assert str(p6) == ' - x*y'
75 assert str(p7) == 'x*y'
76 assert str(p8) == 'x*z'
77 assert str(p9) == 'x*y'
78 assert p0.val == 0
79 assert str(p10) == '%s' % f_0
80 assert str(p11) == '%s' % f_1
81 assert str(p12) == '-%s' % f_1
82 assert str(p13) == '-%s' % f_1
83 assert repr(p14) == "Product([FloatValue(%s)])" % f_0
84 assert repr(p14.expand()) == "FloatValue(%s)" % f_0
85
86 assert p1 == p1
87 assert p1 != p7
88 assert p4 != p3
89 assert p2 == p9
90 assert p2 != p3
91
92 assert p0.ops() == 0
93 assert p1.ops() == 0
94 assert p2.ops() == 1
95 assert p3.ops() == 2
96 assert p4.ops() == 0
97 assert p5.ops() == 3
98 assert p6.ops() == 1
99 assert p7.ops() == 1
100 assert p8.ops() == 1
101 assert p9.ops() == 1
102 assert p10.ops() == 0
103 assert p14.ops() == 0
104
105 # Test hash
106 ll = [p3]
107 d = {p3: 0}
108 p10 = Product([f1, s0, s1])
109
110 assert p3 in ll
111 assert p3 in d
112 assert p10 in ll
113 assert p10 in d
114 assert p2 not in ll
115 assert p2 not in d