<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Squarespace Site Server v5.11.81 (http://www.squarespace.com/) on Fri, 24 Feb 2012 15:21:33 GMT--><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rss="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:cc="http://web.resource.org/cc/"><rss:channel rdf:about="http://markwatson.us/updates/"><rss:title>Updates</rss:title><rss:link>http://markwatson.us/updates/</rss:link><rss:description></rss:description><dc:language>en-US</dc:language><dc:date>2012-02-24T15:21:33Z</dc:date><admin:generatorAgent rdf:resource="http://www.squarespace.com/">Squarespace Site Server v5.11.81 (http://www.squarespace.com/)</admin:generatorAgent><rss:items><rdf:Seq><rdf:li rdf:resource="http://markwatson.us/updates/functional-lagrange-interpolation.html"/><rdf:li rdf:resource="http://markwatson.us/updates/python-super_split.html"/><rdf:li rdf:resource="http://markwatson.us/updates/dmoz-scraper-1.html"/></rdf:Seq></rss:items></rss:channel><rss:item rdf:about="http://markwatson.us/updates/functional-lagrange-interpolation.html"><rss:title>Functional Lagrange Interpolation</rss:title><rss:link>http://markwatson.us/updates/functional-lagrange-interpolation.html</rss:link><dc:creator>Mark Watson</dc:creator><dc:date>2011-05-06T20:52:00Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p>I recently implemented Lagrange’s interpolation method in python, and I thought I would share it here. I’m posting it because I think it’s the most nested functions I ever used in python. I would have like to use partial functions, but python doesn’t support those (well not without using functools), so I made do with nested functions...</p>

<p>Probably don’t use the code in a production environment or for anything serious because it’s probably very slow. I’m 90% sure numpy has a better implementation.</p>

<pre class="brush: python">
# Tested on Windows 7 and Python 2.7

# the code
def lagrangian_interpolate(samples):
    """
    Takes some samples as a list of tuples and returns a function that's
    a lagrangian interpolation of all the samples.
    """
    X = 0 # the tuple index of the X variable in the samples
    Y = 1 # the tuple index of the Y variable in the samples
    n = len(samples)
    # define the L function as a function generator that generates L functions
    # for a given i
    def L(i):
        "This function generates an L function for a given x_i"
        def L_gen(x):
            ret = []
            for j in xrange(n):
                if j != i:
                    ret.append((x - samples[j][X])/(samples[i][X] - samples[j][X]))
            return reduce(lambda a,b: a*b, ret)
        return L_gen

    return lambda x: sum(L(i)(x) * samples[i][Y] for i in xrange(n))

# main
prob_1 = lagrangian_interpolate([(2,1.4142),(2.5,1.5811),(3.0,1.7321)])
print prob_1(2.2)

prob_1_b = lagrangian_interpolate([(2,1.4142),(2.5,1.5811),(2.7,1.6432)])
print prob_1_b(2.2)

prob_2 = lagrangian_interpolate([(2.0,1.4142),(2.5,1.5811),(3.0,1.7321),(3.5,1.8708)])
print prob_2(2.8)
</pre>]]></content:encoded></rss:item><rss:item rdf:about="http://markwatson.us/updates/python-super_split.html"><rss:title>Python super_split()</rss:title><rss:link>http://markwatson.us/updates/python-super_split.html</rss:link><dc:creator>Mark Watson</dc:creator><dc:date>2011-05-02T20:55:00Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p>I recently needed some code that splits python strings based on multiple delimitors, so I wrote a quick function to do so. I figured it&rsquo;s generally useful so I&rsquo;m posting it here.</p>
<pre class="brush: python">def super_split(string, delim):
    segment = ''
    for c in string:
        if c in delim:
            yield segment
            segment = ''
        else:
            segment += c
    yield segment

</pre>]]></content:encoded></rss:item><rss:item rdf:about="http://markwatson.us/updates/dmoz-scraper-1.html"><rss:title>DMOZ Scraper</rss:title><rss:link>http://markwatson.us/updates/dmoz-scraper-1.html</rss:link><dc:creator>Mark Watson</dc:creator><dc:date>2011-04-30T19:57:00Z</dc:date><dc:subject></dc:subject><content:encoded><![CDATA[<p>I’m really only posting this code so I can check out the syntax highlighter in jekyll.</p>

<p>It’s a class that scrapes URLs from Dmoz given a search term. Not fancy, I’m just using it for a machine learning project. It does what I need for now.</p>

<pre class="brush: python">
import mechanize
from BeautifulSoup import BeautifulSoup

class Dmoz(object):
    def __init__(self):
        self.br = mechanize.Browser()

    def get_page_urls(self, term):
        result = self.br.open("http://www.dmoz.org/search?q="+term)
        result_html = result.read()
        soup = BeautifulSoup(result_html)
        sites_obj = soup.find('ol', {"class": "site"})
        if sites_obj:
            sites = sites_obj('li')
            urls = [x('a', recursive=False)[0]['href'] for x in sites]
            return urls
        else:
            return []

def main():
    # eg:
    dm = Dmoz()
    print dm.get_page_urls("Computer Science")

if __name__ == "__main__":
    main()
</pre>]]></content:encoded></rss:item></rdf:RDF>
