[Tutor] integration of polynomials in Python [And SICP for free!]

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 2 May 2001 00:24:51 -0700 (PDT)


On Wed, 2 May 2001, Julieta Rangel wrote:

> I finally finished the differentiation program.  What changes should I make 
> so that instead of differentiating, my program integrates polynomials.  Can 
> anyone help?  Here you have my finished program.  It is not the best program 
> you'll ever see :) but I'm very proud of it.  It took me forever to put it 
> together, eventhough I had to ask you guys for help in writing every line. 
> Thanks a lot! :) Anyway, here you have the program.  What can I do to make 
> it integrate?

Let's have this placed in Useless Python.  If you ever write a script that
you're happy with, feel free to contribute it to the Useless Python
repository.  It's at:

    http://www.lowerstandard.com/python/pythonsource.html

You program looks good; I'm happy that the differentiation works ok.


Integration is actually not too bad either; think about what happens when
we integrate polynomials:

    f(x) = 1x^3 + 2x^2 + 3x + 4

or, if we reverse the order of the coefficients:

    f(x) = 4 + 3x + 2x^2 + 1x^3

If we integrate this, we end up with the polynomial:

    f(x) = 4        3   2     2   3     1   4
           - x   +  - x^   +  - x^   +  - x^
           1        2         3         4


If you try a few examples by hand, I think you'll figure out a way to
fiddle with your list of coefficients to get things working.


Also, I second Alan's recommendation on Structure and Interpretation of
Computer Programs: it's one of the best CS books I've read.  You seem to
have a mathy slant, and I think you'll enjoy this book a LOT.  The authors
are really generous folk: the book is actually published online as well!

    http://mitpress.mit.edu/sicp/

So you don't even need to leave the house... although that's not quite a
good thing.  Take a nice walk first; you'll need to keep your head clear
while reading this stuff.  *grin*  If you like the book, buy it; it has a
nice feel.


Good luck!