diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Real.hs | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/lib/Numeric/LinearAlgebra/Real.hs b/lib/Numeric/LinearAlgebra/Real.hs new file mode 100644 index 0000000..6cba045 --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Real.hs | |||
@@ -0,0 +1,130 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | {- | | ||
3 | Module : Numeric.LinearAlgebra.Real | ||
4 | Copyright : (c) Alberto Ruiz 2012 | ||
5 | License : GPL | ||
6 | |||
7 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
8 | Stability : provisional | ||
9 | |||
10 | Additional functions for real arrays. | ||
11 | |||
12 | -} | ||
13 | ----------------------------------------------------------------------------- | ||
14 | |||
15 | module Numeric.LinearAlgebra.Real( | ||
16 | (<>), (*>), (<*), (<\>), (\>), | ||
17 | vector, | ||
18 | linspace, | ||
19 | eye, | ||
20 | zeros, ones, | ||
21 | diagl, | ||
22 | row, | ||
23 | col, | ||
24 | (#),(&), (//), blocks, | ||
25 | rand, randn | ||
26 | ) where | ||
27 | |||
28 | import Numeric.LinearAlgebra hiding ((<>), (<\>), linspace) | ||
29 | import qualified Numeric.LinearAlgebra as LA | ||
30 | import System.Random(randomIO) | ||
31 | |||
32 | linspace :: Int -> (Double,Double) -> Vector Double | ||
33 | linspace = LA.linspace | ||
34 | |||
35 | |||
36 | infixl 7 <> | ||
37 | -- | Matrix product ('multiply') | ||
38 | (<>) :: Field t => Matrix t -> Matrix t -> Matrix t | ||
39 | (<>) = multiply | ||
40 | |||
41 | infixl 7 *> | ||
42 | -- | matrix x vector | ||
43 | (*>) :: Field t => Matrix t -> Vector t -> Vector t | ||
44 | m *> v = flatten $ m <> (asColumn v) | ||
45 | |||
46 | infixl 7 <* | ||
47 | -- | vector x matrix | ||
48 | (<*) :: Field t => Vector t -> Matrix t -> Vector t | ||
49 | v <* m = flatten $ (asRow v) <> m | ||
50 | |||
51 | |||
52 | -- | Least squares solution of a linear system for several right-hand sides, similar to the \\ operator of Matlab\/Octave. (\<\\\>) = 'linearSolveSVD'. | ||
53 | (<\>) :: (Field a) => Matrix a -> Matrix a -> Matrix a | ||
54 | infixl 7 <\> | ||
55 | (<\>) = linearSolveSVD | ||
56 | |||
57 | -- | Least squares solution of a linear system for a single right-hand side. See '(\<\\\>)'. | ||
58 | (\>) :: (Field a) => Matrix a -> Vector a -> Vector a | ||
59 | infixl 7 \> | ||
60 | m \> v = flatten (m <\> reshape 1 v) | ||
61 | |||
62 | -- | Pseudorandom matrix with uniform elements between 0 and 1. | ||
63 | randm :: RandDist | ||
64 | -> Int -- ^ rows | ||
65 | -> Int -- ^ columns | ||
66 | -> IO (Matrix Double) | ||
67 | randm d r c = do | ||
68 | seed <- randomIO | ||
69 | return (reshape c $ randomVector seed d (r*c)) | ||
70 | |||
71 | -- | Pseudorandom matrix with uniform elements between 0 and 1. | ||
72 | rand :: Int -> Int -> IO (Matrix Double) | ||
73 | rand = randm Uniform | ||
74 | |||
75 | -- | Pseudorandom matrix with normal elements | ||
76 | randn :: Int -> Int -> IO (Matrix Double) | ||
77 | randn = randm Gaussian | ||
78 | |||
79 | -- | Real identity matrix. | ||
80 | eye :: Int -> Matrix Double | ||
81 | eye = ident | ||
82 | |||
83 | -- | Create a real vector from a list. | ||
84 | vector :: [Double] -> Vector Double | ||
85 | vector = fromList | ||
86 | |||
87 | -- | Create a real diagonal matrix from a list. | ||
88 | diagl :: [Double] -> Matrix Double | ||
89 | diagl = diag . vector | ||
90 | |||
91 | -- | Create a matrix or zeros. | ||
92 | zeros :: Int -- ^ rows | ||
93 | -> Int -- ^ columns | ||
94 | -> Matrix Double | ||
95 | zeros r c = konst 0 (r,c) | ||
96 | |||
97 | -- | Create a matrix or ones. | ||
98 | ones :: Int -- ^ rows | ||
99 | -> Int -- ^ columns | ||
100 | -> Matrix Double | ||
101 | ones r c = konst 1 (r,c) | ||
102 | |||
103 | -- | Concatenation of real vectors. | ||
104 | infixl 9 # | ||
105 | (#) :: Vector Double -> Vector Double -> Vector Double | ||
106 | a # b = join [a,b] | ||
107 | |||
108 | -- | Horizontal concatenation of real matrices. | ||
109 | infixl 8 & | ||
110 | (&) :: Matrix Double -> Matrix Double -> Matrix Double | ||
111 | a & b = fromBlocks [[a,b]] | ||
112 | |||
113 | -- | Vertical concatenation of real matrices. | ||
114 | (//) :: Matrix Double -> Matrix Double -> Matrix Double | ||
115 | infixl 7 // | ||
116 | a // b = fromBlocks [[a],[b]] | ||
117 | |||
118 | |||
119 | -- | Real block matrix from a rectangular list of lists. | ||
120 | blocks :: [[Matrix Double]] -> Matrix Double | ||
121 | blocks = fromBlocks | ||
122 | |||
123 | -- | A real matrix with a single row, created from a list of elements. | ||
124 | row :: [Double] -> Matrix Double | ||
125 | row = asRow . vector | ||
126 | |||
127 | -- | A real matrix with a single column, created from a list of elements. | ||
128 | col :: [Double] -> Matrix Double | ||
129 | col = asColumn . vector | ||
130 | |||