<?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; Programming</title>
	<atom:link href="http://mathemagicio.us/tag/programming/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>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>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>
