Skip to content

Commit

Permalink
Merge pull request diofant#732 from skirpichev/misc-poly
Browse files Browse the repository at this point in the history
Misc poly fixes
  • Loading branch information
skirpichev committed Dec 11, 2018
2 parents c7b8ea4 + afc1b57 commit ba2017e
Show file tree
Hide file tree
Showing 61 changed files with 383 additions and 801 deletions.
2 changes: 1 addition & 1 deletion diofant/calculus/optimization.py
Expand Up @@ -140,7 +140,7 @@ def minimize_univariate(f, x, dom):
elif dom.is_FiniteSet:
for p in dom.args:
extr[p] = f.subs({x: p})
else: # pragma: no cover
else:
raise NotImplementedError

if extr:
Expand Down
2 changes: 1 addition & 1 deletion diofant/calculus/singularities.py
Expand Up @@ -48,7 +48,7 @@ def singularities(f, x):
elif f.func in (log, sign) and len(f.args) == 1:
guess |= singularities(f.args[0], x)
guess |= {s[x] for s in solve(f.args[0], x) if s[x].is_real}
else: # pragma: no cover
else:
raise NotImplementedError

for s in guess:
Expand Down
3 changes: 2 additions & 1 deletion diofant/concrete/gosper.py
Expand Up @@ -101,7 +101,8 @@ def gosper_term(f, n):
elif not N:
D = {K - N + 1, Integer(0)}
else:
D = {K - N + 1, (B.nth(N - 1) - A.nth(N - 1))/A.LC()}
D = {K - N + 1, (B.coeff_monomial(n**(N - 1)) -
A.coeff_monomial(n**(N - 1)))/A.LC()}

for d in set(D):
if not d.is_Integer or d < 0:
Expand Down
2 changes: 1 addition & 1 deletion diofant/concrete/summations.py
Expand Up @@ -202,7 +202,7 @@ def _eval_derivative(self, x):
if limit[0] not in df.free_symbols:
rv = rv.doit()
return rv
else: # pragma: no cover
else:
raise NotImplementedError('Lower and upper bound expected.')

