module Numeric.LinearAlgebra.Util(
disp,
zeros, ones,
diagl,
row,
col,
(&),(!), (#),
rand, randn,
cross,
norm
) where
import Numeric.LinearAlgebra
import System.Random(randomIO)
disp :: Int -> Matrix Double -> IO ()
disp n = putStrLn . dispf n
randm :: RandDist
-> Int
-> Int
-> IO (Matrix Double)
randm d r c = do
seed <- randomIO
return (reshape c $ randomVector seed d (r*c))
rand :: Int -> Int -> IO (Matrix Double)
rand = randm Uniform
randn :: Int -> Int -> IO (Matrix Double)
randn = randm Gaussian
diagl :: [Double] -> Matrix Double
diagl = diag . fromList
zeros :: Int
-> Int
-> Matrix Double
zeros r c = konst 0 (r,c)
ones :: Int
-> Int
-> Matrix Double
ones r c = konst 1 (r,c)
infixl 3 &
(&) :: Vector Double -> Vector Double -> Vector Double
a & b = join [a,b]
infixl 3 !
(!) :: Matrix Double -> Matrix Double -> Matrix Double
a ! b = fromBlocks [[a,b]]
(#) :: Matrix Double -> Matrix Double -> Matrix Double
infixl 2 #
a # b = fromBlocks [[a],[b]]
row :: [Double] -> Matrix Double
row = asRow . fromList
col :: [Double] -> Matrix Double
col = asColumn . fromList
cross :: Vector Double -> Vector Double -> Vector Double
cross x y | dim x == 3 && dim y == 3 = fromList [z1,z2,z3]
| otherwise = error $ "cross ("++show x++") ("++show y++")"
where
[x1,x2,x3] = toList x
[y1,y2,y3] = toList y
z1 = x2*y3x3*y2
z2 = x3*y1x1*y3
z3 = x1*y2x2*y1
norm :: Vector Double -> Double
norm = pnorm PNorm2