Skip to content

Commit 7491113

Browse files
committed
Add subs method
1 parent 2114066 commit 7491113

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/sage/rings/polynomial/infinite_polynomial_element.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@
5858
returns non-negative integers, then ``c^P`` means to apply ``P`` to
5959
the variable indices occurring in ``c``.
6060
61+
If you want to substitute variables more generally, use the ``.subs``
62+
method::
63+
64+
sage: R.<x,y> = InfinitePolynomialRing(QQ)
65+
sage: f = x[1] + x[1]*x[2]*x[3]
66+
sage: f.subs({x[1]: x[0]})
67+
x_3*x_2*x_0 + x_0
68+
sage: g = x[0] + x[1] + y[0]
69+
sage: g.subs({x[0]: y[0]})
70+
x_1 + 2*y_0
71+
6172
TESTS:
6273
6374
We test whether coercion works, even in complicated cases in which
@@ -433,6 +444,41 @@ def __getattr__(self, s):
433444
except AttributeError:
434445
raise AttributeError('%s has no attribute %s' % (self.__class__, s))
435446

447+
def subs(self, in_dict):
448+
"""
449+
Substitute variables in an infinite polynomial.
450+
451+
INPUT:
452+
453+
- ``in_dict`` - (optional) dictionary of inputs
454+
455+
OUTPUT:
456+
457+
- new object if substitution is possible, otherwise self.
458+
459+
EXAMPLES::
460+
461+
sage: R.<x,y> = InfinitePolynomialRing(QQ)
462+
sage: f = x[1] + x[1]*x[2]*x[3]
463+
sage: f.subs({x[1]: x[0]})
464+
x_3*x_2*x_0 + x_0
465+
466+
sage: f.subs({x[1]: y[1]})
467+
x_3*x_2*y_1 + y_1
468+
469+
The substitution returns the original polynomial if you try
470+
to substitute a variable not present.
471+
472+
sage: g = x[0] + x[1]
473+
sage: g.subs({y[0]:x[0]})
474+
x_1 + x_0
475+
"""
476+
P = self.parent()._P
477+
478+
in_dict = {P(i):P(j) for i,j in in_dict.items()}
479+
480+
return self.parent(self._p.subs(in_dict))
481+
436482
def ring(self):
437483
"""
438484
The ring which ``self`` belongs to.

0 commit comments

Comments
 (0)