Skip to content

Commit

Permalink
Merged in amcrae/ufl/amcrae/power-est-fix (pull request #75)
Browse files Browse the repository at this point in the history
Improve degree estimation for power

Approved-by: Miklós Homolya <m.homolya14@imperial.ac.uk>
Approved-by: Martin Sandve Alnæs <martinal@simula.no>
  • Loading branch information
Andrew McRae authored and miklos1 committed Jul 20, 2017
2 parents 2c63469 + 1f7b23a commit 89a0b92
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions ufl/algorithms/estimate_degrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ufl.algorithms.multifunction import MultiFunction
from ufl.corealg.map_dag import map_expr_dags
from ufl.checks import is_cellwise_constant
from ufl.constantvalue import IntValue


class IrreducibleInt(int):
Expand Down Expand Up @@ -221,25 +222,23 @@ def division(self, v, *ops):
return self._add_degrees(v, *ops)

def power(self, v, a, b):
"""If b is an integer:
"""If b is a positive integer:
degree(a**b) == degree(a)*b
otherwise use the heuristic
degree(a**b) == degree(a)*2"""
degree(a**b) == degree(a) + 2"""
f, g = v.ufl_operands
try:
gi = abs(int(g))
if isinstance(a, int):
return a*gi
else:
return tuple(foo*gi for foo in a)
except:
pass
# Something to a non-integer power, this is just a heuristic
# with no background
if isinstance(a, int):
return a*2
else:
return tuple(foo*2 for foo in a)

if isinstance(g, IntValue):
gi = g.value()
if gi >= 0:
if isinstance(a, int):
return a*gi
else:
return tuple(foo*gi for foo in a)

# Something to a non-(positive integer) power, e.g. float,
# negative integer, Coefficient, etc.
return self._add_degrees(v, a, 2)

def atan_2(self, v, a, b):
"""Using the heuristic
Expand Down

0 comments on commit 89a0b92

Please sign in to comment.