| 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 testSymbolOperators(): |
| 33 |
"Test binary operators" |
| 34 |
|
| 35 |
f_0 = format["float"](0) |
| 36 |
f_1 = format["float"](1) |
| 37 |
f_2 = format["float"](2) |
| 38 |
f_3 = format["float"](3) |
| 39 |
f_0_5 = format["float"](0.5) |
| 40 |
f0 = FloatValue(0.0) |
| 41 |
f2 = FloatValue(2.0) |
| 42 |
fm1 = FloatValue(-1.0) |
| 43 |
fm3 = FloatValue(-3.0) |
| 44 |
|
| 45 |
x = Symbol("x", GEO) |
| 46 |
y = Symbol("y", GEO) |
| 47 |
z = Symbol("z", GEO) |
| 48 |
|
| 49 |
p0 = Product([f2, x]) |
| 50 |
p1 = Product([x, y]) |
| 51 |
p2 = Product([f2, z]) |
| 52 |
p3 = Product([y, x, z]) |
| 53 |
|
| 54 |
S0 = Sum([x, y]) |
| 55 |
S1 = Sum([x, z]) |
| 56 |
|
| 57 |
F0 = Fraction(f2, y) |
| 58 |
F1 = Fraction(x, y) |
| 59 |
F2 = Fraction(x, S0) |
| 60 |
F3 = Fraction(x, y) |
| 61 |
F4 = Fraction(p0, y) |
| 62 |
F5 = Fraction(fm3, y) |
| 63 |
|
| 64 |
# Test Symbol '+' |
| 65 |
assert str(x + f2) == '(%s + x)' % f_2 |
| 66 |
assert str(x + x) == '%s*x' % f_2 |
| 67 |
assert str(x + y) == '(x + y)' |
| 68 |
assert str(x + p0) == '%s*x' % f_3 |
| 69 |
assert str(x + p1) == '(x + x*y)' |
| 70 |
assert str(x + S0) == '(x + x + y)' |
| 71 |
assert str(x + F0) == '(x + %s/y)' % f_2 |
| 72 |
|
| 73 |
# Test Symbol '-' |
| 74 |
assert str(x - f2) == '(x-%s)' % f_2 |
| 75 |
assert str(x - x) == '%s' % f_0 |
| 76 |
assert str(x - y) == '(x - y)' |
| 77 |
assert str(x - p0) == ' - x' |
| 78 |
assert str(x - p1) == '(x - x*y)' |
| 79 |
assert str(x - S0) == '(x - (x + y))' |
| 80 |
assert str(x - F5) == '(x - -%s/y)' % f_3 |
| 81 |
|
| 82 |
# Test Symbol '*', only need to test float, symbol and product. Sum and |
| 83 |
# fraction are handled by 'other' |
| 84 |
assert str(x * f2) == '%s*x' % f_2 |
| 85 |
assert str(x * y) == 'x*y' |
| 86 |
assert str(x * p1) == 'x*x*y' |
| 87 |
|
| 88 |
# Test Symbol '/' |
| 89 |
assert str(x / f2) == '%s*x' % f_0_5 |
| 90 |
assert str(x / x) == '%s' % f_1 |
| 91 |
assert str(x / y) == 'x/y' |
| 92 |
assert str(x / S0) == 'x/(x + y)' |
| 93 |
assert str(x / p0) == '%s' % f_0_5 |
| 94 |
assert str(y / p1) == '%s/x' % f_1 |
| 95 |
assert str(z / p0) == '%s*z/x' % f_0_5 |
| 96 |
assert str(z / p1) == 'z/(x*y)' |
| 97 |
with pytest.raises(Exception): |
| 98 |
truediv(x, F0) |
| 99 |
with pytest.raises(Exception): |
| 100 |
truediv(y, FloatValue(0)) |