| 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 testFractionOperators(): |
| 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_5 = format["float"](5) |
| 39 |
|
| 40 |
f2 = FloatValue(2.0) |
| 41 |
fm3 = FloatValue(-3.0) |
| 42 |
|
| 43 |
x = Symbol("x", GEO) |
| 44 |
y = Symbol("y", GEO) |
| 45 |
|
| 46 |
p0 = Product([f2, x]) |
| 47 |
p1 = Product([x, y]) |
| 48 |
|
| 49 |
S0 = Sum([x, y]) |
| 50 |
|
| 51 |
F0 = Fraction(f2, y) |
| 52 |
F1 = Fraction(x, y) |
| 53 |
F2 = Fraction(x, S0) |
| 54 |
F3 = Fraction(x, y) |
| 55 |
F4 = Fraction(p0, y) |
| 56 |
F5 = Fraction(Product([fm3, x]), y) |
| 57 |
|
| 58 |
# Test Fraction '+' |
| 59 |
assert str(F0 + f2) == '(%s + %s/y)' % (f_2, f_2) |
| 60 |
assert str(F1 + x) == '(x + x/y)' |
| 61 |
assert str(F1 + p0) == '(%s*x + x/y)' % f_2 |
| 62 |
assert str(F1 + S0) == '(x + y + x/y)' |
| 63 |
assert str(F1 + F3) == '%s*x/y' % f_2 |
| 64 |
assert str(F0 + F1) == '(%s + x)/y' % f_2 |
| 65 |
assert str(F2 + F4) == '(%s*x/y + x/(x + y))' % f_2 |
| 66 |
|
| 67 |
# Test Fraction '-' |
| 68 |
assert str(F0 - f2) == '(%s/y-%s)' % (f_2, f_2) |
| 69 |
assert str(F1 - x) == '(x/y - x)' |
| 70 |
assert str(F1 - p0) == '(x/y-%s*x)' % f_2 |
| 71 |
assert str(F1 - S0) == '(x/y - (x + y))' |
| 72 |
assert str(F1 - F3) == '%s' % f_0 |
| 73 |
assert str(F4 - F1) == 'x/y' |
| 74 |
assert str(F4 - F5) == '%s*x/y' % f_5 |
| 75 |
assert str(F0 - F1) == '(%s - x)/y' % f_2 |
| 76 |
assert str(F2 - F4) == '(x/(x + y) - %s*x/y)' % f_2 |
| 77 |
|
| 78 |
# Test Fraction '*' |
| 79 |
assert str(F1 * f2) == '%s*x/y' % f_2 |
| 80 |
assert str(F1 * x) == 'x*x/y' |
| 81 |
assert str(F1 * p1) == 'x*x' |
| 82 |
assert str(F1 * S0) == '(x + x*x/y)' |
| 83 |
assert repr(F1 * S0) == repr(Sum([x, Fraction(Product([x, x]), y)])) |
| 84 |
assert str(F1 * F0) == '%s*x/(y*y)' % f_2 |
| 85 |
|
| 86 |
# Test Fraction '/' |
| 87 |
assert str(F0 / f2) == '%s/y' % f_1 |
| 88 |
assert str(F1 / x) == '%s/y' % f_1 |
| 89 |
assert str(F4 / p1) == '%s/(y*y)' % f_2 |
| 90 |
assert str(F4 / x) == '%s/y' % f_2 |
| 91 |
assert str(F2 / y) == 'x/(x*y + y*y)' |
| 92 |
assert str(F0 / S0) == '%s/(x*y + y*y)' % f_2 |
| 93 |
|
| 94 |
with pytest.raises(Exception): |
| 95 |
truediv(F0 / F0) |