def _eval_simplify(self, ratio, measure):
Expand Down
4 changes: 2 additions & 2 deletions diofant/core/evalf.py
Expand Up @@ -636,7 +636,7 @@ def evalf_trig(v, prec, options):
func = mpf_cos
elif isinstance(v, sin):
func = mpf_sin
else: # pragma: no cover
else:
raise NotImplementedError
arg = v.args[0]
# 20 extra bits is possibly overkill. It does make the need
Expand All @@ -652,7 +652,7 @@ def evalf_trig(v, prec, options):
return fone, None, prec, None
elif isinstance(v, sin):
return None, None, None, None
else: # pragma: no cover
else:
raise NotImplementedError
# For trigonometric functions, we are interested in the
# fixed-point (absolute) accuracy of the argument.
Expand Down
6 changes: 1 addition & 5 deletions diofant/core/expr.py
Expand Up @@ -984,7 +984,6 @@ def coeff(self, x, n=1, right=False):
diofant.core.expr.Expr.as_coeff_Mul
diofant.core.expr.Expr.as_independent
diofant.polys.polytools.Poly.coeff_monomial
diofant.polys.polytools.Poly.nth
Examples
========
Expand Down Expand Up @@ -1298,8 +1297,6 @@ def as_coefficient(self, expr):
Poly(x*E + 2*E, x, E, domain='ZZ')
>>> p.coeff_monomial(E)
2
>>> p.nth(0, 1)
2
Since the following cannot be written as a product containing
E as a factor, None is returned. (If the coefficient ``2*x`` is
Expand All @@ -1323,7 +1320,6 @@ def as_coefficient(self, expr):
as_coeff_Mul: separate the multiplicative constant from an expression
as_independent: separate x-dependent terms/factors from others
diofant.polys.polytools.Poly.coeff_monomial: efficiently find the single coefficient of a monomial in Poly
diofant.polys.polytools.Poly.nth: like coeff_monomial but powers of monomial terms are used
"""

r = self.extract_multiplicatively(expr)
Expand Down Expand Up @@ -1806,7 +1802,7 @@ def extract_multiplicatively(self, c):
return
else:
return quotient
else: # pragma: no cover
else:
raise NotImplementedError
elif self.is_Add:
cs, ps = self.primitive()
Expand Down
2 changes: 1 addition & 1 deletion diofant/core/mul.py
Expand Up @@ -834,7 +834,7 @@ def _matches(self, expr, repl_dict={}):
a = self.func(*nc1)
if not isinstance(a, self.func):
repl_dict = a._matches(self.func(*nc2), repl_dict)
else: # pragma: no cover
else:
raise NotImplementedError
return repl_dict or None

Expand Down
4 changes: 2 additions & 2 deletions diofant/core/multidimensional.py
Expand Up @@ -22,7 +22,7 @@ def apply_on_element(f, args, kwargs, n):
elif isinstance(n, str):
structure = kwargs[n]
is_arg = False
else: # pragma: no cover
else:
raise NotImplementedError

# Define reduced function that is only dependend of the specified argument.
Expand Down Expand Up @@ -122,7 +122,7 @@ def wrapper(*args, **kwargs):
except KeyError:
continue
is_arg = False
else: # pragma: no cover
else:
raise NotImplementedError

if hasattr(entry, "__iter__"):
Expand Down
2 changes: 1 addition & 1 deletion diofant/core/operations.py
Expand Up @@ -235,7 +235,7 @@ def _matches_commutative(self, expr, repl_dict={}):
i += 0
continue

else: # pragma: no cover
else:
raise NotImplementedError

break # if we didn't continue, there is nothing more to do
Expand Down
4 changes: 2 additions & 2 deletions diofant/core/relational.py
Expand Up @@ -108,7 +108,7 @@ def canonical(self):
pass
elif r.func in (Eq, Ne):
r = r.func(*ordered(r.args), evaluate=False)
else: # pragma: no cover
else:
raise NotImplementedError
if r.lhs.is_Number and not r.rhs.is_Number:
r = r.reversed
Expand Down Expand Up @@ -208,7 +208,7 @@ def as_set(self):

if len(syms) == 1:
sym = syms.pop()
else: # pragma: no cover
else:
raise NotImplementedError("Sorry, Relational.as_set procedure"
" is not yet implemented for"
" multivariate expressions")
Expand Down
6 changes: 3 additions & 3 deletions diofant/functions/elementary/piecewise.py
Expand Up @@ -240,7 +240,7 @@ def _eval_interval(self, sym, a, b):
elif ((b >= lower) is S.true) and ((b <= upper) is S.true):
rep = a
val = e._eval_interval(sym, a, b)
else: # pragma: no cover
else:
raise NotImplementedError(
"""The evaluation of a Piecewise interval when both the lower
and the upper limit are symbolic is not yet implemented.""")
Expand Down Expand Up @@ -326,7 +326,7 @@ def _sort_expr_cond(self, sym, a, b, targetcond=None):
upper = Min(cond2.gts, upper)
elif cond2.gts == sym:
lower = Max(cond2.lts, lower)
else: # pragma: no cover
else:
raise NotImplementedError(
"Unable to handle interval evaluation of expression.")
else:
Expand All @@ -335,7 +335,7 @@ def _sort_expr_cond(self, sym, a, b, targetcond=None):
lower = -oo # e.g. x <= 0 ---> -oo <= 0
elif cond.gts == sym: # part 1a: ... that can be expanded
upper = oo # e.g. x >= 0 ---> oo >= 0
else: # pragma: no cover
else:
raise NotImplementedError(
"Unable to handle interval evaluation of expression.")

Expand Down
4 changes: 2 additions & 2 deletions diofant/functions/special/bessel.py
Expand Up @@ -700,13 +700,13 @@ def spherical_jn(n, x):

def f(x):
return spherical_jn(n, x)
else: # pragma: no cover
else:
raise NotImplementedError("Unknown method.")

def solver(f, x):
if method == "scipy":
root = newton(f, x)
else: # pragma: no cover
else:
raise NotImplementedError("Unknown method.")
return root

Expand Down
2 changes: 1 addition & 1 deletion diofant/geometry/ellipse.py
Expand Up @@ -744,7 +744,7 @@ def is_tangent(self, o):
inter = self._do_line_intersection(seg)
c += len([True for point in inter if point in seg])
return c == 1
else: # pragma: no cover
else:
raise NotImplementedError("Unknown argument type")

def normal_lines(self, p, prec=None):
Expand Down
2 changes: 1 addition & 1 deletion diofant/geometry/plane.py
Expand Up @@ -726,5 +726,5 @@ def is_coplanar(self, o):
return not cancel(self.equation(x, y, z)/o.equation(x, y, z)).has(x, y, z)
elif isinstance(o, Point3D):
return o in self
else: # pragma: no cover
else:
raise NotImplementedError
2 changes: 1 addition & 1 deletion diofant/geometry/polygon.py
Expand Up @@ -686,7 +686,7 @@ def distance(self, o):
return dist
elif isinstance(o, Polygon) and self.is_convex() and o.is_convex():
return self._do_poly_distance(o)
else: # pragma: no cover
else:
raise NotImplementedError

def _do_poly_distance(self, e2):
Expand Down
16 changes: 8 additions & 8 deletions diofant/integrals/prde.py
Expand Up @@ -183,7 +183,7 @@ def prde_linear_constraints(a, b, G, DE):

if not all(ri.is_zero for _, ri in Q):
N = max(ri.degree(DE.t) for _, ri in Q)
M = Matrix(N + 1, m, lambda i, j: Q[j][1].nth(i))
M = Matrix(N + 1, m, lambda i, j: Q[j][1].coeff_monomial((i,)))
else:
M = Matrix() # No constraints, return the empty matrix.

Expand Down Expand Up @@ -302,7 +302,7 @@ def prde_no_cancel_b_large(b, Q, n, DE):

for N in range(n, -1, -1): # [n, ..., 0]
for i in range(m):
si = Q[i].nth(N + db)/b.LC()
si = Q[i].coeff_monomial((N + db,))/b.LC()
sitn = Poly(si*DE.t**N, DE.t)
H[i] = H[i] + sitn
Q[i] = Q[i] - derivation(sitn, DE) - b*sitn
Expand All @@ -312,7 +312,7 @@ def prde_no_cancel_b_large(b, Q, n, DE):
M = zeros(0, 2)
else:
dc = max(qi.degree(DE.t) for qi in Q)
M = Matrix(dc + 1, m, lambda i, j: Q[j].nth(i))
M = Matrix(dc + 1, m, lambda i, j: Q[j].coeff_monomial((i,)))
A, u = constant_system(M, zeros(dc + 1, 1), DE)
c = eye(m)
A = A.row_join(zeros(A.rows, m)).col_join(c.row_join(-c))
Expand All @@ -336,22 +336,22 @@ def prde_no_cancel_b_small(b, Q, n, DE):

for N in range(n, 0, -1): # [n, ..., 1]
for i in range(m):
si = Q[i].nth(N + DE.d.degree(DE.t) - 1)/(N*DE.d.LC())
si = Q[i].coeff_monomial((N + DE.d.degree(DE.t) - 1,))/(N*DE.d.LC())
sitn = Poly(si*DE.t**N, DE.t)
H[i] = H[i] + sitn
Q[i] = Q[i] - derivation(sitn, DE) - b*sitn

if b.degree(DE.t) > 0:
for i in range(m):
si = Poly(Q[i].nth(b.degree(DE.t))/b.LC(), DE.t)
si = Poly(Q[i].coeff_monomial((b.degree(DE.t),))/b.LC(), DE.t)
H[i] = H[i] + si
Q[i] = Q[i] - derivation(si, DE) - b*si
if all(qi.is_zero for qi in Q):
dc = -1
M = Matrix()
else:
dc = max(qi.degree(DE.t) for qi in Q)
M = Matrix(dc + 1, m, lambda i, j: Q[j].nth(i))
M = Matrix(dc + 1, m, lambda i, j: Q[j].coeff_monomial((i,)))
A, u = constant_system(M, zeros(dc + 1, 1), DE)
c = eye(m)
A = A.row_join(zeros(A.rows, m)).col_join(c.row_join(-c))
Expand Down Expand Up @@ -469,7 +469,7 @@ def parametric_log_deriv_heu(fa, fd, wa, wd, DE, c1=None):
C = max(p.degree(DE.t), q.degree(DE.t))

if q.degree(DE.t) > B:
eqs = [p.nth(i) - c1*q.nth(i) for i in range(B + 1, C + 1)]
eqs = [p.coeff_monomial((i,)) - c1*q.coeff_monomial((i,)) for i in range(B + 1, C + 1)]
s = solve(eqs, c1)
if not s or not s[0][c1].is_Rational:
# deg(q) > B, no solution for c.
Expand Down Expand Up @@ -510,7 +510,7 @@ def parametric_log_deriv_heu(fa, fd, wa, wd, DE, c1=None):
u1, r1 = (fa*l.quo(fd)).div(z) # (l*f).div(z)
u2, r2 = (wa*l.quo(wd)).div(z) # (l*w).div(z)

eqs = [r1.nth(i) - c1*r2.nth(i) for i in range(z.degree(DE.t))]
eqs = [r1.coeff_monomial((i,)) - c1*r2.coeff_monomial((i,)) for i in range(z.degree(DE.t))]
s = solve(eqs, c1)
if not s or not s[0][c1].is_Rational:
# deg(q) <= B, no solution for c.
Expand Down
12 changes: 6 additions & 6 deletions diofant/integrals/risch.py
Expand Up @@ -763,8 +763,8 @@ def as_poly_1t(p, t, z):
one_t_part *= Poly(t**r, t)

one_t_part = one_t_part.replace(t, z) # z will be 1/t
if pd.nth(d):
one_t_part *= Poly(1/pd.nth(d), z, expand=False)
if pd.coeff_monomial((d,)):
one_t_part *= Poly(1/pd.coeff_monomial((d,)), z, expand=False)
ans = t_part.as_poly(t, z, expand=False) + one_t_part.as_poly(t, z,
expand=False)

Expand Down Expand Up @@ -1361,11 +1361,11 @@ def integrate_hyperexponential_polynomial(p, DE, z):
# If you get AttributeError: 'NoneType' object has no attribute 'nth'
# then this should really not have expand=False
# But it shouldn't happen because p is already a Poly in t and z
a = p.as_poly(z, expand=False).nth(-i)
a = p.as_poly(z, expand=False).coeff_monomial((-i,))
else:
# If you get AttributeError: 'NoneType' object has no attribute 'nth'
# then this should really not have expand=False
a = p.as_poly(t1, expand=False).nth(i)
a = p.as_poly(t1, expand=False).coeff_monomial((i,))

aa, ad = frac_in(a, DE.t, field=True)
aa, ad = aa.cancel(ad, include=True)
Expand Down Expand Up @@ -1419,7 +1419,7 @@ def integrate_hyperexponential(a, d, DE, z=None, conds='piecewise'):

qa, qd, b = integrate_hyperexponential_polynomial(pp, DE, z)

i = pp.nth(0, 0)
i = pp.coeff_monomial(1)

ret = ((g1[0].as_expr()/g1[1].as_expr()).subs(s)
+ residue_reduce_to_basic(g2, DE, z))
Expand Down Expand Up @@ -1456,7 +1456,7 @@ def integrate_hypertangent_polynomial(p, DE):
# XXX: Make sure that sqrt(-1) is not in k.
q, r = polynomial_reduce(p, DE)
a = DE.d.exquo(Poly(DE.t**2 + 1, DE.t))
c = Poly(r.nth(1)/(2*a.as_expr()), DE.t)
c = Poly(r.coeff_monomial((1,))/(2*a.as_expr()), DE.t)
return q, c


Expand Down
4 changes: 2 additions & 2 deletions diofant/logic/algorithms/dpll2.py
Expand Up @@ -95,13 +95,13 @@ def __init__(self, clauses, variables, var_settings, symbols=None,
self.heur_lit_unset = self._vsids_lit_unset
self.heur_clause_added = self._vsids_clause_added

else: # pragma: no cover
else:
raise NotImplementedError

if 'none' == clause_learning:
self.add_learned_clause = lambda x: None
self.compute_conflict = lambda: None
else: # pragma: no cover
else:
raise NotImplementedError

# Create the base level
Expand Down
6 changes: 3 additions & 3 deletions diofant/logic/boolalg.py
Expand Up @@ -343,7 +343,7 @@ def as_set(self):
from ..sets import Intersection
if len(self.free_symbols) == 1:
return Intersection(*[arg.as_set() for arg in self.args])
else: # pragma: no cover
else:
raise NotImplementedError("Sorry, And.as_set has not yet been"
" implemented for multivariate"
" expressions")
Expand Down Expand Up @@ -409,7 +409,7 @@ def as_set(self):
from ..sets import Union
if len(self.free_symbols) == 1:
return Union(*[arg.as_set() for arg in self.args])
else: # pragma: no cover
else:
raise NotImplementedError("Sorry, Or.as_set has not yet been"
" implemented for multivariate"
" expressions")
Expand Down Expand Up @@ -493,7 +493,7 @@ def as_set(self):
"""
if len(self.free_symbols) == 1:
return self.args[0].as_set().complement(S.Reals)
else: # pragma: no cover
else:
raise NotImplementedError("Sorry, Not.as_set has not yet been"
" implemented for mutivariate"
" expressions")
Expand Down
2 changes: 1 addition & 1 deletion diofant/logic/inference.py
Expand Up @@ -77,7 +77,7 @@ def satisfiable(expr, algorithm="dpll2", all_models=False):
elif algorithm == "dpll2":
from .algorithms.dpll2 import dpll_satisfiable
return dpll_satisfiable(expr, all_models)
else: # pragma: no cover
else:
raise NotImplementedError


Expand Down
2 changes: 1 addition & 1 deletion diofant/matrices/dense.py
Expand Up @@ -795,7 +795,7 @@ def __delitem__(self, key):
del self._mat[j + i*self.cols]
self.cols -= 1
return
else: # pragma: no cover
else:
raise NotImplementedError

# Utility functions
Expand Down

0 comments on commit ba2017e

Please sign in to comment.