diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-02-04 09:14:12 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-02-04 09:14:12 +0000 |
commit | ce4a3244139bc645bda4b563e6b2be7c7304e911 (patch) | |
tree | 2fcb7fec7962615ddbe1f0667f0b05f2e4b4d4aa | |
parent | 3f5bf5985d3da0e4d01cd9c126cb781cb6fc28ef (diff) |
added examples/HMatrixReal.hs
-rw-r--r-- | examples/HMatrixReal.hs | 104 | ||||
-rw-r--r-- | hmatrix.cabal | 4 |
2 files changed, 107 insertions, 1 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 | |||
4 | module 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 | |||
17 | import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/)) | ||
18 | import System.Random(randomIO) | ||
19 | |||
20 | infixl 7 <> | ||
21 | -- | Matrix product ('multiply') | ||
22 | (<>) :: Field t => Matrix t -> Matrix t -> Matrix t | ||
23 | (<>) = multiply | ||
24 | |||
25 | infixl 7 *> | ||
26 | -- | matrix x vector | ||
27 | (*>) :: Field t => Matrix t -> Vector t -> Vector t | ||
28 | m *> v = flatten $ m <> (asColumn v) | ||
29 | |||
30 | infixl 7 <* | ||
31 | -- | vector x matrix | ||
32 | (<*) :: Field t => Vector t -> Matrix t -> Vector t | ||
33 | v <* 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 | ||
38 | infixl 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 | ||
43 | infixl 7 \> | ||
44 | m \> v = flatten (m <\> reshape 1 v) | ||
45 | |||
46 | -- | Pseudorandom matrix with uniform elements between 0 and 1. | ||
47 | rand :: Int -- ^ rows | ||
48 | -> Int -- ^ columns | ||
49 | -> IO (Matrix Double) | ||
50 | rand r c = do | ||
51 | seed <- randomIO | ||
52 | return (reshape c $ randomVector seed Uniform (r*c)) | ||
53 | |||
54 | -- | Real identity matrix. | ||
55 | eye :: Int -> Matrix Double | ||
56 | eye = ident | ||
57 | |||
58 | -- | Create a real vector from a list. | ||
59 | vector :: [Double] -> Vector Double | ||
60 | vector = fromList | ||
61 | |||
62 | -- | Create a real diagonal matrix from a list. | ||
63 | diagl :: [Double] -> Matrix Double | ||
64 | diagl = diag . vector | ||
65 | |||
66 | -- | Create a matrix or zeros. | ||
67 | zeros :: Int -- ^ rows | ||
68 | -> Int -- ^ columns | ||
69 | -> Matrix Double | ||
70 | zeros r c = reshape c (constant 0 (r*c)) | ||
71 | |||
72 | -- | Create a matrix or ones. | ||
73 | ones :: Int -- ^ rows | ||
74 | -> Int -- ^ columns | ||
75 | -> Matrix Double | ||
76 | ones r c = reshape c (constant 1 (r*c)) | ||
77 | |||
78 | -- | Concatenation of real vectors. | ||
79 | infixl 9 # | ||
80 | (#) :: Vector Double -> Vector Double -> Vector Double | ||
81 | a # b = join [a,b] | ||
82 | |||
83 | -- | Horizontal concatenation of real matrices. | ||
84 | infixl 8 & | ||
85 | (&) :: Matrix Double -> Matrix Double -> Matrix Double | ||
86 | a & b = fromBlocks [[a,b]] | ||
87 | |||
88 | -- | Vertical concatenation of real matrices. | ||
89 | infixl 7 // | ||
90 | (//) :: Matrix Double -> Matrix Double -> Matrix Double | ||
91 | a // b = fromBlocks [[a],[b]] | ||
92 | |||
93 | -- | Real block matrix from a rectangular list of lists. | ||
94 | blocks :: [[Matrix Double]] -> Matrix Double | ||
95 | blocks = fromBlocks | ||
96 | |||
97 | -- | A real matrix with a single row, create from a list of elements. | ||
98 | row :: [Double] -> Matrix Double | ||
99 | row = asRow . vector | ||
100 | |||
101 | -- | A real matrix with a single column, created from a list of elements. | ||
102 | col :: [Double] -> Matrix Double | ||
103 | col = asColumn . vector | ||
104 | |||
diff --git a/hmatrix.cabal b/hmatrix.cabal index 0fb772f..6f6c832 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -37,6 +37,7 @@ extra-source-files: examples/tests.hs | |||
37 | examples/error.hs | 37 | examples/error.hs |
38 | examples/devel/wrappers.hs | 38 | examples/devel/wrappers.hs |
39 | examples/devel/functions.c | 39 | examples/devel/functions.c |
40 | examples/HMatrixReal.hs | ||
40 | 41 | ||
41 | extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, | 42 | extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, |
42 | lib/Numeric/LinearAlgebra/LAPACK/clapack.h | 43 | lib/Numeric/LinearAlgebra/LAPACK/clapack.h |
@@ -69,7 +70,7 @@ library | |||
69 | Build-Depends: haskell98, | 70 | Build-Depends: haskell98, |
70 | QuickCheck, HUnit, | 71 | QuickCheck, HUnit, |
71 | storable-complex, | 72 | storable-complex, |
72 | process, random | 73 | process |
73 | 74 | ||
74 | Extensions: ForeignFunctionInterface, | 75 | Extensions: ForeignFunctionInterface, |
75 | CPP | 76 | CPP |
@@ -166,3 +167,4 @@ library | |||
166 | 167 | ||
167 | extra-libraries: | 168 | extra-libraries: |
168 | extra-lib-dirs: | 169 | extra-lib-dirs: |
170 | |||