<?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>Shriphani Palakodety &#187; Mathematics</title>
	<atom:link href="http://shriphani.com/blog/category/mathematics/feed/" rel="self" type="application/rss+xml" />
	<link>http://shriphani.com/blog</link>
	<description>In Pursuit Of Truth and Beauty</description>
	<lastBuildDate>Sat, 28 Aug 2010 23:54:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Summer and Fall 2010 + SURF 2010</title>
		<link>http://shriphani.com/blog/2010/08/26/summer-and-fall-2010-surf-2010/</link>
		<comments>http://shriphani.com/blog/2010/08/26/summer-and-fall-2010-surf-2010/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 05:03:07 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Daily life]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA["OpenGL"]]></category>
		<category><![CDATA["poster presentation"]]></category>
		<category><![CDATA["poster"]]></category>
		<category><![CDATA["schedule"]]></category>
		<category><![CDATA["semester"]]></category>
		<category><![CDATA["surf 2010"]]></category>
		<category><![CDATA["visual analytics"]]></category>
		<category><![CDATA[college]]></category>
		<category><![CDATA[SURF]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=649</guid>
		<description><![CDATA[So, 4 days into this semester I think I have decided my schedule. I am assuming I have a relatively light semester owing to some stupidity I engaged in (screw ups with the coursework). Anyway, here&#8217;s my coursework for this semester: CS 352 &#8211; Compilers (supposed to be hard) CS 314 &#8211; Numerical Methods (this [...]]]></description>
			<content:encoded><![CDATA[<p>So, 4 days into this semester I think I have decided my schedule. I am assuming I have a relatively light semester owing to some stupidity I engaged in (screw ups with the coursework). Anyway, here&#8217;s my coursework for this semester:</p>
<ul>
<li>CS 352 &#8211; Compilers (supposed to be hard)</li>
<li>CS 314 &#8211; Numerical Methods (this course looked very interesting).</li>
<li>MA 375 &#8211; Discrete Math (to consolidate my probability concepts and to study graphs deeply)</li>
<li>COM 217 &#8211; Had to take it sometime.</li>
<li>CS 490 &#8211; Bioinformatics independent study.</li>
</ul>
<p>So, I chickened out of ECE 270 since Compilers + Digital Systems would be suicide and since CS 497 is planned for semester #6, I am in a tight spot since one more CS course can cause my GPA in all courses to tank so I am stuck here with this stupid setup. Also, I took the Phy test out and I am not sure if that went very well so I will have to wait.</p>
<p>Anyway, enough about the current semester. Summer was the most eventful time of my 2 years here @ Purdue. First, I worked for Dr. David S. Ebert in his Visualization laboratory (<a href="http://cobweb.ecn.purdue.edu/purpl/">PURPL</a>) on an <a href="https://engineering.purdue.edu/PURVAC/proj_emergency.html">emergency response system</a> (the project screenshots shown there are still old. They probably won&#8217;t be updated in a while). It was extremely interesting since I got to work with OpenGL (I am not sure I will ever get a chance to work with it again) and got to learn about computer graphics. Here is a list of things we got done in the summer:</p>
<ul>
<li>A 3D picking algorithm (nothing too fancy. This took a while since I had to read OpenGL documentation about their coordinate systems).</li>
<li>A controllable camera (one you can turn, zoom in/out with using keystrokes)</li>
<li>A couple of camera angles (The high angle mode and the first person mode). Getting them to follow characters smoothly was a crazy issue.</li>
<li>Then we implemented a path based attribute visualization technique which became the topic for my poster.</li>
</ul>
<p>I stood with a poster @ the SURF symposium and I won &#8220;Top Poster&#8221; and my grad mentor, <a href="http://vrinside.net/inside/">S. Kim</a>, earned &#8220;Graduate Student Mentor of the Summer&#8221; so I would rate my SURF 2010 experience as one of the top 10 experiences of my life (the other 9 are yet to arrive).</p>
<p>Now, I might just go and join SIG-APP or something like that to spend spare time. Now, here are some goals I set for myself this semester:</p>
<ul>
<li>Prepare for the GRE (it sucks but it has to be done).</li>
<li>Learn how to speak Japanese reasonably well.</li>
<li>Consolidate my probability knowledge (I would really like to learn it well this time round).</li>
<li>Finish reading a book on NP Complete problems.</li>
</ul>
<p>Well, I just hope I don&#8217;t screw up anything this year since this is my last chance to build a great resume for grad school (and achieve some lofty goals which are too childish for even this blog) before applications season begins.</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2010/08/26/summer-and-fall-2010-surf-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bipartite Matching And Max Flow &#8211; Tweaks And Other Observations</title>
		<link>http://shriphani.com/blog/2010/07/16/bipartite-matching-and-max-flow/</link>
		<comments>http://shriphani.com/blog/2010/07/16/bipartite-matching-and-max-flow/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 00:45:16 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[bipartite-matching]]></category>
		<category><![CDATA[flow]]></category>
		<category><![CDATA[matching]]></category>
		<category><![CDATA[max-flow]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=598</guid>
		<description><![CDATA[A look at the bipartite matching problem and some observations about solving them using max-flow.]]></description>
			<content:encoded><![CDATA[<p>When I was in Dr. Frederickson&#8217;s CS 381 class (if you go to Purdue CS and are wondering if you should sit in his CS 381, I would say YES), I learned how to solve a simple class of Matching problems, biparitite matching.</p>
<p>So, you have 2 sets of vertices and no 2 members of the same set are connected. Now, given a bipartite graph, we are supposed to find a maximum matching for it.</p>
<p>The simplest way to solve this problem would be to use integer valued flow. Here&#8217;s our sample graph:</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2010/07/bipartite11.jpg"><img class="aligncenter size-medium wp-image-601" title="bipartite1" src="http://shriphani.com/blog/wp-content/uploads/2010/07/bipartite11-300x225.jpg" alt="" width="300" height="225" /> </a>Well, the obvious solution is to direct the edges from the set on the left (marked L in the pic below) to the set on the right (marked R in the pic below) and to make a source vertex and a sink vertex. Next, to apply integer valued flow algorithms on this network, set the capacity of each edge to 1.</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2010/07/bipartite2.jpg"><img class="aligncenter size-medium wp-image-605" title="bipartite2" src="http://shriphani.com/blog/wp-content/uploads/2010/07/bipartite2-300x225.jpg" alt="" width="300" height="225" /></a>PS, I haven&#8217;t noted the capacities in the graph above because that would introduce too much clutter. But the capacities are indeed 1. When you run any method on this network that computes maximum integer valued flow (Choose between Ford-Fulkerson and Edmonds-Karp), you are going to have a flow of 0 or 1 on the edges in the graph. The edges that have flow = 1 on them are the ones that are included in the matching and those that are not in the matching have a flow value = 0. Here is one of the possible matchings:</p>
<p>The edges that are untouched have a flow value = 1 (also equal to the capacity of the edge) and those that look like they&#8217;ve been shot (i.e have holes in them, have a flow of 0 and hence are not in the matching).</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2010/07/bipartite3.jpg"><img class="aligncenter size-medium wp-image-607" title="bipartite3" src="http://shriphani.com/blog/wp-content/uploads/2010/07/bipartite3-300x225.jpg" alt="" width="300" height="225" /></a>Well, everyone knows this stuff. Else you wouldn&#8217;t have approached this line is &lt; 10 seconds. Anyway, in one very productive office hour session with Dr. Frederickson, he pointed out the importance of using an approach to solve a problem. In this case, why do we set a capacity of 1 and compute max integer-valued flow? Well, considering I have a really low IQ, I had to look at this problem as if it were a communication network. Setting a capacity of 1 on each edge and then computing max flow would give us edges with a flow value of 1 or 0. That means we either use the edge to communicate or we don&#8217;t (this is why we set a capacity of 1).</p>
<p>So, extending this thinking to problems that bipartite matching can solve, let us consider the simple problem:</p>
<p><strong>B is a set of boys who need to be paired up with girls in set G. Any boy can be paired with any girl. </strong></p>
<p>So, the solution would involve what we just did.</p>
<p>Now, we can modify the problem so:</p>
<p><strong>B is a set of boys who need to be paired up with girls in set G for a performance at their community center. Each boy can appear at most thrice and each girl can appear at most 4 times and a pair can appear at most twice.</strong></p>
<p>Extending my &#8220;communication network&#8221; idea, this problem can also be solved by computing a max flow on this network. But we need to set the capacities on each edge correctly.</p>
<ul>
<li>We have an edge from a source <strong>S</strong> to every boy. Now, a boy can appear at most thrice. This essentially means, the boy can be picked once, twice, three times (or if he really sucks, he might not be picked at all). So, the capacity you set on this edge is <strong>3</strong>.</li>
<li>Now, we have an edge from every girl to a sink <strong>T</strong>. Remember a girl can be picked at most 4 times. So, the capacity of the edge from every girl to the sink is <strong>4</strong>.</li>
<li>Now, each pair can appear twice so every edge from set <strong>B</strong> to set <strong>G</strong> gets a capacity of 2.</li>
</ul>
<p>That&#8217;s it. Computing max flow on this should give us a decent matching!</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2010/07/16/bipartite-matching-and-max-flow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hebb&#8217;s Rule</title>
		<link>http://shriphani.com/blog/2009/02/09/hebbs-rule/</link>
		<comments>http://shriphani.com/blog/2009/02/09/hebbs-rule/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 00:58:30 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=321</guid>
		<description><![CDATA[This post is slightly psychology influenced. Well, in the Psychology class I have been taking this semester (Introduction To Cognitive Learning), I was taught about Neural Networks and a certain Hebb&#8217;s Rule which allows a network to remember a previous configuration and use this configuration to interpret new information. We use the following model to [...]]]></description>
			<content:encoded><![CDATA[<p>This post is slightly psychology influenced. Well, in the Psychology class I have been taking this semester (Introduction To Cognitive Learning), I was taught about Neural Networks and a certain Hebb&#8217;s Rule which allows a network to remember a previous configuration and use this configuration to interpret new information. We use the following model to represent a cell:</p>
<p>1. A cell (neuron) has only two activation states: On or Off.</p>
<ul>
<li> On: Cell has high firing rate. (High number of action potentials fired in unit time).</li>
<li> Off: Cell has low firing rate.</li>
</ul>
<p>2. Cell weights are reciprocal. Weights symbolize the size of the synapse between two cells. Greater size = greater strength.</p>
<p>3. If cells fire simultaneously, they develop positive weights. The active cells (activation = on) inhibit the inactive cells (develop negative weights.</p>
<p>4. The network organizes itself based on sensory input. And new sensory info is interpreted by previously developed weights.</p>
<p>5. The network updates itself one at a time.</p>
<p>6. The activation of a cell can be determined by the following formula:</p>
<p>1 if &sum;w<sub>ij</sub>a<sub>j</sub> &gt; 0<br />
0 otherwise</p>
<p>The weights lend a sort of memory to the network since weights influence the interpretation of subsequent sensory input.</p>
<p>Based on these rules, I wrote something that simulated a network that implemented the Hebb&#8217;s rule.</p>
<p>First, we need to describe a neuron. Our neurons represent an uppercase letter of the english alphabet. We also need a way to represent the weights with each of the other neurons.</p>
<p>So a basic class to describe a neuron:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">class</span> Neuron:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, a, weight_dict<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;A Represents The Cell&#8217;</span>s Content<span class="st0">&#8221;</span><span class="st0">&#8216;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.content = a #what letter of the alphabet a cell represents<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.weights = weight_dict #only contains contents of each neuron.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.state = 0 #activation state initially on.</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; def __str__(self):<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return &quot;Activation: &quot;+str(self.state)+&quot;<span class="es0">\n</span>Weights: &quot;+str(self.weights)</span></div>
<p>Now, the network has a collection of neurons (26 in our case which signify each letter of the alphabet). Hence, the init method for the neuron class:</p>
<div class="dean_ch" style="white-space: nowrap;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">neurons</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">flush</span> = <span class="br0">&#123;</span><span class="br0">&#125;</span> <span class="co1">#weight calculator while updating activities</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">possibilities</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> char <span class="kw1">in</span> <span class="kw3">string</span>.<span class="me1">uppercase</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">flush</span><span class="br0">&#91;</span>char<span class="br0">&#93;</span> = <span class="nu0">0</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> letter <span class="kw1">in</span> <span class="kw3">string</span>.<span class="me1">uppercase</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; weights = <span class="kw3">copy</span>.<span class="kw3">copy</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">flush</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; weights.<span class="me1">pop</span><span class="br0">&#40;</span>letter<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = Neuron<span class="br0">&#40;</span>letter, weights<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">neurons</span>.<span class="me1">append</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span></div>
<p>Now that we have a network that is populated, we need a method that receives input and figures out the changes in the weights between cells (or the strength of the connection between cells). This method is then fed by another method that figures out the overall change in the weights and computer which cells are active:</p>
<div class="dean_ch" style="white-space: nowrap;">
<span class="kw1">def</span> recvInput<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&quot;Sensory Interface: &quot;</span><span class="br0">&#41;</span> <span class="co1">#Get a word</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> a != <span class="st0">&quot;QUIT&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b = <span class="kw2">list</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>b<span class="br0">&#41;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> neuron <span class="kw1">in</span> <span class="kw2">self</span>.<span class="me1">neurons</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char = b<span class="br0">&#91;</span>i<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> neuron.<span class="me1">content</span> == char:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neuron.<span class="me1">state</span> = <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reqd_list = b<span class="br0">&#91;</span><span class="nu0">0</span>:i<span class="br0">&#93;</span>+b<span class="br0">&#91;</span>i<span class="nu0">+1</span>:<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> char <span class="kw1">in</span> <span class="kw3">string</span>.<span class="me1">uppercase</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> char <span class="kw1">in</span> reqd_list:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neuron.<span class="me1">weights</span><span class="br0">&#91;</span>char<span class="br0">&#93;</span>+=<span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> neuron.<span class="me1">weights</span>.<span class="me1">has_key</span><span class="br0">&#40;</span>char<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neuron.<span class="me1">weights</span><span class="br0">&#91;</span>char<span class="br0">&#93;</span>-=<span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span></p>
<p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">checkInfo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">possibleInfo</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;The possible interpretations of this input might be anagrams of the subsets of: <span class="es0">\n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="kw2">self</span>.<span class="me1">possibilities</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&quot;Sensory Interface: &quot;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> checkInfo<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; possibilities = <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> neuron <span class="kw1">in</span> <span class="kw2">self</span>.<span class="me1">neurons</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> neuron.<span class="me1">state</span> == <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> info <span class="kw1">in</span> neuron.<span class="me1">weights</span>.<span class="me1">keys</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">flush</span><span class="br0">&#91;</span>info<span class="br0">&#93;</span> += neuron.<span class="me1">weights</span><span class="br0">&#91;</span>info<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="kw2">self</span>.<span class="me1">flush</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> neuron <span class="kw1">in</span> <span class="kw2">self</span>.<span class="me1">neurons</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">self</span>.<span class="me1">flush</span><span class="br0">&#91;</span>neuron.<span class="me1">content</span><span class="br0">&#93;</span> &gt; <span class="nu0">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neuron.<span class="me1">state</span> = <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neuron.<span class="me1">state</span> = <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> char <span class="kw1">in</span> <span class="kw2">self</span>.<span class="me1">flush</span>.<span class="me1">keys</span><span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">flush</span><span class="br0">&#91;</span>char<span class="br0">&#93;</span> = <span class="nu0">0</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> possibleInfo<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">possibilities</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> neuron <span class="kw1">in</span> <span class="kw2">self</span>.<span class="me1">neurons</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> neuron.<span class="me1">state</span> == <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">possibilities</span>.<span class="me1">append</span><span class="br0">&#40;</span>neuron.<span class="me1">content</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>Then we run this network with:</p>
<div class="dean_ch" style="white-space: nowrap;">
words = Network<span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>words.<span class="me1">recvInput</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>Before I run this, let me mention that this thing only accepts uppercase letters and breaks if a letter is repeated in a word. This is because neurons rarely have their own axon connected to their own dendrite (neurons are not connected to themselves).</p>
<p>So, a sample run:</p>
<pre>
tark-b-183:scripts shriphani\> python hebb.py
Sensory Interface: HELP
{'A': -4, 'C': -4, 'B': -4, 'E': 3, 'D': -4, 'G': -4, 'F': -4, 'I': -4, 'H': 3, 'K': -4, 'J': -4, 'M': -4, 'L': 3, 'O': -4, 'N': -4, 'Q': -4, 'P': 3, 'S': -4, 'R': -4, 'U': -4, 'T': -4, 'W': -4, 'V': -4, 'Y': -4, 'X': -4, 'Z': -4}
The possible interpretations of this input might be anagrams of the subsets of: 

['E', 'H', 'L', 'P']
Sensory Interface: LOSER
{'A': -9, 'C': -9, 'B': -9, 'E': 7, 'D': -9, 'G': -9, 'F': -9, 'I': -9, 'H': -2, 'K': -9, 'J': -9, 'M': -9, 'L': 7, 'O': 0, 'N': -9, 'Q': -9, 'P': -2, 'S': 0, 'R': 0, 'U': -9, 'T': -9, 'W': -9, 'V': -9, 'Y': -9, 'X': -9, 'Z': -9}
The possible interpretations of this input might be anagrams of the subsets of: 

['E', 'L']
Sensory Interface: HOMELY
{'A': -12, 'C': -12, 'B': -12, 'E': 9, 'D': -12, 'G': -12, 'F': -12, 'I': -12, 'H': 4, 'K': -12, 'J': -12, 'M': -1, 'L': 9, 'O': 4, 'N': -12, 'Q': -12, 'P': -6, 'S': -6, 'R': -6, 'U': -12, 'T': -12, 'W': -12, 'V': -12, 'Y': -1, 'X': -12, 'Z': -12}
The possible interpretations of this input might be anagrams of the subsets of: 

['E', 'H', 'L', 'O']
Sensory Interface: LOSER
{'A': -17, 'C': -17, 'B': -17, 'E': 13, 'D': -17, 'G': -17, 'F': -17, 'I': -17, 'H': -5, 'K': -17, 'J': -17, 'M': -9, 'L': 13, 'O': 8, 'N': -17, 'Q': -17, 'P': -11, 'S': 1, 'R': 1, 'U': -17, 'T': -17, 'W': -17, 'V': -17, 'Y': -9, 'X': -17, 'Z': -17}
The possible interpretations of this input might be anagrams of the subsets of: 

['E', 'L', 'O', 'R', 'S']
</pre>
<p>So we observe that in the first case, four neurons were activated. The word &#8220;LOSER&#8221; led to H and P being deactivated because the synapse weakened. The next input &#8220;HOMELY&#8221; causes H and O to activate because O began developing positive weights with other cells and when LOSER was submitted as input and developed a strong enough connection when it was seen in another sensory input. When LOSER is flashed again, it leads to H being forgotten and causes R and S to activate. </p>
<p>Now, a particular case which (I think) indicates the use of this &#8220;learning&#8221; technique is when we are playing scrabble. Assume that I have words such as NEAR in my memory and when I see N and E on a board, I should be able to complete these words. Watch:</p>
<pre>
Sensory Interface: NEAR
{'A': 3, 'C': -4, 'B': -4, 'E': 3, 'D': -4, 'G': -4, 'F': -4, 'I': -4, 'H': -4, 'K': -4, 'J': -4, 'M': -4, 'L': -4, 'O': -4, 'N': 3, 'Q': -4, 'P': -4, 'S': -4, 'R': 3, 'U': -4, 'T': -4, 'W': -4, 'V': -4, 'Y': -4, 'X': -4, 'Z': -4}
The possible interpretations of this input might be anagrams of the subsets of: 

['A', 'E', 'N', 'R']
Sensory Interface: NE
{'A': 1, 'C': -6, 'B': -6, 'E': 4, 'D': -6, 'G': -6, 'F': -6, 'I': -6, 'H': -6, 'K': -6, 'J': -6, 'M': -6, 'L': -6, 'O': -6, 'N': 4, 'Q': -6, 'P': -6, 'S': -6, 'R': 1, 'U': -6, 'T': -6, 'W': -6, 'V': -6, 'Y': -6, 'X': -6, 'Z': -6}
The possible interpretations of this input might be anagrams of the subsets of: 

['A', 'E', 'N', 'R']
Sensory Interface:
</pre>
<p>Well, this semester, I have already been taught some insanely great stuff. I have a bunch of exams coming up. Great scores will be the perfect way to make an awesome semester even better.</p>
<p>The script can be downloaded <a href="http://shriphani.com/scripts/hebb.py" target="_blank">here</a>.<br />
The material here was used from Professor Greg Francis&#8217; <a href="http://www1.psych.purdue.edu/~gfrancis/Classes/PSY200/L08.pdf" target="_blank">notes on Neural Learning</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2009/02/09/hebbs-rule/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Post&#8217;s Machine Emulator</title>
		<link>http://shriphani.com/blog/2009/01/17/posts-machine-emulator/</link>
		<comments>http://shriphani.com/blog/2009/01/17/posts-machine-emulator/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 19:42:35 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=313</guid>
		<description><![CDATA[I was bored as usual and since Combat Arms has begun to slip in my list of pastimes, I decided to write a Post&#8217;s Machine Emulator as described in the booklet by V. A. Uspensky titled &#8220;Post&#8217;s Machine&#8221; ( Amazon Link This machine has a tape (infinitely long) and the reel has cells which can [...]]]></description>
			<content:encoded><![CDATA[<p>I was bored as usual and since Combat Arms has begun to slip in my list of pastimes, I decided to write a Post&#8217;s Machine Emulator as described in the booklet by V. A. Uspensky titled &#8220;Post&#8217;s Machine&#8221; ( <a href="http://www.amazon.com/Posts-Machine-V-Uspensky/dp/0828523886/ref=sr_1_6?ie=UTF8&amp;s=books&amp;qid=1232212383&amp;sr=1-6" target="_blank">Amazon Link</a></p>
<p>This machine has a tape (infinitely long) and the reel has cells which can be marked or unmarked. I decided to represent a marked cell with a &#8220;1&#8243; and an unmarked cell as &#8220;0&#8243;.</p>
<p>The head of this machine can move both ways and can mark and at the same time remove the mark from the other side.</p>
<p>The machine can be instructed using the syntax:</p>
<pre>number, action, jump</pre>
<p>The position of the head is indicated by a structure \&#8221;[]\&#8221; around the cell. Example:</p>
<pre>Initial Stage
.....[0], 0, 0, 0, 0, .....</pre>
<p>I used the &#8220;&#8230;..&#8221; to indicate that the remnant cells are all unmarked.</p>
<p>So here are the Head and Tape classes:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">class</span> Cell:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Class describing the cell of a tape&#8217;</span><span class="st0">&#8221;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, mark=<span class="nu0">0</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Each cell is unmarked by default&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">mark</span> = mark</p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> setMark<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">mark</span> = <span class="nu0">1</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> remMark<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">mark</span> = <span class="nu0">0</span> </p>
<p><span class="kw1">class</span> Tape:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Class describing the infinite reel in the machine&#8217;</span><span class="st0">&#8221;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Each tape has an infinite number of cells both ways&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">reel</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">5</span><span class="br0">&#41;</span>: <span class="co1">#we display only the required elements at a time</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell = Cell<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">reel</span>.<span class="me1">append</span><span class="br0">&#40;</span>cell<span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__str__</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; printstr = <span class="st0">&quot;&#8230;..&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> cell <span class="kw1">in</span> <span class="kw2">self</span>.<span class="me1">reel</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printstr += <span class="kw2">str</span><span class="br0">&#40;</span>cell.<span class="me1">mark</span><span class="br0">&#41;</span>+<span class="st0">&quot;,&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; printstr += <span class="st0">&quot;&#8230;..&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> printstr</p>
<p><span class="kw1">class</span> Head:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Class describing the head&#8217;</span><span class="st0">&#8221;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, tape, position=<span class="nu0">0</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">position</span> = position<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">tape</span> = tape</p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> moveLeft<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">self</span>.<span class="me1">position</span> != <span class="nu0">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">position</span> -= <span class="nu0">1</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extension = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extension.<span class="me1">append</span><span class="br0">&#40;</span>Cell<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">tape</span> = extension + tape.<span class="me1">reel</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> etchMark<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span><span class="br0">&#91;</span><span class="kw2">self</span>.<span class="me1">position</span><span class="br0">&#93;</span>.<span class="me1">setMark</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> remMark<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span><span class="br0">&#91;</span><span class="kw2">self</span>.<span class="me1">position</span><span class="br0">&#93;</span>.<span class="me1">remMark</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> moveRight<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">self</span>.<span class="me1">position</span> == <span class="kw2">len</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span><span class="br0">&#41;</span> &#8211; <span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span>.<span class="me1">append</span><span class="br0">&#40;</span>Cell<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">position</span> += <span class="nu0">1</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> getPosition<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">self</span>.<span class="me1">position</span></div>
<p>Then comes the Machine class which takes a tape as an argument. So that you can insert your own tape or use the default tape:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">class</span> Machine:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Class describing the Post Machine&#8217;</span><span class="st0">&#8221;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, tape<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">tape</span> = tape<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">head</span> = Head<span class="br0">&#40;</span>tape<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="kw3">commands</span> = <span class="br0">&#123;</span><span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> parseInstruction<span class="br0">&#40;</span><span class="kw2">self</span>, number, action, jump<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Instruction is of form &quot;[number, action, jump]&quot;&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="kw3">commands</span><span class="br0">&#91;</span>number<span class="br0">&#93;</span> = <span class="br0">&#91;</span>action, jump<span class="br0">&#93;</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> beginExecution<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; instruction = <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; action = <span class="kw2">self</span>.<span class="kw3">commands</span><span class="br0">&#91;</span>instruction<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> action != <span class="st0">&quot;exit&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> action == <span class="st0">&quot;&lt;&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">head</span>.<span class="me1">moveLeft</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> action == <span class="st0">&quot;&gt;&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">head</span>.<span class="me1">moveRight</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> action == <span class="st0">&quot;mark&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">head</span>.<span class="me1">etchMark</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> action == <span class="st0">&quot;unmark&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">head</span>.<span class="me1">remMark</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="kw2">self</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instruction = <span class="kw2">self</span>.<span class="kw3">commands</span><span class="br0">&#91;</span>instruction<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action = <span class="kw2">self</span>.<span class="kw3">commands</span><span class="br0">&#91;</span>instruction<span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span></p>
<p>&nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__str__</span><span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;This returns tape with the head displayed&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a = <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">tape</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; posn = <span class="kw2">self</span>.<span class="me1">head</span>.<span class="me1">getPosition</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; temp = <span class="st0">&quot;&#8230;..&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span><span class="br0">&#41;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> i == posn:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp += <span class="st0">&quot;[&quot;</span>+<span class="kw2">str</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">mark</span><span class="br0">&#41;</span>+<span class="st0">&quot;], &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp += <span class="kw2">str</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">tape</span>.<span class="me1">reel</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>.<span class="me1">mark</span><span class="br0">&#41;</span> + <span class="st0">&quot;, &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; temp += <span class="st0">&quot;&#8230;..&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> temp</div>
<p>Next comes a function that reads an input file for instructions:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> run<span class="br0">&#40;</span>input_file<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; infile = <span class="kw2">open</span><span class="br0">&#40;</span>input_file, <span class="st0">&#8216;r&#8217;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; b = infile.<span class="me1">readlines</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> instruction <span class="kw1">in</span> b:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; splits = instruction.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot;,&quot;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number = <span class="kw2">int</span><span class="br0">&#40;</span>splits<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">strip</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; action = splits<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">strip</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> action == <span class="st0">&quot;exit&quot;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.<span class="me1">parseInstruction</span><span class="br0">&#40;</span>number, action, <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jump = <span class="kw2">int</span><span class="br0">&#40;</span>splits<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>.<span class="me1">strip</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.<span class="me1">parseInstruction</span><span class="br0">&#40;</span>number, action, jump<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">except</span> <span class="kw2">KeyError</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span><br />
&nbsp; &nbsp; infile.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; a.<span class="me1">beginExecution</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
<p>A bunch of instructions which need to be written to some file:</p>
<pre>0, &gt;, 2
1, &lt;, 0
2, &gt;, 3
3, &gt;, 4
4, &gt;, 5
5, mark, 6
6, &gt;, 7
7, exit</pre>
<p>The output:</p>
<pre>
pal-178-222:~ shriphani$ python post.py
Initial Stage
.....[0], 0, 0, 0, 0, .....
File: input_file
.....0, [0], 0, 0, 0, .....
.....0, 0, [0], 0, 0, .....
.....0, 0, 0, [0], 0, .....
.....0, 0, 0, 0, [0], .....
.....0, 0, 0, 0, [1], .....
.....0, 0, 0, 0, 1, [0], .....
pal-178-222:~ shriphani$ python post.py
</pre>
<p>The full script can be obtained at <a href="http://shriphani.com/scripts/post.py" target="_blank">http://shriphani.com/scripts/post.py</a></p>
<p>Well, later.</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2009/01/17/posts-machine-emulator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RPN Calculator &#8211; Keeping Boredom Away</title>
		<link>http://shriphani.com/blog/2009/01/14/rpn-calculator/</link>
		<comments>http://shriphani.com/blog/2009/01/14/rpn-calculator/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 20:09:35 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Daily life]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=308</guid>
		<description><![CDATA[Alright, those who know me well are bound to ask, 19 credit hours and you are bored? Answer: Yes. It is still the first week and we don&#8217;t seem to have gotten beyond the &#8220;introduce your loser self to the other non-interested members of the class&#8221; and since I have only 1 programming intensive course [...]]]></description>
			<content:encoded><![CDATA[<p>Alright, those who know me well are bound to ask, 19 credit hours and you are bored? Answer: Yes. It is still the first week and we don&#8217;t seem to have gotten beyond the &#8220;introduce your loser self to the other non-interested members of the class&#8221; and since I have only 1 programming intensive course this semester, I should have lots of time to kill. </p>
<p>Well, my first script this semester is an RPN calculator. Totally useless for the vast majority of people out there but they know what to expect from someone who likes theoretical computer science.</p>
<p>So, I first began with a stack &#8211; in this case an array. I then keep populating it with numbers and every time the state of the stack changes, it prints out the current state.</p>
<p>If an operation is entered (only +, -, * and /), it performs the operation, replaces the appropriate positions in the stack and then prints out the current state.</p>
<p>Here is the code, Its been a while since I wrote any OOP stuff in Python and the &#8220;self&#8221; especially feels weird after an entire semester of Java&#8230;</p>
<div class="dean_ch" style="white-space: nowrap;">
<span class="co1">#!/usr/bin/env python</span><br />
<span class="co1">#Author: Shriphani Palakodety</span><br />
<span class="co1">#Mail: shriphani@shriphani.com</span><br />
<span class="co1">#RPN Calculator</span></p>
<p><span class="kw1">class</span> Machine:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, stack=<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> modifyStack<span class="br0">&#40;</span><span class="kw2">self</span>, element<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;add to stack if input is float else run operation or complain&#8217;</span><span class="st0">&#8221;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">self</span>.<span class="me1">isOperator</span><span class="br0">&#40;</span>element<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">operate</span><span class="br0">&#40;</span>element<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">printStack</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> <span class="kw2">self</span>.<span class="me1">isFloat</span><span class="br0">&#40;</span>element<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">append</span><span class="br0">&#40;</span>element<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">printStack</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;Only numbers and operators&quot;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> isFloat<span class="br0">&#40;</span><span class="kw2">self</span>, char<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Checks whether the last item entered in the stack is a float or not&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">float</span><span class="br0">&#40;</span>char<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">except</span> <span class="kw2">ValueError</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">False</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">True</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> isOperator<span class="br0">&#40;</span><span class="kw2">self</span>, char<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Checks whether the last item entered in the stack is an operator or not&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> char == <span class="st0">&#8216;+&#8217;</span> <span class="kw1">or</span> char == <span class="st0">&#8216;-&#8217;</span> <span class="kw1">or</span> char == <span class="st0">&#8216;*&#8217;</span> <span class="kw1">or</span> char == <span class="st0">&#8216;/&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">False</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> operate<span class="br0">&#40;</span><span class="kw2">self</span>, <span class="kw3">operator</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Perform the stated operation on the last two elements in the stack&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operand1 = <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">stack</span><span class="br0">&#91;</span><span class="nu0">-1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; operand2 = <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">self</span>.<span class="me1">stack</span><span class="br0">&#91;</span><span class="nu0">-2</span><span class="br0">&#93;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">pop</span><span class="br0">&#40;</span><span class="nu0">-1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">pop</span><span class="br0">&#40;</span><span class="nu0">-1</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw3">operator</span>==<span class="st0">&#8216;+&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">append</span><span class="br0">&#40;</span>operand1+operand2<span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> <span class="kw3">operator</span>==<span class="st0">&#8216;-&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">append</span><span class="br0">&#40;</span>operand2-operand1<span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> <span class="kw3">operator</span>==<span class="st0">&#8216;*&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">append</span><span class="br0">&#40;</span>operand1*operand2<span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> <span class="kw3">operator</span>==<span class="st0">&#8216;/&#8217;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span>.<span class="me1">append</span><span class="br0">&#40;</span><span class="kw2">float</span><span class="br0">&#40;</span>operand2<span class="br0">&#41;</span>/<span class="kw2">float</span><span class="br0">&#40;</span>operand1<span class="br0">&#41;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="st0">&quot;God only knows what operation you have typed in&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">stack</span> += <span class="br0">&#91;</span>operand2, operand1<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">self</span>.<span class="me1">printStack</span><span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">def</span> printStack<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> <span class="kw2">self</span>.<span class="me1">stack</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="kw1">def</span> kickOff<span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; calc = Machine<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="kw2">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a = <span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&#8216;Input: &#8216;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> a != <span class="st0">&#8221;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calc.<span class="me1">modifyStack</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; calc.<span class="me1">printStack</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">raise</span> <span class="kw2">SystemExit</span></p>
<p>
<span class="kw1">print</span><span class="st0">&quot;Press Enter to Quit<span class="es0">\n</span>&quot;</span><br />
kickOff<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>The calculator in action:</p>
<pre>
pal-179-083:~ shriphani$ python rpn.py
Press Enter to Quit

Input: 1
['1']
Input: 2
['1', '2']
Input: 3
['1', '2', '3']
Input: 4
['1', '2', '3', '4']
Input: 5
['1', '2', '3', '4', '5']
Input: +
['1', '2', '3', 9.0]
Input: h
Only numbers and operators
Input: -
['1', '2', -6.0]
Input:
['1', '2', -6.0]
</pre>
<p>In other news, I am really pissed with Picasa since I can&#8217;t see how many people have viewed my pics (<a href="http://picasaweb.google.com/shriphanip">http://picasaweb.google.com/shriphanip</a> in case you are interested), I have begun filling my playlists with songs played at apple keynotes and in apple ads. Friends say this is an obsession with everything with the Apple stamp on it. I basically don&#8217;t care. </p>
<p>There has always been this one question bugging me since last semester. In Java:</p>
<div class="dean_ch" style="white-space: nowrap;">
&nbsp;<span class="kw2">class</span> Test<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">static</span> <span class="kw4">void</span> main<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> a = <span class="nu0">012345</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">System</span></a>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
<p>When I run this:</p>
<pre>
pal-179-083:~ shriphani$ java Test
5349
pal-179-083:~ shriphani$
</pre>
<p>What is happening here?</p>
<p>Anyway, looking forward to this semester. Hope my GPA stays at 4.0</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2009/01/14/rpn-calculator/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Curious Fractions</title>
		<link>http://shriphani.com/blog/2008/11/14/curious-fractions/</link>
		<comments>http://shriphani.com/blog/2008/11/14/curious-fractions/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 16:52:11 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=279</guid>
		<description><![CDATA[While suffering from conjuctivitis, fever, a sore throat and what seems to be a case of swollen tonsils determined to make nutrition a pain for the next week or two, I decided to solve a problem on Project Euler. The question I picked was Problem 33: Find all fractions with the given unorthodox cancelling method [...]]]></description>
			<content:encoded><![CDATA[<p>While suffering from conjuctivitis, fever, a sore throat and what seems to be a case of swollen tonsils determined to make nutrition a pain for the next week or two, I decided to solve a problem on <a href="http://projecteuler.net" target="_blank">Project Euler</a>. The question I picked was <a href="http://projecteuler.net/index.php?section=problems&amp;id=33" target="_blank">Problem 33</a>: Find all fractions with the given unorthodox cancelling method (just click the fscking link for heaven&#8217;s sake!).</p>
<p>I thought this problem can be solved by beginning with a base fraction and adding a numbers in the tens place to both the numerator and the denominator, then adding a number in the units place to the numerator and the denominator, adding to the units place in the numerator and the tens in the denominator and the other way round.</p>
<p>The code needed (in Python2.6 &#8211; just love the Fractions module there):</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#!/usr/bin/env python</span><br />
<span class="co1">#Author: Shriphani Palakodety</span><br />
<span class="co1">#Blog: http://shriphani.com/blog</span><br />
<span class="co1">#Mail: shriphani@shriphani.com</span></p>
<p>frac_list = <span class="br0">&#91;</span><span class="br0">&#93;</span></p>
<p><span class="kw1">from</span> fractions <span class="kw1">import</span> Fraction</p>
<p>prod = <span class="nu0">1</span></p>
<p><span class="kw1">def</span> findLeft<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Given a denominator, numerator pair, return the double digit possibilities by appending chars to the left&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; <span class="kw1">global</span> prod<br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">10</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> i == num <span class="kw1">or</span> i == den:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> Fraction<span class="br0">&#40;</span><span class="nu0">10</span>*i+num, <span class="nu0">10</span>*i+den<span class="br0">&#41;</span>==Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frac_list.<span class="me1">append</span><span class="br0">&#40;</span>Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> num,den</p>
<p><span class="kw1">def</span> findRight<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">global</span> prod<br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">10</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> i == num <span class="kw1">or</span> i == den:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> Fraction<span class="br0">&#40;</span><span class="nu0">10</span>*num+i, <span class="nu0">10</span>*den+i<span class="br0">&#41;</span>==Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> num, den<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frac_list.<span class="me1">append</span><span class="br0">&#40;</span>Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">def</span> findAlt<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">global</span> prod<br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">10</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> i == num <span class="kw1">or</span> i == den:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> Fraction<span class="br0">&#40;</span><span class="nu0">10</span>*num+i, <span class="nu0">10</span>*i+den<span class="br0">&#41;</span>==Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frac_list.<span class="me1">append</span><span class="br0">&#40;</span>Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span><span class="br0">&#41;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">elif</span> Fraction<span class="br0">&#40;</span><span class="nu0">10</span>*i+num, <span class="nu0">10</span>*den+i<span class="br0">&#41;</span>==Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frac_list.<span class="me1">append</span><span class="br0">&#40;</span>Fraction<span class="br0">&#40;</span>num, den<span class="br0">&#41;</span><span class="br0">&#41;</span></p>
<p><span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">10</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">for</span> j <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">10</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> i==j:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findLeft<span class="br0">&#40;</span>i, j<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findRight<span class="br0">&#40;</span>i, j<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findAlt<span class="br0">&#40;</span>i, j<span class="br0">&#41;</span></p>
<p>prod = <span class="nu0">1</span><br />
<span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>frac_list<span class="br0">&#41;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">if</span> frac_list<span class="br0">&#91;</span>i<span class="br0">&#93;</span> &lt; <span class="nu0">1</span>/<span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; prod *= frac_list<span class="br0">&#91;</span>i<span class="br0">&#93;</span></p>
<p><span class="kw1">print</span> prod</div>
<p>BTW, it is funny how total nutjobs with no clue about probes, payloads, science or physics take up the job of reporting the Chandrayaan&#8217;s progress.</p>
<p>Peace&#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2008/11/14/curious-fractions/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Awesome Developments</title>
		<link>http://shriphani.com/blog/2008/10/30/awesome-developments/</link>
		<comments>http://shriphani.com/blog/2008/10/30/awesome-developments/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 10:26:23 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=266</guid>
		<description><![CDATA[So, I&#8217;ve had the best week of my life and let me tell you folks, nothing&#8217;s better than sitting in front of one of the most awesome boxes in the world &#8211; the macbook pro. Yes, the alpha and the omega is here, I am now the proud owner of a macbook pro (not the [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ve had the best week of my life and let me tell you folks, nothing&#8217;s better than sitting in front of one of the most awesome boxes in the world &#8211; the macbook pro. Yes, the alpha and the omega is here, I am now the proud owner of a macbook pro (not the new ones, this is the older version). So, here are the specs:</p>
<p>2.5 GHz</p>
<p>2 GB RAM (Vista was a PITA on 2 Gigs, OS X is such a beauty)</p>
<p>512 MB NVidia 8600M GeForce</p>
<p>Intel Core2Duo (obviously)</p>
<p> </p>
<p>And so, here are a few select screenshots:</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-4.png"><img class="alignnone size-medium wp-image-267" title="Pick Window" src="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-4-300x187.png" alt="" width="300" height="187" /></a></p>
<p>And of course:</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-5.png"><img class="alignnone size-medium wp-image-268" title="Dashboard" src="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-5-300x187.png" alt="" width="300" height="187" /></a></p>
<p>And then comes:</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-6.png"><img class="alignnone size-medium wp-image-269" title="Komodo" src="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-6-300x187.png" alt="" width="300" height="187" /></a></p>
<p>By the way, that is how beautiful Komodo looks.</p>
<p>Then of course:</p>
<p><a href="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-7.png"><img class="alignnone size-medium wp-image-270" title="iPhoto" src="http://shriphani.com/blog/wp-content/uploads/2008/10/picture-7-300x187.png" alt="" width="300" height="187" /></a></p>
<p>That is what iPhoto looks like. </p>
<p>BTW, between a tough courseload which does not require me to use Python, I managed to squeeze some time for myself so I could automate a few modular arithmetic algorithms.</p>
<p>The first one I shall be dealing with is modular exponentiation. </p>
<p>If we have a<sup>k</sup> mod n, then we can express it as<br />
(a mod n)<sup>k</sup> mod n. </p>
<p>This allows us to express this in the form of code as:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#!/usr/bin/env python</span><br />
<span class="co1">#Author: Shriphani Palakodety</span><br />
<span class="co1">#Mail: spalakod@purdue.edu</span><br />
<span class="co1">#Blog: http://shriphani.com/blog</span></p>
<p><span class="kw1">def</span> modular<span class="br0">&#40;</span>base, exponent, divisor<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; false_exponent = <span class="nu0">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; result = <span class="nu0">1</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> false_exponent &lt; exponent:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result = <span class="br0">&#40;</span><span class="br0">&#40;</span>base%divisor<span class="br0">&#41;</span>*result<span class="br0">&#41;</span>%divisor<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">print</span> result<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false_exponent+=<span class="nu0">1</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> result<br />
&nbsp;</div>
<p>Then of course comes finding the modular inverse using euclid&#8217;s algorithm:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> extended<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> a % b == <span class="nu0">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>x, y<span class="br0">&#41;</span> = extended<span class="br0">&#40;</span>b, a%b<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="br0">&#40;</span>y, x-y*<span class="br0">&#40;</span>a/b<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>That should return a tuple whose first element is the GCD. Well, I am going to have fun with the Macbook.</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2008/10/30/awesome-developments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Awesome Problem</title>
		<link>http://shriphani.com/blog/2008/09/25/awesome-problem/</link>
		<comments>http://shriphani.com/blog/2008/09/25/awesome-problem/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 00:48:39 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=234</guid>
		<description><![CDATA[  I had the opportunity to work on an interesting problem:   Find a decent approximation for the Zeta function (large values), given by: So, to make any sort of headway, I decided that I would have to look at the graph. Hence, I whipped up a script in Python: #!/usr/bin/env python #Author: Shriphani Palakodety [...]]]></description>
			<content:encoded><![CDATA[<p> </p>
<p>I had the opportunity to work on an interesting problem:</p>
<p> </p>
<p>Find a decent approximation for the Zeta function (large values), given by:</p>
<p><img class="alignnone" src="http://euclid.hamline.edu/~arundquist/latex/showequation.php?eqn_id=68451" alt="" width="58" height="45" /></p>
<p>So, to make any sort of headway, I decided that I would have to look at the graph. Hence, I whipped up a script in Python:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#!/usr/bin/env python</span><br />
<span class="co1">#Author: Shriphani Palakodety</span><br />
<span class="co1">#Mail: shriphani@shriphani.com</span><br />
<span class="co1">#Blog: http://shriphani.com/blog</span></p>
<p><span class="co1">#Generate the plot of the Zeta function.</span></p>
<p><span class="kw1">from</span> pylab <span class="kw1">import</span> * <span class="co1">#imports the matplotlib graphing library</span></p>
<p><span class="kw1">def</span> zetaValue<span class="br0">&#40;</span>n<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Returns the value of the Zeta Function n&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; val = <span class="nu0">0</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">range</span><span class="br0">&#40;</span><span class="nu0">1</span>, n<span class="nu0">+1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; zeta_term = <span class="kw2">float</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>/<span class="kw2">float</span><span class="br0">&#40;</span>i**<span class="nu0">2</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; val += zeta_term<br />
&nbsp; &nbsp; <span class="kw1">return</span> val</p>
<p><span class="kw1">def</span> makePlotRange<span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Generates an array of form [domain_list, values_list] for 1&lt;1000&#8242;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; num_list = <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="co1">#This contains the domains</span><br />
&nbsp; &nbsp; range_list = <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="co1">#Contains value returned by Zeta function for every element in domain</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">1000</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; num_list.<span class="me1">append</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; range_list.<span class="me1">append</span><span class="br0">&#40;</span>zetaValue<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> num_list, range_list</p>
<p>plot_range = makePlotRange<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="co1">#gets the arrays</span></p>
<p>plot<span class="br0">&#40;</span>plot_range<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, plot_range<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="st0">&#8216;bs&#8217;</span><span class="br0">&#41;</span><br />
savefig<span class="br0">&#40;</span><span class="st0">&#8216;zetaplot.png&#8217;</span><span class="br0">&#41;</span><br />
show<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
<p>This looks a like:</p>
<p> </p>
<div id="attachment_237" class="wp-caption aligncenter" style="width: 510px"><a href="http://shriphani.com/blog/wp-content/uploads/2008/09/zetaplot.png"><img class="size-full wp-image-237" title="Plot of the Zeta function" src="http://shriphani.com/blog/wp-content/uploads/2008/09/zetaplot.png" alt="plot of the zeta function" width="500" height="342" /></a><p class="wp-caption-text">plot of the zeta function</p></div>
<p>Observe this plot. We can see that there is a horizontal asymptote at x = 1.65 . </p>
<p>Hence,</p>
<p style="text-align: center;"><img class="alignnone" src="http://euclid.hamline.edu/~arundquist/latex/showequation.php?eqn_id=68301" alt="" width="116" height="23" /></p>
<p style="text-align: left;">This leads me to conclude one bloody thing:</p>
<p style="text-align: center;"><img class="alignnone" src="http://euclid.hamline.edu/~arundquist/latex/showequation.php?eqn_id=67492" alt="" width="133" height="18" /></p>
<p style="text-align: left;">And,</p>
<p style="text-align: center;"><img class="alignnone" src="http://euclid.hamline.edu/~arundquist/latex/showequation.php?eqn_id=68302" alt="" width="93" height="23" /></p>
<p style="text-align: left;">Now, it shouldn&#8217;t come a surprise to all math or CS majors that g(x) is a function whose denominator has a higher degree than the numerator. So for the sake of simplicity, I shall assume that;</p>
<p style="text-align: center;"><img class="alignnone" src="http://euclid.hamline.edu/~arundquist/latex/showequation.php?eqn_id=68229" alt="" width="96" height="18" /></p>
<p style="text-align: left;">Another reason prompting my eagerness to pick that particular function is because of the following observation:</p>
<p style="text-align: left;"> </p>
<ul>
<li>1/x is a rectangular hyperbola which looks like:
<div id="attachment_238" class="wp-caption aligncenter" style="width: 510px"><a href="http://shriphani.com/blog/wp-content/uploads/2008/09/rect_hyp.png"><img class="size-full wp-image-238" title="rect_hyp" src="http://shriphani.com/blog/wp-content/uploads/2008/09/rect_hyp.png" alt="Rectangular Hyperbola" width="500" height="500" /></a><p class="wp-caption-text">Rectangular Hyperbola</p></div></li>
<li>If you use -1/x , the graph is rotated about the X axis and it looks like:   
<p><div id="attachment_239" class="wp-caption aligncenter" style="width: 510px"><a href="http://shriphani.com/blog/wp-content/uploads/2008/09/inv_rect_hyp.png"><img class="size-full wp-image-239" title="inv_rect_hyp" src="http://shriphani.com/blog/wp-content/uploads/2008/09/inv_rect_hyp.png" alt="Inverted Rectangular Hyperbola" width="500" height="500" /></a><p class="wp-caption-text">Inverted Rectangular Hyperbola</p></div></li>
<li>So, if you look at the positive X axis, we have already managed to replicate behavior of the Zeta function. All we need to do is move it up and ensure that the limit stays.</li>
<li>To move this graph up, just add 1.65 to f(x) and it will work:   
<p><div id="attachment_240" class="wp-caption aligncenter" style="width: 510px"><a href="http://shriphani.com/blog/wp-content/uploads/2008/09/inv_rect_hyp_up.png"><img class="size-full wp-image-240" title="inv_rect_hyp_up" src="http://shriphani.com/blog/wp-content/uploads/2008/09/inv_rect_hyp_up.png" alt="Inverted Rectangular Hyperbola Moved Up by 1.65" width="500" height="500" /></a><p class="wp-caption-text">Inverted Rectangular Hyperbola Moved Up by 1.65</p></div></li>
<li>So, the limits match, the behavior matches and all we need to do is test:</li>
</ul>
<div>I fired up Python again and wrote this code to perform the final test:</div>
<div class="dean_ch" style="white-space: nowrap;"><span class="co1">#!/usr/bin/env python</span><br />
<span class="co1">#Author: Shriphani Palakodety</span><br />
<span class="co1">#Mail: shriphani@shriphani.com</span><br />
<span class="co1">#Blog: http://shriphani.com/blog</span></p>
<p><span class="kw1">from</span> pylab <span class="kw1">import</span> * <span class="co1">#imports the matplotlib graphing library</span></p>
<p><span class="kw1">def</span> zetaValue<span class="br0">&#40;</span>n<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Returns the value of the Zeta Function n&#8217;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; val = <span class="nu0">0</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">range</span><span class="br0">&#40;</span><span class="nu0">1</span>, n<span class="nu0">+1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; zeta_term = <span class="kw2">float</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>/<span class="kw2">float</span><span class="br0">&#40;</span>i**<span class="nu0">2</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; val += zeta_term<br />
&nbsp; &nbsp; <span class="kw1">return</span> val</p>
<p><span class="kw1">def</span> makePlotRange<span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Generates an array of form [domain_list, values_list] for 1&lt;1000&#8242;</span><span class="st0">&#8221;</span><br />
&nbsp; &nbsp; num_list = <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="co1">#This contains the domains</span><br />
&nbsp; &nbsp; range_list = <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="co1">#Contains value returned by Zeta function for every element in domain</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">1001</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; num_list.<span class="me1">append</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; range_list.<span class="me1">append</span><span class="br0">&#40;</span>zetaValue<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> num_list, range_list</p>
<p>plot_range = makePlotRange<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="co1">#gets the arrays</span></p>
<p><span class="kw1">def</span> hypoFunction<span class="br0">&#40;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; num_list = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; range_list = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">1001</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; num_list.<span class="me1">append</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; range_list.<span class="me1">append</span><span class="br0">&#40;</span><span class="kw2">float</span><span class="br0">&#40;</span><span class="nu0">-1</span><span class="br0">&#41;</span>/<span class="kw2">float</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span> + <span class="nu0">1.65</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> num_list, range_list</p>
<p>other_range = hypoFunction<span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>plot<span class="br0">&#40;</span>plot_range<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, plot_range<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="st0">&#8216;bs&#8217;</span>, other_range<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>, other_range<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>, <span class="st0">&#8216;ro&#8217;</span><span class="br0">&#41;</span></p>
<p>savefig<span class="br0">&#40;</span><span class="st0">&#8216;zetaplot.png&#8217;</span><span class="br0">&#41;</span></p>
<p>show<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
<p>And this looks like:</p>
<p> </p>
<p> </p>
<p> </p>
<p><div id="attachment_242" class="wp-caption aligncenter" style="width: 510px"><a href="http://shriphani.com/blog/wp-content/uploads/2008/09/zetaplot1.png"><img class="size-full wp-image-242" title="zetaplot1" src="http://shriphani.com/blog/wp-content/uploads/2008/09/zetaplot1.png" alt="A Plot of Proposed Approximation and The Zeta Function" width="500" height="342" /></a><p class="wp-caption-text">A Plot of Proposed Approximation and The Zeta Function</p></div>
<p>Hey, this thing seems to replicate the behavior of the Zeta function way below x=100. Assignment done !!!!</p>
<p> </p>
<p>I had another question which is coming up in the next post. I am in love with Discrete math.</p>
<p>Anyway, I attended this job fair thingy and got free stuff from Google, MS and others (MS was giving us ping-pong balls&#8230;). Google&#8217;s pens are serving me well. Anyway, I am sure I won&#8217;t be accepted as an intern anywhere thanks to the fact that every company thinks that freshmen are losers and don&#8217;t deserve to be emloyed right in their first year.</p>
<p>Anyway, keep waiting for the second approximation I did.</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2008/09/25/awesome-problem/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pythagoras and lambdas&#8230;..</title>
		<link>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/</link>
		<comments>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 22:13:45 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=214</guid>
		<description><![CDATA[Here is the first bit of code I&#8217;ve ever written at college (on my own computer though, I&#8217;ve concentration problems in the lab). I was solving problem 39 of the project euler problem set (and I am ashamed of my work). So the question is: If p is the perimeter of a right angle triangle [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the first bit of code I&#8217;ve ever written at college (on my own computer though, I&#8217;ve concentration problems in the lab). I was solving problem 39 of the project euler problem set (and I am ashamed of my work). So the question is:</p>
<div class="problem_content">
<p><em>If </em><em>p is the perimeter of a right angle triangle with integral length sides, {</em><em>a,</em><em>b,</em><em>c}, there are exactly three solutions for </em><em>p = 120.</em></p>
<p style="text-align: center;"><em>{20,48,52}, {24,45,51}, {30,40,50}</em></p>
<p><em>For which value of </em><em>p <img style="vertical-align: middle;" src="http://projecteuler.net/images/symbol_lt.gif" border="0" alt="" width="10" height="10" /> 1000, is the number of solutions maximised?</em></p>
<p>So, I decided that I could look for Pythagorean triplets using the following snippet:</p></div>
<div class="dean_ch" style="white-space: nowrap;">
<span class="kw1">def</span> checkCond<span class="br0">&#40;</span>a, b, c<span class="br0">&#41;</span>: <span class="co1">#c is the bloody hypotenuse and a&lt;b</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> c ** <span class="nu0">2</span> == a ** <span class="nu0">2</span> + b ** <span class="nu0">2</span> <span class="kw1">and</span> a + b + c &lt; <span class="nu0">1000</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">False</span></p>
<p><span class="kw1">for</span> c <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span><span class="nu0">1000</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> b <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span>c<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> a <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span>b<span class="nu0">+1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> checkCond<span class="br0">&#40;</span>a, b, c<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> peri_count.<span class="me1">has_key</span><span class="br0">&#40;</span>a+b+c<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peri_count<span class="br0">&#91;</span>a+b+c<span class="br0">&#93;</span> += <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peri_count<span class="br0">&#91;</span>a+b+c<span class="br0">&#93;</span> = <span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span><br />
&nbsp;</div>
<p>That gives us a dictionary whose (key, value) pairs are (perimeter, count). We need the max count and then pick the corresponding perimeter. I hence wrote a small function (which was very inefficient) to make the value the keys and the keys the values. I think it is the second time I used &#8220;lambda&#8221;:</p>
<div class="dean_ch" style="white-space: nowrap;">
<span class="kw1">def</span> flip<span class="br0">&#40;</span>dict_name<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#8221;</span><span class="st0">&#8216;Flip the dictionary&#8217;</span>s <span class="br0">&#40;</span>key, value<span class="br0">&#41;</span> pairs to <span class="br0">&#40;</span>value, key<span class="br0">&#41;</span><span class="st0">&#8221;</span><span class="st0">&#8216;<br />
&nbsp; &nbsp; &nbsp; &nbsp; return dict(map(lambda item: (item[1], item[0]), dict_name.items()))<br />
</span></div>
<p>and finally, to complete the script:</p>
<div class="dean_ch" style="white-space: nowrap;">
peri_count = flip<span class="br0">&#40;</span>peri_count<span class="br0">&#41;</span></p>
<p>max_no = <span class="kw2">max</span><span class="br0">&#40;</span>peri_count.<span class="me1">keys</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="kw1">print</span> peri_count<span class="br0">&#91;</span>max_no<span class="br0">&#93;</span><br />
&nbsp;</div>
<p>So, here are the rather shameful execution stats:</p>
<pre>
$ time python euler39.py
840

real    3m15.140s
user    0m0.046s
sys     0m0.015s
</pre>
<p>I suggest you throw a glance at the awesome header that <a href="http://desk.stinkpot.org:8080/blog">http://desk.stinkpot.org:8080/blog/</a> has.</p>
<p>Happy Coding. </p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2008/08/23/pythagoras-lambdas/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Factoradic</title>
		<link>http://shriphani.com/blog/2008/08/13/factoradic/</link>
		<comments>http://shriphani.com/blog/2008/08/13/factoradic/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 19:02:28 +0000</pubDate>
		<dc:creator>Shriphani</dc:creator>
				<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://shriphani.com/blog/?p=200</guid>
		<description><![CDATA[I was trying to solve the lexicographic permutation problem yesterday and out of plain curiosity, I decided to carry out some research on Wikipedia. I found this thing called factoradic which is a factorial-based number system. I conveniently used this thing to approach the solution and decided to work out how factoradic actually worked. I [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to solve the lexicographic permutation problem yesterday and out of plain curiosity, I decided to carry out some research on Wikipedia. I found this thing called factoradic which is a factorial-based number system. I conveniently used this thing to approach the solution and decided to work out how factoradic actually worked.</p>
<p>I decided to assign this thing called a rank to the permutation. So assuming that we have 9 digits [1, 2, 3, 4, 5, 6, 7, 8, 9] and that we need to find the 2092nd lexicographic permutation, we begin as follows.</p>
<p>Assume that the first digit is 1 (the smallest of the digits we have). There are 8 remaining digits. So, the maximum number starting with one should occupy the 40320th position ( 8! = 40320 ). Since 2092 &lt; 40320, we know that the first digit is 1.</p>
<p>Now, we place the next smallest digit beside 1. We have 7 digits left and the maximum number whose 1st two digits are 1 and 2 occupies the position 5040. 2092 &lt; 5040 and hence, we can confirm that the first two digits are 1 and 2 indeed. We now append 3 at the end of our current permutation. We have 6 other digits and hence the maximum number of numbers we can get with 123 in front is 720. Now, 2092 &gt; 720 and this means that the number placed 2092 is greater than &#8220;123xxxxxx&#8221; where the &#8220;x&#8221; represent the digits we have not come across yet.</p>
<p>We now replace 3 by 4 (the permutation becomes &#8220;124xxxxxx&#8221;). The maximum number of numbers is 720 + 720 = 1440 which is still lesser than 2092 and we can safely replace 4 by 5 and the maximum position we get is 1440 + 720 = 2160 which is greater than 2092. So the permutation now looks like &#8220;125xxxxxx&#8221;.</p>
<p>Placing the minimum number from the remaining numbers after 5 in our permutation (the number is 3), we get something like &#8220;1253xxxxx&#8221;. The maximum rank is 1440 + 5! = 1440 + 120 = 1570. This falls short of 2092. So we replace 3 by 4, the maximum position is 1440 + 120 + 120 = 1690. Still short, put 6 there, the maximum position is 1810. Put 7 there, the rank becomes 1930. Put 8 there and the rank becomes 2050. Still short, replace 8 by 9 and 2170 is what we get. So our permutation now looks like &#8220;1259xxxxx&#8221;.</p>
<p>It should be straightforward from here on. Proceed in this fashion and you should end up at 125963784.</p>
<p>If you look at the above procedure and note down the number of replacements, you get the factoradic! It is as simple as that.</p>
<p>So, I whipped up a small script to get the factoradic:</p>
<p>First a straightforward factorial function:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> factorial<span class="br0">&#40;</span>n<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">if</span> n==<span class="nu0">0</span> <span class="kw1">or</span> n==<span class="nu0">1</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">1</span><br />
&nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> n * factorial<span class="br0">&#40;</span>n<span class="nu0">-1</span><span class="br0">&#41;</span></div>
<p>Then, a function that greedily chooses the digit of the factoradic:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> genFact<span class="br0">&#40;</span>decimal_num, position<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span>position, <span class="nu0">-1</span>, <span class="nu0">-1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; fact = factorial<span class="br0">&#40;</span>position<span class="br0">&#41;</span> * i<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> fact &lt; decimal_num:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> i, decimal_num &#8211; fact<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">continue</span></div>
<p>Finally a function that generates the entire factoradic:</p>
<div class="dean_ch" style="white-space: nowrap;"><span class="kw1">def</span> getFactoradic<span class="br0">&#40;</span>decimal_num, length<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; factoradic = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; num = decimal_num<br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span>length<span class="nu0">-1</span>, <span class="nu0">-1</span>, <span class="nu0">-1</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; bag = genFact<span class="br0">&#40;</span>num, i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; factoradic.<span class="me1">append</span><span class="br0">&#40;</span>bag<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num = bag<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> factoradic</div>
<p>Testing this on 2092 with an initial number containing 9 elements, we get:<br />
[0,0,2,5,2,0,1,1,0]</p>
<p>Which leads us to the answer from my calculations.</p>
<p>I decided to solve the project euler problem (<a href="http://projecteuler.net/index.php?section=problems&amp;id=24">number 24</a>) on lexicographic permutations using this algorithm and the factoradic I obtained was [2, 6, 6, 2, 5, 1, 2, 1, 1, 0] which leads us to 2783915460 which turned out to be the right answer.</p>
<p>You can obtain the factoradic generating script at <a href="http://shriphani.com/scripts/factoradic.py" target="_blank">http://shriphani.com/scripts/factoradic.py</a> .</p>
]]></content:encoded>
			<wfw:commentRss>http://shriphani.com/blog/2008/08/13/factoradic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
