summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-02-02 13:31:51 +0000
committerAlberto Ruiz <aruiz@um.es>2010-02-02 13:31:51 +0000
commit25592aea904c4f29ea1c06210adb52dae99ba95c (patch)
treef1d79a56cbc16fa17b3567cf146a120f91ce63d5
parent7e736bc767942e38f4c0cf4a4d8a324b17c0316d (diff)
added Numeric.LinearAlgebra.Util
-rw-r--r--CHANGES4
-rw-r--r--hmatrix.cabal7
-rw-r--r--lib/Numeric/LinearAlgebra/Util.hs132
3 files changed, 139 insertions, 4 deletions
diff --git a/CHANGES b/CHANGES
index 2c50d37..3d7ad38 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,7 +1,9 @@
10.8.3.0 10.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
60.8.2.0 80.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{- |
3Module : Numeric.LinearAlgebra.Util
4Copyright : (c) Alberto Ruiz 2010
5License : GPL
6
7Maintainer : Alberto Ruiz (aruiz at um dot es)
8Stability : provisional
9Portability : portable
10
11Alternative interface and utilities for creation of real arrays, useful to work in interactive mode.
12
13-}
14-----------------------------------------------------------------------------
15
16module 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
32import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/))
33import Data.Packed.Internal.Common(table,splitEvery)
34import System.Random(randomIO)
35
36
37infixl 7 <>
38-- | Matrix product ('multiply')
39(<>) :: Field t => Matrix t -> Matrix t -> Matrix t
40(<>) = multiply
41
42infixl 7 *>
43-- | matrix x vector
44(*>) :: Field t => Matrix t -> Vector t -> Vector t
45m *> v = flatten $ m <> (asColumn v)
46
47infixl 7 <*
48-- | vector x matrix
49(<*) :: Field t => Vector t -> Matrix t -> Vector t
50v <* 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
55infixl 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
60infixl 7 \>
61m \> v = flatten (m <\> reshape 1 v)
62
63-- | Pseudorandom matrix with uniform elements between 0 and 1.
64rand :: Int -- ^ rows
65 -> Int -- ^ columns
66 -> IO (Matrix Double)
67rand r c = do
68 seed <- randomIO
69 return (reshape c $ randomVector seed Uniform (r*c))
70
71-- | Real identity matrix.
72eye :: Int -> Matrix Double
73eye = ident
74
75-- | Create a real vector from a list.
76vector :: [Double] -> Vector Double
77vector = fromList
78
79-- | Create a real diagonal matrix from a list.
80diagl :: [Double] -> Matrix Double
81diagl = diag . vector
82
83-- | Create a matrix or zeros.
84zeros :: Int -- ^ rows
85 -> Int -- ^ columns
86 -> Matrix Double
87zeros r c = reshape c (constant 0 (r*c))
88
89-- | Create a matrix or ones.
90ones :: Int -- ^ rows
91 -> Int -- ^ columns
92 -> Matrix Double
93ones r c = reshape c (constant 1 (r*c))
94
95
96-- | Concatenation of real vectors.
97infixl 9 #
98(#) :: Vector Double -> Vector Double -> Vector Double
99a # b = join [a,b]
100
101
102-- | Horizontal concatenation of real matrices.
103infixl 8 &
104(&) :: Matrix Double -> Matrix Double -> Matrix Double
105a & b = fromBlocks [[a,b]]
106
107-- | Vertical concatenation of real matrices.
108infixl 7 //
109(//) :: Matrix Double -> Matrix Double -> Matrix Double
110a // b = fromBlocks [[a],[b]]
111
112-- | Real block matrix from a rectangular list of lists.
113blocks :: [[Matrix Double]] -> Matrix Double
114blocks = fromBlocks
115
116-- | A real matrix with a single row, create from a list of elements.
117row :: [Double] -> Matrix Double
118row = asRow . vector
119
120-- | A real matrix with a single column, created from a list of elements.
121col :: [Double] -> Matrix Double
122col = asColumn . vector
123
124-- | Tool to display matrices with latex syntax.
125latexFormat :: 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
130latexFormat 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