Debian Codegraph @

test/unit/symbolics/test_sum_operators.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.reduce_operations import operation_count, expand_operations, reduce_operations
24 from ffc.quadrature.symbolics import *
25 from ffc.quadrature.sumobj import _group_fractions
26 from ffc.quadrature.cpp import format, set_float_formatting
27 from ffc.parameters import FFC_PARAMETERS
28 set_float_formatting(FFC_PARAMETERS['precision'])
29 from ffc.log import error, push_level, pop_level, CRITICAL
30
31
32 def testSumOperators():
33 "Test binary operators"
34
35 f_0_5 = format["float"](0.5)
36 f_1 = format["float"](1)
37 f_2 = format["float"](2)
38 f_3 = format["float"](3)
39 f_6 = format["float"](6)
40 f2 = FloatValue(2.0)
41 fm3 = FloatValue(-3.0)
42
43 x = Symbol("x", GEO)
44 y = Symbol("y", GEO)
45 z = Symbol("z", GEO)
46
47 p0 = Product([f2, x])
48 p1 = Product([x, y])
49
50 S0 = Sum([x, y])
51 S1 = Sum([x, z])
52
53 F0 = Fraction(p0, y)
54
55 # Test Sum '+'
56 assert str(S0 + f2) == '(%s + x + y)' % f_2
57 assert str(S0 + x) == '(x + x + y)'
58 assert str(S0 + p0) == '(x + y + %s*x)' % f_2
59 assert str(S0 + S0) == '(x + x + y + y)'
60 assert str(S0 + F0) == '(x + y + %s*x/y)' % f_2
61
62 # Test Sum '-'
63 assert str(S0 - f2) == '(x + y-%s)' % f_2
64 assert str(S0 - fm3) == '(x + y + %s)' % f_3
65 assert str(S0 - x) == '(x + y - x)'
66 assert str(S0 - p0) == '(x + y-%s*x)' % f_2
67 assert str(S0 - Product([fm3, p0])) == '(x + y + %s*x)' % f_6
68 assert str(S0 - S0) == '(x + y - (x + y))'
69 assert str(S0 - F0) == '(x + y - %s*x/y)' % f_2
70
71 # Test Sum '*'
72 assert str(S0 * f2) == '(%s*x + %s*y)' % (f_2, f_2)
73 assert str(S0 * x) == '(x*x + x*y)'
74 assert str(S0 * p0) == '(%s*x*x + %s*x*y)' % (f_2, f_2)
75 assert str(S0 * S0) == '(%s*x*y + x*x + y*y)' % f_2
76 assert str(S0 * F0) == '(%s*x + %s*x*x/y)' % (f_2, f_2)
77
78 # Test Sum '/'
79 assert str(S0 / f2) == '(%s*x + %s*y)' % (f_0_5, f_0_5)
80 assert str(S0 / x) == '(%s + y/x)' % f_1
81 assert str(S0 / p0) == '(%s + %s*y/x)' % (f_0_5, f_0_5)
82 assert str(S0 / p1) == '(%s/x + %s/y)' % (f_1, f_1)
83 assert str(S0 / S0) == '(x + y)/(x + y)'
84 assert str(S0 / S1) == '(x + y)/(x + z)'
85
86 with pytest.raises(Exception):
87 truediv(S0, FloatValue(0))
88 with pytest.raises(Exception):
89 truediv(S0, F0)