Skip to content

Commit

Permalink
Merge branch 'misc' into fix-18492
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Mar 20, 2022
2 parents e92fc13 + d73cbe0 commit 422f0b6
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 46 deletions.
7 changes: 5 additions & 2 deletions diofant/core/expr.py
Expand Up @@ -437,6 +437,8 @@ def is_constant(self, *wrt, **flags):
True
"""
from ..functions import Piecewise

simplify = flags.get('simplify', True)

# Except for expressions that contain units, only one of these should
Expand Down Expand Up @@ -512,15 +514,16 @@ def is_constant(self, *wrt, **flags):
deriv = expr.diff(w)
if simplify:
deriv = deriv.simplify()
if deriv != 0:
if deriv:
if not (deriv.is_Number or pure_complex(deriv)):
if flags.get('failing_number', False):
return failing_number
else:
assert deriv.free_symbols
return # dead line provided _random returns None in such cases
return False
return True
if not expr.has(Piecewise):
return True

def equals(self, other, failing_expression=False):
"""Return True if self == other, False if it doesn't, or None. If
Expand Down
3 changes: 0 additions & 3 deletions diofant/core/function.py
Expand Up @@ -29,7 +29,6 @@

import collections
import inspect
import typing

import mpmath
import mpmath.libmp as mlib
Expand Down Expand Up @@ -714,8 +713,6 @@ class WildFunction(Function, AtomicExpr):
"""

include: set[typing.Any] = set()

def __init__(self, name, **assumptions):
from ..sets.sets import FiniteSet, Set
self.name = name
Expand Down
12 changes: 3 additions & 9 deletions diofant/functions/elementary/piecewise.py
Expand Up @@ -285,7 +285,7 @@ def _sort_expr_cond(self, sym, a, b, targetcond=None):
independent_expr_cond = []
if isinstance(targetcond, Relational) and targetcond.has(sym):
targetcond = solve_univariate_inequality(targetcond, sym)
for expr, cond in self.args:
for expr, cond in self.args: # pragma: no branch
if isinstance(cond, Relational) and cond.has(sym):
cond = solve_univariate_inequality(cond, sym)
if isinstance(cond, Or):
Expand All @@ -295,7 +295,7 @@ def _sort_expr_cond(self, sym, a, b, targetcond=None):
expr_cond.append((expr, cond))
if cond == true:
break
for expr, cond in expr_cond:
for expr, cond in expr_cond: # pragma: no branch
if cond == true:
independent_expr_cond.append((expr, cond))
default = self.func(*independent_expr_cond)
Expand Down Expand Up @@ -407,10 +407,6 @@ def _sort_expr_cond(self, sym, a, b, targetcond=None):
int_expr.extend(holes)
if targetcond == true:
return [(h[0], h[1], None) for h in holes]
elif holes and default is None:
raise ValueError('Called interval evaluation over piecewise ' # noqa: SFS101
'function on undefined intervals %s' %
', '.join([str((h[0], h[1])) for h in holes]))

return int_expr

Expand All @@ -425,16 +421,14 @@ def _eval_power(self, other):
def _eval_subs(self, old, new):
"""Piecewise conditions may contain bool which are not of Basic type."""
args = list(self.args)
for i, (e, c) in enumerate(args):
for i, (e, c) in enumerate(args): # pragma: no branch
c = c._subs(old, new)
if c != false:
e = e._subs(old, new)
args[i] = e, c
if c == true:
return self.func(*args)

return self.func(*args)

def _eval_transpose(self):
return self.func(*[(e.transpose(), c) for e, c in self.args])

Expand Down
8 changes: 0 additions & 8 deletions diofant/printing/ccode.py
Expand Up @@ -181,14 +181,6 @@ def _print_NegativeInfinity(self, expr):
return '-HUGE_VAL'

def _print_Piecewise(self, expr):
if expr.args[-1].cond != true:
# We need the last conditional to be a True, otherwise the resulting
# function may not return a result.
raise ValueError('All Piecewise expressions must contain an '
'(expr, True) statement to be used as a default '
'condition. Without one, the generated '
'expression may not evaluate to anything under '
'some condition.')
lines = []
if expr.has(Assignment):
for i, (e, c) in enumerate(expr.args):
Expand Down
8 changes: 0 additions & 8 deletions diofant/printing/fcode.py
Expand Up @@ -130,14 +130,6 @@ def _get_loop_opening_ending(self, indices):
return open_lines, close_lines

def _print_Piecewise(self, expr):
if expr.args[-1].cond != true:
# We need the last conditional to be a True, otherwise the resulting
# function may not return a result.
raise ValueError('All Piecewise expressions must contain an '
'(expr, True) statement to be used as a default '
'condition. Without one, the generated '
'expression may not evaluate to anything under '
'some condition.')
lines = []
if expr.has(Assignment):
for i, (e, c) in enumerate(expr.args):
Expand Down
9 changes: 1 addition & 8 deletions diofant/printing/latex.py
Expand Up @@ -16,7 +16,6 @@
from ..core.function import _coeff_isneg
from ..core.operations import AssocOp
from ..core.relational import Relational
from ..logic import true
from ..utilities import default_sort_key, has_variety
from .conventions import requires_partial, split_super_sub
from .precedence import PRECEDENCE, precedence
Expand Down Expand Up @@ -1236,13 +1235,7 @@ def _print_Relational(self, expr):
def _print_Piecewise(self, expr):
ecpairs = [r'%s & \text{for}\: %s' % (self._print(e), self._print(c))
for e, c in expr.args[:-1]]
if expr.args[-1].cond == true:
ecpairs.append(r'%s & \text{otherwise}' %
self._print(expr.args[-1].expr))
else:
ecpairs.append(r'%s & \text{for}\: %s' %
(self._print(expr.args[-1].expr),
self._print(expr.args[-1].cond)))
ecpairs.append(r'%s & \text{otherwise}' % self._print(expr.args[-1].expr))
tex = r'\begin{cases} %s \end{cases}'
return tex % r' \\'.join(ecpairs)

Expand Down
8 changes: 0 additions & 8 deletions diofant/printing/octave.py
Expand Up @@ -379,14 +379,6 @@ def _print_zeta(self, expr):
return self._print_not_supported(expr)

def _print_Piecewise(self, expr):
if expr.args[-1].cond != true:
# We need the last conditional to be a True, otherwise the resulting
# function may not return a result.
raise ValueError('All Piecewise expressions must contain an '
'(expr, True) statement to be used as a default '
'condition. Without one, the generated '
'expression may not evaluate to anything under '
'some condition.')
lines = []
if self._settings['inline']:
# Express each (cond, expr) pair in a nested Horner form:
Expand Down
4 changes: 4 additions & 0 deletions diofant/tests/core/test_expr.py
Expand Up @@ -1457,6 +1457,10 @@ def test_is_constant():

assert Integer(2).is_constant() is True

for _ in range(5):
assert Piecewise((x, (x < 0)), (0, True)).is_constant() is not True
assert Piecewise((1, (x < 0)), (0, True)).is_constant() is not True


def test_equals():
assert (-3 - sqrt(5) + (-sqrt(10)/2 - sqrt(2)/2)**2).equals(0)
Expand Down

0 comments on commit 422f0b6

Please sign in to comment.