[Tutor] inserting items into a list
Sheila King
sheila@thinkspot.net
Thu, 03 May 2001 16:27:00 -0700
Julieta,
I really wouldn't go about this, quite that way. It isn't necessary to
generate separate lists, filled with zeros, and then sum those to get your
result. I will repeat here, something that I posted a few days back. Perhaps
you missed it, because there was so much new stuff for you to learn at that
point.
Posted on April 29th:
SK> In order to multiply two polynomials, you would need to take each
SK> coefficient in the one list and multiply it be each coefficient in the
SK> other list.
SK>
SK> So, your suggested problem
SK> (2x^3 + x^2 + 3x + 2) * (x^2 + 1)
SK>
SK> might look like this:
SK> p1 = [2, 3, 1, 2]
SK> p2 = [1, 0, 1]
SK>
SK> I would note, that since you are multiplying a 3rd degree polynomial by a
SK> 2nd degree one, that your result should be fifth degree. You could find
SK> this out by doing len(p1)-1 added to len(p2)-1 then create a new
SK> list to store the results of your multiplication. Give it length of
SK> 5 and initialize all spots to zero. Let's call this new list for the
SK> result, p3.
SK>
SK> Now create a loop to go through each element of p1.
SK> Start with p1[0] and multiply it by p2[0] and add the result to p3[0].
SK> Now multiply p1[0] with p2[1] and add the result to p3[1].
SK> Now multiply p1[0] with p2[2] and add the result to p3[2].
SK>
SK> All done with p1[0]. Now the loop moves to p1[1].
SK> Multiply p1[1] with p2[0] and add the result to p3[1].
SK> Now multiply p1[1] with p2[1] and add the result to p3[2].
SK> Now multiply p1[1] with p2[2] and add the result to p3[3].
SK>
SK> Note, that the indices indicate the exponent on your variable. So, p1[1]
SK> is an x^1 coefficient and p2[2] is an x^2 coefficient, so the result needs
SK> to be an x^3 coefficient, so add that result to p3[3].
SK>
SK> Proceed on to p1[2] and p1[3] in the same manner. So, you have a loop on
SK> p1, and inside that loop is another loop that iterates over all the
SK> indices in p2.
SK>
SK> I hope this is helpful.
If what I wrote above isn't clear, please ask and I will try to explain it
better.
--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/
On Thu, 03 May 2001 12:37:35 -0500, "Julieta Rangel"
<julieta_rangel@hotmail.com> wrote about [Tutor] inserting items into a list:
:I'm trying to come up with a definition for multiplication of polynomials.
:Say I want to multiply (2 + 3x + x^2 + 2x^3) times (1+0x+2x^2), so I created
:two lists of coefficients. List 1 =[2,3,1,2] and list 2= [1,0,2]. To
:multiply these two I need to take each element from list 1 and multiply it
:by all of the elements of list 2, and create a new list for each result.
:For example, by multiplying the first element of list 1 by all of the
:elemens of list 2 I would get [2,0,4], from the second #, I would get
:[3,0,6], then [1,0,2] and finally [2,0,4]. I need to find a way to insert 3
:zeroes at the end of list [2,0,4]; 1 zero at the beginning and two at the
:end of [3,0,6]; 2 zeroes a the beginning and one at the end of [1,0,2], and
:finally 3 zeroes at the beginning of [2,0,4]. This way the new lists would
:look like:
:
:[2,0,4,0,0,0]
:[0,3,0,6,0,0]
:[0,0,1,0,2,0]
:[0,0,0,2,0,4]
:
:and it would be just a matter of adding those lists to figure out the
:result: 2 + 3x + 5x^2 + 8x^3 + 2x^4 + 4x^5. As you can see from the
:subject, my problem is inserting those zeroes at the specific locations.
:Can you help me?
:
:Julieta
:
:If you notice, there is a little pattern
:_________________________________________________________________
:Get your FREE download of MSN Explorer at http://explorer.msn.com
:
:
:_______________________________________________
:Tutor maillist - Tutor@python.org
:http://mail.python.org/mailman/listinfo/tutor