<?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>{Thoughts}</title>
	<atom:link href="http://iam.elbenshira.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://iam.elbenshira.com</link>
	<description>Secrets revealed.</description>
	<lastBuildDate>Thu, 09 Apr 2009 20:27:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Integral Calculus in Haskell</title>
		<link>http://iam.elbenshira.com/archives/151_integral-calculus-in-haskell/</link>
		<comments>http://iam.elbenshira.com/archives/151_integral-calculus-in-haskell/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 20:27:23 +0000</pubDate>
		<dc:creator>Elben Shira</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[calculus]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://iam.elbenshira.com/?p=151</guid>
		<description><![CDATA[We started learning Haskell in Professor Misra&#8217;s class. Since this is the first functional programming language I have been exposed to, I decided to write an integrator.

small_dx = 0.0001

-- integrate a function f from a to b
integrate f a b = integrateGen f a b small_dx 0

-- Generalize the integrate function to take
-- advantage of [...]]]></description>
			<content:encoded><![CDATA[<p>We started learning Haskell in Professor Misra&#8217;s class. Since this is the first functional programming language I have been exposed to, I decided to write an integrator.</p>
<pre class="brush: plain;">
small_dx = 0.0001

-- integrate a function f from a to b
integrate f a b = integrateGen f a b small_dx 0

-- Generalize the integrate function to take
-- advantage of tail recursion.
-- integrate a function f from x to b
integrateGen f x b dx sum
 | x &gt; b = sum
 | otherwise = integrateGen f (x+dx) b dx (sum + (f x) * dx)

f1 x = 1
f2 x = x**2
f3 x = x * exp (x**2)

-- normal distribution
std = 1
mean = 0
normal x =
  (1/(std*sqrt (2*pi)))*exp(-(x-mean)**2 / (2*std**2))
</pre>
<p><code>integrate</code> takes in three parameters, <code>f</code> is a function, and <code>a</code> and <code>b</code> are doubles. If you are familiar with Haskell types, here is the type of <code>integrate</code>:<br />
<code><br />
*Main> :t integrate<br />
integrate :: (Double -> Double) -> Double -> Double -> Double<br />
</code><br />
<code>integrate</code> calls a general version of <code>integrate</code> called <code>integrateGen</code>. <code>integrateGen</code> takes advantage of <a href="http://en.wikipedia.org/wiki/Tail_recursion">tail recursion</a>, which allows the compiler to internally convert the recursive function (which would take a lot of stack space) to an iterative function. The <code>dx</code> in <code>integrateGen</code> is the usual calculus <img src='http://s.wordpress.com/latex.php?latex=dx%20&#038;bg=ffffff&#038;fg=000000&#038;s=-2' alt='dx ' title='dx ' class='latex' />, and <code>sum</code> holds what we have calculated so far.</p>
<p>The functions we declared are:<br />
<img src='http://s.wordpress.com/latex.php?latex=f_%7B1%7D%28x%29%20%3D%201.%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f_{1}(x) = 1. ' title='f_{1}(x) = 1. ' class='latex' /><br />
<img src='http://s.wordpress.com/latex.php?latex=f_%7B2%7D%28x%29%20%3D%20x%5E%7B2%7D.%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f_{2}(x) = x^{2}. ' title='f_{2}(x) = x^{2}. ' class='latex' /><br />
<img src='http://s.wordpress.com/latex.php?latex=f_%7B3%7D%28x%29%20%3D%20x%20%5Cexp%7B%5C%7Bx%5E%7B2%7D%5C%7D%7D.%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f_{3}(x) = x \exp{\{x^{2}\}}. ' title='f_{3}(x) = x \exp{\{x^{2}\}}. ' class='latex' /> where <img src='http://s.wordpress.com/latex.php?latex=%5Cexp%7B%5C%7B%5Cphi%5C%7D%7D%20%5Cequiv%20e%5E%7B%5Cphi%7D.&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\exp{\{\phi\}} \equiv e^{\phi}.' title='\exp{\{\phi\}} \equiv e^{\phi}.' class='latex' /></p>
<p>Now we are ready to test this baby out. Let&#8217;s start with an easy one:</p>
<pre class="brush: plain; light: true;">
*Main&gt; integrate f1 0 5
5.000000000001686
</pre>
<p>This is pretty close to the actual answer: <img src='http://s.wordpress.com/latex.php?latex=%5Cint_%7B0%7D%5E%7B5%7D%20f_%7B1%7D%28x%29%20%5C%20dx%20%3D%20%5Cint_%7B0%7D%5E%7B5%7D%201%20%5C%20dx%20%3D%20x%5Cbigr%5Cvert%5E%7B5%7D_%7B0%7D%20%3D%205.&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\int_{0}^{5} f_{1}(x) \ dx = \int_{0}^{5} 1 \ dx = x\bigr\vert^{5}_{0} = 5.' title='\int_{0}^{5} f_{1}(x) \ dx = \int_{0}^{5} 1 \ dx = x\bigr\vert^{5}_{0} = 5.' class='latex' /></p>
<p>Let&#8217;s try a more interesting problem:</p>
<pre class="brush: plain; light: true;">
*Main&gt; integrate f3 0 1
0.8592768342831555
</pre>
<p>The true answer is:</p>
<p><img src='http://s.wordpress.com/latex.php?latex=%5Cdisplaystyle%5Cint_%7B0%7D%5E%7B1%7D%20f_%7B3%7D%28x%29%20%5C%20dx%20%3D%20%5Cint_%7B0%7D%5E%7B1%7D%20x%20%5Cexp%7B%5C%7Bx%5E%7B2%7D%5C%7D%7D%20%5C%20dx%20%3D%20%5Cfrac%7B%5Cexp%7B%5C%7Bx%5E%7B2%7D%5C%7D%7D%7D%7B2%7D%20%5C%20%5Cbiggr%5Cvert%5E%7B1%7D_%7B0%7D%20%3D%20%5Cfrac%7Be%7D%7B2%7D%20-%20%5Cfrac%7B1%7D%7B2%7D%20%5Capprox%200.859141.&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\displaystyle\int_{0}^{1} f_{3}(x) \ dx = \int_{0}^{1} x \exp{\{x^{2}\}} \ dx = \frac{\exp{\{x^{2}\}}}{2} \ \biggr\vert^{1}_{0} = \frac{e}{2} - \frac{1}{2} \approx 0.859141.' title='\displaystyle\int_{0}^{1} f_{3}(x) \ dx = \int_{0}^{1} x \exp{\{x^{2}\}} \ dx = \frac{\exp{\{x^{2}\}}}{2} \ \biggr\vert^{1}_{0} = \frac{e}{2} - \frac{1}{2} \approx 0.859141.' class='latex' /></p>
<p>Next time, we will use this little program to do some useful statistical calculations.</p>
<h3>Optimizations</h3>
<p>We generalized integrate to take advantage of tail recursion, but for some reason, the GHC interpreter does not figure this out. As a result, we can get stack overflows when we have large limits. So we are forced to compile it with optimizations turned on. First we need to add a main function:</p>
<pre class="brush: plain; light: true;">
-- replace with whatever you wanted to integrate
main = print (integrate funct2 0 999)
</pre>
<p>Then we compile it with optimizations turned on:</p>
<pre class="brush: plain;">
$ ghc -O --make filename.hs
</pre>
<p>This is one thing that bugs me about functional programming. Thinking about and writing programs recursively may be more elegant and easier to understand, but then we are putting our trust on the compiler for optimizations, which is scary if we don&#8217;t understand how the compiler works. But nevertheless, Haskell is a fun and powerful language, and I can&#8217;t wait to do some other cool things with it!</p>
]]></content:encoded>
			<wfw:commentRss>http://iam.elbenshira.com/archives/151_integral-calculus-in-haskell/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Playing with Memory in C++</title>
		<link>http://iam.elbenshira.com/archives/100_playing-with-memory-in-c/</link>
		<comments>http://iam.elbenshira.com/archives/100_playing-with-memory-in-c/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 21:30:14 +0000</pubDate>
		<dc:creator>Elben Shira</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://iam.elbenshira.com/?p=100</guid>
		<description><![CDATA[I gave a two-hour lecture over C++ a couple of months ago. It was oriented towards Java programmers, so naturally I bashed in pointers and stack versus heap memory allocations into their heads. One of the examples I demoed was this:

// The outputs given is based on my machine; it may
// be different on yours.

#include [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a two-hour lecture over C++ a couple of months ago. It was oriented towards Java programmers, so naturally I bashed in pointers and stack versus heap memory allocations into their heads. One of the examples I demoed was this:</p>
<pre class="brush: cpp;">
// The outputs given is based on my machine; it may
// be different on yours.

#include &lt;iostream&gt;
using namespace std;

int main() {
  int *p = new int(144);
  cout &lt;&lt; *p &lt;&lt; endl;   // output: 144

  delete p;
  cout &lt;&lt; *p &lt;&lt; endl;   // output: 144 (weird?)

  char *z = new char('a');
  cout &lt;&lt; *p &lt;&lt; endl;   // output: 97 (crazy!)
  // note: 97 is the numerical representation of 'a'.

  delete z;
  return 0;
}
</pre>
<p>This example reveals how a pointer works internally. After line 11, we have no guarantee about what data <code>p</code> points to, so we should not use <code>p</code> ever again. But because we are evil programmers, we output <code>*p</code> on line 12 and line 15 and get unexpected but entertaining results. Why is 144 the output of line 12 even though <code>p</code> was deleted? And what is up with the output of line 15?</p>
<p>The keyword <code>delete</code> takes, as an argument, a pointer. In reality, <code>delete</code> doesn&#8217;t actually delete anything. Instead, <code>delete</code> lets the operating system know that the memory blocks (on the heap) the pointer is pointing to are now up for grabs. That is, new data can replace the &#8220;deleted&#8221; data. But we do not add any new data onto the heap before line 12, so the value 144 is never replaced. This is why line 12 still outputs 144.</p>
<p>Line 14 creates a new character &#8216;a&#8217; on the heap. We have no control over where &#8216;a&#8217; is placed in memory. On my machine, however, the operating system decided to put &#8216;a&#8217; at the exact memory address where 144 used to be. As a result, when we print out <code>*p</code>, we get 97, which is the ASCII numerical representation of &#8216;a&#8217;.</p>
<p>Fun stuff.</p>
]]></content:encoded>
			<wfw:commentRss>http://iam.elbenshira.com/archives/100_playing-with-memory-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The beginning of {Thoughts}</title>
		<link>http://iam.elbenshira.com/archives/152_the_beginning_of_thoughts/</link>
		<comments>http://iam.elbenshira.com/archives/152_the_beginning_of_thoughts/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 07:54:31 +0000</pubDate>
		<dc:creator>Elben Shira</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iam.elbenshira.com/?p=1</guid>
		<description><![CDATA[I am a literate person, and thus I write {Thoughts}. What better explanation exists?
]]></description>
			<content:encoded><![CDATA[<p>I am a literate person, and thus I write {Thoughts}. What better explanation exists?</p>
]]></content:encoded>
			<wfw:commentRss>http://iam.elbenshira.com/archives/152_the_beginning_of_thoughts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

