Skip to content

Commit 623021f

Browse files
committed
Punt to call and add doctests
1 parent 7491113 commit 623021f

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

src/sage/rings/polynomial/infinite_polynomial_element.py

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ def __call__(self, *args, **kwargs):
328328
sage: a(x_1=x[100])
329329
x_100 + x_0
330330
331+
sage: M = matrix([[1,1],[2,0]])
332+
sage: a(x_1=M)
333+
[x_0 + 1 1]
334+
[ 2 x_0]
331335
"""
332336
# Replace any InfinitePolynomials by their underlying polynomials
333337
if hasattr(self._p, 'variables'):
@@ -444,40 +448,85 @@ def __getattr__(self, s):
444448
except AttributeError:
445449
raise AttributeError('%s has no attribute %s' % (self.__class__, s))
446450

447-
def subs(self, in_dict):
451+
def subs(self, fixed=None, **kwargs):
448452
"""
449453
Substitute variables in an infinite polynomial.
450454
451455
INPUT:
452456
453-
- ``in_dict`` - (optional) dictionary of inputs
457+
- ``fixed`` - (optional) dict with variable:value pairs
458+
- ``**kwargs`` - names parameters
454459
455460
OUTPUT:
456-
457-
- new object if substitution is possible, otherwise self.
461+
a new polynomial
458462
459463
EXAMPLES::
460464
461465
sage: R.<x,y> = InfinitePolynomialRing(QQ)
462466
sage: f = x[1] + x[1]*x[2]*x[3]
467+
468+
Passing ``fixed={x[1]: x[0]}``. Note that the keys::
469+
463470
sage: f.subs({x[1]: x[0]})
464471
x_3*x_2*x_0 + x_0
465472
466-
sage: f.subs({x[1]: y[1]})
473+
Passing the variables as names parameters::
474+
475+
sage: f.subs(x_1=y[1])
467476
x_3*x_2*y_1 + y_1
477+
sage: f.subs(x_1=y[1], x_2=2)
478+
2*x_3*y_1 + y_1
468479
469480
The substitution returns the original polynomial if you try
470-
to substitute a variable not present.
481+
to substitute a variable not present::
471482
472483
sage: g = x[0] + x[1]
473-
sage: g.subs({y[0]:x[0]})
484+
sage: g.subs({y[0]: x[0]})
474485
x_1 + x_0
475-
"""
476-
P = self.parent()._P
477486
478-
in_dict = {P(i):P(j) for i,j in in_dict.items()}
487+
The substitution can also handle matrices. It will
488+
return a matrix whose entries are polynomials in countably
489+
many variables::
490+
491+
sage: M = matrix([[1,0],[0,2]])
492+
sage: N = matrix([[0,3],[4,0]])
493+
sage: g = x[0]^2 + 3*x[1]
494+
sage: g.subs({'x_0': M})
495+
[3*x_1 + 1 0]
496+
[ 0 3*x_1 + 4]
497+
sage: g.subs({x[0]: M, x[1]: N})
498+
[ 1 9]
499+
[12 4]
500+
501+
You can only pass one of ``fixed`` or ``kwargs`` at a time::
502+
503+
sage: g.subs(fixed={x[0]: M}, x_1=N)
504+
Traceback (most recent call last):
505+
...
506+
ValueError: pass only one of fixed or kwargs
507+
sage: g.subs({x[0]: M}, x_1=N)
508+
Traceback (most recent call last):
509+
...
510+
ValueError: pass only one of fixed or kwargs
511+
512+
TESTS::
479513
480-
return self.parent(self._p.subs(in_dict))
514+
sage: g.subs(fixed=x[0], x_1=N)
515+
Traceback (most recent call last):
516+
...
517+
ValueError: fixed must be a dict
518+
"""
519+
if fixed:
520+
if not isinstance(fixed, dict):
521+
raise ValueError('fixed must be a dict')
522+
if kwargs:
523+
raise ValueError('pass only one of fixed or kwargs')
524+
kwargs = fixed
525+
try:
526+
return self(**kwargs)
527+
except TypeError:
528+
str_kwargs = {str(k): v for k,v in kwargs.items()}
529+
return self(**str_kwargs)
481530

482531
def ring(self):
483532
"""

0 commit comments

Comments
 (0)