Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commitbf942ae

Browse files
author
Release Manager
committed
Trac #34581: Implement substitution in InfinitePolynomialRing
Add the ability to substitute variables in `InfinitePolynomialRing`.{{{sage: R.<z> = InfinitePolynomialRing(QQ)sage: f = z[1] + z[1]*z[2]*z[3]sage: f.subs({z[1]:z[0]})---------------------------------------------------------------------------TypeError Traceback (most recent calllast)Input In [3], in <cell line: 1>()----> 1 f.subs({z[Integer(1)]:z[Integer(0)]})File ~/Applications/sage/src/sage/structure/element.pyx:830, insage.structure.element.Element.subs() 828 if str(gen) in kwds: 829 variables.append(kwds[str(gen)])--> 830 elif in_dict and gen in in_dict: 831 variables.append(in_dict[gen]) 832 else:TypeError: unhashable type: 'InfinitePolynomialGen'}}}URL:https://trac.sagemath.org/34581Reported by: tkarnTicket author(s): Trevor K. KarnReviewer(s): Tomer Bauer, Travis Scrimshaw
2 parents88fd702 +a56e345 commitbf942ae

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

‎src/sage/rings/polynomial/infinite_polynomial_element.py‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@
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 you can use the standard polynomial
62+
methods, such as
63+
:meth:`~sage.rings.polynomial.infinite_polynomial_element.InfinitePolynomial_sparse.subs`::
64+
65+
sage: R.<x,y> = InfinitePolynomialRing(QQ)
66+
sage: f = x[1] + x[1]*x[2]*x[3]
67+
sage: f.subs({x[1]: x[0]})
68+
x_3*x_2*x_0 + x_0
69+
sage: g = x[0] + x[1] + y[0]
70+
sage: g.subs({x[0]: y[0]})
71+
x_1 + 2*y_0
72+
6173
TESTS:
6274
6375
We test whether coercion works, even in complicated cases in which
@@ -317,6 +329,10 @@ def __call__(self, *args, **kwargs):
317329
sage: a(x_1=x[100])
318330
x_100 + x_0
319331
332+
sage: M = matrix([[1,1],[2,0]])
333+
sage: a(x_1=M)
334+
[x_0 + 1 1]
335+
[ 2 x_0]
320336
"""
321337
# Replace any InfinitePolynomials by their underlying polynomials
322338
ifhasattr(self._p,'variables'):
@@ -433,6 +449,88 @@ def __getattr__(self, s):
433449
exceptAttributeError:
434450
raiseAttributeError('%s has no attribute %s'% (self.__class__,s))
435451

452+
defsubs(self,fixed=None,**kwargs):
453+
"""
454+
Substitute variables in ``self``.
455+
456+
INPUT:
457+
458+
- ``fixed`` -- (optional) ``dict`` with ``{variable:value}`` pairs
459+
- ``**kwargs`` -- named parameters
460+
461+
OUTPUT:
462+
463+
the resulting substitution
464+
465+
EXAMPLES::
466+
467+
sage: R.<x,y> = InfinitePolynomialRing(QQ)
468+
sage: f = x[1] + x[1]*x[2]*x[3]
469+
470+
Passing ``fixed={x[1]: x[0]}``. Note that the keys may be given
471+
using the generators of the infinite polynomial ring
472+
or as a string::
473+
474+
sage: f.subs({x[1]: x[0]})
475+
x_3*x_2*x_0 + x_0
476+
sage: f.subs({'x_1': x[0]})
477+
x_3*x_2*x_0 + x_0
478+
479+
Passing the variables as names parameters::
480+
481+
sage: f.subs(x_1=y[1])
482+
x_3*x_2*y_1 + y_1
483+
sage: f.subs(x_1=y[1], x_2=2)
484+
2*x_3*y_1 + y_1
485+
486+
The substitution returns the original polynomial if you try
487+
to substitute a variable not present::
488+
489+
sage: g = x[0] + x[1]
490+
sage: g.subs({y[0]: x[0]})
491+
x_1 + x_0
492+
493+
The substitution can also handle matrices::
494+
495+
sage: M = matrix([[1,0],[0,2]])
496+
sage: N = matrix([[0,3],[4,0]])
497+
sage: g = x[0]^2 + 3*x[1]
498+
sage: g.subs({'x_0': M})
499+
[3*x_1 + 1 0]
500+
[ 0 3*x_1 + 4]
501+
sage: g.subs({x[0]: M, x[1]: N})
502+
[ 1 9]
503+
[12 4]
504+
505+
If you pass both ``fixed`` and ``kwargs``, any conflicts
506+
will defer to ``fixed``::
507+
508+
sage: R.<x,y> = InfinitePolynomialRing(QQ)
509+
sage: f = x[0]
510+
sage: f.subs({x[0]:1})
511+
1
512+
sage: f.subs(x_0=5)
513+
5
514+
sage: f.subs({x[0]:1}, x_0=5)
515+
1
516+
517+
TESTS::
518+
519+
sage: g.subs(fixed=x[0], x_1=N)
520+
Traceback (most recent call last):
521+
...
522+
ValueError: fixed must be a dict
523+
"""
524+
iffixed:
525+
ifnotisinstance(fixed,dict):
526+
raiseValueError('fixed must be a dict')
527+
kwargs.update(fixed)
528+
try:
529+
returnself(**kwargs)
530+
exceptTypeError:
531+
str_kwargs= {str(k):vfork,vinkwargs.items()}
532+
returnself(**str_kwargs)
533+
436534
defring(self):
437535
"""
438536
The ring which ``self`` belongs to.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp