summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--hmatrix.cabal2
-rw-r--r--lib/Numeric/LinearAlgebra/Real.hs117
-rw-r--r--lib/Numeric/LinearAlgebra/Util.hs98
3 files changed, 99 insertions, 118 deletions
diff --git a/hmatrix.cabal b/hmatrix.cabal
index a772746..7ffa01d 100644
--- a/hmatrix.cabal
+++ b/hmatrix.cabal
@@ -110,7 +110,7 @@ library
110 Numeric.LinearAlgebra, 110 Numeric.LinearAlgebra,
111 Numeric.LinearAlgebra.LAPACK, 111 Numeric.LinearAlgebra.LAPACK,
112 Numeric.LinearAlgebra.Algorithms, 112 Numeric.LinearAlgebra.Algorithms,
113 Numeric.LinearAlgebra.Real, 113 Numeric.LinearAlgebra.Util,
114 Graphics.Plot, 114 Graphics.Plot,
115 Data.Packed.ST, 115 Data.Packed.ST,
116 Data.Packed.Development 116 Data.Packed.Development
diff --git a/lib/Numeric/LinearAlgebra/Real.hs b/lib/Numeric/LinearAlgebra/Real.hs
deleted file mode 100644
index 8478ee7..0000000
--- a/lib/Numeric/LinearAlgebra/Real.hs
+++ /dev/null
@@ -1,117 +0,0 @@
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 module Numeric.LinearAlgebra
27) where
28
29import Numeric.LinearAlgebra hiding ((<>), (<\>), linspace)
30import qualified Numeric.LinearAlgebra as LA
31import System.Random(randomIO)
32
33linspace :: Int -> (Double,Double) -> Vector Double
34linspace = LA.linspace
35
36
37infixl 7 <>
38-- | Matrix product
39(<>) ::Mul a b c => a Double -> b Double -> c Double
40(<>) = (LA.<>)
41
42
43infixl 7 <\>
44-- | Least squares solution of a linear system
45(<\>) ::LSDiv b c => Matrix Double -> b Double -> c Double
46(<\>) = (LA.<\>)
47
48
49-- | Pseudorandom matrix with uniform elements between 0 and 1.
50randm :: RandDist
51 -> Int -- ^ rows
52 -> Int -- ^ columns
53 -> IO (Matrix Double)
54randm d r c = do
55 seed <- randomIO
56 return (reshape c $ randomVector seed d (r*c))
57
58-- | Pseudorandom matrix with uniform elements between 0 and 1.
59rand :: Int -> Int -> IO (Matrix Double)
60rand = randm Uniform
61
62-- | Pseudorandom matrix with normal elements
63randn :: Int -> Int -> IO (Matrix Double)
64randn = randm Gaussian
65
66-- | Real identity matrix.
67eye :: Int -> Matrix Double
68eye = ident
69
70-- | Create a real vector from a list.
71vector :: [Double] -> Vector Double
72vector = fromList
73
74-- | Create a real diagonal matrix from a list.
75diagl :: [Double] -> Matrix Double
76diagl = diag . vector
77
78-- | Create a matrix or zeros.
79zeros :: Int -- ^ rows
80 -> Int -- ^ columns
81 -> Matrix Double
82zeros r c = konst 0 (r,c)
83
84-- | Create a matrix or ones.
85ones :: Int -- ^ rows
86 -> Int -- ^ columns
87 -> Matrix Double
88ones r c = konst 1 (r,c)
89
90-- | Concatenation of real vectors.
91infixl 3 #
92(#) :: Vector Double -> Vector Double -> Vector Double
93a # b = join [a,b]
94
95-- | Horizontal concatenation of real matrices.
96infixl 3 !
97(!) :: Matrix Double -> Matrix Double -> Matrix Double
98a ! b = fromBlocks [[a,b]]
99
100-- | Vertical concatenation of real matrices.
101(#) :: Matrix Double -> Matrix Double -> Matrix Double
102infixl 2 #
103a # b = fromBlocks [[a],[b]]
104
105
106-- | Real block matrix from a rectangular list of lists.
107blocks :: [[Matrix Double]] -> Matrix Double
108blocks = fromBlocks
109
110-- | A real matrix with a single row, created from a list of elements.
111row :: [Double] -> Matrix Double
112row = asRow . vector
113
114-- | A real matrix with a single column, created from a list of elements.
115col :: [Double] -> Matrix Double
116col = asColumn . vector
117
diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs
new file mode 100644
index 0000000..416b23f
--- /dev/null
+++ b/lib/Numeric/LinearAlgebra/Util.hs
@@ -0,0 +1,98 @@
1-----------------------------------------------------------------------------
2{- |
3Module : Numeric.LinearAlgebra.Util
4Copyright : (c) Alberto Ruiz 2012
5License : GPL
6
7Maintainer : Alberto Ruiz (aruiz at um dot es)
8Stability : provisional
9
10-}
11-----------------------------------------------------------------------------
12
13module Numeric.LinearAlgebra.Util(
14 disp,
15 zeros, ones,
16 diagl,
17 row,
18 col,
19 (&),(!), (#),
20 rand, randn,
21 cross
22) where
23
24import Numeric.LinearAlgebra
25import System.Random(randomIO)
26
27
28disp :: Int -> Matrix Double -> IO ()
29-- ^ show a matrix with given number of digits after the decimal point
30disp n = putStrLn . dispf n
31
32-- | pseudorandom matrix with uniform elements between 0 and 1
33randm :: RandDist
34 -> Int -- ^ rows
35 -> Int -- ^ columns
36 -> IO (Matrix Double)
37randm d r c = do
38 seed <- randomIO
39 return (reshape c $ randomVector seed d (r*c))
40
41-- | pseudorandom matrix with uniform elements between 0 and 1
42rand :: Int -> Int -> IO (Matrix Double)
43rand = randm Uniform
44
45-- | pseudorandom matrix with normal elements
46randn :: Int -> Int -> IO (Matrix Double)
47randn = randm Gaussian
48
49-- | create a real diagonal matrix from a list
50diagl :: [Double] -> Matrix Double
51diagl = diag . fromList
52
53-- | a real matrix of zeros
54zeros :: Int -- ^ rows
55 -> Int -- ^ columns
56 -> Matrix Double
57zeros r c = konst 0 (r,c)
58
59-- | a real matrix of ones
60ones :: Int -- ^ rows
61 -> Int -- ^ columns
62 -> Matrix Double
63ones r c = konst 1 (r,c)
64
65-- | concatenation of real vectors
66infixl 3 &
67(&) :: Vector Double -> Vector Double -> Vector Double
68a & b = join [a,b]
69
70-- | horizontal concatenation of real matrices
71infixl 3 !
72(!) :: Matrix Double -> Matrix Double -> Matrix Double
73a ! b = fromBlocks [[a,b]]
74
75-- | vertical concatenation of real matrices
76(#) :: Matrix Double -> Matrix Double -> Matrix Double
77infixl 2 #
78a # b = fromBlocks [[a],[b]]
79
80-- | create a single row real matrix from a list
81row :: [Double] -> Matrix Double
82row = asRow . fromList
83
84-- | create a single column real matrix from a list
85col :: [Double] -> Matrix Double
86col = asColumn . fromList
87
88cross :: Vector Double -> Vector Double -> Vector Double
89-- ^ cross product of dimension 3 real vectors
90cross x y | dim x == 3 && dim y == 3 = fromList [z1,z2,z3]
91 | otherwise = error $ "cross ("++show x++") ("++show y++")"
92 where
93 [x1,x2,x3] = toList x
94 [y1,y2,y3] = toList y
95 z1 = x2*y3-x3*y2
96 z2 = x3*y1-x1*y3
97 z3 = x1*y2-x2*y1
98