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