<?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>mathemagicio.us &#187; CS</title>
	<atom:link href="http://mathemagicio.us/tag/cs/feed/" rel="self" type="application/rss+xml" />
	<link>http://mathemagicio.us</link>
	<description>Ramblings and musings about math, science and truth</description>
	<lastBuildDate>Wed, 14 Oct 2009 01:02:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A plea for notational common sense</title>
		<link>http://mathemagicio.us/2009/10/13/a-plea-for-notational-common-sense/</link>
		<comments>http://mathemagicio.us/2009/10/13/a-plea-for-notational-common-sense/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 01:02:47 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=585</guid>
		<description><![CDATA[Tradition can often be a retarding force in mathematics. Advancements in logic were held back by centuries due to a collective belief that Aristotle had it all figured out and there was nothing left to discover. Non-Euclidean geometry wasn&#8217;t seriously studied until the 1800s because no one could think of the parallel postulate being false.
We [...]]]></description>
			<content:encoded><![CDATA[<p>Tradition can often be a retarding force in mathematics. Advancements in logic were held back by <em>centuries</em> due to a collective belief that Aristotle had it all figured out and there was nothing left to discover. Non-Euclidean geometry wasn&#8217;t seriously studied until the 1800s because no one could think of the parallel postulate being false.</p>
<p>We nowadays tend to recognize the dangers of blindly following tradition, yet in virtually all areas of mathematics we continue to adhere to notational lunacy, and pass it on to future generations starting as early as in junior high school.</p>
<p>This lunacy is so deeply ingrained in our minds that we see nothing wrong with the following expression:</p>
<p style="text-align: center;"><img src='/wp-content/plugins/latexrender/pictures/a9da9b7a8748bc7e5a54805ffc7a30e3_3.5pt.png' title='\sin(f(x)^2)' alt='\sin(f(x)^2)'  style="vertical-align:-3.5pt;" ></p>
<p>Let&#8217;s decipher the above. We start with a number <em>x</em>, apply <em>f</em>, square the result, and finally apply <em>sin</em>. Yet how do we recover this order of operations from the symbols above? Starting at <em>x</em> in the middle we move left to <em>f</em>, right to the <img src='/wp-content/plugins/latexrender/pictures/51bb910207e57c5f8062d01966d7a221_1.0pt.png' title='\bullet^2 ' alt='\bullet^2 '  style="vertical-align:-1.0pt;" >, and far left to the <em>sin</em>.</p>
<p>Although we read and write left-to-right, and so naturally imagine time evolving in this direction, the notation <em>f(x)</em> (due to Euler) means the opposite: start with <em>x</em>, then apply <em>f</em>. Even worse is when we have an expression like <em>f(g(h(x)))</em>. The very first operation is written on the very right, and the final operation is written on the very left, in complete contrast to how we normally think.</p>
<p>Worse still is that we do read <em>some</em> operations left-to-right, like raising to a power, and so we end up mentally zig-zagging across mathematical writing to even decipher mathematical expressions.</p>
<p>But worst of all is the following: when we write a function <img src='/wp-content/plugins/latexrender/pictures/e0fcfcf1bc30612af09825187fbbad7d_2.94444pt.png' title='f:A\to B' alt='f:A\to B'  style="vertical-align:-2.94444pt;" >, <em>A</em> denotes the input domain, and <em>B</em> denotes the output domain: input on the left, output on the right. As we&#8217;d expect in a sane world. If we also have a function <img src='/wp-content/plugins/latexrender/pictures/f54369708b7c914b474a63972dc1f05a_2.94444pt.png' title='g:B\to C' alt='g:B\to C'  style="vertical-align:-2.94444pt;" >, this left-to-right notation suggests we should be able to follow the arrows (<em>A</em> to <em>B</em> to <em>C</em>) to compose <em>f</em> and <em>g</em>. Yet, of course, the composite is called <em>gf</em> rather than <em>fg</em>. When reading <em>gf</em> we are meant to mentally reverse the direction of the arrows, so that <img src='/wp-content/plugins/latexrender/pictures/e4e23d429467d6664f8498cc5566b38a_2.94444pt.png' title='g:C\gets B' alt='g:C\gets B'  style="vertical-align:-2.94444pt;" > and <img src='/wp-content/plugins/latexrender/pictures/898cb53cb1c1b732b75ed411ece724e8_2.94444pt.png' title='f:B\gets A' alt='f:B\gets A'  style="vertical-align:-2.94444pt;" >, although we never write this way!</p>
<p>This is psychotic behaviour, and we should put a stop to it. I suggest we forget tradition, and instead succumb to notational common sense to write expressions like <em>x.f.g</em> rather than <em>g(f(x))</em>.</p>
<p>This may seem bizarre at first, but at least one group of scientists adopted this notation years ago. In object-oriented programming languages a mental shift is required, where one stops thinking that <em>functions have certain inputs</em>, and instead starts to see that <em>inputs have certain functions</em>. In doing so, the left-to-right notation becomes completely natural, and we would write <code>deck.sort.pop</code> to sort a deck of cards and remove the last one, rather than <code>pop(sort(deck))</code> as we&#8217;d expect in the mathematicians&#8217; ridiculous notation.</p>
<p>I believe that a notational shift would make all of our lives easier in the long run, but of course it will likely never happen. Just as negative charge will always correspond to an excess of electrons, Euler&#8217;s notation is an unfortunate convention that is simply known by too many people. As a result, already-difficult subjects like category theory are made even more maddening for silly reasons, and high school students have far more difficulty learning precalculus than they should.</p>
<p>Help us, Barack Obama, we need Change.</p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/10/13/a-plea-for-notational-common-sense/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twenty questions, in the degenerate case when 20 = lg(n)</title>
		<link>http://mathemagicio.us/2009/08/03/twenty-questions-in-the-degenerate-case-when-20-lgn/</link>
		<comments>http://mathemagicio.us/2009/08/03/twenty-questions-in-the-degenerate-case-when-20-lgn/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 03:40:35 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Complexity]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=554</guid>
		<description><![CDATA[A new post at Computational Complexity asks the following question:
(Problem 1.) You are trying to determine a secret integer x between 1 and n inclusive, using only queries of the form &#8220;is x = a (mod b)?&#8221; for integers a and b of your choosing. How many queries are required to determine x in the [...]]]></description>
			<content:encoded><![CDATA[<p>A new <a href="http://blog.computationalcomplexity.org/2009/08/find-number-again.html">post at Computational Complexity</a> asks the following question:</p>
<p style="padding-left: 30px; ">(Problem 1.) You are trying to determine a secret integer <em>x</em> between 1 and <em>n </em>inclusive, using only queries of the form &#8220;is x = <em>a</em> (mod <em>b</em>)?&#8221; for integers <em>a</em> and <em>b</em> of your choosing. How many queries are required to determine <em>x</em> in the worst case?</p>
<p>And more restrictively,</p>
<p style="padding-left: 30px; ">(Problem 2.) Same as above but you are only allowed to make queries where <em>b</em> is prime.</p>
<p>The comments section seems to reveal at least a partial solution, but these are fun problems, so at least give them a shot before reading the solution there (or here.) If you&#8217;d like the solution spoiled anyway, read on!<span id="more-554"></span></p>
<p>First let&#8217;s talk about a problem that has a more intuitive solution. Namely, the same problem of finding an integer between 1 and <em>n</em>, but where we are allowed to ask questions of the form &#8220;is x <em>≤</em> a?&#8221; for our choice of integers <em>a</em>.</p>
<p>Computer scientists in the audience should recognize the solution: first, find out about the midpoint. That is, whether <em>x</em> ≤ <em>n</em>/2. Whatever the answer is, we split the search space in half! That is, we originally knew that <em>x</em> must be one of <em>n</em> values. After the first query, we know that <em>x</em> must be one of <em>n</em>/2 values: either 1 ≤ x ≤ n/2 or n/2 &lt; x ≤ n. Either way, we can simply repeat the process again! After the second query, we know it must be one of <em>n</em>/4 values. And so on. For example, for 1 ≤ <em>x</em> ≤ 8, the following sequence of questions would reveal that the answer is <em>x</em> = 3.</p>
<p style="text-align: center; "><img class="aligncenter size-medium wp-image-555" title="Binary search" src="http://mathemagicio.us/wp-content/uploads/2009/08/binary-search-300x285.gif" alt="Binary search" width="240" height="228" /></p>
<p>It should be easy to convince yourself of the fact that you can only make these divisions lg(<em>n</em>) (that&#8217;s log-base-2*) times before you determine <em>x</em>.</p>
<p>Furthermore, this is the best we can do: any algorithm that finds <em>x</em> using only true/false queries requires at least lg(<em>n</em>) queries in the worst case. Why? Essentially, because this is how many bits there are in the binary expansion of <em>x</em>. Take <em>n</em> = 8 for example. If you could determine any 1 ≤ <em>x ≤ 8</em> using only two queries in the worst case, then any such <em>x</em> could be written uniquely using only 2 bits, which is impossible.</p>
<p>But let&#8217;s go back to Problem 1. Notice that the above argument applies for <em>any</em> algorithm that may only make queries about <em>x</em> whose answers are boolean. So we may immediately say that <em>at least</em> lg(<em>n</em>) queries are required by any algorithm that solves it.</p>
<p>However, is the answer again <em>exactly</em> lg(<em>n</em>)? Our binary search problem had the &#8220;nice&#8221; property that no matter what the answer to our previous query was, we could always construct <em>another</em> query that cut the search space in half <em>again</em>. Any boolean query may be thought of as a filter that eliminates a certain portion of the search space (which here is simply the set {1, 2, &#8230;, n}.) But not all queries may have this nice property. For example, if we were only allowed to ask questions of the form &#8220;is <em>x</em> = <em>a</em>?&#8221; then we would need to ask <em>n &#8211; 1</em> questions in the worst case—because no matter in what order we guess numbers, our first <em>n &#8211; 1</em> guesses could be wrong.</p>
<p>It turns out that the queries in Problem 1 do indeed have this nice property. The algorithm works like so:</p>
<ol>
<li>Ask whether <em>x</em> = 0 (mod 2). Doing so cuts the search space in half.</li>
<li>If the answer above was Yes, then ask whether <em>x</em> = 0 (mod 4). Since the previous answer was Yes, it must be the case that either <em>x</em> = 0 (mod 4) or <em>x</em> = 2 (mod 4), and the answer to this query again cuts the search space in half.</li>
<li>If the answer above was No, then ask whether <em>x</em> = 1 (mod 4). From the same reasoning as above, we again cut the search space in half.</li>
<li>Repeat the process using mod 8, 16, 32, etc., crafting the appropriate query.</li>
</ol>
<p>It shouldn&#8217;t be too hard to find the correct question to ask in each case. As an example,</p>
<p style="text-align: center; "><img class="aligncenter size-medium wp-image-557" title="Problem 1 solution example" src="http://mathemagicio.us/wp-content/uploads/2009/08/amodb-300x285.png" alt="Problem 1 solution example" width="240" height="228" /></p>
<p style="text-align: center;">
<p>This process can always be completed in lg(<em>n</em>) queries, which is the answer to Problem 1.</p>
<p>Note, however, that we always use a modulus of the form <img src='/wp-content/plugins/latexrender/pictures/fe401f62231ac24e3399751a415a4eaa_1.0pt.png' title='2^k' alt='2^k'  style="vertical-align:-1.0pt;" >, so this solution doesn&#8217;t help us solve Problem 2, where the modulus must always be prime! Don&#8217;t panic; I will discuss a solution to this problem in my next blog post.</p>
<p>* Here I&#8217;m assuming that <em>n</em> is a power of 2 to make the formula simpler, but if it isn&#8217;t then the correct answer is simply the <em>ceiling</em> of lg(<em>n</em>). That is, round lg(<em>n</em>) up. In fact, whenever I write lg(<em>n</em>) you can assume this is what I mean.</p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/08/03/twenty-questions-in-the-degenerate-case-when-20-lgn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Who says computer scientists don&#8217;t know how to have fun?</title>
		<link>http://mathemagicio.us/2009/07/30/who-says-computer-scientists-dont-know-how-to-have-fun/</link>
		<comments>http://mathemagicio.us/2009/07/30/who-says-computer-scientists-dont-know-how-to-have-fun/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 02:23:02 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Complexity]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Science!]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=541</guid>
		<description><![CDATA[Who says computer scientists don&#8217;t know how to have fun? As a counterexample, consider a new post over at Gödel&#8217;s Lost Letter that was obviously the result of some Wednesday-night festivities and intoxication.
I think, in a sense, we are the accelerators. As we smash ideas together sometimes we discover the further structure of our fundamental particles—our complexity classes.
Duuuuude.
]]></description>
			<content:encoded><![CDATA[<p>Who says computer scientists don&#8217;t know how to have fun? As a counterexample, consider a new <a href="http://rjlipton.wordpress.com/2009/07/30/complexity-classes-meet-particle-physics/">post</a> over at Gödel&#8217;s Lost Letter that was obviously the result of some Wednesday-night festivities and intoxication.</p>
<blockquote><p>I think, in a sense, we are the accelerators. As we smash ideas together sometimes we discover the further structure of our fundamental particles—our complexity classes.</p></blockquote>
<p><em>Duuuuude</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/07/30/who-says-computer-scientists-dont-know-how-to-have-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>This physically pains me</title>
		<link>http://mathemagicio.us/2009/05/21/this-physically-pains-me/</link>
		<comments>http://mathemagicio.us/2009/05/21/this-physically-pains-me/#comments</comments>
		<pubDate>Fri, 22 May 2009 03:58:23 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=436</guid>
		<description><![CDATA[Here&#8217;s a real excerpt (slightly modified to protect the stupid, and now fixed) from a web app I inherited. GRAAAAAAAARGH!

def encrypt_password
  ...
  self.salt = md5&#40;&#34;#{Time.now.to_s}-#{login}&#34;&#41;
  ...
end
&#160;
def generate_confirm_hash
  ...
  self.confirm_hash = md5&#40;&#34;#{Time.now.to_s}-#{email}&#34;&#41;
  ..
end

Pro-tip for hiring managers: ask candidates to identify problems in the above code, and smack any who fail [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a real excerpt (slightly modified to protect the stupid, and now fixed) from a web app I inherited. <strong><em>GRAAAAAAAARGH!</em></strong></p>

<div class="wp_syntax"><div class="code"><pre class="rails rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> encrypt_password
  ...
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">salt</span> = md5<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{Time.now.to_s}-#{login}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> generate_confirm_hash
  ...
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">confirm_hash</span> = md5<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{Time.now.to_s}-#{email}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  ..
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Pro-tip for hiring managers: ask candidates to identify problems in the above code, and smack any who fail to do so.</p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/05/21/this-physically-pains-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pop quiz</title>
		<link>http://mathemagicio.us/2009/05/04/pop-quiz/</link>
		<comments>http://mathemagicio.us/2009/05/04/pop-quiz/#comments</comments>
		<pubDate>Mon, 04 May 2009 23:35:15 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=405</guid>
		<description><![CDATA[All right, class. Who can tell me what the following program will output? Bear with me, I promise this gets interesting.

1
2
3
4
5
6
int main &#40;&#41;
&#123;
  int x = 0, y = 0;
  y = ++x + 10;
  printf&#40;&#34;%d, %d\n&#34;, x, y&#41;;
&#125;

That&#8217;s easy, it&#8217;s exactly what you expect: &#8220;1, 11&#8243;.
What about if you replace line [...]]]></description>
			<content:encoded><![CDATA[<p>All right, class. Who can tell me what the following program will output? Bear with me, I promise this gets interesting.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
  y <span style="color: #339933;">=</span> <span style="color: #339933;">++</span>x <span style="color: #339933;">+</span> <span style="color: #0000dd;">10</span>;
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d, %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s easy, it&#8217;s exactly what you expect: &#8220;1, 11&#8243;.</p>
<p>What about if you replace line 4 with the following?</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">y <span style="color: #339933;">=</span> x<span style="color: #339933;">++</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">10</span>;</pre></div></div>

<p>Again, easy! This time, the expression <code>x++</code> evaluates to the value of <code>x</code> <em>before</em> it is incremented, so the answer is &#8220;1, 10&#8243;.</p>
<p>Now, what about this?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
  x <span style="color: #339933;">=</span> <span style="color: #339933;">++</span>x <span style="color: #339933;">+</span> <span style="color: #0000dd;">10</span>;
  <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is no different than the first example, except that we&#8217;ve chucked the variable <code>y</code>, so the answer is &#8220;11&#8243;.</p>
<p>Last question: (and I&#8217;m sure you can see where this is going) what if you replace line 4 with the following?</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;">x <span style="color: #339933;">=</span> x<span style="color: #339933;">++</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">10</span>;</pre></div></div>

<p>&nbsp;</p>
<p><span style="font-size: 200%">I&#8217;m obviously trying to trick you, so you must realize that the answer is surprisingly also &#8220;11&#8243;, but can anyone tell me <em>why</em> it is so?</span></p>
<p>&nbsp;</p>
<p><strong>Extra credit:</strong> explain what happens if you replace line 4 with this seemingly-equivalent statement:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">10</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/05/04/pop-quiz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8230;fool me ε times, shame on my constant-depth circuit</title>
		<link>http://mathemagicio.us/2009/02/20/fool-me-%ce%b5-times-shame-on-my-constant-depth-circuit/</link>
		<comments>http://mathemagicio.us/2009/02/20/fool-me-%ce%b5-times-shame-on-my-constant-depth-circuit/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 07:32:28 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Complexity]]></category>
		<category><![CDATA[CS]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=315</guid>
		<description><![CDATA[Virtually everyone with a CS blog (see Scott Aaronson, Luca Trevisan, and the Complexity Blog) has for the past several weeks been going on about Mark Braverman&#8217;s recent proof of the Linial-Nisan conjecture. I don&#8217;t have too much to add to the discussion, being a neophyte to much of this stuff, but I did attend a talk [...]]]></description>
			<content:encoded><![CDATA[<p>Virtually everyone with a CS blog (see <a href="http://scottaaronson.com/blog/?p=381">Scott Aaronson</a>, <a href="http://lucatrevisan.wordpress.com/2009/01/23/bounded-independence-ac0-and-random-3sat/">Luca Trevisan</a>, and the <a href="http://weblog.fortnow.com/2009/01/fooling-constant-depth-circuits.html">Complexity Blog</a>) has for the past several weeks been going on about <a href="http://www.cs.toronto.edu/~mbraverm/">Mark Braverman</a>&#8217;s recent <a href="http://www.cs.toronto.edu/~mbraverm/FoolAC0v6.pdf">proof</a> of the Linial-Nisan conjecture. I don&#8217;t have too much to add to the discussion, being a neophyte to much of this stuff, but I did attend a talk Braverman gave at the University of Calgary today, in which he outlined his proof (which is remarkably short, for a proof of a conjecture that took nearly 20 years to solve.) Braverman illustrated his arguments with pictures, which really helped me get a handle on his techniques.<span id="more-315"></span></p>
<p>The content of the theorem relates to what strength of pseudo-randomness is &#8220;good enough&#8221; to fool circuits of a significantly limited size and depth into thinking the pseudo-randomness is truly random. As an analogy, the D in HD video is becoming ever and ever more H (currently we&#8217;re up to a resolution of <a href="http://en.wikipedia.org/wiki/UHDV">7680&#215;4320</a>) but our brains have a finite capacity and our eyes aren&#8217;t perfect lenses. Increase the resolution high enough, and eventually we won&#8217;t be able to tell the difference between the HD video signal and a &#8220;true&#8221; view of the world. Similarly, we&#8217;d like to know whether certain limited models of computation can tell apart real randomness from approximate randomness.</p>
<p>The Linial-Nisan conjecture restricts us to logical circuits used to compute answers to decision problems/boolean functions F<sup>1</sup> made up of AND, OR and NOT gates<sup>2</sup>, where the number of gates is allowed to be polynomial in size<sup>3</sup>  but the <em>depth</em> of the circuit must remain constant across all instances.</p>
<p>This class of circuits is called <a href="http://qwiki.stanford.edu/wiki/Complexity_Zoo:A#ac0">AC</a><sup><a href="http://qwiki.stanford.edu/wiki/Complexity_Zoo:A#ac0">0</a></sup>, and these conditions are more restrictive than they first seem. You might point out that any boolean function whatsoever can be computed by a depth-2 circuit (for example, in <a href="http://en.wikipedia.org/wiki/Disjunctive_normal_form">disjunctive normal form</a>.) And that&#8217;s true, but the number of gates then required might be superpolynomial in size. For example, even doing something as relatively simple as computing the parity of <em>n</em> bits cannot be done in AC<sup>0</sup>—you either need to increase the depth of the circuit as the problem grows in size, or have access to more complicated gates, or use more than a polynomial number of gates.</p>
<p>The question, then, is what kind of probability distributions over {0,1}<sup>n</sup> look identical to the uniform distribution to AC<sup>0</sup> circuits. In particular, the Linial-Nisan conjecture asks about distributions that <em>are</em> uniform when you restrict them to any k &lt; n bits, but are no longer uniform as a whole. For example, consider <img src='/wp-content/plugins/latexrender/pictures/6bd54b513b6790b82dc14d99c9e13e6d_3.33333pt.png' title='x_1,\ldots,x_{n-1}' alt='x_1,\ldots,x_{n-1}'  style="vertical-align:-3.33333pt;" > chosen uniformly, and <img src='/wp-content/plugins/latexrender/pictures/dd1f6010b56d72b28e5eca4433fb298b_3.33333pt.png' title='x_n = x_1 \oplus x_2 \oplus \cdots \oplus x_{n-1}' alt='x_n = x_1 \oplus x_2 \oplus \cdots \oplus x_{n-1}'  style="vertical-align:-3.33333pt;" >. The last variable is determined entirely by the others so the n bits as a whole aren&#8217;t uniformly random, but if you pick any <em>k = n-1</em> bits at a time you <em>do</em> get a uniform distribution. Do such distributions look uniform to AC<sup>0</sup> circuits? And if so, how small can <em>k</em> be? (Imagine a distribution half of whose bits are determined by the others. Can such a fundamentally non-random distribution be detected by an AC<sup>0</sup> circuit?)</p>
<p>Mark Braverman proved that <em>k</em> can be very low indeed. It need only be polylogarithmic (for example, of order <img src='/wp-content/plugins/latexrender/pictures/fc373df650005868942ae3a19309d27c_3.5pt.png' title='(\lg m)^4' alt='(\lg m)^4'  style="vertical-align:-3.5pt;" > for depth-2 circuits, in <em>m</em> being the number of gates.) In general, the exponent is quadratic in the depth to which we restrict ourselves. Asymptotically, this is tiny! And many suspect this bound is still too high, as <img src='/wp-content/plugins/latexrender/pictures/f6e82c6c82b4fa3ba24f495e5a6dec3d_3.5pt.png' title='k=(\lg m)^2' alt='k=(\lg m)^2'  style="vertical-align:-3.5pt;" > suffices for depth-2 circuits, leading to the suspicion that the tight bound is <img src='/wp-content/plugins/latexrender/pictures/8af064fe70d458134ed03c135c906236_3.5pt.png' title='k=(\lg m)^{O(d)}' alt='k=(\lg m)^{O(d)}'  style="vertical-align:-3.5pt;" >.</p>
<p>How does Braverman prove this result? He constructs low-degree polynomials that sufficiently approximate and &#8220;sandwich&#8221; the function <em>F</em> in question (think of the squeeze theorem from your Calculus 1 days), which was previously known to be sufficient to prove the result.</p>
<p>The first kind of approximating polynomials are due to Linial, Manser and Nisan way back in 1993. These LMN polynomials are created by taking the canonical, polynomial expansion of the function (for example, by interpolation) and simply chopping off the high-order terms. Such polynomials approximate <em>F</em> very well on average, but may not be equal to <em>F</em> at many, if any points. In other words, there is a small <img src='/wp-content/plugins/latexrender/pictures/998200580551054a6a2f1461037d915d_1.0pt.png' title='\ell^2' alt='\ell^2'  style="vertical-align:-1.0pt;" > distance between them.</p>
<div id="attachment_369" class="wp-caption aligncenter" style="width: 210px"><img class="size-full wp-image-369" title="lmn-approx" src="http://mathemagicio.us/wp-content/uploads/2009/02/lmn-approx.png" alt="lmn-approx" width="200" height="145" /><p class="wp-caption-text">LMN approximation (red) to a boolean function F (black)</p></div>
<p>The second kind of approximation is due to Razborov and Smolensky. This time, the approximating polynomial agrees with the function <em>F</em> on most inputs, but it may behave wildly wherever it doesn&#8217;t.</p>
<div id="attachment_372" class="wp-caption aligncenter" style="width: 210px"><img class="size-full wp-image-372" title="rs-approx" src="http://mathemagicio.us/wp-content/uploads/2009/02/rs-approx.png" alt="Razborov-Smolensky approximation (blue) of the same function" width="200" height="145" /><p class="wp-caption-text">Razborov-Smolensky approximation (blue) of the same function</p></div>
<p>(In fact, the jumpiness in the blue graph can be far more wild than the picture suggests, making it an awful approximation at these points.) At any rate, neither of these approximations behave quite the way we want them to. What we&#8217;d like is a low-degree approximation of <em>F</em> that agrees with it on most inputs (as in the blue approximation) and doesn&#8217;t stray too far away when it doesn&#8217;t (as in the red approximation.)</p>
<p>What Braverman discovered is that although the blue approximation can be very, <strong>very</strong> wrong at some points, this error can be checked in AC<sup>0</sup>! In particular, there&#8217;s a AC<sup>0</sup> function ε that takes on the value 1 whenever the blue approximation disagrees with <em>F</em> (and possibly at some other points that give false positives, but not too many.) This function ε tells us when we need to correct the blue approximation. Then, Braverman applies the red, wavy approximation to ε and takes its negation, giving us a low-degree polynomial which (nearly) vanishes whenever the blue approximation is inaccurate.</p>
<div id="attachment_381" class="wp-caption aligncenter" style="width: 190px"><img class="size-full wp-image-381" title="epsilon" src="http://mathemagicio.us/wp-content/uploads/2009/02/epsilon.png" alt="Approximation of 1-ε" width="180" height="190" /><p class="wp-caption-text">Approximation of 1-ε (orange)</p></div>
<p>Then he just multiplies the blue and orange functions together to create a new approximation! Doing this kills the blue guy whenever he misbehaves, resulting in a polynomial that approximates <em>F</em> in the nice ways we wanted, and which is still of low-enough degree to be usable.</p>
<p>That&#8217;s pretty much all there is to it! All that&#8217;s left to do is to fill in the gaps with some algebraic magic, and the claim is proved. Not bad at all.</p>
<p><sup>1</sup> For example, is <em>x</em> prime? Or, does the input graph have a vertex of degree <em>5</em>?<br />
<sup>2</sup> We allow the AND and OR gates to take any number of inputs, rather than only two.<br />
<sup>3</sup> For example, the circuit that computes the answer for inputs of size <em>n</em> could have <em>n</em><sup>13</sup> gates.</p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/02/20/fool-me-%ce%b5-times-shame-on-my-constant-depth-circuit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I love you, Barack Obama</title>
		<link>http://mathemagicio.us/2009/01/10/i-love-you-barack-obama/</link>
		<comments>http://mathemagicio.us/2009/01/10/i-love-you-barack-obama/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 18:22:21 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Obama]]></category>
		<category><![CDATA[Youtube]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=254</guid>
		<description><![CDATA[Despite rumors to the contrary, Barack Obama is not a secret bubble sorter.

]]></description>
			<content:encoded><![CDATA[<p>Despite rumors to the contrary, Barack Obama is not a secret bubble sorter.</p>
<p><span style='text-align:center; display: block;'><object width='425' height='350'><param name='movie' value='http://www.youtube.com/v/k4RRi_ntQc8'></param><param name='wmode' value='transparent'></param><embed src='http://www.youtube.com/v/k4RRi_ntQc8' type='application/x-shockwave-flash' wmode='transparent' width='425' height='350'></embed></object></span></p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/01/10/i-love-you-barack-obama/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Easy-peasy symbolic computation with Ruby</title>
		<link>http://mathemagicio.us/2009/01/03/easy-peasy-symbolic-computation-with-ruby/</link>
		<comments>http://mathemagicio.us/2009/01/03/easy-peasy-symbolic-computation-with-ruby/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 11:49:28 +0000</pubDate>
		<dc:creator>Mark Przepiora</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CS]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://mathemagicio.us/?p=120</guid>
		<description><![CDATA[Ruby is a wonderful language, largely deserving of the fanaticism surrounding it. There are a number of ways you can exploit its syntax to write concise, beautiful code. For example, to shuffle an array&#8230;

deck.sort_by&#123; rand &#125;

&#8230;or to pick out certain elements of one&#8230;

deck.find_all&#123; &#124;card&#124; card.suit == Clubs &#125;

&#8230;or to seamlessly cache computations.

def average_earnings
  @average_earnings [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby is a wonderful language, largely deserving of the fanaticism surrounding it. There are a number of ways you can exploit its syntax to write concise, beautiful code. For example, to shuffle an array&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">deck.<span style="color:#9900CC;">sort_by</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#CC0066; font-weight:bold;">rand</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>&#8230;or to pick out certain elements of one&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">deck.<span style="color:#9900CC;">find_all</span><span style="color:#006600; font-weight:bold;">&#123;</span> |card| card.<span style="color:#9900CC;">suit</span> == Clubs <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>&#8230;or to seamlessly cache computations.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> average_earnings
  <span style="color:#0066ff; font-weight:bold;">@average_earnings</span> ||= some_lengthy_computation
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>(Above, the ||= operator acts analogously to the familiar += operator. So if the instance variable @average_earnings already has a non-nil value, it is returned without any further computation. If on the other hand it is nil, then some_lengthy_computation is performed, @average_earnings is set to it, and returned.)</p>
<p>In addition, there are also a number of ridiculously short applications written in it, including a <a href="http://blogs.msdn.com/abhinaba/archive/2005/10/14/ruby-webserver-in-70-lines-of-code.aspx">web server</a> in 70 lines of code, a <a href="http://www.rubyinside.com/beast-an-open-source-rails-forum-in-500-lines-of-code-191.html">message board</a> application in 500 lines, and its slightly more verbose <a href="http://www.williambharding.com/blog/rails/savage-beast-20/">successor</a>.</p>
<p>In addition to these, I present a proof of concept of my own: Mathematica and Maple-like symbolic differentiation in about a hundred lines of code.</p>
<p><span id="more-120"></span>The idea is to have a Function class, which is subclassed by actual functions (Cos, Exp, Constant, etc.) as well as by operators (Addition, Multiplication, etc.)</p>
<p>Ruby allows us to overload operators, so we can add, subtract, multiply, and divide functions naturally, and define &#8220;f + g&#8221; as an instantiation of the Add class (which we will define later!)</p>
<p>Each subclass of Function must implement two methods: (1) diff, which returns another Function object (its derivative), and (2) the indexing method [], so given a Function object f and number x in its domain, we can evaluate f(x) via &#8220;f[x]&#8220;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Function
  <span style="color:#008000; font-style:italic;"># Skeleton. Returns: another Function</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> diff
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Skeleton. Returns: a number</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>index<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Begin algebraic operations</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>
    Add.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>,a<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>
    a = Constant.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> a.<span style="color:#9900CC;">kind_of</span>? <span style="color:#CC00FF; font-weight:bold;">Numeric</span>
    Multiply.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>,a<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>
    a = Constant.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> a.<span style="color:#9900CC;">kind_of</span>? <span style="color:#CC00FF; font-weight:bold;">Numeric</span>
    Divide.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>,a<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">+</span> a <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">-</span>@
    <span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#008000; font-style:italic;"># End algebraic operations</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>All binary operations are initialized the same way and have the same basic structure: they act in some way on two functions specified upon creation of the object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BinaryOperation <span style="color:#006600; font-weight:bold;">&lt;</span> Function
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>f, g<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@f</span> = f
    <span style="color:#0066ff; font-weight:bold;">@g</span> = g
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Multiply subclasses it as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Multiply <span style="color:#006600; font-weight:bold;">&lt;</span> BinaryOperation
  <span style="color:#9966CC; font-weight:bold;">def</span> diff
    <span style="color:#0066ff; font-weight:bold;">@f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@g</span>.<span style="color:#9900CC;">diff</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#0066ff; font-weight:bold;">@f</span>.<span style="color:#9900CC;">diff</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#0066ff; font-weight:bold;">@g</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@f</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#0066ff; font-weight:bold;">@g</span><span style="color:#006600; font-weight:bold;">&#91;</span>x<span style="color:#006600; font-weight:bold;">&#93;</span> 
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>We can similarly define Add and Divide. For any operation, we simply need to know how to differentiate the operation, and how to evaluate it.</p>
<p>We can also define some actual functions. For example,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Sin <span style="color:#006600; font-weight:bold;">&lt;</span> Function
  <span style="color:#9966CC; font-weight:bold;">def</span> diff
    Cos.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sin</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Cos <span style="color:#006600; font-weight:bold;">&lt;</span> Function
  <span style="color:#9966CC; font-weight:bold;">def</span> diff
    <span style="color:#006600; font-weight:bold;">-</span>Sin.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">cos</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Exp <span style="color:#006600; font-weight:bold;">&lt;</span> Function
  <span style="color:#9966CC; font-weight:bold;">def</span> diff
    <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">exp</span><span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Once we have all of our operations and functions defined, we can start computing and plotting functions and their derivatives,</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby ruby" style="font-family:monospace;">f = Cos.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">/</span> Exp.<span style="color:#9900CC;">new</span>
g = f.<span style="color:#9900CC;">diff</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span>0..10<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |a|
  x = a.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">10</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ft#{x}t#{f[x]}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&#40;</span>0..10<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |a|
  x = a.<span style="color:#9900CC;">to_f</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">10</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;f't#{x}t#{g[x]}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>With a little additional code, we could also print out functions symbolically.</p>
<p>Not bad for less than a couple hundred lines of code.</p>
]]></content:encoded>
			<wfw:commentRss>http://mathemagicio.us/2009/01/03/easy-peasy-symbolic-computation-with-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
