diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Util.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Util.hs | 98 |
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 | {- | | ||
3 | Module : Numeric.LinearAlgebra.Util | ||
4 | Copyright : (c) Alberto Ruiz 2012 | ||
5 | License : GPL | ||
6 | |||
7 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
8 | Stability : provisional | ||
9 | |||
10 | -} | ||
11 | ----------------------------------------------------------------------------- | ||
12 | |||
13 | module Numeric.LinearAlgebra.Util( | ||
14 | disp, | ||
15 | zeros, ones, | ||
16 | diagl, | ||
17 | row, | ||
18 | col, | ||
19 | (&),(!), (#), | ||
20 | rand, randn, | ||
21 | cross | ||
22 | ) where | ||
23 | |||
24 | import Numeric.LinearAlgebra | ||
25 | import System.Random(randomIO) | ||
26 | |||
27 | |||
28 | disp :: Int -> Matrix Double -> IO () | ||
29 | -- ^ show a matrix with given number of digits after the decimal point | ||
30 | disp n = putStrLn . dispf n | ||
31 | |||
32 | -- | pseudorandom matrix with uniform elements between 0 and 1 | ||
33 | randm :: RandDist | ||
34 | -> Int -- ^ rows | ||
35 | -> Int -- ^ columns | ||
36 | -> IO (Matrix Double) | ||
37 | randm 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 | ||
42 | rand :: Int -> Int -> IO (Matrix Double) | ||
43 | rand = randm Uniform | ||
44 | |||
45 | -- | pseudorandom matrix with normal elements | ||
46 | randn :: Int -> Int -> IO (Matrix Double) | ||
47 | randn = randm Gaussian | ||
48 | |||
49 | -- | create a real diagonal matrix from a list | ||
50 | diagl :: [Double] -> Matrix Double | ||
51 | diagl = diag . fromList | ||
52 | |||
53 | -- | a real matrix of zeros | ||
54 | zeros :: Int -- ^ rows | ||
55 | -> Int -- ^ columns | ||
56 | -> Matrix Double | ||
57 | zeros r c = konst 0 (r,c) | ||
58 | |||
59 | -- | a real matrix of ones | ||
60 | ones :: Int -- ^ rows | ||
61 | -> Int -- ^ columns | ||
62 | -> Matrix Double | ||
63 | ones r c = konst 1 (r,c) | ||
64 | |||
65 | -- | concatenation of real vectors | ||
66 | infixl 3 & | ||
67 | (&) :: Vector Double -> Vector Double -> Vector Double | ||
68 | a & b = join [a,b] | ||
69 | |||
70 | -- | horizontal concatenation of real matrices | ||
71 | infixl 3 ! | ||
72 | (!) :: Matrix Double -> Matrix Double -> Matrix Double | ||
73 | a ! b = fromBlocks [[a,b]] | ||
74 | |||
75 | -- | vertical concatenation of real matrices | ||
76 | (#) :: Matrix Double -> Matrix Double -> Matrix Double | ||
77 | infixl 2 # | ||
78 | a # b = fromBlocks [[a],[b]] | ||
79 | |||
80 | -- | create a single row real matrix from a list | ||
81 | row :: [Double] -> Matrix Double | ||
82 | row = asRow . fromList | ||
83 | |||
84 | -- | create a single column real matrix from a list | ||
85 | col :: [Double] -> Matrix Double | ||
86 | col = asColumn . fromList | ||
87 | |||
88 | cross :: Vector Double -> Vector Double -> Vector Double | ||
89 | -- ^ cross product of dimension 3 real vectors | ||
90 | cross 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 | |||