<?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>~Nacho/blog &#187; Math</title>
	<atom:link href="http://criptonita.com/~nacho/blog/category/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://criptonita.com/~nacho/blog</link>
	<description>the cutting edge of my mind!</description>
	<lastBuildDate>Fri, 22 Apr 2011 14:01:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Reverse Polish Notation Evaluation in Python</title>
		<link>http://criptonita.com/~nacho/blog/2011/02/23/reverse-polish-notation-evaluation-in-python/</link>
		<comments>http://criptonita.com/~nacho/blog/2011/02/23/reverse-polish-notation-evaluation-in-python/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 00:03:21 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=305</guid>
		<description><![CDATA[This introduction is followed by some Python code (function evaluate_postfix_expr) to evaluate expressions (only integers, but may be extended with ease) in Reverse Polish Notation (RPN). Some simple tests are also included in the bundle. I agree it&#8217;s a little useless, but I thought it might be useful for someone (CS students maybe?). If you [...]]]></description>
			<content:encoded><![CDATA[<p>This introduction is followed by some Python code (function evaluate_postfix_expr) to evaluate expressions (only integers, but may be extended with ease) in Reverse Polish Notation (RPN). Some simple tests are also included in the bundle.</p>
<p>I agree it&#8217;s a little useless, but I thought it might be useful for someone (CS students maybe?). If you want to examine the stack in each iteration you only have to turn debugging on. That can be accomplished by changing <em>logging.INFO</em> to <em>logging.DEBUG</em> (line 7).</p>
<p>Copy, distribute or do whatever you want with it. It&#8217;s released in the public domain.</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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span><span style="color: black;">&#41;</span>
&nbsp;
operators_table = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'+'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__add__</span>, 
             <span style="color: #483d8b;">'-'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__sub__</span>,
             <span style="color: #483d8b;">'*'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__mul__</span>,
             <span style="color: #483d8b;">'/'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__div__</span>,
             <span style="color: #483d8b;">'^'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__pow__</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>._message = <span style="color: #483d8b;">&quot;Expression error: %s&quot;</span> <span style="color: #66cc66;">%</span> message
    <span style="color: #ff7700;font-weight:bold;">def</span> _get_message<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>: 
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._message
    message = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span>_get_message<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TestEvaluation<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> test_correct<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">666</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;666&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>+<span style="color: #ff4500;">3</span>-<span style="color: #ff4500;">6</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 + 6 -&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 * 4 +&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 4 + *&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;3   3  *     3  *      3 *&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">7</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;7 2 / 4 ^&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 ^ 4 ^&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>+<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>+<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>-<span style="color: #ff4500;">3</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;5 1 2 + 4 * 3 - +&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_malformed<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;+&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;2 +&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;+ 2 2&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;2 2&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;2 2 + -&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;a 2 -&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> evaluate_postfix_expr<span style="color: black;">&#40;</span>expr<span style="color: black;">&#41;</span>:
    atoms = <span style="color: #dc143c;">re</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\s</span>+&quot;</span>, expr<span style="color: black;">&#41;</span>
    stack = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> 
    <span style="color: #ff7700;font-weight:bold;">for</span> atom <span style="color: #ff7700;font-weight:bold;">in</span> atoms:
        <span style="color: #ff7700;font-weight:bold;">if</span> atom <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;+&quot;</span>, <span style="color: #483d8b;">&quot;-&quot;</span>, <span style="color: #483d8b;">&quot;*&quot;</span>, <span style="color: #483d8b;">&quot;/&quot;</span>, <span style="color: #483d8b;">&quot;^&quot;</span><span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                op2 = stack.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                op1 = stack.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Too few operands (unbalanced)&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Calculating %d %s %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>op1, atom, op2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            atom = operators_table<span style="color: black;">&#91;</span>atom<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span>op1, op2<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                atom = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>atom<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Unable to parse '%s' as integer&quot;</span> <span style="color: #66cc66;">%</span> atom<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            stack.<span style="color: black;">append</span><span style="color: black;">&#40;</span>atom<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">MemoryError</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Too long expression&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Pushed element %d. Stack status: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>atom, stack<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>stack<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> stack.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Too many operands (unbalanced)&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2011/02/23/reverse-polish-notation-evaluation-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rolle, another mathematical key</title>
		<link>http://criptonita.com/~nacho/blog/2006/09/08/rolle-another-math-key/</link>
		<comments>http://criptonita.com/~nacho/blog/2006/09/08/rolle-another-math-key/#comments</comments>
		<pubDate>Fri, 08 Sep 2006 14:56:04 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=70</guid>
		<description><![CDATA[Breaking news: Vista could divide by zero in its final release!. Well, now serious stuff, like few posts ago, time to mention another mathematical milestone: Let f be differentiable on the open interval (a,b) and continuous on the closed interval [a,b]. If f(a)==f(b), then there is at least one point c in (a,b) where f&#8217;(c)==0 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Breaking news:</strong> <a href="http://www.microsoft.com/latam/windowsvista/">Vista</a> could divide by zero in its final release!. </p>
<p>Well, now serious stuff, like <a href="http://criptonita.com/~nacho/blog/?p=43">few posts ago</a>, time to mention another mathematical milestone:</p>
<blockquote><p>Let <strong>f</strong> be differentiable on the open interval (a,b) and continuous on the closed interval [a,b]. If <strong>f(a)==f(b)</strong>, then there is at least one point c in (a,b) where <strong>f&#8217;(c)==0</strong></p></blockquote>
<p>That&#8217;s all folks!</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2006/09/08/rolle-another-math-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fermat&#8217;s last theorem</title>
		<link>http://criptonita.com/~nacho/blog/2006/03/23/fermats-last-theorem/</link>
		<comments>http://criptonita.com/~nacho/blog/2006/03/23/fermats-last-theorem/#comments</comments>
		<pubDate>Thu, 23 Mar 2006 21:28:24 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Free time]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=43</guid>
		<description><![CDATA[Time to remember one of the most famous mathematical milestone: When n > 2 there aren&#8217;t non-zero integer solutions for x, y and z for the equation: x^n + y^n = z^n This theorem was enunciated near 1665 and demonstrated in 1995 by Andrew Wiles, sweets that the science gives to the humanity.]]></description>
			<content:encoded><![CDATA[<p>Time to remember one of the most famous mathematical milestone:</p>
<p><em>When n > 2 there aren&#8217;t non-zero integer solutions for x, y and z for the equation:</em></p>
<p><strong>x^n + y^n = z^n</strong></p>
<p>This theorem was enunciated near 1665 and demonstrated in 1995 by Andrew Wiles, sweets that the science gives to the humanity.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2006/03/23/fermats-last-theorem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

