ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 03 May 2019 18:00:34 -0500Defining family of multivariable polynomialshttp://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/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.Thu, 02 May 2019 21:41:35 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/Comment by slelievre for <p>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}$$</p>
<p>So far the attempts that I've had are of the form:</p>
<pre><code>sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])
</code></pre>
<p>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.</p>
<p>Would love some help or a hint.</p>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46443#post-id-46443Thanks for taking all the comments into account and editing your question!Fri, 03 May 2019 18:00:34 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46443#post-id-46443Comment by slelievre for <p>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}$$</p>
<p>So far the attempts that I've had are of the form:</p>
<pre><code>sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])
</code></pre>
<p>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.</p>
<p>Would love some help or a hint.</p>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46435#post-id-46435Comments:
- 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 codeFri, 03 May 2019 11:58:46 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46435#post-id-46435Comment by DCL for <p>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}$$</p>
<p>So far the attempts that I've had are of the form:</p>
<pre><code>sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])
</code></pre>
<p>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.</p>
<p>Would love some help or a hint.</p>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46433#post-id-46433That 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.Fri, 03 May 2019 08:27:01 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46433#post-id-46433Comment by Emmanuel Charpentier for <p>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}$$</p>
<p>So far the attempts that I've had are of the form:</p>
<pre><code>sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])
</code></pre>
<p>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.</p>
<p>Would love some help or a hint.</p>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46429#post-id-46429As you write them, your $p_k$ are first-degrees polynomials of $n$ variables at most. Is that really what you want ?Fri, 03 May 2019 01:55:42 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46429#post-id-46429Comment by vdelecroix for <p>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}$$</p>
<p>So far the attempts that I've had are of the form:</p>
<pre><code>sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])
</code></pre>
<p>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.</p>
<p>Would love some help or a hint.</p>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46428#post-id-46428Your $p_k$ silently depends on $n$. So you want to fix $n$ and study the $p_k$?Fri, 03 May 2019 01:07:34 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46428#post-id-46428Answer by slelievre for <p>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}$$</p>
<p>So far the attempts that I've had are of the form:</p>
<pre><code>sage: h = lambda k:sum([var('d_%d' %(i+1)) for i in range(k)])
</code></pre>
<p>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.</p>
<p>Would love some help or a hint.</p>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?answer=46434#post-id-46434Sage 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]
x_2
sage: x[9876]
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:
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))
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_1Fri, 03 May 2019 11:55:25 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?answer=46434#post-id-46434Comment by DCL for <p>Sage has a constructor for polynomial rings in countably many variables.</p>
<p>Using this constructor, the polynomials $p_{n, k}$ in the question can be defined as follows.</p>
<p>Define the polynomial ring $\mathbb{Q}[x_0, x_1, x_2, ...]$:</p>
<pre><code>sage: R.<x> = InfinitePolynomialRing(QQ)
</code></pre>
<p>Now <code>x[j]</code> gives $x_j$.</p>
<pre><code>sage: x[2]
x_2
sage: x[9876]
x_9876
</code></pre>
<p>Define a function <code>p</code> such that <code>p(n, k)</code> gives $p_{n, k}$:</p>
<pre><code>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))
</code></pre>
<p>Now <code>p(n, k)</code> returns $p_{n, k}$:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46437#post-id-46437Thanks, this works perfectly!Fri, 03 May 2019 13:08:23 -0500http://ask.sagemath.org/question/46426/defining-family-of-multivariable-polynomials/?comment=46437#post-id-46437