
Hi. I'm trying to write some code which involves lots of matrix multiplications, but whenever I do too many, I get stack overflows (in both GHCi 6.4.2, and Hugs May 2006). The following code exhibits the problem. import List (transpose) u <.> v = sum $ zipWith (*) u v a <<*>> b = multMx a (transpose b) where multMx [] _ = [] multMx (u:us) bT = map (u <.>) bT : multMx us bT id3 = [[1,0,0],[0,1,0],[0,0,1]] test = iterate (<<*>> id3) id3 !! 1000000 I tried to fix the problem using seq, as follows: iterate' f x = x : seq x' (iterate' f x') where x' = f x test' = iterate' (<<*>> id3) id3 !! 1000000 However, in both cases, the code causes stack overflows in both interpreters. (It sometimes kills GHCi, which I guess is a bug.) Any ideas? Thanks.

I'm trying to write some code which involves lots of matrix multiplications, but whenever I do too many, I get stack overflows (in both GHCi 6.4.2, and Hugs May 2006). The following code exhibits the problem. .. I tried to fix the problem using seq, as follows:
iterate' f x = x : seq x' (iterate' f x') where x' = f x
since you're working with lists, and nested at that, seq isn't going to buy much
(it'll evaluate the matrix to being non-empty, without forcing its rows, let alone
elements). you might find the recent thread on avoiding temporary arrays interesting:
http://www.haskell.org/pipermail/haskell-cafe/2007-March/023286.html
if you like to stay with binary lists, you might want to consider using strict lists
(no unevaluated heads or tails. i happen to have some strict list code lying around
from that earlier thread which addresses your problem:-)
hth,
claus
import List (transpose)
u <.> v = summulS u v
a <<*>> b = multMx a (transposeS b)
where
multMx Nil _ = Nil
multMx (u:
participants (2)
-
Claus Reinke
-
DavidA