Debian Codegraph @

test/unit/symbolics/test_float_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 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 test_Float_Operators():
33 "Test binary operators"
34
35 f0 = FloatValue(0.0)
36 f2 = FloatValue(2.0)
37 f3 = FloatValue(3.0)
38 fm1 = FloatValue(-1.0)
39 fm3 = FloatValue(-3.0)
40
41 x = Symbol("x", GEO)
42 y = Symbol("y", GEO)
43 z = Symbol("z", GEO)
44
45 p0 = Product([f2, x])
46 p1 = Product([x, y])
47 p2 = Product([f2, z])
48 p3 = Product([y, x, z])
49 p4 = Product([fm1, f2, x])
50
51 S0 = Sum([p0, fm3])
52 S1 = Sum([x, y])
53 S2 = Sum([S1, fm3])
54 S3 = Sum([p4, fm3])
55 S4 = Sum([fm3, Product([fm1, Sum([x, y])])])
56
57 F0 = Fraction(f2, y)
58 F1 = Fraction(FloatValue(-1.5), x)
59 F2 = Fraction(fm3, S1)
60
61 SF0 = Sum([f3, F1])
62 SF1 = Sum([f3, Product([fm1, F1])])
63
64 # Test FloatValue '+'
65 assert str(f2 + fm3) == str(fm1)
66 assert str(f2 + fm3 + fm3 + f2 + f2) == str(f0)
67 assert str(f0 + p0) == str(p0)
68 assert str(fm3 + p0) == str(S0)
69 assert str(fm3 + S1) == str(S2)
70 assert str(f3 + F1) == str(SF0)
71
72 # Test FloatValue '-'
73 assert str(f2 - fm3) == str(FloatValue(5))
74 assert str(f0 - p0) == str(p4)
75 assert str(fm3 - p0) == str(S3)
76 assert str(fm3 - S1) == str(S4)
77 assert str(f3 - F1) == str(SF1)
78
79 # Test FloatValue '*', only need one because all other cases are
80 # handled by 'other'
81 assert str(f2 * f2) == '%s' % format["float"](4)
82
83 # Test FloatValue '/'
84 assert str(fm3 / f2) == str(FloatValue(-1.5))
85 assert str(f2 / y) == str(F0)
86 assert str(fm3 / p0) == str(F1)
87 assert str(fm3 / S1) == str(F2)
88
89 # Silence output
90 push_level(CRITICAL)
91 with pytest.raises(Exception):
92 truediv(f2, F0)
93 with pytest.raises(Exception):
94 truediv(f2, f0)
95 with pytest.raises(Exception):
96 truediv(f2, Product([f0, y]))
97 pop_level()