diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-05-21 09:57:03 +0200 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-05-21 09:57:03 +0200 |
commit | e07c3dee7235496b71a89233106d93f6cc94ada1 (patch) | |
tree | 1ad29c3fc93ee076ad68e3ee759c9a3357f9cd5b /packages/base/src/Numeric | |
parent | 92de588b82945bb251a056c34a8ef0c00cb00e5a (diff) |
Numeric.Container and Numeric.LinearAlgebra moved to base
Diffstat (limited to 'packages/base/src/Numeric')
-rw-r--r-- | packages/base/src/Numeric/Container.hs | 239 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra.hs (renamed from packages/base/src/Numeric/LinearAlgebra/Base.hs) | 4 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Data.hs | 2 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Devel.hs | 2 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Random.hs | 2 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Util.hs | 2 | ||||
-rw-r--r-- | packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs | 2 |
7 files changed, 246 insertions, 7 deletions
diff --git a/packages/base/src/Numeric/Container.hs b/packages/base/src/Numeric/Container.hs new file mode 100644 index 0000000..c715dac --- /dev/null +++ b/packages/base/src/Numeric/Container.hs | |||
@@ -0,0 +1,239 @@ | |||
1 | {-# LANGUAGE TypeFamilies #-} | ||
2 | {-# LANGUAGE FlexibleContexts #-} | ||
3 | {-# LANGUAGE FlexibleInstances #-} | ||
4 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
5 | {-# LANGUAGE FunctionalDependencies #-} | ||
6 | {-# LANGUAGE UndecidableInstances #-} | ||
7 | |||
8 | ----------------------------------------------------------------------------- | ||
9 | -- | | ||
10 | -- Module : Numeric.Container | ||
11 | -- Copyright : (c) Alberto Ruiz 2010-14 | ||
12 | -- License : BSD3 | ||
13 | -- Maintainer : Alberto Ruiz | ||
14 | -- Stability : provisional | ||
15 | -- | ||
16 | -- Basic numeric operations on 'Vector' and 'Matrix', including conversion routines. | ||
17 | -- | ||
18 | -- The 'Container' class is used to define optimized generic functions which work | ||
19 | -- on 'Vector' and 'Matrix' with real or complex elements. | ||
20 | -- | ||
21 | -- Some of these functions are also available in the instances of the standard | ||
22 | -- numeric Haskell classes provided by "Numeric.LinearAlgebra". | ||
23 | -- | ||
24 | ----------------------------------------------------------------------------- | ||
25 | {-# OPTIONS_HADDOCK hide #-} | ||
26 | |||
27 | module Numeric.Container ( | ||
28 | -- * Basic functions | ||
29 | module Data.Packed, | ||
30 | konst, build, | ||
31 | linspace, | ||
32 | diag, ident, | ||
33 | ctrans, | ||
34 | -- * Generic operations | ||
35 | Container(..), | ||
36 | -- * Matrix product | ||
37 | Product(..), udot, dot, (◇), | ||
38 | Mul(..), | ||
39 | Contraction(..), | ||
40 | optimiseMult, | ||
41 | mXm,mXv,vXm,LSDiv(..), | ||
42 | outer, kronecker, | ||
43 | -- * Element conversion | ||
44 | Convert(..), | ||
45 | Complexable(), | ||
46 | RealElement(), | ||
47 | |||
48 | RealOf, ComplexOf, SingleOf, DoubleOf, | ||
49 | |||
50 | IndexOf, | ||
51 | module Data.Complex, | ||
52 | -- * IO | ||
53 | module Data.Packed.IO | ||
54 | ) where | ||
55 | |||
56 | import Data.Packed hiding (stepD, stepF, condD, condF, conjugateC, conjugateQ) | ||
57 | import Data.Packed.Internal.Numeric | ||
58 | import Data.Complex | ||
59 | import Numeric.LinearAlgebra.Algorithms(Field,linearSolveSVD) | ||
60 | import Data.Monoid(Monoid(mconcat)) | ||
61 | import Data.Packed.IO | ||
62 | |||
63 | ------------------------------------------------------------------ | ||
64 | |||
65 | {- | Creates a real vector containing a range of values: | ||
66 | |||
67 | >>> linspace 5 (-3,7::Double) | ||
68 | fromList [-3.0,-0.5,2.0,4.5,7.0]@ | ||
69 | |||
70 | >>> linspace 5 (8,2+i) :: Vector (Complex Double) | ||
71 | fromList [8.0 :+ 0.0,6.5 :+ 0.25,5.0 :+ 0.5,3.5 :+ 0.75,2.0 :+ 1.0] | ||
72 | |||
73 | Logarithmic spacing can be defined as follows: | ||
74 | |||
75 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
76 | -} | ||
77 | linspace :: (Container Vector e) => Int -> (e, e) -> Vector e | ||
78 | linspace 0 (a,b) = fromList[(a+b)/2] | ||
79 | linspace n (a,b) = addConstant a $ scale s $ fromList $ map fromIntegral [0 .. n-1] | ||
80 | where s = (b-a)/fromIntegral (n-1) | ||
81 | |||
82 | -------------------------------------------------------- | ||
83 | |||
84 | class Contraction a b c | a b -> c | ||
85 | where | ||
86 | infixl 7 <.> | ||
87 | {- | Matrix product, matrix - vector product, and dot product | ||
88 | |||
89 | Examples: | ||
90 | |||
91 | >>> let a = (3><4) [1..] :: Matrix Double | ||
92 | >>> let v = fromList [1,0,2,-1] :: Vector Double | ||
93 | >>> let u = fromList [1,2,3] :: Vector Double | ||
94 | |||
95 | >>> a | ||
96 | (3><4) | ||
97 | [ 1.0, 2.0, 3.0, 4.0 | ||
98 | , 5.0, 6.0, 7.0, 8.0 | ||
99 | , 9.0, 10.0, 11.0, 12.0 ] | ||
100 | |||
101 | matrix × matrix: | ||
102 | |||
103 | >>> disp 2 (a <.> trans a) | ||
104 | 3x3 | ||
105 | 30 70 110 | ||
106 | 70 174 278 | ||
107 | 110 278 446 | ||
108 | |||
109 | matrix × vector: | ||
110 | |||
111 | >>> a <.> v | ||
112 | fromList [3.0,11.0,19.0] | ||
113 | |||
114 | dot product: | ||
115 | |||
116 | >>> u <.> fromList[3,2,1::Double] | ||
117 | 10 | ||
118 | |||
119 | For complex vectors the first argument is conjugated: | ||
120 | |||
121 | >>> fromList [1,i] <.> fromList[2*i+1,3] | ||
122 | 1.0 :+ (-1.0) | ||
123 | |||
124 | >>> fromList [1,i,1-i] <.> complex a | ||
125 | fromList [10.0 :+ 4.0,12.0 :+ 4.0,14.0 :+ 4.0,16.0 :+ 4.0] | ||
126 | |||
127 | -} | ||
128 | (<.>) :: a -> b -> c | ||
129 | |||
130 | |||
131 | instance (Product t, Container Vector t) => Contraction (Vector t) (Vector t) t where | ||
132 | u <.> v = conj u `udot` v | ||
133 | |||
134 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where | ||
135 | (<.>) = mXv | ||
136 | |||
137 | instance (Container Vector t, Product t) => Contraction (Vector t) (Matrix t) (Vector t) where | ||
138 | (<.>) v m = (conj v) `vXm` m | ||
139 | |||
140 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | ||
141 | (<.>) = mXm | ||
142 | |||
143 | |||
144 | -------------------------------------------------------------------------------- | ||
145 | |||
146 | class Mul a b c | a b -> c where | ||
147 | infixl 7 <> | ||
148 | -- | Matrix-matrix, matrix-vector, and vector-matrix products. | ||
149 | (<>) :: Product t => a t -> b t -> c t | ||
150 | |||
151 | instance Mul Matrix Matrix Matrix where | ||
152 | (<>) = mXm | ||
153 | |||
154 | instance Mul Matrix Vector Vector where | ||
155 | (<>) m v = flatten $ m <> asColumn v | ||
156 | |||
157 | instance Mul Vector Matrix Vector where | ||
158 | (<>) v m = flatten $ asRow v <> m | ||
159 | |||
160 | -------------------------------------------------------------------------------- | ||
161 | |||
162 | class LSDiv c where | ||
163 | infixl 7 <\> | ||
164 | -- | least squares solution of a linear system, similar to the \\ operator of Matlab\/Octave (based on linearSolveSVD) | ||
165 | (<\>) :: Field t => Matrix t -> c t -> c t | ||
166 | |||
167 | instance LSDiv Vector where | ||
168 | m <\> v = flatten (linearSolveSVD m (reshape 1 v)) | ||
169 | |||
170 | instance LSDiv Matrix where | ||
171 | (<\>) = linearSolveSVD | ||
172 | |||
173 | -------------------------------------------------------------------------------- | ||
174 | |||
175 | class Konst e d c | d -> c, c -> d | ||
176 | where | ||
177 | -- | | ||
178 | -- >>> konst 7 3 :: Vector Float | ||
179 | -- fromList [7.0,7.0,7.0] | ||
180 | -- | ||
181 | -- >>> konst i (3::Int,4::Int) | ||
182 | -- (3><4) | ||
183 | -- [ 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0 | ||
184 | -- , 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0 | ||
185 | -- , 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0, 0.0 :+ 1.0 ] | ||
186 | -- | ||
187 | konst :: e -> d -> c e | ||
188 | |||
189 | instance Container Vector e => Konst e Int Vector | ||
190 | where | ||
191 | konst = konst' | ||
192 | |||
193 | instance Container Vector e => Konst e (Int,Int) Matrix | ||
194 | where | ||
195 | konst = konst' | ||
196 | |||
197 | -------------------------------------------------------------------------------- | ||
198 | |||
199 | class Build d f c e | d -> c, c -> d, f -> e, f -> d, f -> c, c e -> f, d e -> f | ||
200 | where | ||
201 | -- | | ||
202 | -- >>> build 5 (**2) :: Vector Double | ||
203 | -- fromList [0.0,1.0,4.0,9.0,16.0] | ||
204 | -- | ||
205 | -- Hilbert matrix of order N: | ||
206 | -- | ||
207 | -- >>> let hilb n = build (n,n) (\i j -> 1/(i+j+1)) :: Matrix Double | ||
208 | -- >>> putStr . dispf 2 $ hilb 3 | ||
209 | -- 3x3 | ||
210 | -- 1.00 0.50 0.33 | ||
211 | -- 0.50 0.33 0.25 | ||
212 | -- 0.33 0.25 0.20 | ||
213 | -- | ||
214 | build :: d -> f -> c e | ||
215 | |||
216 | instance Container Vector e => Build Int (e -> e) Vector e | ||
217 | where | ||
218 | build = build' | ||
219 | |||
220 | instance Container Matrix e => Build (Int,Int) (e -> e -> e) Matrix e | ||
221 | where | ||
222 | build = build' | ||
223 | |||
224 | -------------------------------------------------------------------------------- | ||
225 | |||
226 | -- | alternative unicode symbol (25c7) for the contraction operator '(\<.\>)' | ||
227 | (◇) :: Contraction a b c => a -> b -> c | ||
228 | infixl 7 ◇ | ||
229 | (◇) = (<.>) | ||
230 | |||
231 | -- | dot product: @cdot u v = 'udot' ('conj' u) v@ | ||
232 | dot :: (Container Vector t, Product t) => Vector t -> Vector t -> t | ||
233 | dot u v = udot (conj u) v | ||
234 | |||
235 | -------------------------------------------------------------------------------- | ||
236 | |||
237 | optimiseMult :: Monoid (Matrix t) => [Matrix t] -> Matrix t | ||
238 | optimiseMult = mconcat | ||
239 | |||
diff --git a/packages/base/src/Numeric/LinearAlgebra/Base.hs b/packages/base/src/Numeric/LinearAlgebra.hs index 8d44d26..96bf29f 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Base.hs +++ b/packages/base/src/Numeric/LinearAlgebra.hs | |||
@@ -8,7 +8,7 @@ Stability : provisional | |||
8 | 8 | ||
9 | -} | 9 | -} |
10 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
11 | module Numeric.LinearAlgebra.Base ( | 11 | module Numeric.LinearAlgebra ( |
12 | 12 | ||
13 | -- * Basic types and data processing | 13 | -- * Basic types and data processing |
14 | module Numeric.LinearAlgebra.Data, | 14 | module Numeric.LinearAlgebra.Data, |
@@ -132,7 +132,7 @@ import Numeric.LinearAlgebra.Data | |||
132 | 132 | ||
133 | import Numeric.Matrix() | 133 | import Numeric.Matrix() |
134 | import Numeric.Vector() | 134 | import Numeric.Vector() |
135 | import Data.Packed.Numeric | 135 | import Numeric.Container |
136 | import Numeric.LinearAlgebra.Algorithms | 136 | import Numeric.LinearAlgebra.Algorithms |
137 | import Numeric.LinearAlgebra.Util | 137 | import Numeric.LinearAlgebra.Util |
138 | import Numeric.LinearAlgebra.Random | 138 | import Numeric.LinearAlgebra.Random |
diff --git a/packages/base/src/Numeric/LinearAlgebra/Data.hs b/packages/base/src/Numeric/LinearAlgebra/Data.hs index 45fc00c..7e8af03 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Data.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Data.hs | |||
@@ -62,7 +62,7 @@ module Numeric.LinearAlgebra.Data( | |||
62 | 62 | ||
63 | import Data.Packed.Vector | 63 | import Data.Packed.Vector |
64 | import Data.Packed.Matrix | 64 | import Data.Packed.Matrix |
65 | import Data.Packed.Numeric | 65 | import Numeric.Container |
66 | import Numeric.LinearAlgebra.Util | 66 | import Numeric.LinearAlgebra.Util |
67 | import Data.Complex | 67 | import Data.Complex |
68 | 68 | ||
diff --git a/packages/base/src/Numeric/LinearAlgebra/Devel.hs b/packages/base/src/Numeric/LinearAlgebra/Devel.hs index b5ef60d..c41db2d 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Devel.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Devel.hs | |||
@@ -60,7 +60,7 @@ module Numeric.LinearAlgebra.Devel( | |||
60 | import Data.Packed.Foreign | 60 | import Data.Packed.Foreign |
61 | import Data.Packed.Development | 61 | import Data.Packed.Development |
62 | import Data.Packed.ST | 62 | import Data.Packed.ST |
63 | import Data.Packed.Numeric(Container,Contraction,LSDiv,Product, | 63 | import Numeric.Container(Container,Contraction,LSDiv,Product, |
64 | Complexable(),RealElement(), | 64 | Complexable(),RealElement(), |
65 | RealOf, ComplexOf, SingleOf, DoubleOf, IndexOf) | 65 | RealOf, ComplexOf, SingleOf, DoubleOf, IndexOf) |
66 | import Data.Packed | 66 | import Data.Packed |
diff --git a/packages/base/src/Numeric/LinearAlgebra/Random.hs b/packages/base/src/Numeric/LinearAlgebra/Random.hs index b36c7a3..7afd658 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Random.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Random.hs | |||
@@ -20,7 +20,7 @@ module Numeric.LinearAlgebra.Random ( | |||
20 | ) where | 20 | ) where |
21 | 21 | ||
22 | import Numeric.Vectorized | 22 | import Numeric.Vectorized |
23 | import Data.Packed.Numeric | 23 | import Numeric.Container |
24 | import Numeric.LinearAlgebra.Algorithms | 24 | import Numeric.LinearAlgebra.Algorithms |
25 | import System.Random(randomIO) | 25 | import System.Random(randomIO) |
26 | 26 | ||
diff --git a/packages/base/src/Numeric/LinearAlgebra/Util.hs b/packages/base/src/Numeric/LinearAlgebra/Util.hs index 440f6d1..2f91e18 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Util.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Util.hs | |||
@@ -48,7 +48,7 @@ module Numeric.LinearAlgebra.Util( | |||
48 | vtrans | 48 | vtrans |
49 | ) where | 49 | ) where |
50 | 50 | ||
51 | import Data.Packed.Numeric | 51 | import Numeric.Container |
52 | import Numeric.LinearAlgebra.Algorithms hiding (i) | 52 | import Numeric.LinearAlgebra.Algorithms hiding (i) |
53 | import Numeric.Matrix() | 53 | import Numeric.Matrix() |
54 | import Numeric.Vector() | 54 | import Numeric.Vector() |
diff --git a/packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs b/packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs index 1d4e089..e4cba8f 100644 --- a/packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs +++ b/packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs | |||
@@ -16,7 +16,7 @@ module Numeric.LinearAlgebra.Util.Convolution( | |||
16 | corr2, conv2, separable | 16 | corr2, conv2, separable |
17 | ) where | 17 | ) where |
18 | 18 | ||
19 | import Data.Packed.Numeric | 19 | import Numeric.Container |
20 | 20 | ||
21 | 21 | ||
22 | vectSS :: Element t => Int -> Vector t -> Matrix t | 22 | vectSS :: Element t => Int -> Vector t -> Matrix t |