summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Util.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Util.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Util.hs98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs
new file mode 100644
index 0000000..416b23f
--- /dev/null
+++ b/lib/Numeric/LinearAlgebra/Util.hs
@@ -0,0 +1,98 @@
1-----------------------------------------------------------------------------
2{- |
3Module : Numeric.LinearAlgebra.Util
4Copyright : (c) Alberto Ruiz 2012
5License : GPL
6
7Maintainer : Alberto Ruiz (aruiz at um dot es)
8Stability : provisional
9
10-}
11-----------------------------------------------------------------------------
12
13module Numeric.LinearAlgebra.Util(
14 disp,
15 zeros, ones,
16 diagl,
17 row,
18 col,
19 (&),(!), (#),
20 rand, randn,
21 cross
22) where
23
24import Numeric.LinearAlgebra
25import System.Random(randomIO)
26
27
28disp :: Int -> Matrix Double -> IO ()
29-- ^ show a matrix with given number of digits after the decimal point
30disp n = putStrLn . dispf n
31
32-- | pseudorandom matrix with uniform elements between 0 and 1
33randm :: RandDist
34 -> Int -- ^ rows
35 -> Int -- ^ columns
36 -> IO (Matrix Double)
37randm d r c = do
38 seed <- randomIO
39 return (reshape c $ randomVector seed d (r*c))
40
41-- | pseudorandom matrix with uniform elements between 0 and 1
42rand :: Int -> Int -> IO (Matrix Double)
43rand = randm Uniform
44
45-- | pseudorandom matrix with normal elements
46randn :: Int -> Int -> IO (Matrix Double)
47randn = randm Gaussian
48
49-- | create a real diagonal matrix from a list
50diagl :: [Double] -> Matrix Double
51diagl = diag . fromList
52
53-- | a real matrix of zeros
54zeros :: Int -- ^ rows
55 -> Int -- ^ columns
56 -> Matrix Double
57zeros r c = konst 0 (r,c)
58
59-- | a real matrix of ones
60ones :: Int -- ^ rows
61 -> Int -- ^ columns
62 -> Matrix Double
63ones r c = konst 1 (r,c)
64
65-- | concatenation of real vectors
66infixl 3 &
67(&) :: Vector Double -> Vector Double -> Vector Double
68a & b = join [a,b]
69
70-- | horizontal concatenation of real matrices
71infixl 3 !
72(!) :: Matrix Double -> Matrix Double -> Matrix Double
73a ! b = fromBlocks [[a,b]]
74
75-- | vertical concatenation of real matrices
76(#) :: Matrix Double -> Matrix Double -> Matrix Double
77infixl 2 #
78a # b = fromBlocks [[a],[b]]
79
80-- | create a single row real matrix from a list
81row :: [Double] -> Matrix Double
82row = asRow . fromList
83
84-- | create a single column real matrix from a list
85col :: [Double] -> Matrix Double
86col = asColumn . fromList
87
88cross :: Vector Double -> Vector Double -> Vector Double
89-- ^ cross product of dimension 3 real vectors
90cross x y | dim x == 3 && dim y == 3 = fromList [z1,z2,z3]
91 | otherwise = error $ "cross ("++show x++") ("++show y++")"
92 where
93 [x1,x2,x3] = toList x
94 [y1,y2,y3] = toList y
95 z1 = x2*y3-x3*y2
96 z2 = x3*y1-x1*y3
97 z3 = x1*y2-x2*y1
98