| 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.symbolics import * |
| 24 |
from ffc.quadrature.sumobj import _group_fractions |
| 25 |
from ffc.quadrature.cpp import format, set_float_formatting |
| 26 |
from ffc.parameters import FFC_PARAMETERS |
| 27 |
set_float_formatting(FFC_PARAMETERS['precision']) |
| 28 |
|
| 29 |
|
| 30 |
def testNotFinished(): |
| 31 |
"Stuff that would be nice to implement." |
| 32 |
|
| 33 |
f_1 = format["float"](1) |
| 34 |
f_2 = format["float"](2) |
| 35 |
f_4 = format["float"](4) |
| 36 |
f_8 = format["float"](8) |
| 37 |
|
| 38 |
f0 = FloatValue(4) |
| 39 |
f1 = FloatValue(2) |
| 40 |
f2 = FloatValue(8) |
| 41 |
s0 = Symbol("x", GEO) |
| 42 |
s1 = Symbol("y", GEO) |
| 43 |
s2 = Symbol("z", GEO) |
| 44 |
a = Symbol("a", GEO) |
| 45 |
b = Symbol("b", GEO) |
| 46 |
c = Symbol("c", GEO) |
| 47 |
|
| 48 |
# Aux. expressions |
| 49 |
p0 = Product([f1, s0]) |
| 50 |
p1 = Product([f2, s1]) |
| 51 |
p2 = Product([s0, s1]) |
| 52 |
|
| 53 |
F0 = Fraction(f0, s0) |
| 54 |
|
| 55 |
S0 = Sum([p0, p1]) |
| 56 |
S1 = Sum([s0, p2]) |
| 57 |
S2 = Sum([FloatValue(1), s1]) |
| 58 |
S3 = Sum([F0, F0]) |
| 59 |
|
| 60 |
# Thing to be implemented |
| 61 |
e0 = f0 / S0 |
| 62 |
e1 = s0 / S1 |
| 63 |
e2 = S2 / S1 |
| 64 |
e3 = _group_fractions(S3) |
| 65 |
e4 = Sum([Fraction(f1 * s0, a * b * c), Fraction(s0, a * b)]).expand().reduce_ops() |
| 66 |
|
| 67 |
# Tests that pass the current implementation |
| 68 |
assert str(e0) == '%s/(%s*x + %s*y)' % (f_4, f_2, f_8) |
| 69 |
assert str(e1) == 'x/(x + x*y)' |
| 70 |
assert str(e2) == '(%s + y)/(x + x*y)' % f_1 |
| 71 |
assert str(e3) == '%s/x' % f_8 |
| 72 |
assert str(e4) == 'x*(%s/(a*b) + %s/(a*b*c))' % (f_1, f_2) |
| 73 |
|
| 74 |
# Tests that should pass in future implementations (change NotEqual to Equal) |
| 75 |
assert str(e0) != '%s/(x + %s*y)' % (f_2, f_4) |
| 76 |
assert str(e1) != '%s/(%s + y)' % (f_1, f_1) |
| 77 |
assert str(e2) != '%s/x' % f_1 |
| 78 |
assert str(e4) != 'x*(%s/c + %s)/(a*b)' % (f_2, f_1) |
| 79 |
|
| 80 |
# TODO: Would it be a good idea to reduce expressions |
| 81 |
# wrt. var_type without first expanding? |
| 82 |
E0 = Product([Sum([Product([Symbol('B0', BASIS), Product([Symbol('B1', BASIS), Sum([s0]), Sum([s0])])]), |
| 83 |
Product([Symbol('B0', BASIS), Symbol('B1', BASIS)])])]) |
| 84 |
Er0 = E0.reduce_vartype(BASIS) |
| 85 |
Ex0 = E0.expand().reduce_vartype(BASIS) |
| 86 |
assert Ex0[0][1] != Er0[0][1].expand() |
| 87 |
|
| 88 |
# Both of these reductions should work at the same time |
| 89 |
# 1) 2/(x/(a+b) + y/(a+b)) --> 2(a+b)/(x+y) |
| 90 |
# 2) 2/(x + y/(a+b)) --> no reduction, or if divisions are more expensive |
| 91 |
# 3) 2/(x + y/(a+b)) --> 2(a+b)/((a+b)x + y) |