# monomial

monomial, a FORTRAN77 code which enumerates, lists, ranks, unranks and randomizes multivariate monomials in a space of D dimensions, with total degree less than N, equal to N, or lying within a given range.

A (univariate) monomial in 1 variable x is simply any (nonnegative integer) power of x:

```        1, x, x^2, x^3, ...
```
The exponent of x is termed the degree of the monomial.

Since any polynomial p(x) can be written as

```        p(x) = c(0) * x^0 + c(1) * x^1 + c(2) * x^2 + ... + c(n) * x^n
```
we may regard the monomials as a natural basis for the space of polynomials, in which case the coefficients may be regarded as the coordinates of the polynomial.

A (multivariate) monomial in D variables x(1), x(2), ..., x(d) is a product of the form

```        x(1)^e(1) * x(2)^e(2) * ... * x(d)^e(d)
```
where e(1) through e(d) are nonnegative integers. The sum of the exponents is termed the degree of the monomial.

Any polynomial in D variables can be written as a linear combination of monomials in D variables. The "total degree" of the polynomial is the maximum of the degrees of the monomials that it comprises. For instance, a polynomial in D = 2 variables of total degree 3 might have the form:

```        p(x,y) = c(0,0) x^0 y^0
+ c(1,0) x^1 y^0 + c(0,1) x^0 y^1
+ c(2,0) x^2 y^0 + c(1,1) x^1 y^1 + c(0,2) x^0 y^2
+ c(3,0) x^3 y^0 + c(2,1) x^2 y^1 + c(1,2) x^1 y^2 + c(0,3) x^0 y^3
```
The monomials in D variables can be regarded as a natural basis for the polynomials in D variables.

For multidimensional polynomials, a number of orderings are possible. Two common orderings are "grlex" (graded lexicographic) and "grevlex" (graded reverse lexicographic). Once an ordering is imposed, each monomial in D variables has a rank, and it is possible to ask (and answer!) the following questions:

• How many monomials are there in D dimensions, of degree N, or up to and including degree N, or between degrees N1 and N2?
• Can you list in rank order the monomials in D dimensions, of degree N, or up to and including degree N, or between degrees N1 and N2?
• Given a monomial in D dimensions, can you determine the rank it holds in the list of all such monomials?
• Given a rank, can you determine the monomial in D dimensions that occupies that position in the list of all such monomials?
• Can you select at random a monomial in D dimensions from the set of all such monomials of degree up to N?
• As mentioned, two common orderings for monomials are "grlex" (graded lexicographic) and "grevlex" (graded reverse lexicographic). The word "graded" in both names indicates that, for both orderings, one monomial is "less" than another if its total degree is less. Thus, for both orderings, xyz^2 is less than y^5 because a monomial of degree 4 is less than a monomial of degree 5.

But what happens when we compare two monomials of the same degree? For the lexicographic ordering, one monomial is less than another if its vector of exponents is lexicographically less. Given two vectors v1=(x1,y1,z1) and v2=(x2,y2,z2), v1 is less than v2 if

