diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-05 08:11:17 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-05 08:11:17 +0000 |
commit | fa4e2233a873bbfee26939c013b56acc160bca7b (patch) | |
tree | ba2152dfd8ae8ffa6ead19c1924747c2134a3190 /lib/Numeric/LinearAlgebra | |
parent | b59a56c22f7e4aa518046c41e049e5bf1cdf8204 (diff) |
refactor Numeric Vector/Matrix and classes
Diffstat (limited to 'lib/Numeric/LinearAlgebra')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 2 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Interface.hs | 4 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK.hs | 1 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 143 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests.hs | 1 |
5 files changed, 17 insertions, 134 deletions
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 7d2f84d..14bf5d8 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -77,8 +77,8 @@ module Numeric.LinearAlgebra.Algorithms ( | |||
77 | import Data.Packed.Internal hiding ((//)) | 77 | import Data.Packed.Internal hiding ((//)) |
78 | import Data.Packed.Matrix | 78 | import Data.Packed.Matrix |
79 | import Data.Complex | 79 | import Data.Complex |
80 | import Numeric.LinearAlgebra.LAPACK as LAPACK | ||
81 | import Numeric.LinearAlgebra.Linear | 80 | import Numeric.LinearAlgebra.Linear |
81 | import Numeric.LinearAlgebra.LAPACK as LAPACK | ||
82 | import Data.List(foldl1') | 82 | import Data.List(foldl1') |
83 | import Data.Array | 83 | import Data.Array |
84 | 84 | ||
diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs index 13d175b..fa3e209 100644 --- a/lib/Numeric/LinearAlgebra/Interface.hs +++ b/lib/Numeric/LinearAlgebra/Interface.hs | |||
@@ -25,8 +25,8 @@ module Numeric.LinearAlgebra.Interface( | |||
25 | (<|>),(<->), | 25 | (<|>),(<->), |
26 | ) where | 26 | ) where |
27 | 27 | ||
28 | import Data.Packed.Vector | 28 | import Numeric.Vector |
29 | import Data.Packed.Matrix | 29 | import Numeric.Matrix |
30 | import Numeric.LinearAlgebra.Algorithms | 30 | import Numeric.LinearAlgebra.Algorithms |
31 | import Numeric.LinearAlgebra.Linear | 31 | import Numeric.LinearAlgebra.Linear |
32 | 32 | ||
diff --git a/lib/Numeric/LinearAlgebra/LAPACK.hs b/lib/Numeric/LinearAlgebra/LAPACK.hs index eec3035..8888712 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK.hs +++ b/lib/Numeric/LinearAlgebra/LAPACK.hs | |||
@@ -44,6 +44,7 @@ module Numeric.LinearAlgebra.LAPACK ( | |||
44 | import Data.Packed.Internal | 44 | import Data.Packed.Internal |
45 | import Data.Packed.Matrix | 45 | import Data.Packed.Matrix |
46 | import Data.Complex | 46 | import Data.Complex |
47 | import Numeric.Container | ||
47 | import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale)) | 48 | import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale)) |
48 | import Foreign | 49 | import Foreign |
49 | import Foreign.C.Types (CInt) | 50 | import Foreign.C.Types (CInt) |
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 778b976..9048204 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -19,33 +19,31 @@ Basic optimized operations on vectors and matrices. | |||
19 | module Numeric.LinearAlgebra.Linear ( | 19 | module Numeric.LinearAlgebra.Linear ( |
20 | -- * Linear Algebra Typeclasses | 20 | -- * Linear Algebra Typeclasses |
21 | Vectors(..), | 21 | Vectors(..), |
22 | Linear(..), | ||
23 | -- * Products | 22 | -- * Products |
24 | Product(..), | 23 | Product(..), |
25 | mXm,mXv,vXm, | 24 | mXm,mXv,vXm, |
26 | outer, kronecker, | 25 | outer, kronecker, |
27 | -- * Creation of numeric vectors | 26 | -- * Modules |
28 | constant, linspace | 27 | module Numeric.Vector, |
28 | module Numeric.Matrix, | ||
29 | module Numeric.Container | ||
29 | ) where | 30 | ) where |
30 | 31 | ||
31 | import Data.Packed.Internal | 32 | import Data.Packed.Internal.Common |
32 | import Data.Packed.Matrix | 33 | import Data.Packed.Matrix |
33 | import Data.Complex | 34 | import Data.Complex |
35 | import Numeric.Container | ||
36 | import Numeric.Vector | ||
37 | import Numeric.Matrix | ||
34 | import Numeric.GSL.Vector | 38 | import Numeric.GSL.Vector |
35 | import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ) | 39 | import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ) |
36 | 40 | ||
37 | import Control.Monad(ap) | ||
38 | |||
39 | -- | basic Vector functions | 41 | -- | basic Vector functions |
40 | class Num e => Vectors a e where | 42 | class Num e => Vectors a e where |
41 | -- the C functions sumX are twice as fast as using foldVector | 43 | -- the C functions sumX are twice as fast as using foldVector |
42 | vectorSum :: a e -> e | 44 | vectorSum :: a e -> e |
43 | vectorProd :: a e -> e | 45 | vectorProd :: a e -> e |
44 | absSum :: a e -> e | 46 | absSum :: a e -> e |
45 | vectorMin :: a e -> e | ||
46 | vectorMax :: a e -> e | ||
47 | minIdx :: a e -> Int | ||
48 | maxIdx :: a e -> Int | ||
49 | dot :: a e -> a e -> e | 47 | dot :: a e -> a e -> e |
50 | norm1 :: a e -> e | 48 | norm1 :: a e -> e |
51 | norm2 :: a e -> e | 49 | norm2 :: a e -> e |
@@ -57,153 +55,36 @@ instance Vectors Vector Float where | |||
57 | vectorProd = prodF | 55 | vectorProd = prodF |
58 | norm2 = toScalarF Norm2 | 56 | norm2 = toScalarF Norm2 |
59 | absSum = toScalarF AbsSum | 57 | absSum = toScalarF AbsSum |
60 | vectorMin = toScalarF Min | ||
61 | vectorMax = toScalarF Max | ||
62 | minIdx = round . toScalarF MinIdx | ||
63 | maxIdx = round . toScalarF MaxIdx | ||
64 | dot = dotF | 58 | dot = dotF |
65 | norm1 = toScalarF AbsSum | 59 | norm1 = toScalarF AbsSum |
66 | normInf = vectorMax . vectorMapF Abs | 60 | normInf = maxElement . vectorMapF Abs |
67 | 61 | ||
68 | instance Vectors Vector Double where | 62 | instance Vectors Vector Double where |
69 | vectorSum = sumR | 63 | vectorSum = sumR |
70 | vectorProd = prodR | 64 | vectorProd = prodR |
71 | norm2 = toScalarR Norm2 | 65 | norm2 = toScalarR Norm2 |
72 | absSum = toScalarR AbsSum | 66 | absSum = toScalarR AbsSum |
73 | vectorMin = toScalarR Min | ||
74 | vectorMax = toScalarR Max | ||
75 | minIdx = round . toScalarR MinIdx | ||
76 | maxIdx = round . toScalarR MaxIdx | ||
77 | dot = dotR | 67 | dot = dotR |
78 | norm1 = toScalarR AbsSum | 68 | norm1 = toScalarR AbsSum |
79 | normInf = vectorMax . vectorMapR Abs | 69 | normInf = maxElement . vectorMapR Abs |
80 | 70 | ||
81 | instance Vectors Vector (Complex Float) where | 71 | instance Vectors Vector (Complex Float) where |
82 | vectorSum = sumQ | 72 | vectorSum = sumQ |
83 | vectorProd = prodQ | 73 | vectorProd = prodQ |
84 | norm2 = (:+ 0) . toScalarQ Norm2 | 74 | norm2 = (:+ 0) . toScalarQ Norm2 |
85 | absSum = (:+ 0) . toScalarQ AbsSum | 75 | absSum = (:+ 0) . toScalarQ AbsSum |
86 | vectorMin = ap (@>) minIdx | ||
87 | vectorMax = ap (@>) maxIdx | ||
88 | minIdx = minIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
89 | maxIdx = maxIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
90 | dot = dotQ | 76 | dot = dotQ |
91 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapQ Abs | 77 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapQ Abs |
92 | normInf = (:+ 0) . vectorMax . fst . fromComplex . vectorMapQ Abs | 78 | normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapQ Abs |
93 | 79 | ||
94 | instance Vectors Vector (Complex Double) where | 80 | instance Vectors Vector (Complex Double) where |
95 | vectorSum = sumC | 81 | vectorSum = sumC |
96 | vectorProd = prodC | 82 | vectorProd = prodC |
97 | norm2 = (:+ 0) . toScalarC Norm2 | 83 | norm2 = (:+ 0) . toScalarC Norm2 |
98 | absSum = (:+ 0) . toScalarC AbsSum | 84 | absSum = (:+ 0) . toScalarC AbsSum |
99 | vectorMin = ap (@>) minIdx | ||
100 | vectorMax = ap (@>) maxIdx | ||
101 | minIdx = minIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
102 | maxIdx = maxIdx . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
103 | dot = dotC | 85 | dot = dotC |
104 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapC Abs | 86 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapC Abs |
105 | normInf = (:+ 0) . vectorMax . fst . fromComplex . vectorMapC Abs | 87 | normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapC Abs |
106 | |||
107 | ---------------------------------------------------- | ||
108 | |||
109 | -- | Basic element-by-element functions. | ||
110 | class (Element e, Container c) => Linear c e where | ||
111 | -- | create a structure with a single element | ||
112 | scalar :: e -> c e | ||
113 | scale :: e -> c e -> c e | ||
114 | -- | scale the element by element reciprocal of the object: | ||
115 | -- | ||
116 | -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@ | ||
117 | scaleRecip :: e -> c e -> c e | ||
118 | addConstant :: e -> c e -> c e | ||
119 | add :: c e -> c e -> c e | ||
120 | sub :: c e -> c e -> c e | ||
121 | -- | element by element multiplication | ||
122 | mul :: c e -> c e -> c e | ||
123 | -- | element by element division | ||
124 | divide :: c e -> c e -> c e | ||
125 | equal :: c e -> c e -> Bool | ||
126 | |||
127 | |||
128 | instance Linear Vector Float where | ||
129 | scale = vectorMapValF Scale | ||
130 | scaleRecip = vectorMapValF Recip | ||
131 | addConstant = vectorMapValF AddConstant | ||
132 | add = vectorZipF Add | ||
133 | sub = vectorZipF Sub | ||
134 | mul = vectorZipF Mul | ||
135 | divide = vectorZipF Div | ||
136 | equal u v = dim u == dim v && vectorMax (vectorMapF Abs (sub u v)) == 0.0 | ||
137 | scalar x = fromList [x] | ||
138 | |||
139 | instance Linear Vector Double where | ||
140 | scale = vectorMapValR Scale | ||
141 | scaleRecip = vectorMapValR Recip | ||
142 | addConstant = vectorMapValR AddConstant | ||
143 | add = vectorZipR Add | ||
144 | sub = vectorZipR Sub | ||
145 | mul = vectorZipR Mul | ||
146 | divide = vectorZipR Div | ||
147 | equal u v = dim u == dim v && vectorMax (vectorMapR Abs (sub u v)) == 0.0 | ||
148 | scalar x = fromList [x] | ||
149 | |||
150 | instance Linear Vector (Complex Double) where | ||
151 | scale = vectorMapValC Scale | ||
152 | scaleRecip = vectorMapValC Recip | ||
153 | addConstant = vectorMapValC AddConstant | ||
154 | add = vectorZipC Add | ||
155 | sub = vectorZipC Sub | ||
156 | mul = vectorZipC Mul | ||
157 | divide = vectorZipC Div | ||
158 | equal u v = dim u == dim v && vectorMax (mapVector magnitude (sub u v)) == 0.0 | ||
159 | scalar x = fromList [x] | ||
160 | |||
161 | instance Linear Vector (Complex Float) where | ||
162 | scale = vectorMapValQ Scale | ||
163 | scaleRecip = vectorMapValQ Recip | ||
164 | addConstant = vectorMapValQ AddConstant | ||
165 | add = vectorZipQ Add | ||
166 | sub = vectorZipQ Sub | ||
167 | mul = vectorZipQ Mul | ||
168 | divide = vectorZipQ Div | ||
169 | equal u v = dim u == dim v && vectorMax (mapVector magnitude (sub u v)) == 0.0 | ||
170 | scalar x = fromList [x] | ||
171 | |||
172 | instance (Linear Vector a, Container Matrix) => (Linear Matrix a) where | ||
173 | scale x = liftMatrix (scale x) | ||
174 | scaleRecip x = liftMatrix (scaleRecip x) | ||
175 | addConstant x = liftMatrix (addConstant x) | ||
176 | add = liftMatrix2 add | ||
177 | sub = liftMatrix2 sub | ||
178 | mul = liftMatrix2 mul | ||
179 | divide = liftMatrix2 divide | ||
180 | equal a b = cols a == cols b && flatten a `equal` flatten b | ||
181 | scalar x = (1><1) [x] | ||
182 | |||
183 | |||
184 | ---------------------------------------------------- | ||
185 | |||
186 | {- | creates a vector with a given number of equal components: | ||
187 | |||
188 | @> constant 2 7 | ||
189 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ | ||
190 | -} | ||
191 | constant :: Element a => a -> Int -> Vector a | ||
192 | -- constant x n = runSTVector (newVector x n) | ||
193 | constant = constantD -- about 2x faster | ||
194 | |||
195 | {- | Creates a real vector containing a range of values: | ||
196 | |||
197 | @\> linspace 5 (-3,7) | ||
198 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | ||
199 | |||
200 | Logarithmic spacing can be defined as follows: | ||
201 | |||
202 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
203 | -} | ||
204 | linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e | ||
205 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | ||
206 | where s = (b-a)/fromIntegral (n-1) | ||
207 | 88 | ||
208 | ---------------------------------------------------- | 89 | ---------------------------------------------------- |
209 | 90 | ||
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs index 43a62e5..5b42226 100644 --- a/lib/Numeric/LinearAlgebra/Tests.hs +++ b/lib/Numeric/LinearAlgebra/Tests.hs | |||
@@ -21,6 +21,7 @@ module Numeric.LinearAlgebra.Tests( | |||
21 | --, runBigTests | 21 | --, runBigTests |
22 | ) where | 22 | ) where |
23 | 23 | ||
24 | import Data.Packed.Random | ||
24 | import Numeric.LinearAlgebra | 25 | import Numeric.LinearAlgebra |
25 | import Numeric.LinearAlgebra.LAPACK | 26 | import Numeric.LinearAlgebra.LAPACK |
26 | import Numeric.LinearAlgebra.Tests.Instances | 27 | import Numeric.LinearAlgebra.Tests.Instances |