ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 19 Jul 2014 02:59:44 +0200How Do I Extract Terms Containing Certain Coefficients From A Polynomial?https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/More specifically, I am trying to do small signal analysis on a power system, and I have some polynomial like this:
-D*I_L*Ron - I_L*Ron*d - D*Ron*i_L - Ron*d*i_L + D*V_g + V_g*d + D*v_g + d*v_g + D*V_C/n + V_C*d/n + D*v_C/n + d*v_C/n - V_C/n - v_C/n
I would like to take out terms including one of i_L, d, v_g, v_C, but not more. How can I do this? Fri, 18 Jul 2014 23:21:38 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/Answer by Rongcui Dong for <p>More specifically, I am trying to do small signal analysis on a power system, and I have some polynomial like this:</p>
<pre><code>-D*I_L*Ron - I_L*Ron*d - D*Ron*i_L - Ron*d*i_L + D*V_g + V_g*d + D*v_g + d*v_g + D*V_C/n + V_C*d/n + D*v_C/n + d*v_C/n - V_C/n - v_C/n
</code></pre>
<p>I would like to take out terms including one of i_L, d, v_g, v_C, but not more. How can I do this? </p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?answer=23488#post-id-23488Thanks for **Luca**'s answer! I am just expanding the code here
p1 = []
for m in p.iterator():
sum = 0
for x in (i_L, d, v_g, v_C):
sum += m({x:0}).is_zero()
if sum == 1:
p1.append(m)
p1Sat, 19 Jul 2014 01:42:31 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?answer=23488#post-id-23488Comment by Rongcui Dong for <p>Thanks for <strong>Luca</strong>'s answer! I am just expanding the code here</p>
<pre><code>p1 = []
for m in p.iterator():
sum = 0
for x in (i_L, d, v_g, v_C):
sum += m({x:0}).is_zero()
if sum == 1:
p1.append(m)
p1
</code></pre>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23491#post-id-23491Thank you! When I copied it, it just disappeared...Sat, 19 Jul 2014 02:58:17 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23491#post-id-23491Comment by Luca for <p>Thanks for <strong>Luca</strong>'s answer! I am just expanding the code here</p>
<pre><code>p1 = []
for m in p.iterator():
sum = 0
for x in (i_L, d, v_g, v_C):
sum += m({x:0}).is_zero()
if sum == 1:
p1.append(m)
p1
</code></pre>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23490#post-id-23490Careful: there's an indentation level missing after the second forSat, 19 Jul 2014 02:40:34 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23490#post-id-23490Answer by Gregory Bard for <p>More specifically, I am trying to do small signal analysis on a power system, and I have some polynomial like this:</p>
<pre><code>-D*I_L*Ron - I_L*Ron*d - D*Ron*i_L - Ron*d*i_L + D*V_g + V_g*d + D*v_g + d*v_g + D*V_C/n + V_C*d/n + D*v_C/n + d*v_C/n - V_C/n - v_C/n
</code></pre>
<p>I would like to take out terms including one of i_L, d, v_g, v_C, but not more. How can I do this? </p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?answer=23481#post-id-23481# Quick Question
...Did you intend I_L and i_L to be distinct? did you intend D and d to be distinct? how about v_C and V_C? In Sage, the capitalization matter, and these would be treated as distinct.
# Solution
Assuming that you wanted them to be distinct, the following will work:
var("D I_L Ron d i_L V_g v_g V_C n v_C")
f = -D*I_L*Ron - I_L*Ron*d - D*Ron*i_L - Ron*d*i_L + D*V_g + V_g*d + D*v_g + d*v_g + D*V_C/n + V_C*d/n + D*v_C/n + d*v_C/n - V_C/n - v_C/n
print "Before:"
print f
print "After:"
print f(i_L=0, d=0, v_g=0, v_C=0)
# Output
Before:
-D*I_L*Ron - I_L*Ron*d - D*Ron*i_L - Ron*d*i_L + D*V_g + V_g*d + D*v_g + d*v_g + D*V_C/n + V_C*d/n + D*v_C/n + d*v_C/n - V_C/n - v_C/n
After:
-D*I_L*Ron + D*V_g + D*V_C/n - V_C/n
Sat, 19 Jul 2014 00:36:53 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?answer=23481#post-id-23481Comment by Rongcui Dong for <h1>Quick Question</h1>
<p>...Did you intend I_L and i_L to be distinct? did you intend D and d to be distinct? how about v_C and V_C? In Sage, the capitalization matter, and these would be treated as distinct.</p>
<h1>Solution</h1>
<p>Assuming that you wanted them to be distinct, the following will work:</p>
<p>var("D I_L Ron d i_L V_g v_g V_C n v_C")</p>
<p>f = -D<em>I_L</em>Ron - I_L<em>Ron</em>d - D<em>Ron</em>i_L - Ron<em>d</em>i_L + D<em>V_g + V_g</em>d + D<em>v_g + d</em>v_g + D<em>V_C/n + V_C</em>d/n + D<em>v_C/n + d</em>v_C/n - V_C/n - v_C/n</p>
<p>print "Before:"</p>
<p>print f</p>
<p>print "After:"</p>
<p>print f(i_L=0, d=0, v_g=0, v_C=0)</p>
<h1>Output</h1>
<p>Before:</p>
<p>-D<em>I_L</em>Ron - I_L<em>Ron</em>d - D<em>Ron</em>i_L - Ron<em>d</em>i_L + D<em>V_g + V_g</em>d + D<em>v_g + d</em>v_g + D<em>V_C/n + V_C</em>d/n + D<em>v_C/n + d</em>v_C/n - V_C/n - v_C/n</p>
<p>After:</p>
<p>-D<em>I_L</em>Ron + D<em>V_g + D</em>V_C/n - V_C/n</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23483#post-id-23483Not quite... Yes, i_L and I_L are different. However, it is the lower-case ones that are being preserved: i_L, d, etc. So this is the opposite of what I need... I am reading about pattern match in document, but I have not found useful information yet.Sat, 19 Jul 2014 01:22:18 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23483#post-id-23483Answer by Luca for <p>More specifically, I am trying to do small signal analysis on a power system, and I have some polynomial like this:</p>
<pre><code>-D*I_L*Ron - I_L*Ron*d - D*Ron*i_L - Ron*d*i_L + D*V_g + V_g*d + D*v_g + d*v_g + D*V_C/n + V_C*d/n + D*v_C/n + d*v_C/n - V_C/n - v_C/n
</code></pre>
<p>I would like to take out terms including one of i_L, d, v_g, v_C, but not more. How can I do this? </p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?answer=23482#post-id-23482I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing `p` is your polynomial expression, here's a fancy solution
[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
There's certainly many other, but I am afraid none is going to be very simple. A word of explanation:
- The first `for` loops over each monomial.
- For each monomial `m`, `m({x:0})` evaluates the monomial at the point `x=0`, where `x` ranges over `i_L`, `d`, etc.
- If `is_zero()` returns `True`, the variable is obviously in the monomial.
- In a summation, `True` and `False` get converted to 0 and 1, thus `sum(...) == 1` guarantees that the monomial contains exactly one of the variables `i_l`, `d`, etc.
Thanks for this refreshing riddle :)Sat, 19 Jul 2014 00:48:16 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?answer=23482#post-id-23482Comment by Luca for <p>I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing <code>p</code> is your polynomial expression, here's a fancy solution</p>
<pre><code>[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
</code></pre>
<p>There's certainly many other, but I am afraid none is going to be very simple. A word of explanation: </p>
<ul>
<li>The first <code>for</code> loops over each monomial. </li>
<li>For each monomial <code>m</code>, <code>m({x:0})</code> evaluates the monomial at the point <code>x=0</code>, where <code>x</code> ranges over <code>i_L</code>, <code>d</code>, etc.</li>
<li>If <code>is_zero()</code> returns <code>True</code>, the variable is obviously in the monomial. </li>
<li>In a summation, <code>True</code> and <code>False</code> get converted to 0 and 1, thus <code>sum(...) == 1</code> guarantees that the monomial contains exactly one of the variables <code>i_l</code>, <code>d</code>, etc.</li>
</ul>
<p>Thanks for this refreshing riddle :)</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23489#post-id-23489To convert back to polynomial, just sum() over the list.Sat, 19 Jul 2014 02:37:15 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23489#post-id-23489Comment by Rongcui Dong for <p>I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing <code>p</code> is your polynomial expression, here's a fancy solution</p>
<pre><code>[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
</code></pre>
<p>There's certainly many other, but I am afraid none is going to be very simple. A word of explanation: </p>
<ul>
<li>The first <code>for</code> loops over each monomial. </li>
<li>For each monomial <code>m</code>, <code>m({x:0})</code> evaluates the monomial at the point <code>x=0</code>, where <code>x</code> ranges over <code>i_L</code>, <code>d</code>, etc.</li>
<li>If <code>is_zero()</code> returns <code>True</code>, the variable is obviously in the monomial. </li>
<li>In a summation, <code>True</code> and <code>False</code> get converted to 0 and 1, thus <code>sum(...) == 1</code> guarantees that the monomial contains exactly one of the variables <code>i_l</code>, <code>d</code>, etc.</li>
</ul>
<p>Thanks for this refreshing riddle :)</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23484#post-id-23484What is that syntax? I don't know about this in python...Sat, 19 Jul 2014 01:26:10 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23484#post-id-23484Comment by Rongcui Dong for <p>I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing <code>p</code> is your polynomial expression, here's a fancy solution</p>
<pre><code>[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
</code></pre>
<p>There's certainly many other, but I am afraid none is going to be very simple. A word of explanation: </p>
<ul>
<li>The first <code>for</code> loops over each monomial. </li>
<li>For each monomial <code>m</code>, <code>m({x:0})</code> evaluates the monomial at the point <code>x=0</code>, where <code>x</code> ranges over <code>i_L</code>, <code>d</code>, etc.</li>
<li>If <code>is_zero()</code> returns <code>True</code>, the variable is obviously in the monomial. </li>
<li>In a summation, <code>True</code> and <code>False</code> get converted to 0 and 1, thus <code>sum(...) == 1</code> guarantees that the monomial contains exactly one of the variables <code>i_l</code>, <code>d</code>, etc.</li>
</ul>
<p>Thanks for this refreshing riddle :)</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23485#post-id-23485Wow, after some careful study, this is indeed a fancy solution which is equivalent to 7 lines of code. What is this syntax called? Is it a standard python feature?Sat, 19 Jul 2014 01:36:39 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23485#post-id-23485Comment by Rongcui Dong for <p>I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing <code>p</code> is your polynomial expression, here's a fancy solution</p>
<pre><code>[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
</code></pre>
<p>There's certainly many other, but I am afraid none is going to be very simple. A word of explanation: </p>
<ul>
<li>The first <code>for</code> loops over each monomial. </li>
<li>For each monomial <code>m</code>, <code>m({x:0})</code> evaluates the monomial at the point <code>x=0</code>, where <code>x</code> ranges over <code>i_L</code>, <code>d</code>, etc.</li>
<li>If <code>is_zero()</code> returns <code>True</code>, the variable is obviously in the monomial. </li>
<li>In a summation, <code>True</code> and <code>False</code> get converted to 0 and 1, thus <code>sum(...) == 1</code> guarantees that the monomial contains exactly one of the variables <code>i_l</code>, <code>d</code>, etc.</li>
</ul>
<p>Thanks for this refreshing riddle :)</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23486#post-id-23486Also, after I get the list, what should I do next to convert back to polynomial?Sat, 19 Jul 2014 01:37:55 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23486#post-id-23486Comment by Rongcui Dong for <p>I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing <code>p</code> is your polynomial expression, here's a fancy solution</p>
<pre><code>[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
</code></pre>
<p>There's certainly many other, but I am afraid none is going to be very simple. A word of explanation: </p>
<ul>
<li>The first <code>for</code> loops over each monomial. </li>
<li>For each monomial <code>m</code>, <code>m({x:0})</code> evaluates the monomial at the point <code>x=0</code>, where <code>x</code> ranges over <code>i_L</code>, <code>d</code>, etc.</li>
<li>If <code>is_zero()</code> returns <code>True</code>, the variable is obviously in the monomial. </li>
<li>In a summation, <code>True</code> and <code>False</code> get converted to 0 and 1, thus <code>sum(...) == 1</code> guarantees that the monomial contains exactly one of the variables <code>i_l</code>, <code>d</code>, etc.</li>
</ul>
<p>Thanks for this refreshing riddle :)</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23487#post-id-23487I found it, it's called _list comprehension_. Thank you!Sat, 19 Jul 2014 01:40:23 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23487#post-id-23487Comment by Rongcui Dong for <p>I am not sure how I should interpret your criterion: do you want to allow a variable to appear more than once in the same monomial? Supposing you do, and supposing <code>p</code> is your polynomial expression, here's a fancy solution</p>
<pre><code>[m for m in p.iterator() if sum(m({x:0}).is_zero() for x in (i_L, d, v_g, v_C)) == 1]
</code></pre>
<p>There's certainly many other, but I am afraid none is going to be very simple. A word of explanation: </p>
<ul>
<li>The first <code>for</code> loops over each monomial. </li>
<li>For each monomial <code>m</code>, <code>m({x:0})</code> evaluates the monomial at the point <code>x=0</code>, where <code>x</code> ranges over <code>i_L</code>, <code>d</code>, etc.</li>
<li>If <code>is_zero()</code> returns <code>True</code>, the variable is obviously in the monomial. </li>
<li>In a summation, <code>True</code> and <code>False</code> get converted to 0 and 1, thus <code>sum(...) == 1</code> guarantees that the monomial contains exactly one of the variables <code>i_l</code>, <code>d</code>, etc.</li>
</ul>
<p>Thanks for this refreshing riddle :)</p>
https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23492#post-id-23492OK... Well, I searched for all kinds of constructors and omitted this simple way...Sat, 19 Jul 2014 02:59:44 +0200https://ask.sagemath.org/question/23477/how-do-i-extract-terms-containing-certain-coefficients-from-a-polynomial/?comment=23492#post-id-23492