summaryrefslogtreecommitdiff
path: root/lib
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 /lib
parent819b1d83ec3eabc62974b19cb09f2f30fc20a6a9 (diff)
new module Numeric.LinearAlgebra.Real
Diffstat (limited to 'lib')
-rw-r--r--lib/Numeric/LinearAlgebra/Real.hs130
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{- |
3Module : Numeric.LinearAlgebra.Real
4Copyright : (c) Alberto Ruiz 2012
5License : GPL
6
7Maintainer : Alberto Ruiz (aruiz at um dot es)
8Stability : provisional
9
10Additional functions for real arrays.
11
12-}
13-----------------------------------------------------------------------------
14
15module 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
28import Numeric.LinearAlgebra hiding ((<>), (<\>), linspace)
29import qualified Numeric.LinearAlgebra as LA
30import System.Random(randomIO)
31
32linspace :: Int -> (Double,Double) -> Vector Double
33linspace = LA.linspace
34
35
36infixl 7 <>
37-- | Matrix product ('multiply')
38(<>) :: Field t => Matrix t -> Matrix t -> Matrix t
39(<>) = multiply
40
41infixl 7 *>
42-- | matrix x vector
43(*>) :: Field t => Matrix t -> Vector t -> Vector t
44m *> v = flatten $ m <> (asColumn v)
45
46infixl 7 <*
47-- | vector x matrix
48(<*) :: Field t => Vector t -> Matrix t -> Vector t
49v <* 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
54infixl 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
59infixl 7 \>
60m \> v = flatten (m <\> reshape 1 v)
61
62-- | Pseudorandom matrix with uniform elements between 0 and 1.
63randm :: RandDist
64 -> Int -- ^ rows
65 -> Int -- ^ columns
66 -> IO (Matrix Double)
67randm 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.
72rand :: Int -> Int -> IO (Matrix Double)
73rand = randm Uniform
74
75-- | Pseudorandom matrix with normal elements
76randn :: Int -> Int -> IO (Matrix Double)
77randn = randm Gaussian
78
79-- | Real identity matrix.
80eye :: Int -> Matrix Double
81eye = ident
82
83-- | Create a real vector from a list.
84vector :: [Double] -> Vector Double
85vector = fromList
86
87-- | Create a real diagonal matrix from a list.
88diagl :: [Double] -> Matrix Double
89diagl = diag . vector
90
91-- | Create a matrix or zeros.
92zeros :: Int -- ^ rows
93 -> Int -- ^ columns
94 -> Matrix Double
95zeros r c = konst 0 (r,c)
96
97-- | Create a matrix or ones.
98ones :: Int -- ^ rows
99 -> Int -- ^ columns
100 -> Matrix Double
101ones r c = konst 1 (r,c)
102
103-- | Concatenation of real vectors.
104infixl 9 #
105(#) :: Vector Double -> Vector Double -> Vector Double
106a # b = join [a,b]
107
108-- | Horizontal concatenation of real matrices.
109infixl 8 &
110(&) :: Matrix Double -> Matrix Double -> Matrix Double
111a & b = fromBlocks [[a,b]]
112
113-- | Vertical concatenation of real matrices.
114(//) :: Matrix Double -> Matrix Double -> Matrix Double
115infixl 7 //
116a // b = fromBlocks [[a],[b]]
117
118
119-- | Real block matrix from a rectangular list of lists.
120blocks :: [[Matrix Double]] -> Matrix Double
121blocks = fromBlocks
122
123-- | A real matrix with a single row, created from a list of elements.
124row :: [Double] -> Matrix Double
125row = asRow . vector
126
127-- | A real matrix with a single column, created from a list of elements.
128col :: [Double] -> Matrix Double
129col = asColumn . vector
130