diff options
-rw-r--r-- | hmatrix.cabal | 2 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Real.hs | 117 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Util.hs | 98 |
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 | {- | | ||
3 | Module : Numeric.LinearAlgebra.Real | ||
4 | Copyright : (c) Alberto Ruiz 2012 | ||
5 | License : GPL | ||
6 | |||
7 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
8 | Stability : provisional | ||
9 | |||
10 | Additional functions for real arrays. | ||
11 | |||
12 | -} | ||
13 | ----------------------------------------------------------------------------- | ||
14 | |||
15 | module 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 | |||
29 | import Numeric.LinearAlgebra hiding ((<>), (<\>), linspace) | ||
30 | import qualified Numeric.LinearAlgebra as LA | ||
31 | import System.Random(randomIO) | ||
32 | |||
33 | linspace :: Int -> (Double,Double) -> Vector Double | ||
34 | linspace = LA.linspace | ||
35 | |||
36 | |||
37 | infixl 7 <> | ||
38 | -- | Matrix product | ||
39 | (<>) ::Mul a b c => a Double -> b Double -> c Double | ||
40 | (<>) = (LA.<>) | ||
41 | |||
42 | |||
43 | infixl 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. | ||
50 | randm :: RandDist | ||
51 | -> Int -- ^ rows | ||
52 | -> Int -- ^ columns | ||
53 | -> IO (Matrix Double) | ||
54 | randm 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. | ||
59 | rand :: Int -> Int -> IO (Matrix Double) | ||
60 | rand = randm Uniform | ||
61 | |||
62 | -- | Pseudorandom matrix with normal elements | ||
63 | randn :: Int -> Int -> IO (Matrix Double) | ||
64 | randn = randm Gaussian | ||
65 | |||
66 | -- | Real identity matrix. | ||
67 | eye :: Int -> Matrix Double | ||
68 | eye = ident | ||
69 | |||
70 | -- | Create a real vector from a list. | ||
71 | vector :: [Double] -> Vector Double | ||
72 | vector = fromList | ||
73 | |||
74 | -- | Create a real diagonal matrix from a list. | ||
75 | diagl :: [Double] -> Matrix Double | ||
76 | diagl = diag . vector | ||
77 | |||
78 | -- | Create a matrix or zeros. | ||
79 | zeros :: Int -- ^ rows | ||
80 | -> Int -- ^ columns | ||
81 | -> Matrix Double | ||
82 | zeros r c = konst 0 (r,c) | ||
83 | |||
84 | -- | Create a matrix or ones. | ||
85 | ones :: Int -- ^ rows | ||
86 | -> Int -- ^ columns | ||
87 | -> Matrix Double | ||
88 | ones r c = konst 1 (r,c) | ||
89 | |||
90 | -- | Concatenation of real vectors. | ||
91 | infixl 3 # | ||
92 | (#) :: Vector Double -> Vector Double -> Vector Double | ||
93 | a # b = join [a,b] | ||
94 | |||
95 | -- | Horizontal concatenation of real matrices. | ||
96 | infixl 3 ! | ||
97 | (!) :: Matrix Double -> Matrix Double -> Matrix Double | ||
98 | a ! b = fromBlocks [[a,b]] | ||
99 | |||
100 | -- | Vertical concatenation of real matrices. | ||
101 | (#) :: Matrix Double -> Matrix Double -> Matrix Double | ||
102 | infixl 2 # | ||
103 | a # b = fromBlocks [[a],[b]] | ||
104 | |||
105 | |||
106 | -- | Real block matrix from a rectangular list of lists. | ||
107 | blocks :: [[Matrix Double]] -> Matrix Double | ||
108 | blocks = fromBlocks | ||
109 | |||
110 | -- | A real matrix with a single row, created from a list of elements. | ||
111 | row :: [Double] -> Matrix Double | ||
112 | row = asRow . vector | ||
113 | |||
114 | -- | A real matrix with a single column, created from a list of elements. | ||
115 | col :: [Double] -> Matrix Double | ||
116 | col = 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 | {- | | ||
3 | Module : Numeric.LinearAlgebra.Util | ||
4 | Copyright : (c) Alberto Ruiz 2012 | ||
5 | License : GPL | ||
6 | |||
7 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
8 | Stability : provisional | ||
9 | |||
10 | -} | ||
11 | ----------------------------------------------------------------------------- | ||
12 | |||
13 | module Numeric.LinearAlgebra.Util( | ||
14 | disp, | ||
15 | zeros, ones, | ||
16 | diagl, | ||
17 | row, | ||
18 | col, | ||
19 | (&),(!), (#), | ||
20 | rand, randn, | ||
21 | cross | ||
22 | ) where | ||
23 | |||
24 | import Numeric.LinearAlgebra | ||
25 | import System.Random(randomIO) | ||
26 | |||
27 | |||
28 | disp :: Int -> Matrix Double -> IO () | ||
29 | -- ^ show a matrix with given number of digits after the decimal point | ||
30 | disp n = putStrLn . dispf n | ||
31 | |||
32 | -- | pseudorandom matrix with uniform elements between 0 and 1 | ||
33 | randm :: RandDist | ||
34 | -> Int -- ^ rows | ||
35 | -> Int -- ^ columns | ||
36 | -> IO (Matrix Double) | ||
37 | randm 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 | ||
42 | rand :: Int -> Int -> IO (Matrix Double) | ||
43 | rand = randm Uniform | ||
44 | |||
45 | -- | pseudorandom matrix with normal elements | ||
46 | randn :: Int -> Int -> IO (Matrix Double) | ||
47 | randn = randm Gaussian | ||
48 | |||
49 | -- | create a real diagonal matrix from a list | ||
50 | diagl :: [Double] -> Matrix Double | ||
51 | diagl = diag . fromList | ||
52 | |||
53 | -- | a real matrix of zeros | ||
54 | zeros :: Int -- ^ rows | ||
55 | -> Int -- ^ columns | ||
56 | -> Matrix Double | ||
57 | zeros r c = konst 0 (r,c) | ||
58 | |||
59 | -- | a real matrix of ones | ||
60 | ones :: Int -- ^ rows | ||
61 | -> Int -- ^ columns | ||
62 | -> Matrix Double | ||
63 | ones r c = konst 1 (r,c) | ||
64 | |||
65 | -- | concatenation of real vectors | ||
66 | infixl 3 & | ||
67 | (&) :: Vector Double -> Vector Double -> Vector Double | ||
68 | a & b = join [a,b] | ||
69 | |||
70 | -- | horizontal concatenation of real matrices | ||
71 | infixl 3 ! | ||
72 | (!) :: Matrix Double -> Matrix Double -> Matrix Double | ||
73 | a ! b = fromBlocks [[a,b]] | ||
74 | |||
75 | -- | vertical concatenation of real matrices | ||
76 | (#) :: Matrix Double -> Matrix Double -> Matrix Double | ||
77 | infixl 2 # | ||
78 | a # b = fromBlocks [[a],[b]] | ||
79 | |||
80 | -- | create a single row real matrix from a list | ||
81 | row :: [Double] -> Matrix Double | ||
82 | row = asRow . fromList | ||
83 | |||
84 | -- | create a single column real matrix from a list | ||
85 | col :: [Double] -> Matrix Double | ||
86 | col = asColumn . fromList | ||
87 | |||
88 | cross :: Vector Double -> Vector Double -> Vector Double | ||
89 | -- ^ cross product of dimension 3 real vectors | ||
90 | cross 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 | |||