@@ -328,6 +328,10 @@ def __call__(self, *args, **kwargs):
328
328
sage: a(x_1=x[100])
329
329
x_100 + x_0
330
330
331
+ sage: M = matrix([[1,1],[2,0]])
332
+ sage: a(x_1=M)
333
+ [x_0 + 1 1]
334
+ [ 2 x_0]
331
335
"""
332
336
# Replace any InfinitePolynomials by their underlying polynomials
333
337
if hasattr (self ._p , 'variables' ):
@@ -444,40 +448,85 @@ def __getattr__(self, s):
444
448
except AttributeError :
445
449
raise AttributeError ('%s has no attribute %s' % (self .__class__ , s ))
446
450
447
- def subs (self , in_dict ):
451
+ def subs (self , fixed = None , ** kwargs ):
448
452
"""
449
453
Substitute variables in an infinite polynomial.
450
454
451
455
INPUT:
452
456
453
- - ``in_dict`` - (optional) dictionary of inputs
457
+ - ``fixed`` - (optional) dict with variable:value pairs
458
+ - ``**kwargs`` - names parameters
454
459
455
460
OUTPUT:
456
-
457
- - new object if substitution is possible, otherwise self.
461
+ a new polynomial
458
462
459
463
EXAMPLES::
460
464
461
465
sage: R.<x,y> = InfinitePolynomialRing(QQ)
462
466
sage: f = x[1] + x[1]*x[2]*x[3]
467
+
468
+ Passing ``fixed={x[1]: x[0]}``. Note that the keys::
469
+
463
470
sage: f.subs({x[1]: x[0]})
464
471
x_3*x_2*x_0 + x_0
465
472
466
- sage: f.subs({x[1]: y[1]})
473
+ Passing the variables as names parameters::
474
+
475
+ sage: f.subs(x_1=y[1])
467
476
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
468
479
469
480
The substitution returns the original polynomial if you try
470
- to substitute a variable not present.
481
+ to substitute a variable not present::
471
482
472
483
sage: g = x[0] + x[1]
473
- sage: g.subs({y[0]:x[0]})
484
+ sage: g.subs({y[0]: x[0]})
474
485
x_1 + x_0
475
- """
476
- P = self .parent ()._P
477
486
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::
479
513
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 )
481
530
482
531
def ring (self ):
483
532
"""
0 commit comments