Ask Your Question

Infinite polynomial ring with several index

asked 2019-05-10 20:54:43 +0200

ianncunha gravatar image

I know it's easy to create a infinite polynomial ring:


But how can I make a infinite polynomial ring with variables indexed by pairs, like x_(a,b)?

edit retag flag offensive close merge delete


How much of the InfinitePolynomialRing do you need? I have modified the code of Sage that implements this function and related ones, but I don't have complete functionality. For now, I only have polynomial algebra implemented. Also, it is not possible to use "x_(a,b)", since there is a Sage function that validates variable names, and that is not a valid one. What I used is "x[a,b]" which creates a "x_a_b".

I could send you the modified code, so you can complete the implementation as needed by you.

dsejas gravatar imagedsejas ( 2019-05-12 07:12:21 +0200 )edit

Nice! If you can send it to me it would be great!

ianncunha gravatar imageianncunha ( 2019-05-13 19:41:31 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-05-11 22:53:10 +0200

slelievre gravatar image

One workaround would be to use an infinite polynomial ring in $x$ and $y$, with $x_i y_j$ standing for $x_{i, j}$.

sage: R.<x, y> = InfinitePolynomialRing(ZZ)
sage: x
sage: y
sage: x[3]*y[4]
sage: y[6]*x[1]
edit flag offensive delete link more


Yeah, I thought of that. But I would like to know if there is any way to put a function (or something) that returns in the form x_a_b, for a infinite set.

By the way, is easy (but not so good-looking) to define like

def z(a,b):
    z=var('z_%d_%d' %(i,j))
    return z
ianncunha gravatar imageianncunha ( 2019-05-13 19:52:54 +0200 )edit

answered 2019-05-14 06:05:58 +0200

dsejas gravatar image

updated 2019-05-16 05:05:33 +0200

Hello, @ianncunha. Here is the modified code. You can use a program like diff or meld to make comparisons with the original code. (By the way, this is a modification of the file <sage>/rings/polynomial/ I have removed most of the comments to able to reverse-engineer the code. However, I have kept the copyright note and added my name as "modified by". I hope this is enough to respect the authorship and legal notices. If somebody finds I made something wrong regarding license/authorship, please DO let me know so I can make the corrections.) You just have to save the code in a file, let's say "", and make from poly import *; this rest is like using regular Sage commands for infinite polynomial rings.

On the other hand, I can share with you an alternative less complicated that I just discovered a few hours ago. You can manually add members to an infinite polynomial ring as follows. First, you need to import the PolynomialRing constructor:

from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing

Then, you can create a Polynomial ring over $\mathbb{Z}$ by calling

P = PolynomialRing(ZZ, ('x_0_0'))

In this case, the only generator of P is x_0_0. You can make algebra with this element like this:


This will return 12*x_0_0, which is beautiful. You can also check the structure with


which will return

Univariate Polynomial Ring in x_0_0 over Integer Ring

Of course, this is far from what you wanted, but fear not. You can add a new generator as required. For example, suppose you need a x_0_1. You just have to write the following:

P = PolynomialRing(P.base_ring(), P.gens()+('x_0_1',))

The last comma is so that Sage recognizes the tuple; otherwise, it assumes it's a string inside parentheses. Anyway, now you can make algebra with this new element too. For example,


will return

8*x_0_0 + 4*x_0_1

You will also notice that


now says

Multivariate Polynomial Ring in x_0_0, x_0_1 over Integer Ring

so Sage automatically recognizes when you have univariate and multivariate rings. You keep adding variables as needed, without the need for them to be defined sequentially. For example, next you can create x_3_27.

In the depths of the Sage code that deals with Polynomial Rings, this is ROUGHLY equivalent to a "sparse" construction, which is an efficient way. You can wrap all of this code into a new Python class, so that dealing with it is easier.

A minimal working implementation can be found here. You can save the code inside a file, e.g. "" (in the same folder where you call Sage), and, in SageMath, you just have to "from import *". You can then define an infinite polynomial ring with bi-indexed variables using

P.<x> = PolyRing(ZZ)

That's it!

This needs to be completed as required. For example, you can't call, unless you define it first. However, it is a functional implementation. Besides that, the actual Polynomial Ring is stored inside P._P, so you could also call Finally, I have created a minimal test file here that will show you the usage (copy-and-paste it on the REPL).

I really hope this helps! Please, let me know if this solves your problem.

edit flag offensive delete link more


Hello, @ianncunha. Apparently, I made a mistake when uploading the modified code. I have updated the links in my answer. Please, let me know if you can access it.

dsejas gravatar imagedsejas ( 2019-05-16 01:01:18 +0200 )edit

Hello, @ianncunha. I have updated my answer. You can find pointers to a minimal implementation. Please, let me know if this solves your problem. If you require something extra about this, maybe I can help, but you should give me a little time to work. Best regards!

dsejas gravatar imagedsejas ( 2019-05-16 04:54:59 +0200 )edit

Hello, @ianncunha. Since it has been a long time since I posted my answer, I was wondering if it was useful for you. If that is the case, I would appreciate you to tick the answer as correct, so other people can find the answer if they need it.

dsejas gravatar imagedsejas ( 2019-10-15 17:38:28 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-05-10 20:54:43 +0200

Seen: 352 times

Last updated: May 16 '19