Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 7746fa2

Browse files
trac 31530 patch pynac power::subs
1 parent a89f81d commit 7746fa2

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
diff --git a/ginac/power.cpp b/ginac/power.cpp
2+
index 5c07324..837fce3 100644
3+
--- a/ginac/power.cpp
4+
+++ b/ginac/power.cpp
5+
@@ -745,6 +745,10 @@ ex power::subs(const exmap & m, unsigned options) const
6+
if (!are_ex_trivially_equal(basis, subsed_basis)
7+
|| !are_ex_trivially_equal(exponent, subsed_exponent)) {
8+
ex p = power(subsed_basis, subsed_exponent);
9+
+ if (!is_exactly_a<power>(p)) {
10+
+ // trac 30378 and 31530: do not over-substitute
11+
+ return p;
12+
+ }
13+
ex t = ex_to<power>(p).subs_one_level(m, options);
14+
if ((t-*this).is_zero())
15+
return p;

src/sage/symbolic/expression.pyx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,6 +5489,12 @@ cdef class Expression(CommutativeRingElement):
54895489
sage: f = function("f")
54905490
sage: integrate(f(x), x, 0, a).subs(a=cos(a))
54915491
integrate(f(x), x, 0, cos(a))
5492+
5493+
Check that :trac:`31530` is fixed::
5494+
5495+
sage: a, b = var("a b")
5496+
sage: (a + b*x).series(x, 2).subs(a=a, b=b)
5497+
(a) + (b)*x + Order(x^2)
54925498
"""
54935499
cdef dict sdict = {}
54945500
cdef GEx res
@@ -5509,28 +5515,6 @@ cdef class Expression(CommutativeRingElement):
55095515
# Check for duplicate
55105516
_dict_update_check_duplicate(sdict, varkwds)
55115517

5512-
# To work around the pynac bug in :trac:`30378`, we use two steps to do a
5513-
# substitution that only involves plugging expressions into variables, but
5514-
# where some of the expressions include variables that are in self.
5515-
if all(self.parent(k).is_symbol() for k in sdict.keys()):
5516-
dict_vars = tuple(v for k in sdict.keys()
5517-
for v in self.parent(sdict[k]).variables())
5518-
if not set(self.variables()).isdisjoint(dict_vars):
5519-
# Step 1: replace each variable with a new temporary variable
5520-
temp_vars = {v : self.parent().symbol() for v in self.variables()}
5521-
with hold:
5522-
first_step = self.substitute(temp_vars)
5523-
# Step 2: make the original substitutions into the new variables
5524-
result = first_step.substitute({
5525-
temp_vars[v] :
5526-
sdict[v] if v in sdict.keys() else v
5527-
for v in self.variables()})
5528-
if not set(result.variables()).issubset(self.variables() + dict_vars):
5529-
raise RuntimeError("substitution failed")
5530-
return result
5531-
5532-
# We are not in the basic case of only substituting expressions into
5533-
# variables, so we ask Ginac to do the work.
55345518
cdef GExMap smap
55355519
for k, v in sdict.iteritems():
55365520
smap.insert(make_pair((<Expression>self.coerce_in(k))._gobj,

0 commit comments

Comments
 (0)