summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/LinearAlgebra/Util.hs132
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{- |
3Module : Numeric.LinearAlgebra.Util
4Copyright : (c) Alberto Ruiz 2010
5License : GPL
6
7Maintainer : Alberto Ruiz (aruiz at um dot es)
8Stability : provisional
9Portability : portable
10
11Alternative interface and utilities for creation of real arrays, useful to work in interactive mode.
12
13-}
14-----------------------------------------------------------------------------
15
16module 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
32import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/))
33import Data.Packed.Internal.Common(table,splitEvery)
34import System.Random(randomIO)
35
36
37infixl 7 <>
38-- | Matrix product ('multiply')
39(<>) :: Field t => Matrix t -> Matrix t -> Matrix t
40(<>) = multiply
41
42infixl 7 *>
43-- | matrix x vector
44(*>) :: Field t => Matrix t -> Vector t -> Vector t
45m *> v = flatten $ m <> (asColumn v)
46
47infixl 7 <*
48-- | vector x matrix
49(<*) :: Field t => Vector t -> Matrix t -> Vector t
50v <* 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
55infixl 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
60infixl 7 \>
61m \> v = flatten (m <\> reshape 1 v)
62
63-- | Pseudorandom matrix with uniform elements between 0 and 1.
64rand :: Int -- ^ rows
65 -> Int -- ^ columns
66 -> IO (Matrix Double)
67rand r c = do
68 seed <- randomIO
69 return (reshape c $ randomVector seed Uniform (r*c))
70
71-- | Real identity matrix.
72eye :: Int -> Matrix Double
73eye = ident
74
75-- | Create a real vector from a list.
76vector :: [Double] -> Vector Double
77vector = fromList
78
79-- | Create a real diagonal matrix from a list.
80diagl :: [Double] -> Matrix Double
81diagl = diag . vector
82
83-- | Create a matrix or zeros.
84zeros :: Int -- ^ rows
85 -> Int -- ^ columns
86 -> Matrix Double
87zeros r c = reshape c (constant 0 (r*c))
88
89-- | Create a matrix or ones.
90ones :: Int -- ^ rows
91 -> Int -- ^ columns
92 -> Matrix Double
93ones r c = reshape c (constant 1 (r*c))
94
95
96-- | Concatenation of real vectors.
97infixl 9 #
98(#) :: Vector Double -> Vector Double -> Vector Double
99a # b = join [a,b]
100
101
102-- | Horizontal concatenation of real matrices.
103infixl 8 &
104(&) :: Matrix Double -> Matrix Double -> Matrix Double
105a & b = fromBlocks [[a,b]]
106
107-- | Vertical concatenation of real matrices.
108infixl 7 //
109(//) :: Matrix Double -> Matrix Double -> Matrix Double
110a // b = fromBlocks [[a],[b]]
111
112-- | Real block matrix from a rectangular list of lists.
113blocks :: [[Matrix Double]] -> Matrix Double
114blocks = fromBlocks
115
116-- | A real matrix with a single row, create from a list of elements.
117row :: [Double] -> Matrix Double
118row = asRow . vector
119
120-- | A real matrix with a single column, created from a list of elements.
121col :: [Double] -> Matrix Double
122col = asColumn . vector
123
124-- | Tool to display matrices with latex syntax.
125latexFormat :: 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
130latexFormat 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