|
58 | 58 | returns non-negative integers, then ``c^P`` means to apply ``P`` to
|
59 | 59 | the variable indices occurring in ``c``.
|
60 | 60 |
|
| 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 | +
|
61 | 72 | TESTS:
|
62 | 73 |
|
63 | 74 | We test whether coercion works, even in complicated cases in which
|
@@ -433,6 +444,41 @@ def __getattr__(self, s):
|
433 | 444 | except AttributeError:
|
434 | 445 | raise AttributeError('%s has no attribute %s' % (self.__class__, s))
|
435 | 446 |
|
| 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 | + |
436 | 482 | def ring(self):
|
437 | 483 | """
|
438 | 484 | The ring which ``self`` belongs to.
|
|
0 commit comments