1 | initial version |
Sage has a constructor for polynomial rings in countably many variables.
Using this constructor, the polynomials $p_{n, k}$ in the question can be defined as follows.
Define the "infinite polynomial ring":
sage: R.<x> = InfinitePolynomialRing(QQ)
Define a function that can give the $p_{n, k}$:
def p(n, k):
if n not in NN or k not in NN or k > 2*n:
raise ValueError("Unsupported values: n = {}, k = {}".format(n, k))
if k == 0:
return R.zero()
if 1 <= k <= n:
return sum(x[j] for j in (1 .. k))
# case n < k <= 2* n:
return sum(x[j] for j in (1 .. 2*n-k+1))
Use this function:
sage: p(4, 3)
x_3 + x_2 + x_1
sage: p(4, 7)
x_2 + x_1
sage: p(4, 3) + p(4, 7)
x_3 + 2*x_2 + 2*x_1
2 | No.2 Revision |
Sage has a constructor for polynomial rings in countably many variables.
Using this constructor, the polynomials $p_{n, k}$ in the question can be defined as follows.
Define the "infinite polynomial ring":ring $\mathbb{Q}[x_0, x_1, x_2, ...]$:
sage: R.<x> = InfinitePolynomialRing(QQ)
Now x[j]
gives $x_j$.
sage: x[2]
x_2
sage: x[9876]
x_9876
Define a function p
such that can give the p(n, k)
gives $p_{n, k}$:
def p(n, k):
if n not in NN or k not in NN or k > 2*n:
raise ValueError("Unsupported values: n = {}, k = {}".format(n, k))
if k == 0:
return R.zero()
if 1 <= k <= n:
return sum(x[j] for j in (1 .. k))
# case n < k <= 2* n:
return sum(x[j] for j in (1 .. 2*n-k+1))
Use this function:Now p(n, k)
returns $p_{n, k}$:
sage: p(4, 3)
x_3 + x_2 + x_1
sage: p(4, 7)
x_2 + x_1
sage: p(4, 3) + p(4, 7)
x_3 + 2*x_2 + 2*x_1