# polynomial rings

Create a polynomial ring using the usual mathematical notation.
 i1 : R = QQ[x,y,z]; i2 : R o2 = R o2 : PolynomialRing
Notice that after assignment to a global variable, Macaulay2 knows the ring's name, and this name is used when printing the ring.The original description of the ring can be recovered with describe.
 i3 : describe R o3 = QQ[x..z, Degrees => {3:1}, Heft => {1}, MonomialOrder => {MonomialSize => 32}, DegreeRank => 1] {GRevLex => {3:1} } {Position => Up }
Use the following subscript notation to obtain 0,1, or any multiple of 1, as elements in the ring.
 i4 : 0_R o4 = 0 o4 : R i5 : 1_R o5 = 1 o5 : R i6 : 11_R o6 = 11 o6 : R
Obtain the variables (generators) of the ring by subscripting the name of the ring. As always in Macaulay2, indexing starts at 0.
 i7 : R_0^10+R_1^3+R_2 10 3 o7 = x + y + z o7 : R
The number of variables is provided by numgens.
 i8 : numgens R o8 = 3 i9 : apply(numgens R, i -> R_i^i) 2 o9 = {1, y, z } o9 : List i10 : sum(numgens R, i -> R_i^i) 2 o10 = z + y + 1 o10 : R
(See apply and sum.) Use generators to obtain a list of the variables of the ring.
 i11 : gens R o11 = {x, y, z} o11 : List
A matrix (with one row) containing the variables of the ring can be obtained using vars(Ring).
 i12 : vars R o12 = | x y z | 1 3 o12 : Matrix R <--- R
The index of a variable:
 i13 : index x, index y, index z o13 = (0, 1, 2) o13 : Sequence
The coefficient ring can be recovered with coefficientRing.
 i14 : coefficientRing R o14 = QQ o14 : Ring
A random homogeneous element can be obtained with random.
 i15 : random(2,R) 9 2 1 1 2 9 3 2 o15 = -x + -x*y + -y + -x*z + y*z + -z 2 2 2 4 4 o15 : R
A basis of the subspace of ring elements of a given degree can be obtained in matrix form with basis.
 i16 : basis(2,R) o16 = | x2 xy xz y2 yz z2 | 1 6 o16 : Matrix R <--- R
We may construct polynomial rings over polynomial rings.
 i17 : ZZ[a,b,c][d,e,f];
When displaying an element of an iterated polynomial ring, parentheses are used to organize the coefficients recursively, which may themselves be polynomials.
 i18 : (a+d+1)^2 2 2 o18 = d + (2a + 2)d + a + 2a + 1 o18 : ZZ[a..c][d..f]
Variable names may be words.
 i19 : QQ[rho,sigma,tau]; i20 : (rho - sigma)^2 2 2 o20 = rho - 2rho*sigma + sigma o20 : QQ[rho, sigma, tau]
There are various other ways to specify the variables in a polynomial ring. A sequence of variables can be obtained as follows.
 i21 : ZZ[b..k];
In this example, if you had previously assigned either b or k a value that was not a ring generator, then Macaulay2 would complain about this: it would no longer understand what variables you wanted. To get around this, we could either do
 i22 : ZZ[symbol b .. symbol k];
or we may obtain the single-letter variables with vars.
 i23 : vars (0..4) o23 = (a, b, c, d, e) o23 : Sequence i24 : ZZ[vars (0..4),vars(26..30),vars 51] o24 = ZZ[a..e, A..E, Z] o24 : PolynomialRing
Subscripted variables can be used, provided the base for the subscripted variable has not been used for something else.
 i25 : ZZ[t,p_0,p_1,q_0,q_1];
Sequences of subscripted variables can also be used.
 i26 : ZZ[p_(0,0) .. p_(2,1),q_0..q_5] o26 = ZZ[p ..p , q ..q ] 0,0 2,1 0 5 o26 : PolynomialRing i27 : (p_(0,0)+q_2-1)^2 2 2 o27 = p + 2p q + q - 2p - 2q + 1 0,0 0,0 2 2 0,0 2 o27 : ZZ[p ..p , q ..q ] 0,0 2,1 0 5
The subscripts can be much more general, but care is required when using symbols as subscripts, for the symbols may acquire values later that would interfere with your original use of them as symbols. Thus you should protect symbols that will be used in this way.
 i28 : protect xx; protect yy; protect zz; i31 : ZZ[ee_[xx],ee_[yy],ee_[zz]] o31 = ZZ[ee , ee , ee ] [xx] [yy] [zz] o31 : PolynomialRing
Polynomial rings over polynomial rings work:
 i32 : R = QQ[a,b][x] o32 = R o32 : PolynomialRing i33 : (a+b+x)^3 3 2 2 2 3 2 2 3 o33 = x + (3a + 3b)x + (3a + 6a*b + 3b )x + a + 3a b + 3a*b + b o33 : R

Internally, the polynomials in such towers are expressed in terms of a flattened monoid containing all the variables, obtainable with the key FlatMonoid.

 i34 : R.FlatMonoid o34 = monoid[x, a..b, Degrees => {{1}, 2:{0}}, Heft => {2:1}, MonomialOrder => {MonomialSize => 32}, DegreeRank => 2] {0} {1} {GRevLex => {1} } {Position => Up } {GRevLex => {2:1} } o34 : GeneralOrderedMonoid
Some things to watch out for when using polynomial rings:
• Defining a ring twice gives different rings, as far as Macaulay2 is concerned: We use the strict comparison operator === to demonstrate this.  i35 : ZZ[a,b,c] === ZZ[a,b,c] o35 = false
Thus it is a good idea to assign a new ring to a variable for future reference.