Ask Your Question

Defining family of multivariable polynomials

asked 2019-05-03 04:41:35 +0200

DCL gravatar image

updated 2019-05-07 04:26:23 +0200

Brand new to Sage here and trying to define a family of polynomials indexed by natural numbers. In particular, I'd like to be able to generate then perform symbolic calculations with the family of polynomials defined for all $n\in \mathbb{N}$ and all $k=0,\dotsc, 2n$ by $$p_{n,k}=\begin{cases} 0 & \textrm{if } k=0\newline \sum_{j=1}^k x_j&\textrm{if } k\leq n\newline \sum_{j=1}^{2n-k+1}x_j&\textrm{if }k>n \end{cases}$$

So far the attempts that I've had are of the form:

sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])

but I don't seem to easily perform calculations with these. Another method I was trying is defining $\mathbb{Q}[x_0,\dotsc, x_n]$ then trying to define these polynomials using conditional statements. I seem to keep getting errors stating my variables don't exist.

Would love some help or a hint.

edit retag flag offensive close merge delete


Your $p_k$ silently depends on $n$. So you want to fix $n$ and study the $p_k$?

vdelecroix gravatar imagevdelecroix ( 2019-05-03 08:07:34 +0200 )edit

As you write them, your $p_k$ are first-degrees polynomials of $n$ variables at most. Is that really what you want ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-05-03 08:55:42 +0200 )edit

That is correct, I'd like to fix any $n$ then study the $p_k$.

Yes, these will be degree $1$ polynomials in at most $n$ variables.

DCL gravatar imageDCL ( 2019-05-03 15:27:01 +0200 )edit


  • it might make sense to call the polynomials $p_{n, k}$.
  • in the case $0 < k \le n$, the formula should have $x_j$, not $x_k$
  • indenting the code line h = lambda k: ... by 4 spaces will make it display as code
slelievre gravatar imageslelievre ( 2019-05-03 18:58:46 +0200 )edit

Thanks for taking all the comments into account and editing your question!

slelievre gravatar imageslelievre ( 2019-05-04 01:00:34 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-05-03 18:55:25 +0200

slelievre gravatar image

updated 2019-05-03 19:04:16 +0200

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 polynomial ring $\mathbb{Q}[x_0, x_1, x_2, ...]$:

sage: R.<x> = InfinitePolynomialRing(QQ)

Now x[j] gives $x_j$.

sage: x[2]
sage: x[9876]

Define a function p such that 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:
    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))

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
edit flag offensive delete link more


Thanks, this works perfectly!

DCL gravatar imageDCL ( 2019-05-03 20:08:23 +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-03 04:41:35 +0200

Seen: 104 times

Last updated: May 07 '19