• x1 is less than x2;
• or x1 = x2, but y1 is less than y2;
• or x1 = x2, and y1 = y2, but z1 is less than z2;
(For a graded ordering, the third case can't occur, since we have assumed the two monomials have the same degree, and hence the exponents have the same sum.)

Thus, for the grlex ordering, we first order by degree, and then for two monomials of the same degree, we use the lexicographic ordering. Here is how the grlex ordering would arrange monomials in D=3 dimensions.

```        #  monomial   expon
--  ---------  -----
1  1          0 0 0

2        z    0 0 1
3     y       0 1 0
4  x          1 0 0

5        z^2  0 0 2
6     y  z    0 1 1
7     y^2     0 2 0
8  x     z    1 0 1
9  x  y       1 1 0
10  x^2        2 0 0

11        z^3  0 0 3
12     y  z^2  0 1 2
13     y^2z    0 2 1
14     y^3     0 3 0
15  x     z^2  1 0 2
16  x  y  z    1 1 1
17  x  y^2     1 2 0
18  x^2   z    2 0 1
19  x^2y       2 1 0
20  x^3        3 0 0

21        z^4  0 0 4
22     y  z^3  0 1 3
23     y^2z^2  0 2 2
24     y^3z    0 3 1
25     y^4     0 4 0
26  x     z^3  1 0 3
27  x  y  z^2  1 1 2
28  x  y^2z    1 2 1
29  x  y^3     1 3 0
30  x^2   z^2  2 0 2
31  x^2y  z    2 1 1
32  x^2y^2     2 2 0
33  x^3   z    3 0 1
34  x^3y       3 1 0
35  x^4        4 0 0

36        z^5  0 0 5
...  .........  .....
```

For the reverse lexicographic ordering, given two vectors, v1=(x1,y1,z1) and v2=(x2,y2,z2), v1 is less than v2 if:

• z1 is greater than z2;
• or z1 = z2 but y1 is greater than y2;
• or z1 = z2, and y1 = y2, but x1 is greater than x2.
(For a graded ordering, the third case can't occur, since we have assumed the two monomials have the same degree, and hence the exponents have the same sum.)

Thus, for the grevlex ordering, we first order by degree, and then for two monomials of the same degree, we use the reverse lexicographic ordering. Here is how the grevlex ordering would arrange monomials in D=3 dimensions.

```        #  monomial   expon
--  ---------  -----
1  1          0 0 0

2        z    0 0 1
3     y       0 1 0
4  x          1 0 0

5        z^2  0 0 2
6     y  z    0 1 1
7  x     z    1 0 1
8     y^2     0 2 0
9  x  y       1 1 0
10  x^2        2 0 0

11        z^3  0 0 3
12     y  z^2  0 1 2
13  x     z^2  1 0 2
14     y^2z    0 2 1
15  x  y  z    1 1 1
16  x^2   z    2 0 1
17     y^3     0 3 0
18  x  y^2     1 2 0
19  x^2y       2 1 0
20  x^3        3 0 0

21        z^4  0 0 4
22     y  z^3  0 1 3
23  x     z^3  1 0 3
24     y^2z^2  0 2 2
25  x  y  z^2  1 1 2
26  x^2   z^2  2 0 2
27     y^3z^1  0 3 1
28  x  y^2z    1 2 1
29  x^2y  z    2 1 1
30  x^3   z    3 0 1
31     y^4     0 4 0
32  x  y^3     1 3 0
33  x^2y^2     2 2 0
34  x^3y       3 1 0
35  x^4        4 0 0

36        z^5  0 0 5
...  .........  .....
```

### Languages:

monomial is available in a C version and a C++ version and a Fortran90 version and a MATLAB version and an Octave version and a Python version.

### Related Data and Programs:

combo, a FORTRAN77 library which includes routines for ranking, unranking, enumerating and randomly selecting balanced sequences, cycles, graphs, Gray codes, subsets, partitions, permutations, restricted growth functions, Pruefer codes and trees.

hermite_product_polynomial, a FORTRAN77 library which defines Hermite product polynomials, creating a multivariate polynomial as the product of univariate Hermite polynomials.

LEGENDRE_PRODUCT_POLYNOMIAL, a FORTRAN77 library which defines Legendre product polynomials, creating a multivariate polynomial as the product of univariate Legendre polynomials.

POLYNOMIAL, a FORTRAN77 library which adds, multiplies, differentiates, evaluates and prints multivariate polynomials in a space of M dimensions.

SET_THEORY, a FORTRAN77 library which demonstrates MATLAB commands that implement various set theoretic operations.

SUBSET, a FORTRAN77 library which enumerates, generates, ranks and unranks combinatorial objects including combinations, compositions, Gray codes, index sets, partitions, permutations, subsets, and Young tables.

### Source Code:

Last revised on 23 October 2023.