summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-02-04 09:14:12 +0000
committerAlberto Ruiz <aruiz@um.es>2010-02-04 09:14:12 +0000
commitce4a3244139bc645bda4b563e6b2be7c7304e911 (patch)
tree2fcb7fec7962615ddbe1f0667f0b05f2e4b4d4aa /examples
parent3f5bf5985d3da0e4d01cd9c126cb781cb6fc28ef (diff)
added examples/HMatrixReal.hs
Diffstat (limited to 'examples')
-rw-r--r--examples/HMatrixReal.hs104
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/HMatrixReal.hs b/examples/HMatrixReal.hs
new file mode 100644
index 0000000..0edff10
--- /dev/null
+++ b/examples/HMatrixReal.hs
@@ -0,0 +1,104 @@
1
2-- Alternative interface and utilities for creation of real arrays, useful to work in interactive mode.
3
4module HMatrixReal(
5 module Numeric.LinearAlgebra,
6 (<>), (*>), (<*), (<\>), (\>),
7 vector,
8 eye,
9 zeros, ones,
10 diagl,
11 row,
12 col,
13 (#),(&), (//), blocks,
14 rand
15) where
16
17import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/))
18import System.Random(randomIO)
19
20infixl 7 <>
21-- | Matrix product ('multiply')
22(<>) :: Field t => Matrix t -> Matrix t -> Matrix t
23(<>) = multiply
24
25infixl 7 *>
26-- | matrix x vector
27(*>) :: Field t => Matrix t -> Vector t -> Vector t
28m *> v = flatten $ m <> (asColumn v)
29
30infixl 7 <*
31-- | vector x matrix
32(<*) :: Field t => Vector t -> Matrix t -> Vector t
33v <* m = flatten $ (asRow v) <> m
34
35
36-- | Least squares solution of a linear system for several right-hand sides, similar to the \\ operator of Matlab\/Octave. (\<\\\>) = 'linearSolveSVD'.
37(<\>) :: (Field a) => Matrix a -> Matrix a -> Matrix a
38infixl 7 <\>
39(<\>) = linearSolveSVD
40
41-- | Least squares solution of a linear system for a single right-hand side. See '(\<\\\>)'.
42(\>) :: (Field a) => Matrix a -> Vector a -> Vector a
43infixl 7 \>
44m \> v = flatten (m <\> reshape 1 v)
45
46-- | Pseudorandom matrix with uniform elements between 0 and 1.
47rand :: Int -- ^ rows
48 -> Int -- ^ columns
49 -> IO (Matrix Double)
50rand r c = do
51 seed <- randomIO
52 return (reshape c $ randomVector seed Uniform (r*c))
53
54-- | Real identity matrix.
55eye :: Int -> Matrix Double
56eye = ident
57
58-- | Create a real vector from a list.
59vector :: [Double] -> Vector Double
60vector = fromList
61
62-- | Create a real diagonal matrix from a list.
63diagl :: [Double] -> Matrix Double
64diagl = diag . vector
65
66-- | Create a matrix or zeros.
67zeros :: Int -- ^ rows
68 -> Int -- ^ columns
69 -> Matrix Double
70zeros r c = reshape c (constant 0 (r*c))
71
72-- | Create a matrix or ones.
73ones :: Int -- ^ rows
74 -> Int -- ^ columns
75 -> Matrix Double
76ones r c = reshape c (constant 1 (r*c))
77
78-- | Concatenation of real vectors.
79infixl 9 #
80(#) :: Vector Double -> Vector Double -> Vector Double
81a # b = join [a,b]
82
83-- | Horizontal concatenation of real matrices.
84infixl 8 &
85(&) :: Matrix Double -> Matrix Double -> Matrix Double
86a & b = fromBlocks [[a,b]]
87
88-- | Vertical concatenation of real matrices.
89infixl 7 //
90(//) :: Matrix Double -> Matrix Double -> Matrix Double
91a // b = fromBlocks [[a],[b]]
92
93-- | Real block matrix from a rectangular list of lists.
94blocks :: [[Matrix Double]] -> Matrix Double
95blocks = fromBlocks
96
97-- | A real matrix with a single row, create from a list of elements.
98row :: [Double] -> Matrix Double
99row = asRow . vector
100
101-- | A real matrix with a single column, created from a list of elements.
102col :: [Double] -> Matrix Double
103col = asColumn . vector
104