<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Little Off &#187; generator</title>
	<atom:link href="http://www.bemasher.net/archives/tag/generator/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bemasher.net</link>
	<description>Code, Computers, Photography and Guns</description>
	<lastBuildDate>Mon, 21 Nov 2011 06:38:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Power Set Generator</title>
		<link>http://www.bemasher.net/archives/264?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=power-set-generator</link>
		<comments>http://www.bemasher.net/archives/264#comments</comments>
		<pubDate>Fri, 13 Mar 2009 21:34:41 +0000</pubDate>
		<dc:creator>bemasher</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[gray]]></category>
		<category><![CDATA[graycode]]></category>
		<category><![CDATA[power set]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[set]]></category>

		<guid isPermaLink="false">http://www.bemasher.net/?p=264</guid>
		<description><![CDATA[Recently I had a bout of programming withdrawal so I set out to write a power-set generator. So a little background on the power set. The power set of a given set A is the set containing all subsets of A. Suppose that we have a set: The power set of A would be: Now [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had a bout of programming withdrawal so I set out to write a power-set generator.</p>
<p>So a little background on the power set. The power set of a given set A is the set containing all subsets of A. Suppose that we have a set:</p>
<img src='http://s0.wp.com/latex.php?latex=A+%3D+%5C%7Bx%2Cy%2Cz%5C%7D&#038;bg=ffffff&#038;fg=000&#038;s=0' alt='A = &#92;{x,y,z&#92;}' title='A = &#92;{x,y,z&#92;}' class='latex' />
<p>The power set of A would be:</p>
<img src='http://s0.wp.com/latex.php?latex=%5Cmathcal%7BP%7D%28A%29+%3D+%5Cleft%5C%7B%5Cemptyset%2C+%5C%7Bx%5C%7D%2C+%5C%7By%5C%7D%2C+%5C%7Bz%5C%7D%2C+%5C%7Bx%2C+y%5C%7D%2C+%5C%7Bx%2C+z%5C%7D%2C+%5C%7By%2C+z%5C%7D%2C+%5C%7Bx%2C+y%2C+z%5C%7D%5Cright%5C%7D%5C%2C%5C%21.&#038;bg=ffffff&#038;fg=000&#038;s=0' alt='&#92;mathcal{P}(A) = &#92;left&#92;{&#92;emptyset, &#92;{x&#92;}, &#92;{y&#92;}, &#92;{z&#92;}, &#92;{x, y&#92;}, &#92;{x, z&#92;}, &#92;{y, z&#92;}, &#92;{x, y, z&#92;}&#92;right&#92;}&#92;,&#92;!.' title='&#92;mathcal{P}(A) = &#92;left&#92;{&#92;emptyset, &#92;{x&#92;}, &#92;{y&#92;}, &#92;{z&#92;}, &#92;{x, y&#92;}, &#92;{x, z&#92;}, &#92;{y, z&#92;}, &#92;{x, y, z&#92;}&#92;right&#92;}&#92;,&#92;!.' class='latex' />
<p>Now looking more carefully at the power set of A you'll notice that it contains 2 to the power of the cardinality of A subsets, always containing the empty set.</p>
<img src='http://s0.wp.com/latex.php?latex=2%5E%7B%7CA%7C%7D+%3D+8&#038;bg=ffffff&#038;fg=000&#038;s=0' alt='2^{|A|} = 8' title='2^{|A|} = 8' class='latex' />
<p>The code I came up with for this is short and more or less simple<sup>[<a href="http://www.bemasher.net/archives/264#footnote_0_264" id="identifier_0_264" class="footnote-link footnote-identifier-link" title="I lied, i just got a little pythonic functional programming happy.">1</a>]</sup>:</p>
<div class="codecolorer-container python default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">def</span> PowerSet<span style="color: black;">&#40;</span>base<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; power_set <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><br />
&nbsp; &nbsp; b <span style="color: #66cc66;">=</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>base<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> g: power_set.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: base<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: g<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> value: <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: <span style="color: black;">&#40;</span>value <span style="color: #66cc66;">&gt;&gt;</span> x<span style="color: black;">&#41;</span> &amp; <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>b - <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> -<span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> -<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> value: value ^ <span style="color: black;">&#40;</span>value / <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span>**b<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> power_set<br />
<br />
<span style="color: #ff7700;font-weight:bold;">print</span> PowerSet<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div></td></tr></tbody></table></div>
<p>I figured out shortly after I wrote this that I had the right general idea but in this particular case, since I'm not actually using set types... the graycode I'm generating is useless for this sort of thing.</p>
<p>The point of generating graycode for this is that graycode is used for binary counting such that only one digit is changed from one consecutive value to the next. It was originally designed so that mechanical switches in early computers wouldn't cause a race condition while counting fast enough.</p>
<p>In this particular solution using graycode is useful for only having to add one new element to a set at a time which if I were actually using sets, would be faster, but since I'm not, it isn't. I'll probably rewrite it later to make it play nicer with graycode.</p>
<p>The basic procedure here is that given a set A we're going to count from 0 to 2^|A| - 1 and in binary graycode, the 1's determine the elements of the base set that will be added as a new set to the power set and 0's indicate that that element of the base set will be ignored in the current subset.</p>
<ol class="footnotes"><li id="footnote_0_264" class="footnote">I lied, i just got a little pythonic functional programming happy.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://www.bemasher.net/archives/264/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

