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