summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2012-03-14 12:27:09 +0100
committerAlberto Ruiz <aruiz@um.es>2012-03-14 12:27:09 +0100
commit69b0f9c46f09dc9fc5c25e706efb8a7da51d2480 (patch)
tree84bfa82d634605e7ce6f2c297257301ebb51d474 /examples
parent819b1d83ec3eabc62974b19cb09f2f30fc20a6a9 (diff)
new module Numeric.LinearAlgebra.Real
Diffstat (limited to 'examples')
-rw-r--r--examples/Real.hs113
1 files changed, 0 insertions, 113 deletions
diff --git a/examples/Real.hs b/examples/Real.hs
deleted file mode 100644
index 9083b87..0000000
--- a/examples/Real.hs
+++ /dev/null
@@ -1,113 +0,0 @@
1
2-- Alternative interface and utilities for creation of real arrays, useful to work in interactive mode.
3
4module Real(
5 module Numeric.LinearAlgebra,
6 (<>), (*>), (<*), (<\>), (\>),
7 vector,
8 eye,
9 zeros, ones,
10 diagl,
11 row,
12 col,
13 (#),(&), (//), blocks,
14 rand, randn
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.
47randm :: RandDist
48 -> Int -- ^ rows
49 -> Int -- ^ columns
50 -> IO (Matrix Double)
51randm d r c = do
52 seed <- randomIO
53 return (reshape c $ randomVector seed d (r*c))
54
55-- | Pseudorandom matrix with uniform elements between 0 and 1.
56rand :: Int -> Int -> IO (Matrix Double)
57rand = randm Uniform
58
59-- | Pseudorandom matrix with normal elements
60randn :: Int -> Int -> IO (Matrix Double)
61randn = randm Gaussian
62
63-- | Real identity matrix.
64eye :: Int -> Matrix Double
65eye = ident
66
67-- | Create a real vector from a list.
68vector :: [Double] -> Vector Double
69vector = fromList
70
71-- | Create a real diagonal matrix from a list.
72diagl :: [Double] -> Matrix Double
73diagl = diag . vector
74
75-- | Create a matrix or zeros.
76zeros :: Int -- ^ rows
77 -> Int -- ^ columns
78 -> Matrix Double
79zeros r c = konst 0 (r,c)
80
81-- | Create a matrix or ones.
82ones :: Int -- ^ rows
83 -> Int -- ^ columns
84 -> Matrix Double
85ones r c = konst 1 (r,c)
86
87-- | Concatenation of real vectors.
88infixl 9 #
89(#) :: Vector Double -> Vector Double -> Vector Double
90a # b = join [a,b]
91
92-- | Horizontal concatenation of real matrices.
93infixl 8 &
94(&) :: Matrix Double -> Matrix Double -> Matrix Double
95a & b = fromBlocks [[a,b]]
96
97-- | Vertical concatenation of real matrices.
98infixl 7 //
99(//) :: Matrix Double -> Matrix Double -> Matrix Double
100a // b = fromBlocks [[a],[b]]
101
102-- | Real block matrix from a rectangular list of lists.
103blocks :: [[Matrix Double]] -> Matrix Double
104blocks = fromBlocks
105
106-- | A real matrix with a single row, create from a list of elements.
107row :: [Double] -> Matrix Double
108row = asRow . vector
109
110-- | A real matrix with a single column, created from a list of elements.
111col :: [Double] -> Matrix Double
112col = asColumn . vector
113