| 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.cpp import format, set_float_formatting |
| 25 |
from ffc.parameters import FFC_PARAMETERS |
| 26 |
set_float_formatting(FFC_PARAMETERS['precision']) |
| 27 |
|
| 28 |
|
| 29 |
def testExpandOperations(): |
| 30 |
f0 = FloatValue(-1) |
| 31 |
f1 = FloatValue(2) |
| 32 |
f2 = FloatValue(1) |
| 33 |
sx = Symbol("x", GEO) |
| 34 |
sy = Symbol("y", GEO) |
| 35 |
sz = Symbol("z", GEO) |
| 36 |
s0 = Product([FloatValue(-1), Symbol("x", GEO)]) |
| 37 |
s1 = Symbol("y", GEO) |
| 38 |
s2 = Product([FloatValue(5), Symbol("z", IP)]) |
| 39 |
s3 = Product([FloatValue(-4), Symbol("z", GEO)]) |
| 40 |
|
| 41 |
# Random variable values |
| 42 |
x = 2.2 |
| 43 |
y = -0.2 |
| 44 |
z = 1.1 |
| 45 |
|
| 46 |
# Aux. expressions |
| 47 |
P0 = Product([s2, s1]) |
| 48 |
P1 = Product([P0, s0]) |
| 49 |
P2 = Product([P1, s1, P0]) |
| 50 |
P3 = Product([P1, P2]) |
| 51 |
|
| 52 |
S0 = Sum([s2, s1]) |
| 53 |
S1 = Sum([S0, s0]) |
| 54 |
S2 = Sum([S1, s1, S0]) |
| 55 |
S3 = Sum([S1, S2]) |
| 56 |
|
| 57 |
F0 = Fraction(s2, s1) |
| 58 |
F1 = Fraction(F0, s0) |
| 59 |
F2 = Fraction(F1, F0) |
| 60 |
F3 = Fraction(F1, F2) |
| 61 |
|
| 62 |
# Special fractions |
| 63 |
F4 = Fraction(P0, F0) |
| 64 |
F5 = Fraction(Fraction(s0, P0), P0) |
| 65 |
F6 = Fraction(Fraction(Fraction(s1, s0), Fraction(s1, s2)), Fraction(Fraction(s2, s0), Fraction(s1, s0))) |
| 66 |
F7 = Fraction(s1, Product([s1, Symbol("x", GEO)])) |
| 67 |
F8 = Fraction(Sum([sx, Fraction(sy, sx)]), FloatValue(2)) |
| 68 |
|
| 69 |
F4x = F4.expand() |
| 70 |
F5x = F5.expand() |
| 71 |
F6x = F6.expand() |
| 72 |
F7x = F7.expand() |
| 73 |
F8x = F8.expand() |
| 74 |
|
| 75 |
assert round(eval(str(F4)) - eval(str(F4x)), 10) == 0.0 |
| 76 |
assert round(eval(str(F5)) - eval(str(F5x)), 10) == 0.0 |
| 77 |
assert round(eval(str(F6)) - eval(str(F6x)), 10) == 0.0 |
| 78 |
assert round(eval(str(F7)) - eval(str(F7x)), 10) == 0.0 |
| 79 |
assert round(eval(str(F8)) - eval(str(F8x)), 10) == 0.0 |
| 80 |
|
| 81 |
assert F4.ops() == 5 |
| 82 |
assert F4x.ops() == 1 |
| 83 |
assert F5.ops() == 6 |
| 84 |
assert F5x.ops() == 5 |
| 85 |
assert F6.ops() == 9 |
| 86 |
assert F6x.ops() == 1 |
| 87 |
assert F7.ops() == 2 |
| 88 |
assert F7x.ops() == 1 |
| 89 |
assert F8.ops() == 3 |
| 90 |
assert F8x.ops() == 4 |
| 91 |
|
| 92 |
# Expressions that should be expanded |
| 93 |
e0 = Product([P3, F2]) |
| 94 |
e1 = Product([S3, P2]) |
| 95 |
e2 = Product([F3, S1]) |
| 96 |
|
| 97 |
e3 = Sum([P3, F2]) |
| 98 |
e4 = Sum([S3, P2]) |
| 99 |
e5 = Sum([F3, S1]) |
| 100 |
|
| 101 |
e6 = Fraction(P3, F2) |
| 102 |
e7 = Fraction(S3, P2) |
| 103 |
e8 = Fraction(F3, S1) |
| 104 |
e9 = Fraction(S0, s0) |
| 105 |
|
| 106 |
e0x = e0.expand() |
| 107 |
e1x = e1.expand() |
| 108 |
e2x = e2.expand() |
| 109 |
e3x = e3.expand() |
| 110 |
e4x = e4.expand() |
| 111 |
e5x = e5.expand() |
| 112 |
e6x = e6.expand() |
| 113 |
e7x = e7.expand() |
| 114 |
e8x = e8.expand() |
| 115 |
e9x = e9.expand() |
| 116 |
|
| 117 |
assert round(eval(str(e0)) - eval(str(e0x)), 10) == 0.0 |
| 118 |
assert round(eval(str(e1)) - eval(str(e1x)), 10) == 0.0 |
| 119 |
assert round(eval(str(e2)) - eval(str(e2x)), 10) == 0.0 |
| 120 |
assert round(eval(str(e3)) - eval(str(e3x)), 10) == 0.0 |
| 121 |
assert round(eval(str(e4)) - eval(str(e4x)), 10) == 0.0 |
| 122 |
assert round(eval(str(e5)) - eval(str(e5x)), 10) == 0.0 |
| 123 |
assert round(eval(str(e6)) - eval(str(e6x)), 10) == 0.0 |
| 124 |
assert round(eval(str(e7)) - eval(str(e7x)), 10) == 0.0 |
| 125 |
assert round(eval(str(e8)) - eval(str(e8x)), 10) == 0.0 |
| 126 |
assert round(eval(str(e9)) - eval(str(e9x)), 10) == 0.0 |
| 127 |
|
| 128 |
assert e0.ops() == 16 |
| 129 |
assert e0x.ops() == 8 |
| 130 |
assert e1.ops() == 18 |
| 131 |
assert e1x.ops() == 23 |
| 132 |
assert e2.ops() == 14 |
| 133 |
assert e2x.ops() == 9 |
| 134 |
assert e3.ops() == 16 |
| 135 |
assert e3x.ops() == 11 |
| 136 |
assert e4.ops() == 18 |
| 137 |
assert e4x.ops() == 12 |
| 138 |
assert e5.ops() == 14 |
| 139 |
assert e5x.ops() == 6 |
| 140 |
assert e6.ops() == 16 |
| 141 |
assert e6x.ops() == 10 |
| 142 |
assert e7.ops() == 18 |
| 143 |
assert e7x.ops() == 17 |
| 144 |
assert e8.ops() == 14 |
| 145 |
assert e8x.ops() == 8 |
| 146 |
assert e9.ops() == 3 |
| 147 |
assert e9x.ops() == 4 |
| 148 |
|
| 149 |
# More expressions (from old expand tests) |
| 150 |
PF = Product([F0, F1]) |
| 151 |
E0 = Product([s1, f0, S1]) |
| 152 |
E1 = Sum([P0, E0]) |
| 153 |
E2 = Fraction(Sum([Product([f1])]), f2) |
| 154 |
E3 = Sum([F0, F0]) |
| 155 |
E4 = Product([Sum([Product([sx, Sum([sy, Product([Sum([sy, Product([sy, sz]), sy])]), sy])]), |
| 156 |
Product([sx, Sum([Product([sy, sz]), sy])])])]) |
| 157 |
P4 = Product([s1, Sum([s0, s1])]) |
| 158 |
P5 = Product([s0, E0]) |
| 159 |
P6 = Product([s1]) |
| 160 |
S4 = Sum([s1]) |
| 161 |
|
| 162 |
# Create 'real' term that caused me trouble |
| 163 |
P00 = Product([Symbol("Jinv_00", GEO)] * 2) |
| 164 |
P01 = Product([Symbol("Jinv_01", GEO)] * 2) |
| 165 |
P20 = Product([Symbol("Jinv_00", GEO), |
| 166 |
Product([f1, Symbol("Jinv_20", GEO)])]) |
| 167 |
P21 = Product([Symbol("Jinv_01", GEO), |
| 168 |
Product([f1, Symbol("Jinv_21", GEO)])]) |
| 169 |
PS0 = Product([Symbol("Jinv_22", GEO), |
| 170 |
Sum([P00, P01])]) |
| 171 |
PS1 = Product([Product([f0, Symbol("Jinv_02", GEO)]), |
| 172 |
Sum([P20, P21])]) |
| 173 |
SP = Sum([PS0, PS1]) |
| 174 |
|
| 175 |
PFx = PF.expand() |
| 176 |
E0x = E0.expand() |
| 177 |
E1x = E1.expand() |
| 178 |
E2x = E2.expand() |
| 179 |
E3x = E3.expand() |
| 180 |
E4x = E4.expand() |
| 181 |
P4x = P4.expand() |
| 182 |
P5x = P5.expand() |
| 183 |
P6x = P6.expand() |
| 184 |
S4x = S4.expand() |
| 185 |
SPx = SP.expand() |
| 186 |
|
| 187 |
Jinv_00, Jinv_01, Jinv_10, Jinv_02, Jinv_20, Jinv_22, Jinv_21, W1, det = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 188 |
|
| 189 |
assert round(eval(str(SP)) - eval(str(SPx)), 10) == 0.0 |
| 190 |
assert round(eval(str(E0)) - eval(str(E0x)), 10) == 0.0 |
| 191 |
assert round(eval(str(E1)) - eval(str(E1x)), 10) == 0.0 |
| 192 |
assert round(eval(str(E2)) - eval(str(E2x)), 10) == 0.0 |
| 193 |
assert round(eval(str(E3)) - eval(str(E3x)), 10) == 0.0 |
| 194 |
assert round(eval(str(E4)) - eval(str(E4x)), 10) == 0.0 |
| 195 |
assert round(eval(str(SP)) - eval(str(SPx)), 10) == 0.0 |
| 196 |
assert round(eval(str(P4)) - eval(str(P4x)), 10) == 0.0 |
| 197 |
assert round(eval(str(P5)) - eval(str(P5x)), 10) == 0.0 |
| 198 |
assert P6x == s1 |
| 199 |
assert S4x == s1 |
| 200 |
assert PF.ops() == 6 |
| 201 |
assert PFx.ops() == 5 |
| 202 |
assert E0.ops() == 4 |
| 203 |
assert E0x.ops() == 6 |
| 204 |
assert E1.ops() == 7 |
| 205 |
assert E1x.ops() == 3 |
| 206 |
assert E2.ops() == 1 |
| 207 |
assert E2x.ops() == 0 |
| 208 |
assert E3.ops() == 5 |
| 209 |
assert E3x.ops() == 5 |
| 210 |
assert E4.ops() == 10 |
| 211 |
assert E4x.ops() == 6 |
| 212 |
assert SP.ops() == 11 |
| 213 |
assert SPx.ops() == 13 |
| 214 |
assert P4.ops() == 2 |
| 215 |
assert P4x.ops() == 3 |
| 216 |
assert P5.ops() == 5 |
| 217 |
assert P5x.ops() == 9 |