Debian Codegraph @

test/unit/symbolics/test_reduce_operations.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 testReduceOperations():
30
31 f_1 = format["float"](1)
32 f_2 = format["float"](2)
33
34 # Aux. variables
35 f2 = FloatValue(2)
36 f0_5 = FloatValue(0.5)
37 f1 = FloatValue(1.0)
38 fm1 = FloatValue(-1.0)
39
40 x = Symbol("x", GEO)
41 y = Symbol("y", GEO)
42 z = Symbol("z", GEO)
43 a = Symbol("a", GEO)
44 b = Symbol("b", GEO)
45 c = Symbol("c", GEO)
46 d = Symbol("d", GEO)
47
48 # Simple expand and reduce simple float and symbol objects
49 fx2 = f2.expand()
50 xx = x.expand()
51
52 fr2 = fx2.reduce_ops()
53 xr = xx.reduce_ops()
54
55 assert f2 == fr2
56 assert x == xr
57
58 # Test product
59 p0 = f2 * x
60 p1 = y * x
61 p2 = x * f2 / y
62 p3 = x * Sum([x, y])
63
64 px0 = p0.expand()
65 px1 = p1.expand()
66
67 pr0 = px0.reduce_ops()
68 pr1 = px1.reduce_ops()
69
70 assert p0 == pr0
71 assert p1 == pr1
72
73 # Test fraction
74 F0 = Fraction(p0, y)
75 F1 = Fraction(x, p0)
76 F2 = Fraction(p0, p1)
77 F3 = Fraction(Sum([x * x, x * y]), y)
78 F4 = Fraction(Sum([f2 * x, x * y]), a)
79
80 Fx0 = F0.expand()
81 Fx1 = F1.expand()
82 Fx2 = F2.expand()
83 Fx3 = F3.expand()
84 Fx4 = F4.expand()
85
86 Fr0 = Fx0.reduce_ops()
87 Fr1 = Fx1.reduce_ops()
88 Fr2 = Fx2.reduce_ops()
89 Fr3 = Fx3.reduce_ops()
90 Fr4 = Fx4.reduce_ops()
91
92 assert Fr0 == F0
93 assert Fr1 == f0_5
94 assert Fr2 == Fraction(f2, y)
95 assert str(Fr3) == "x*(%s + x/y)" % f_1
96 assert str(Fr4) == "x*(%s + y)/a" % f_2
97
98 # Test sum
99 # TODO: Here we might have to add additional tests
100 S0 = Sum([x, y])
101 S1 = Sum([p0, p1])
102 S2 = Sum([x, p1])
103 S3 = Sum([p0, f2 * y])
104 S4 = Sum([f2 * p1, z * p1])
105 S5 = Sum([x, x * x, x * x * x])
106 S6 = Sum([a * x * x, b * x * x * x, c * x * x, d * x * x * x])
107 S7 = Sum([p0, p1, x * x, f2 * z, y * z])
108 S8 = Sum([a * y, b * y, x * x * x * y, x * x * x * z])
109 S9 = Sum([a * y, b * y, c * y, x * x * x * y, f2 * x * x, x * x * x * z])
110 S10 = Sum([f2 * x * x * y, x * x * y * z])
111 S11 = Sum([f2 * x * x * y * y, x * x * y * y * z])
112 S12 = Sum([f2 * x * x * y * y, x * x * y * y * z, a * z, b * z, c * z])
113 S13 = Sum([Fraction(f1, x), Fraction(f1, y)])
114 S14 = Sum([Fraction(fm1, x), Fraction(fm1, y)])
115 S15 = Sum([Fraction(f2, x), Fraction(f2, x)])
116 S16 = Sum([Fraction(f2 * x, y * z), Fraction(f0_5, y * z)])
117 S17 = Sum([(f2 * x * y) / a, (x * y * z) / b])
118 S18 = Sum([(x * y) / a, (x * z) / a, f2 / a, (f2 * x * y) / a])
119 S19 = Sum([(f2 * x) / a, (x * y) / a, z * x])
120 S20 = Product([Sum([x, y]), Fraction(a, b), Fraction(Product([c, d]), z)])
121 S21 = Sum([a * x, b * x, c * x, x * y, x * z, f2 * y, a * y, b * y, f2 * z, a * z, b * z])
122 S22 = Sum([FloatValue(0.5) * x / y, FloatValue(-0.5) * x / y])
123 S23 = Sum([x * y * z, x * y * y * y * z * z * z, y * y * y * z * z * z * z, z * z * z * z * z])
124
125 Sx0 = S0.expand()
126 Sx1 = S1.expand()
127 Sx2 = S2.expand()
128 Sx3 = S3.expand()
129 Sx4 = S4.expand()
130 Sx5 = S5.expand()
131 Sx6 = S6.expand()
132 Sx7 = S7.expand()
133 Sx8 = S8.expand()
134 Sx9 = S9.expand()
135 Sx10 = S10.expand()
136 Sx11 = S11.expand()
137 Sx12 = S12.expand()
138 Sx13 = S13.expand()
139 Sx14 = S14.expand()
140 Sx15 = S15.expand()
141 Sx16 = S16.expand()
142 Sx17 = S17.expand()
143 Sx18 = S18.expand()
144 Sx19 = S19.expand()
145 Sx20 = S20.expand()
146 Sx21 = S21.expand()
147 Sx22 = S22.expand()
148 Sx23 = S23.expand()
149
150 Sr0 = Sx0.reduce_ops()
151 Sr1 = Sx1.reduce_ops()
152 Sr2 = Sx2.reduce_ops()
153 Sr3 = Sx3.reduce_ops()
154 Sr4 = Sx4.reduce_ops()
155 Sr5 = Sx5.reduce_ops()
156 Sr6 = Sx6.reduce_ops()
157 Sr7 = Sx7.reduce_ops()
158 Sr8 = Sx8.reduce_ops()
159 Sr9 = Sx9.reduce_ops()
160 Sr10 = Sx10.reduce_ops()
161 Sr11 = Sx11.reduce_ops()
162 Sr12 = Sx12.reduce_ops()
163 Sr13 = Sx13.reduce_ops()
164 Sr14 = Sx14.reduce_ops()
165 Sr15 = Sx15.reduce_ops()
166 Sr16 = Sx16.reduce_ops()
167 Sr17 = Sx17.reduce_ops()
168 Sr18 = Sx18.reduce_ops()
169 Sr19 = Sx19.reduce_ops()
170 Sr20 = Sx20.reduce_ops()
171 Sr21 = Sx21.reduce_ops()
172 Sr22 = Sx22.reduce_ops()
173 Sr23 = Sx23.reduce_ops()
174
175 assert Sr0 == S0
176 assert str(Sr1) == "x*(%s + y)" % f_2
177 # TODO: Should this be (x + x*y)?
178 assert str(Sr2) == "x*(%s + y)" % f_1
179 # assert str(Sr2), "(x + x*y)")
180 assert str(Sr3) == "%s*(x + y)" % f_2
181 assert str(Sr4) == "x*y*(%s + z)" % f_2
182 assert str(Sr5) == "x*(%s + x*(%s + x))" % (f_1, f_1)
183 assert str(Sr6) == "x*x*(a + c + x*(b + d))"
184 assert str(Sr7) == "(x*(%s + x + y) + z*(%s + y))" % (f_2, f_2)
185 assert str(Sr8) == "(x*x*x*(y + z) + y*(a + b))"
186 assert str(Sr9) == "(x*x*(%s + x*(y + z)) + y*(a + b + c))" % f_2
187 assert str(Sr10) == "x*x*y*(%s + z)" % f_2
188 assert str(Sr11) == "x*x*y*y*(%s + z)" % f_2
189 assert str(Sr12) == "(x*x*y*y*(%s + z) + z*(a + b + c))" % f_2
190 assert str(Sr13) == "(%s/x + %s/y)" % (f_1, f_1)
191 assert str(Sr14) == "(-%s/x-%s/y)" % (f_1, f_1)
192 assert str(Sr15) == "%s/x" % format["float"](4)
193 assert str(Sr16) == "(%s + %s*x)/(y*z)" % (format["float"](0.5), f_2)
194 assert str(Sr17) == "x*y*(%s/a + z/b)" % f_2
195 assert str(Sr18) == "(%s + x*(z + %s*y))/a" % (f_2, format["float"](3))
196 assert str(Sr19) == "x*(z + (%s + y)/a)" % f_2
197 assert str(Sr20) == "a*c*d*(x + y)/(b*z)"
198 assert str(Sr21) == "(x*(a + b + c + y + z) + y*(%s + a + b) + z*(%s + a + b))" % (f_2, f_2)
199 assert str(Sr22) == "%s" % format["float"](0)
200 assert str(Sr23) == "(x*y*z + z*z*z*(y*y*y*(x + z) + z*z))"
201
202 assert S0.ops() == 1
203 assert Sr0.ops() == 1
204 assert S1.ops() == 3
205 assert Sr1.ops() == 2
206 assert S2.ops() == 2
207 assert Sr2.ops() == 2
208 assert S3.ops() == 3
209 assert Sr3.ops() == 2
210 assert S4.ops() == 5
211 assert Sr4.ops() == 3
212 assert S5.ops() == 5
213 assert Sr5.ops() == 4
214 assert S6.ops() == 13
215 assert Sr6.ops() == 6
216 assert S7.ops() == 9
217 assert Sr7.ops() == 6
218 assert S8.ops() == 11
219 assert Sr8.ops() == 7
220 assert S9.ops() == 16
221 assert Sr9.ops() == 9
222 assert S10.ops() == 7
223 assert Sr10.ops() == 4
224 assert S11.ops() == 9
225 assert Sr11.ops() == 5
226 assert S12.ops() == 15
227 assert Sr12.ops() == 9
228 assert S13.ops() == 3
229 assert Sr13.ops() == 3
230 assert S14.ops() == 3
231 assert Sr14.ops() == 3
232 assert S15.ops() == 3
233 assert Sr15.ops() == 1
234 assert S16.ops() == 6
235 assert Sr16.ops() == 4
236 assert S17.ops() == 7
237 assert Sr17.ops() == 5
238 assert S18.ops() == 11
239 assert Sr18.ops() == 5
240 assert S19.ops() == 7
241 assert Sr19.ops() == 4
242 assert S20.ops() == 6
243 assert Sr20.ops() == 6
244 assert S21.ops() == 21
245 assert Sr21.ops() == 13
246 assert S23.ops() == 21
247 assert Sr23.ops() == 12