Re: [Tutor] Multiplication of polynomials
by Benoit Dupire other posts by this author
Apr 29 2001 7:39PM messages near this date
Re: [Tutor] Multiplication of polynomials
|
Re: [Tutor] Multiplication of polynomials
you would want to have several functions
one to define a polynom as a name
one to add 2 polynoms
one to multiply 2 polynoms
and it would be nice to propose a menu to the user, so that he can
choose what he wants to do
A frame for your program could be ( i did not debug it...)
polynome= {}
def initPolynom():
global polynome
name= raw_input('Polynom name ?')
degree=raw_input('degree ?')
coeff=[]
for i in range (0, degree+1):
coefficient = raw_input('coeff for x^ %i' %i)
coeff.append(coefficient)
polynome[name]=coeff
def multiply():
global polynome
name1= raw_input('Polynom name 1?')
name2= raw_input('Polynom name 2?')
pol1= polynome[name1]
pol2 =polynome[name2]
# perform here the multiplication logic. pol3 = pol1 * pol2
print pol3
name3 = raw_input('Give a name for the result..')
polynome[name3]=pol3
def add():
pass
# and now ... the menu
menu=['init': initPolynom, 'multiply': multiply, 'add': add]
for item in menu.keys():
print item
choice = raw_input('your choice')
if menu.has_key(choice):
menu[choice]()
Julieta Rangel wrote:
> If I want to write a program that multiplies polynomials, just to show
> you how illiterate I am at computer programming, this is what I would
> do: I would need the user to enter the amount of polynomials that will
> be multiplying. Lets say the user wants to multiply two polynomials.
> Once the user enters the amount of polynomials that will be
> multiplying, the computer would have to ask the degree of the
> polynomials, say the first polynomial is of third degree and the
> second is a second degree polynomial (2x^3 + x^2 + 3x + 2) * (x^2 +
> 1)Once the person enters the degree of the polynomials, the computer
> would have to ask the coefficient of each of the terms of the
> polynomials, in this case the computer would have to ask the user to
> enter the coefficient of x^3, x^2, x, and c and then the coefficients
> of the second polynomial. Should I have the computer create two lists
> for each polynomial? In one list I would store all the coefficients
> and in another the exponents of the expression.
>
>
> One list is enough (just the coeff): coeff[0] is coeff for exp. 0,
> etc...
>
>
> I'm assuming that if I have two lists for each polynomial, I could do
> the operations required. Based on the polynomials I have, the lists
> of coefficients would look like (L1) [2,1,3,2] and (L2)[1,0,1] and my
> lists of exponents would look like (L 3) [3,2,1,0] and (L 4)[2,1,0]. I
> would then have the first element in L1 times each element in L2, so
> that I get list 5 [2,0,2]and 1st element in L3 + each element in L4,
> so that I get list 6 [5,4,3]. Then I would have the 2nd element in L1
> times each element in L2 so that I get another list [1,0,1] and also
> have the 2nd element in L3 + each element in L4, so that I get
> [4,3,2]. This would go on for each element and I would end up with 4
> more lists [3,0,3], [3,2,1], [2,0,2], [2,1,0]. I would then take the
> numbers from my lists and have the computer write the coefficients
> along with the exponents: 2x^5 + 0x^4 + 2x^3 + x^4
> +0x^3+x^2+3x^3+0x^2 +3x+ 2x^2+0x+2. I would then have to get rid of
> those terms with zero coefficients and combine like terms. Is this
> crazy or what? How could I accomplish this? Like I told you before,
> this is all new to me, but I'm really interested in finding out how
> this complex task can be accomplished in Python. Can you
> help? Julieta
>
>
> -----------------------------------------------------------------------
> Get your FREE download of MSN Explorer at http://explorer.msn.com
> _______________________________________________ Tutor maillist -
> Tutor@[...].org http://mail.python.org/mailman/listinfo/tutor
--
Benoit Dupire
Graduate Student
----------------
I'd like to buy a new Boomerang. How can i get rid of the old one?
_______________________________________________
Tutor maillist - Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Julieta Rangel
Remco Gerlich
Benoit Dupire
Sheila King
|