<?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>Algorithm.co.il &#187; Research</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/research/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Computing Large Determinants in Python</title>
		<link>http://www.algorithm.co.il/blogs/computer-science/computing-large-determinants-in-python/</link>
		<comments>http://www.algorithm.co.il/blogs/computer-science/computing-large-determinants-in-python/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 19:38:08 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Float]]></category>
		<category><![CDATA[Fractions]]></category>
		<category><![CDATA[Matrix Determinants]]></category>
		<category><![CDATA[Numerical Analysis]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=176</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/computing-large-determinants-in-python/' addthis:title='Computing Large Determinants in Python'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Story: For my seminar work, I had to calculate the determinant of a large integer matrix. In this case, large meant n>=500. You might say that this isn&#8217;t very large and I would agree. However, it is large enough to &#8230; <a href="http://www.algorithm.co.il/blogs/computer-science/computing-large-determinants-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/computer-science/computing-large-determinants-in-python/' addthis:title='Computing Large Determinants in Python' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p><strong>Story:</strong><br />
For my seminar work, I had to calculate the determinant of a large integer matrix.<br />
In this case, large meant n>=500. You might say that this isn&#8217;t very large and I would agree. However, it is large enough to overflow a float and reach +inf. Reaching +inf is bad: once you do, you lose all the accuracy of the computation. Also, since determinant calculations use addition as well as subtraction, if +inf is reached in the middle of the calculation the value might never drop back, even if the determinant is a &#8220;small&#8221; number.</p>
<p>(Note: Even if the calculation hadn&#8217;t reached +inf, but just some very large floating point number, the same phenomenon could have occurred.)</p>
<p>As I said, my matrix was composed of integers. Since a determinant is a sum of multiples of matrix elements, you would expect such a determinant to be an integer as well. It would be nice to take advantage of that fact.</p>
<p>How should one compute such a determinant? Well, luckily Python has support for arbitrarily large integers. Let&#8217;s compute the determinant with them!<br />
Unfortunately, Python&#8217;s numpy computes determinants using LU decomposition, which uses division &#8211; hence, floating point values.<br />
Well, never mind, I know how to calculate a determinant, right?<br />
Ten minutes later the naive determinant calculation by minors was implemented, with one &#8220;minor&#8221; setback &#8211;  it takes O(n!) time.<br />
Googling for integer determinants yielded some articles about division free algorithms, which weren&#8217;t really easy to implement. There was one suggestion about using Rational numbers and Gauss Elimination, with pivots chosen to tame the growth of the nominator and denominator.</p>
<p>So, I hitched up a rational number class, using Python&#8217;s arbitrarily large integers as nominator and denominator. Then, I got some code to do Gauss Elimination, although my pivoting wasn&#8217;t the most fancy. This seemed to work, and I got my desired large determinants.</p>
<p><strong>Epilogue:</strong><br />
Not too long ago, I ran across <a href="http://code.google.com/p/mpmath/">mpmath</a>, a Python library for arbitrarily large floating point numbers. It is possible I could have used it instead of my own rational numbers. Next time I run into a relevant problem I&#8217;ll keep this library in mind.<br />
Also, an even shorter while ago, I became aware that since 2.6 Python boasts a fractions module. This will sure come in handy in the future.</p>
<p><strong>Code:</strong><br />
<a href="http://www.algorithm.co.il/sitecode/intdet.py">Available here</a>. If you intend to use it in Python 2.6 and above, I suggest replacing my Rational class with Python&#8217;s Fraction.<br />
(Note that I adapted this module to my needs. Therefore it has a timer that prints time estimations for computations, and it uses Psyco.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/computer-science/computing-large-determinants-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Small Python Interactive Interface Trick</title>
		<link>http://www.algorithm.co.il/blogs/programming/small-python-interactive-interface-trick/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/small-python-interactive-interface-trick/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 10:12:25 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Utility Functions]]></category>
		<category><![CDATA[Interactive Prompt]]></category>
		<category><![CDATA[NoParens]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=116</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/small-python-interactive-interface-trick/' addthis:title='Small Python Interactive Interface Trick'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Not too long ago, I helped someone with some research work, and one of the research tools was also used from the Python interactive prompt. The interface used was handy enough for development: intuitive, readable, short, and useful. Still, for &#8230; <a href="http://www.algorithm.co.il/blogs/programming/small-python-interactive-interface-trick/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/small-python-interactive-interface-trick/' addthis:title='Small Python Interactive Interface Trick' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Not too long ago, I helped someone with some research work, and one of the research tools was also used from the Python interactive prompt.<br />
The interface used was handy enough for development: intuitive, readable, short, and useful. Still, for interactive research it wasn&#8217;t handy enough.<br />
To improve the script, I decided to use a trick I heard about a long time ago &#8211; using __repr__:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> NoParens<span style="color: black;">&#40;</span><span style="color: #008000;">object</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>, obj<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">obj</span> = obj
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">obj</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        result = <span style="color: #008000;">self</span>.<span style="color: black;">obj</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span></pre></div></div>

<p>And using it looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">In <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>: <span style="color: #ff7700;font-weight:bold;">import</span> noparens
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>: <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
   ...:     <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">4</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>
   ...:
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>: t = noparens.<span style="color: black;">NoParens</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">test</span><span style="color: black;">&#41;</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>: t
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>: <span style="color: #ff4500;">256</span>
&nbsp;
In <span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span>: t,t,t
Out<span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span>: <span style="color: black;">&#40;</span><span style="color: #ff4500;">256</span>, <span style="color: #ff4500;">256</span>, <span style="color: #ff4500;">256</span><span style="color: black;">&#41;</span></pre></div></div>

<p>For research consoles that interact with other programs or devices, such as debuggers and sniffers I found this useful, and made work a bit less annoying.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/small-python-interactive-interface-trick/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

