diff options
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | hmatrix.cabal | 7 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Util.hs | 132 |
3 files changed, 139 insertions, 4 deletions
@@ -1,7 +1,9 @@ | |||
1 | 0.8.3.0 | 1 | 0.8.3.0 |
2 | ======= | 2 | ======= |
3 | 3 | ||
4 | - Matrix arithmetic automatically replicates single row/column matrices | 4 | - New module Numeric.LinearAlgebra.Util |
5 | |||
6 | - Matrix arithmetic automatically replicates single row/column matrices. | ||
5 | 7 | ||
6 | 0.8.2.0 | 8 | 0.8.2.0 |
7 | ======= | 9 | ======= |
diff --git a/hmatrix.cabal b/hmatrix.cabal index 4749640..49d7b05 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -71,7 +71,7 @@ library | |||
71 | Build-Depends: haskell98, | 71 | Build-Depends: haskell98, |
72 | QuickCheck, HUnit, | 72 | QuickCheck, HUnit, |
73 | storable-complex, | 73 | storable-complex, |
74 | process | 74 | process, random |
75 | 75 | ||
76 | Extensions: ForeignFunctionInterface, | 76 | Extensions: ForeignFunctionInterface, |
77 | CPP | 77 | CPP |
@@ -127,8 +127,9 @@ library | |||
127 | Numeric.LinearAlgebra.Tests, | 127 | Numeric.LinearAlgebra.Tests, |
128 | Data.Packed.Convert, | 128 | Data.Packed.Convert, |
129 | Data.Packed.ST, | 129 | Data.Packed.ST, |
130 | Data.Packed.Development | 130 | Data.Packed.Development, |
131 | Data.Packed.Random | 131 | Data.Packed.Random, |
132 | Numeric.LinearAlgebra.Util | ||
132 | other-modules: Data.Packed.Internal, | 133 | other-modules: Data.Packed.Internal, |
133 | Data.Packed.Internal.Common, | 134 | Data.Packed.Internal.Common, |
134 | Data.Packed.Internal.Signatures, | 135 | Data.Packed.Internal.Signatures, |
diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs new file mode 100644 index 0000000..92b66e3 --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Util.hs | |||
@@ -0,0 +1,132 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | {- | | ||
3 | Module : Numeric.LinearAlgebra.Util | ||
4 | Copyright : (c) Alberto Ruiz 2010 | ||
5 | License : GPL | ||
6 | |||
7 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
8 | Stability : provisional | ||
9 | Portability : portable | ||
10 | |||
11 | Alternative interface and utilities for creation of real arrays, useful to work in interactive mode. | ||
12 | |||
13 | -} | ||
14 | ----------------------------------------------------------------------------- | ||
15 | |||
16 | module Numeric.LinearAlgebra.Util( | ||
17 | module Numeric.LinearAlgebra, | ||
18 | (<>), (*>), (<*), (<\>), (\>), | ||
19 | vector, | ||
20 | eye, | ||
21 | zeros, ones, | ||
22 | diagl, | ||
23 | row, | ||
24 | col, | ||
25 | (#),(&), (//), blocks, | ||
26 | rand, | ||
27 | splitEvery, | ||
28 | table, | ||
29 | latexFormat | ||
30 | ) where | ||
31 | |||
32 | import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/)) | ||
33 | import Data.Packed.Internal.Common(table,splitEvery) | ||
34 | import System.Random(randomIO) | ||
35 | |||
36 | |||
37 | infixl 7 <> | ||
38 | -- | Matrix product ('multiply') | ||
39 | (<>) :: Field t => Matrix t -> Matrix t -> Matrix t | ||
40 | (<>) = multiply | ||
41 | |||
42 | infixl 7 *> | ||
43 | -- | matrix x vector | ||
44 | (*>) :: Field t => Matrix t -> Vector t -> Vector t | ||
45 | m *> v = flatten $ m <> (asColumn v) | ||
46 | |||
47 | infixl 7 <* | ||
48 | -- | vector x matrix | ||
49 | (<*) :: Field t => Vector t -> Matrix t -> Vector t | ||
50 | v <* m = flatten $ (asRow v) <> m | ||
51 | |||
52 | |||
53 | -- | Least squares solution of a linear system for several right-hand sides, similar to the \\ operator of Matlab\/Octave. (\<\\\>) = 'linearSolveSVD'. | ||
54 | (<\>) :: (Field a) => Matrix a -> Matrix a -> Matrix a | ||
55 | infixl 7 <\> | ||
56 | (<\>) = linearSolveSVD | ||
57 | |||
58 | -- | Least squares solution of a linear system for a single right-hand side. See '(\<\\\>)'. | ||
59 | (\>) :: (Field a) => Matrix a -> Vector a -> Vector a | ||
60 | infixl 7 \> | ||
61 | m \> v = flatten (m <\> reshape 1 v) | ||
62 | |||
63 | -- | Pseudorandom matrix with uniform elements between 0 and 1. | ||
64 | rand :: Int -- ^ rows | ||
65 | -> Int -- ^ columns | ||
66 | -> IO (Matrix Double) | ||
67 | rand r c = do | ||
68 | seed <- randomIO | ||
69 | return (reshape c $ randomVector seed Uniform (r*c)) | ||
70 | |||
71 | -- | Real identity matrix. | ||
72 | eye :: Int -> Matrix Double | ||
73 | eye = ident | ||
74 | |||
75 | -- | Create a real vector from a list. | ||
76 | vector :: [Double] -> Vector Double | ||
77 | vector = fromList | ||
78 | |||
79 | -- | Create a real diagonal matrix from a list. | ||
80 | diagl :: [Double] -> Matrix Double | ||
81 | diagl = diag . vector | ||
82 | |||
83 | -- | Create a matrix or zeros. | ||
84 | zeros :: Int -- ^ rows | ||
85 | -> Int -- ^ columns | ||
86 | -> Matrix Double | ||
87 | zeros r c = reshape c (constant 0 (r*c)) | ||
88 | |||
89 | -- | Create a matrix or ones. | ||
90 | ones :: Int -- ^ rows | ||
91 | -> Int -- ^ columns | ||
92 | -> Matrix Double | ||
93 | ones r c = reshape c (constant 1 (r*c)) | ||
94 | |||
95 | |||
96 | -- | Concatenation of real vectors. | ||
97 | infixl 9 # | ||
98 | (#) :: Vector Double -> Vector Double -> Vector Double | ||
99 | a # b = join [a,b] | ||
100 | |||
101 | |||
102 | -- | Horizontal concatenation of real matrices. | ||
103 | infixl 8 & | ||
104 | (&) :: Matrix Double -> Matrix Double -> Matrix Double | ||
105 | a & b = fromBlocks [[a,b]] | ||
106 | |||
107 | -- | Vertical concatenation of real matrices. | ||
108 | infixl 7 // | ||
109 | (//) :: Matrix Double -> Matrix Double -> Matrix Double | ||
110 | a // b = fromBlocks [[a],[b]] | ||
111 | |||
112 | -- | Real block matrix from a rectangular list of lists. | ||
113 | blocks :: [[Matrix Double]] -> Matrix Double | ||
114 | blocks = fromBlocks | ||
115 | |||
116 | -- | A real matrix with a single row, create from a list of elements. | ||
117 | row :: [Double] -> Matrix Double | ||
118 | row = asRow . vector | ||
119 | |||
120 | -- | A real matrix with a single column, created from a list of elements. | ||
121 | col :: [Double] -> Matrix Double | ||
122 | col = asColumn . vector | ||
123 | |||
124 | -- | Tool to display matrices with latex syntax. | ||
125 | latexFormat :: Element t | ||
126 | => String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc. | ||
127 | -> (t->String) -- ^ formatting function for the elements: (printf \"%.2f\"), etc. | ||
128 | -> Matrix t -- ^ input matrix | ||
129 | -> String | ||
130 | latexFormat t shf m = "\\begin{"++t++"}\n" ++ dispg " & " " \\\\" shf m ++ "\\end{"++t++"}" | ||
131 | where dispg w l shfun x = unlines $ map (++l) $ lines $ format w shfun x | ||
132 | |||