<?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 Blogs &#187; Fractions</title>
	<atom:link href="http://www.algorithm.co.il/blogs/index.php/tag/fractions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Thu, 22 Apr 2010 21:04:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Computing Large Determinants in Python</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/computing-large-determinants-in-python/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/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[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[computer science]]></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[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 overflow a float and reach +inf. Reaching +inf is bad: once you do, you lose all the [...]]]></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/index.php/programming/python/computing-large-determinants-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
