Cholesky decomposition: Difference between revisions

m (Added the Sidef language)
Line 1,035:
The main module:
<lang haskell>import Data.Array.IArray
import Data.List
import Cholesky
 
takeDrop 0 xs = ([],xs)
takeDrop _ [] = ([],[])
takeDrop n (x:xs) = (x:a,b) where (a,b) = takeDrop (n-1) xs
 
fm _ [] = ""
fm _ [x] = fst x
fm width ((a,b):xs) = a ++ (take (width - b) $ cycle " ") ++ (fm width xs)
 
fmt width row (xs,[]) = fm width xs;
fmt width row (xs,ys) = fm width xs ++ "\n" ++ fmt width row (takeDrop row ys)
 
showMatrice row xs = ys where
vs = map (\s -> let sh = show s in (sh,length sh)) xs
width = (maximum $ snd $ unzip vs) + 2
ys = fmt width row (takeDrop row vs)
 
ex1, ex2 :: Arr
Line 1,049 ⟶ 1,066:
main :: IO ()
main = do
printputStrLn $ showMatrice 3 $ elems $ cholesky ex1
printputStrLn $ showMatrice 4 $ elems $ cholesky ex2</lang>
<b>output:</b>
The resulting matrices are printed as lists, as in the following output:
<pre>
[5.0, 0.0, 0.0,3.0,3.0,0.0,-1.0,1.0,3.0]
3.0 3.0 0.0
[4.242640687119285,0.0,0.0,0.0,5.185449728701349,6.565905201197403,0.0,0.0,12.727922061357857,3.0460384954008553,1.6497422479090704,0.0,9.899494936611665,1.6245538642137891,1.849711005231382,1.3926212476455924]</pre>
-1.0 1.0 3.0
 
4.242640687119285 0.0 0.0 0.0
5.185449728701349 6.565905201197403 0.0 0.0
12.727922061357857 3.0460384954008553 1.6497422479090704 0.0
9.899494936611665 1.6245538642137891 1.849711005231382 1.3926212476455924
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
678

edits