Skip to content

Commit

Permalink
Change code to use .subs({old: new}), not .subs(old, new)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed May 30, 2018
1 parent 39cdc8b commit 5a7ba18
Show file tree
Hide file tree
Showing 141 changed files with 1,153 additions and 1,144 deletions.
4 changes: 2 additions & 2 deletions diofant/calculus/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ def minimize_univariate(f, x, dom):
for p in solve(diff(f, x), x):
p = p[x]
if p in dom:
extr[p] = f.subs(x, p)
extr[p] = f.subs({x: p})
elif dom.is_FiniteSet:
for p in dom.args:
extr[p] = f.subs(x, p)
extr[p] = f.subs({x: p})
else: # pragma: no cover
raise NotImplementedError

Expand Down
2 changes: 1 addition & 1 deletion diofant/calculus/singularities.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def singularities(f, x):
l = Limit(f, x, s, dir="real")
try:
r = l.doit()
if r == l or f.subs(x, s) != r: # pragma: no cover
if r == l or f.subs({x: s}) != r: # pragma: no cover
raise NotImplementedError
except PoleError:
res.add(s)
Expand Down
10 changes: 5 additions & 5 deletions diofant/concrete/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def deltaproduct(f, limit):
if isinstance(limit[1], int) and isinstance(limit[2], int):
result += sum(deltaproduct(newexpr,
(limit[0], limit[1], ik - 1)) *
delta.subs(limit[0], ik) *
delta.subs({limit[0]: ik}) *
deltaproduct(newexpr,
(limit[0], ik + 1, limit[2]))
for ik in range(int(limit[1]), int(limit[2] + 1)))
Expand All @@ -185,7 +185,7 @@ def deltaproduct(f, limit):
result += deltasummation(deltaproduct(newexpr,
(limit[0],
limit[1], k - 1)) *
delta.subs(limit[0], k) *
delta.subs({limit[0]: k}) *
deltaproduct(newexpr, (limit[0],
k + 1, limit[2])),
(k, limit[1], limit[2]),
Expand All @@ -201,7 +201,7 @@ def deltaproduct(f, limit):
return factor(deltaproduct(g, limit))
return product(f, limit)

return (_remove_multiple_delta(f.subs(limit[0], limit[1]) *
return (_remove_multiple_delta(f.subs({limit[0]: limit[1]}) *
KroneckerDelta(limit[2], limit[1])) +
_simplify_delta(KroneckerDelta(limit[2], limit[1] - 1)))

Expand Down Expand Up @@ -293,7 +293,7 @@ def deltasummation(f, limit, no_piecewise=False):
assert len(solns) == 1
value = solns[0][x]
if no_piecewise:
return expr.subs(x, value)
return Piecewise((expr.subs(x, value),
return expr.subs({x: value})
return Piecewise((expr.subs({x: value}),
Interval(*limit[1:3]).as_relational(value)),
(S.Zero, True))
4 changes: 2 additions & 2 deletions diofant/concrete/expr_with_intlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def change_index(self, var, trafo, newvar=None):
else:
limits.append(limit)

function = self.function.subs(var, (var - beta)/alpha)
function = function.subs(var, newvar)
function = self.function.subs({var: (var - beta)/alpha})
function = function.subs({var: newvar})

return self.func(function, *limits)

Expand Down
6 changes: 3 additions & 3 deletions diofant/concrete/expr_with_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ def _eval_subs(self, old, new):
========
>>> from diofant.abc import s
>>> Sum(1/n**s, (n, 1, oo)).subs(s, 2)
>>> Sum(1/n**s, (n, 1, oo)).subs({s: 2})
Sum(n**(-2), (n, 1, oo))
>>> from diofant.abc import a
>>> Integral(a*x**2, x).subs(x, 4)
>>> Integral(a*x**2, x).subs({x: 4})
Integral(a*x**2, (x, 4))
See Also
Expand Down Expand Up @@ -290,7 +290,7 @@ def _eval_subs(self, old, new):
"substitution can not create dummy dependencies")
sub_into_func = True
if sub_into_func:
func = func.subs(old, new)
func = func.subs({old: new})
else:
# old is a Symbol and a dummy variable of some limit
for i, xab in enumerate(limits):
Expand Down
8 changes: 4 additions & 4 deletions diofant/concrete/gosper.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def gosper_term(f, n):

for coeff in coeffs:
if coeff not in solution:
x = x.subs(coeff, 0)
x = x.subs({coeff: 0})

if x is S.Zero:
return # 'f(n)' is *not* Gosper-summable
Expand Down Expand Up @@ -154,11 +154,11 @@ def gosper_sum(f, k):
>>> f = (4*k + 1)*factorial(k)/factorial(2*k + 1)
>>> gosper_sum(f, (k, 0, n))
(-factorial(n) + 2*factorial(2*n + 1))/factorial(2*n + 1)
>>> _.subs(n, 2) == sum(f.subs(k, i) for i in [0, 1, 2])
>>> _.subs({n: 2}) == sum(f.subs({k: i}) for i in [0, 1, 2])
True
>>> gosper_sum(f, (k, 3, n))
(-60*factorial(n) + factorial(2*n + 1))/(60*factorial(2*n + 1))
>>> _.subs(n, 5) == sum(f.subs(k, i) for i in [3, 4, 5])
>>> _.subs({n: 5}) == sum(f.subs({k: i}) for i in [3, 4, 5])
True
References
Expand All @@ -182,7 +182,7 @@ def gosper_sum(f, k):
if indefinite:
result = f*g
else:
result = (f*(g + 1)).subs(k, b) - (f*g).subs(k, a)
result = (f*(g + 1)).subs({k: b}) - (f*g).subs({k: a})

if result is nan:
try:
Expand Down
8 changes: 4 additions & 4 deletions diofant/concrete/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Product(ExprWithIntLimits):
By the same formula we can compute sin(pi/2):
>>> P = pi * x * Product(1 - x**2/k**2, (k, 1, n))
>>> P = P.subs(x, pi/2)
>>> P = P.subs({x: pi/2})
>>> P
pi**2*Product(1 - pi**2/(4*k**2), (k, 1, n))/2
>>> Pe = P.doit()
Expand Down Expand Up @@ -128,7 +128,7 @@ class Product(ExprWithIntLimits):
>>> P = Product(2, (i, 10, n)).doit()
>>> P
2**(n - 9)
>>> P.subs(n, 5)
>>> P.subs({n: 5})
1/16
>>> Product(2, (i, 10, 5)).doit()
1/16
Expand Down Expand Up @@ -235,14 +235,14 @@ def _eval_product(self, term, limits):
return term**(n - a + 1)

if a == n:
return term.subs(k, a)
return term.subs({k: a})

if term.has(KroneckerDelta) and _has_simple_delta(term, limits[0]):
return deltaproduct(term, limits)

dif = n - a
if dif.is_Integer:
return Mul(*[term.subs(k, a + i) for i in range(dif + 1)])
return Mul(*[term.subs({k: a + i}) for i in range(dif + 1)])

elif term.is_polynomial(k):
poly = term.as_poly(k)
Expand Down
40 changes: 20 additions & 20 deletions diofant/concrete/summations.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Sum(AddWithLimits, ExprWithIntLimits):
>>> S = Sum(i, (i, 1, n)).doit()
>>> S
n**2/2 + n/2
>>> S.subs(n, -4)
>>> S.subs({n: -4})
6
>>> Sum(i, (i, 1, -4)).doit()
6
Expand Down Expand Up @@ -274,33 +274,33 @@ def euler_maclaurin(self, m=0, n=0, eps=0, eval_integral=True):
m = min(m, b - a + 1)
if not eps or f.is_polynomial(i):
for k in range(m):
s += f.subs(i, a + k)
s += f.subs({i: a + k})
else:
term = f.subs(i, a)
term = f.subs({i: a})
if term:
test = abs(term.evalf(3)) < eps
if not (test == S.false):
# a symbolic Relational class, can't go further
return term, S.Zero
s += term
for k in range(1, m):
term = f.subs(i, a + k)
term = f.subs({i: a + k})
if abs(term.evalf(3)) < eps and term != 0:
return s, abs(term)
s += term
if b - a + 1 == m:
return s, S.Zero
a += m
x = Dummy('x')
I = Integral(f.subs(i, x), (x, a, b))
I = Integral(f.subs({i: x}), (x, a, b))
if eval_integral:
I = I.doit()
s += I

def fpoint(expr):
if b is oo:
return expr.subs(i, a), 0
return expr.subs(i, a), expr.subs(i, b)
return expr.subs({i: a}), 0
return expr.subs({i: a}), expr.subs({i: b})
fa, fb = fpoint(f)
iterm = (fa + fb)/2
g = f.diff(i)
Expand Down Expand Up @@ -439,7 +439,7 @@ def findrecur(self, F=Function('F'), n=None):
a = Function('a')

def f(i, j):
return self.function.subs([(n, i), (k, j)])
return self.function.subs({n: i, k: j})

I, J, step = 0, 1, 1
y, x, sols = S.Zero, [], {}
Expand Down Expand Up @@ -469,7 +469,7 @@ def f(i, j):
sols = solve(eq, *x)[0]

y = sum(a(i, j)*F(n - j, k - i) for i in range(I) for j in range(J))
y = y.subs(sols).subs(map(lambda a: (a, 1), x))
y = y.subs(sols).subs({_: 1 for _ in x})

return y if y else None

Expand Down Expand Up @@ -534,7 +534,7 @@ def telescopic_direct(L, R, n, limits):
(i, a, b) = limits
s = 0
for m in range(n):
s += L.subs(i, a + m) + R.subs(i, b - m)
s += L.subs({i: a + m}) + R.subs({i: b - m})
return s


Expand All @@ -548,7 +548,7 @@ def telescopic(L, R, limits):
return

k = Wild("k")
sol = (-R).match(L.subs(i, i + k))
sol = (-R).match(L.subs({i: i + k}))
if sol:
s = sol[k]
else:
Expand All @@ -572,7 +572,7 @@ def eval_sum(f, limits):
if i not in f.free_symbols:
return f*(b - a + 1)
if a == b:
return f.subs(i, a)
return f.subs({i: a})

if f.has(KroneckerDelta) and _has_simple_delta(f, limits[0]):
return deltasummation(f, limits)
Expand Down Expand Up @@ -600,7 +600,7 @@ def eval_sum_direct(expr, limits):
(i, a, b) = limits

dif = b - a
return Add(*[expr.subs(i, a + j) for j in range(dif + 1)])
return Add(*[expr.subs({i: a + j}) for j in range(dif + 1)])


def eval_sum_symbolic(f, limits):
Expand Down Expand Up @@ -696,12 +696,12 @@ def _eval_sum_hyper(f, i, a):
from ..polys import Poly, factor

if a != 0:
return _eval_sum_hyper(f.subs(i, i + a), i, 0)
return _eval_sum_hyper(f.subs({i: i + a}), i, 0)

if f.subs(i, 0) == 0:
if simplify(f.subs(i, Dummy('i', integer=True, positive=True))) == 0:
if f.subs({i: 0}) == 0:
if simplify(f.subs({i: Dummy('i', integer=True, positive=True)})) == 0:
return Integer(0), True
return _eval_sum_hyper(f.subs(i, i + 1), i, 0)
return _eval_sum_hyper(f.subs({i: i + 1}), i, 0)

hs = hypersimp(f, i)
if hs is None:
Expand Down Expand Up @@ -739,7 +739,7 @@ def _eval_sum_hyper(f, i, a):
except PolynomialError:
pass

return f.subs(i, 0)*e, h.convergence_statement
return f.subs({i: 0})*e, h.convergence_statement


def eval_sum_hyper(f, i_a_b):
Expand All @@ -755,7 +755,7 @@ def eval_sum_hyper(f, i_a_b):

if b != oo:
if a == -oo:
res = _eval_sum_hyper(f.subs(i, -i), i, -b)
res = _eval_sum_hyper(f.subs({i: -i}), i, -b)
if res is not None:
return Piecewise(res, (old_sum, True))
else:
Expand All @@ -775,7 +775,7 @@ def eval_sum_hyper(f, i_a_b):
if res is not None:
r, c = res
if c == S.false:
f = f.subs(i, Dummy('i', integer=True, positive=True) + a)
f = f.subs({i: Dummy('i', integer=True, positive=True) + a})
if f.is_nonnegative:
return oo
else:
Expand Down
4 changes: 2 additions & 2 deletions diofant/concrete/tests/test_products.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ def test_the_product(m, n):
# g
g = i**3 + 2*i**2 - 3*i
# f = Delta g
f = simplify(g.subs(i, i+1) / g)
f = simplify(g.subs({i: i + 1})/g)
# The product
a = m
b = n - 1
P = Product(f, (i, a, b)).doit()
# Test if Product_{m <= i < n} f(i) = g(n) / g(m)
assert simplify(P / (g.subs(i, n) / g.subs(i, m))) == 1
assert simplify(P/(g.subs({i: n})/g.subs({i: m}))) == 1

# m < n
test_the_product(u, u+v)
Expand Down
Loading

0 comments on commit 5a7ba18

Please sign in to comment.