<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7232.77">
<TITLE>Random matrices</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->

<P><FONT SIZE=2 FACE="Arial">I'm doing some statistical calculations, and I decided to try this out in Haskell to see how it goes. I'm really enjoying using the language, so I hope I can straighten this out so I can keep using it at work.</FONT></P>

<P><FONT SIZE=2 FACE="Arial">I keep getting stack overflows, so I think my code must be too lazy somewhere (that's what that means, right?) Here is my code to build random vectors and matrices.</FONT></P>

<P><FONT SIZE=2 FACE="Arial">--------------------------------------------------------------------------------</FONT>

<BR><FONT SIZE=2 FACE="Arial">type Vector = UArray Int Float</FONT>

<BR><FONT SIZE=2 FACE="Arial">type Matrix = DiffArray Int Vector</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">-- Generate a random number from the unit interval</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomEntry :: State StdGen Float</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomEntry = do g &lt;- get</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (x, g') &lt;- return $ randomR (0,1) g</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; put g'</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return x</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">-- Build an array of n random things</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomArray :: (IArray arr a) =&gt; Int -&gt; State StdGen a -&gt; State StdGen (arr Int a)</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomArray n x = mapState arr . sequence $ replicate n x</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; where</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; arr (a,s) = (array (1,n) $ zip [1..n] a, s)</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">-- A random vector is an array of random entries</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomVector :: Int -&gt; State StdGen Vector</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomVector n = randomArray n randomEntry</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">-- a random matrix is an array of random vectors.</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomMatrix :: Int -&gt; Int -&gt; State StdGen Matrix</FONT>

<BR><FONT SIZE=2 FACE="Arial">randomMatrix i j = randomArray i $ randomVector j</FONT>

<BR><FONT SIZE=2 FACE="Arial">--------------------------------------------------------------------------------</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">I decided to write a tester function to see whether building the random matrices is the root of my problem.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">tester :: Int -&gt; [Matrix]</FONT>

<BR><FONT SIZE=2 FACE="Arial">tester n = fst $ runState vals $ mkStdGen 1</FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; where </FONT>

<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; vals = sequence . repeat $ randomMatrix n n </FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Then if I do</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">main = print $ ((tester 10)!!10000)!5</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">with ghc -O, I get a stack overflow. So all main is trying to do is build an infinite list of 10x10 random matrices, extract the 10000th one, and then look at row 5.</FONT></P>

<P><FONT SIZE=2 FACE="Arial">Stack overflows like this have given me trouble before. I thought using unboxed arrays might help, since they're forced to be strict, but I haven't had any luck with them. Are there any general guidelines I'm overlooking? I think Haskell can be very useful in my work, but I have to get beyond this hurdle. I just figured out the State monad yesterday, so maybe I'm not using it as it's intended? I dunno.</FONT></P>

<P><FONT SIZE=2 FACE="Arial">Thanks,</FONT>

<BR><FONT SIZE=2 FACE="Arial">Chad Scherrer</FONT>
</P>

</BODY>
</HTML>