Writings of a techie wizard
|
Indexes
Categories
Tags
computers (35)
economics (5) education (3) fantasy (3) history (5) info (8) movies (3) politics (40) science (8) Archives
2015‑Jan (1)
2014‑Sep (2) 2014‑Jul (1) 2014‑Jun (1) 2014‑May (1) 2014‑Apr (2) 2014‑Mar (2) 2014‑Jan (1) 2013‑Nov (1) 2013‑Oct (1) 2013‑Sep (2) 2013‑Aug (2) 2013‑Jun (1) 2013‑Apr (1) 2013‑Mar (1) 2013‑Feb (1) 2013‑Jan (2) 2012‑Dec (3) 2012‑Nov (2) 2012‑Oct (1) 2012‑Sep (3) 2012‑Aug (1) 2012‑Jul (1) 2012‑Jun (1) 2012‑Apr (2) 2012‑Mar (2) 2012‑Feb (1) 2012‑Jan (2) 2011‑Dec (1) 2011‑Nov (3) 2011‑Oct (2) 2011‑Sep (4) 2011‑Aug (6) 2011‑Jul (7) 2011‑Jun (8) |
Archive: 2011‑Dec
Thu, 15 Dec 2011
Years ago, Doug McIlroy, the inventor of the Unix pipe, published a paper on techniques for computing the terms of power series. The paper talks about a number of key concepts in programming, such as "lazy" evaluation, that were not well supported by most programming languages at the time, which is why McIlroy spent a good portion of the paper describing an implementation of his techniques in a new language designed by Rob Pike. I came across this paper recently and realized that Python's generators would be a perfect fit for representing power series. They support all the key techniques McIlroy described, particularly "lazy" evaluation (a generator doesn't compute any specific term of its series until it is asked for it in sequence). You can see the Python implementation I came up with on github here. A particularly neat feature is that you can recursively include a Python generator in itself; this allows the recursive nature of many power series to be directly represented in the code. For example, here's the exponential series: def _exp(): for term in integral(EXP, Fraction(1, 1)): yield term EXP = PowerSeries(_exp) No monkey business with factorials; just a generator that recursively integrates itself. This same trick also works for implementing operations on power series; for example, any series can be exponentiated by a method similar to the above. The reciprocal and inverse operations on series use similar tricks, which basically make the code look just like the mathematical descriptions of those operations in McIlroy's paper. Once I got the Python implementation working smoothly, I began checking online to see what other recent implementations of these techniques existed, and found that McIlroy posted an implementation in Haskell on the web in 2007. All of the key operations are one-liners. This is possible because Haskell has built-in support for expressing these operations declaratively, instead of having to define functions and use for loops and so on. So in a sense, my Python implementation is a case of Greenspun's Tenth Rule. But it's still fun. |
Wizard Projects
Site Links
Open Source Projects
Old Open Source Projects
Python Recipes
Fun Stuff
Shameless Plugs
Copyright © 2011-2015
by Peter A. Donis All Rights Reserved |