<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Solving Systems Of Linear Equations</title>
	<atom:link href="http://programmingpraxis.com/2010/07/20/solving-systems-of-linear-equations/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/07/20/solving-systems-of-linear-equations/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Mon, 28 May 2012 03:30:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/07/20/solving-systems-of-linear-equations/#comment-1443</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Wed, 21 Jul 2010 20:26:03 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2630#comment-1443</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2010/07/21/programming-praxis-solving-systems-of-linear-equations/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Control.Arrow
import Data.List
import qualified Data.List.Key as K

mult :: Num a =&gt; [[a]] -&gt; [[a]] -&gt; [[a]]
mult a b = [map (sum . zipWith (*) r) $ transpose b &#124; r &lt;- a]

elim :: Fractional a =&gt; [a] -&gt; [a] -&gt; [a]
elim ~(x:xs) ~(y:ys) = zipWith (-) ys $ map (y / x *) xs

identity :: Num a =&gt; [[a]] -&gt; [[a]]
identity = zipWith (zipWith const) (iterate (0:) (1 : repeat 0))

lu :: Fractional a =&gt; [[a]] -&gt; ([[a]], [[a]])
lu = unzip . map unzip . f where
    f []      = []
    f ~(x:xs) = zip (1 : repeat 0) x :
                zipWith (:) (map (\(y:_) -&gt; (y / head x, 0)) xs)
                            (f $ map (elim x) xs)

perm :: (Fractional a, Ord a) =&gt; [[a]] -&gt; [[a]]
perm m = f $ zip (identity m) m where
    f [] = []
    f xs = a : f (map (second $ elim b) $ delete (a,b) xs)
           where (a,b) = K.maximum (abs . head . snd) xs

lup :: (Fractional a, Ord a) =&gt; [[a]] -&gt; ([[a]], [[a]], [[a]])
lup xs = (perm xs, l, u) where (l,u) = lu $ mult (perm xs) xs

lupsolve :: (Fractional a, Ord a) =&gt; [[a]] -&gt; [a] -&gt; [a]
lupsolve a b = f y u where
    (p,l,u) = lup a
    y = foldl (\x (l&#039;, pb&#039;) -&gt; x ++ [pb&#039; - sum (zipWith (*) x l&#039;)])
              [] (zip l (concat . mult p $ map return b))
    f _ [] = []
    f ~(y&#039;:ys) ~((r:rs):us) = (y&#039; - sum (zipWith (*) rs z)) / r : z
        where z = (f ys $ map tail us)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2010/07/21/programming-praxis-solving-systems-of-linear-equations/" rel="nofollow">http://bonsaicode.wordpress.com/2010/07/21/programming-praxis-solving-systems-of-linear-equations/</a> for a version with comments):</p>
<pre class="brush: css;">
import Control.Arrow
import Data.List
import qualified Data.List.Key as K

mult :: Num a =&gt; [[a]] -&gt; [[a]] -&gt; [[a]]
mult a b = [map (sum . zipWith (*) r) $ transpose b | r &lt;- a]

elim :: Fractional a =&gt; [a] -&gt; [a] -&gt; [a]
elim ~(x:xs) ~(y:ys) = zipWith (-) ys $ map (y / x *) xs

identity :: Num a =&gt; [[a]] -&gt; [[a]]
identity = zipWith (zipWith const) (iterate (0:) (1 : repeat 0))

lu :: Fractional a =&gt; [[a]] -&gt; ([[a]], [[a]])
lu = unzip . map unzip . f where
    f []      = []
    f ~(x:xs) = zip (1 : repeat 0) x :
                zipWith (:) (map (\(y:_) -&gt; (y / head x, 0)) xs)
                            (f $ map (elim x) xs)

perm :: (Fractional a, Ord a) =&gt; [[a]] -&gt; [[a]]
perm m = f $ zip (identity m) m where
    f [] = []
    f xs = a : f (map (second $ elim b) $ delete (a,b) xs)
           where (a,b) = K.maximum (abs . head . snd) xs

lup :: (Fractional a, Ord a) =&gt; [[a]] -&gt; ([[a]], [[a]], [[a]])
lup xs = (perm xs, l, u) where (l,u) = lu $ mult (perm xs) xs

lupsolve :: (Fractional a, Ord a) =&gt; [[a]] -&gt; [a] -&gt; [a]
lupsolve a b = f y u where
    (p,l,u) = lup a
    y = foldl (\x (l', pb') -&gt; x ++ [pb' - sum (zipWith (*) x l')])
              [] (zip l (concat . mult p $ map return b))
    f _ [] = []
    f ~(y':ys) ~((r:rs):us) = (y' - sum (zipWith (*) rs z)) / r : z
        where z = (f ys $ map tail us)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Solving Systems Of Linear Equations &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2010/07/20/solving-systems-of-linear-equations/#comment-1442</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Solving Systems Of Linear Equations &#171; Bonsai Code]]></dc:creator>
		<pubDate>Wed, 21 Jul 2010 20:25:45 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2630#comment-1442</guid>
		<description><![CDATA[[...] Praxis &#8211; Solving Systems Of Linear&#160;Equations By Remco Niemeijer  In yesterday&#8217;s Programming Praxis exercise our task is to implement some more matrix-related functions, [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Solving Systems Of Linear&nbsp;Equations By Remco Niemeijer  In yesterday&#8217;s Programming Praxis exercise our task is to implement some more matrix-related functions, [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: programmingpraxis</title>
		<link>http://programmingpraxis.com/2010/07/20/solving-systems-of-linear-equations/#comment-1441</link>
		<dc:creator><![CDATA[programmingpraxis]]></dc:creator>
		<pubDate>Wed, 21 Jul 2010 13:18:14 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2630#comment-1441</guid>
		<description><![CDATA[Fixed four errors.  I didn&#039;t do that very well, did I?]]></description>
		<content:encoded><![CDATA[<p>Fixed four errors.  I didn&#8217;t do that very well, did I?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/07/20/solving-systems-of-linear-equations/#comment-1438</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Tue, 20 Jul 2010 22:45:15 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=2630#comment-1438</guid>
		<description><![CDATA[Since I&#039;m currently at a conference I don&#039;t have as much time to devote to this as I&#039;d like, but here&#039;s my implementation of LU decomposition:

[sourcecode lang=&quot;css&quot;]
lu :: Fractional a =&gt; [[a]] -&gt; ([[a]], [[a]])
lu = unzip . map unzip . elim where
    elim [] = []
    elim ~((r:rs):xs) = zip (1 : repeat 0) (r:rs) :
                        zipWith (:) (map (\(y:_) -&gt; (y / r, 0)) xs)
                                    (elim $ map sub xs) where
        sub ~(y:ys) = zipWith (-) ys $ map (y / r *) rs
[/sourcecode]

If I have some time before Friday I&#039;ll see if I can work on the rest.

Also, you might want to fix the examples in the exercise, since they contain some errors (e.g. in the first example: 1x3 + 3x4 + 1x0 + 0x0 = 15, not 19). The versions used in the solution code are correct.]]></description>
		<content:encoded><![CDATA[<p>Since I&#8217;m currently at a conference I don&#8217;t have as much time to devote to this as I&#8217;d like, but here&#8217;s my implementation of LU decomposition:</p>
<pre class="brush: css;">
lu :: Fractional a =&gt; [[a]] -&gt; ([[a]], [[a]])
lu = unzip . map unzip . elim where
    elim [] = []
    elim ~((r:rs):xs) = zip (1 : repeat 0) (r:rs) :
                        zipWith (:) (map (\(y:_) -&gt; (y / r, 0)) xs)
                                    (elim $ map sub xs) where
        sub ~(y:ys) = zipWith (-) ys $ map (y / r *) rs
</pre>
<p>If I have some time before Friday I&#8217;ll see if I can work on the rest.</p>
<p>Also, you might want to fix the examples in the exercise, since they contain some errors (e.g. in the first example: 1&#215;3 + 3&#215;4 + 1&#215;0 + 0&#215;0 = 15, not 19). The versions used in the solution code are correct.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

