Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,29 @@
module Cholesky (Arr, cholesky) where
import Data.Array.IArray
import Data.Array.MArray
import Data.Array.Unboxed
import Data.Array.ST
type Idx = (Int,Int)
type Arr = UArray Idx Double
-- Return the (i,j) element of the lower triangular matrix. (We assume the
-- lower array bound is (0,0).)
get :: Arr -> Arr -> Idx -> Double
get a l (i,j) | i == j = sqrt $ a!(j,j) - dot
| i > j = (a!(i,j) - dot) / l!(j,j)
| otherwise = 0
where dot = sum [l!(i,k) * l!(j,k) | k <- [0..j-1]]
-- Return the lower triangular matrix of a Cholesky decomposition. We assume
-- the input is a real, symmetric, positive-definite matrix, with lower array
-- bounds of (0,0).
cholesky :: Arr -> Arr
cholesky a = let n = maxBnd a
in runSTUArray $ do
l <- thaw a
mapM_ (update a l) [(i,j) | i <- [0..n], j <- [0..n]]
return l
where maxBnd = fst . snd . bounds
update a l i = unsafeFreeze l >>= \l' -> writeArray l i (get a l' i)

View file

@ -0,0 +1,30 @@
import Data.Array.IArray
import Data.List
import Cholesky
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 (splitAt row ys)
showMatrice row xs = ys where
vs = map (\s -> let sh = show s in (sh,length sh)) xs
width = (maximum $ snd $ unzip vs) + 1
ys = fmt width row (splitAt row vs)
ex1, ex2 :: Arr
ex1 = listArray ((0,0),(2,2)) [25, 15, -5,
15, 18, 0,
-5, 0, 11]
ex2 = listArray ((0,0),(3,3)) [18, 22, 54, 42,
22, 70, 86, 62,
54, 86, 174, 134,
42, 62, 134, 106]
main :: IO ()
main = do
putStrLn $ showMatrice 3 $ elems $ cholesky ex1
putStrLn $ showMatrice 4 $ elems $ cholesky ex2

View file

@ -0,0 +1,22 @@
import Numeric.LinearAlgebra
a,b :: Matrix R
a = (3><3)
[25, 15, -5
,15, 18, 0
,-5, 0, 11]
b = (4><4)
[ 18, 22, 54, 42
, 22, 70, 86, 62
, 54, 86,174,134
, 42, 62,134,106]
main = do
let sa = sym a
sb = sym b
print sa
print $ chol sa
print sb
print $ chol sb
print $ tr $ chol sb