summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Linear.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-08-26 17:49:45 +0000
committerAlberto Ruiz <aruiz@um.es>2010-08-26 17:49:45 +0000
commit6058e1b17c005be1ea95ebb7d98d9fd15bb538d2 (patch)
treec4277e00c2c92a0ed8f3750255154fa8e2b6fe2d /lib/Numeric/LinearAlgebra/Linear.hs
parentf541d7dbdc8338b1dd1c0538751d837a16740bd8 (diff)
Float matrix product
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs90
1 files changed, 87 insertions, 3 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
index 51e93fb..ae48245 100644
--- a/lib/Numeric/LinearAlgebra/Linear.hs
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -19,15 +19,19 @@ module Numeric.LinearAlgebra.Linear (
19 -- * Linear Algebra Typeclasses 19 -- * Linear Algebra Typeclasses
20 Vectors(..), 20 Vectors(..),
21 Linear(..), 21 Linear(..),
22 -- * Products
23 Prod(..),
24 mXm,mXv,vXm, mulH,
25 outer, kronecker,
22 -- * Creation of numeric vectors 26 -- * Creation of numeric vectors
23 constant, linspace 27 constant, linspace
24) where 28) where
25 29
26import Data.Packed.Internal.Vector 30import Data.Packed.Internal
27import Data.Packed.Internal.Matrix
28import Data.Packed.Matrix 31import Data.Packed.Matrix
29import Data.Complex 32import Data.Complex
30import Numeric.GSL.Vector 33import Numeric.GSL.Vector
34import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ)
31 35
32import Control.Monad(ap) 36import Control.Monad(ap)
33 37
@@ -86,7 +90,7 @@ instance Vectors Vector (Complex Double) where
86---------------------------------------------------- 90----------------------------------------------------
87 91
88-- | Basic element-by-element functions. 92-- | Basic element-by-element functions.
89class (Element e, AutoReal e, Convert e, Container c) => Linear c e where 93class (Element e, AutoReal e, Container c) => Linear c e where
90 -- | create a structure with a single element 94 -- | create a structure with a single element
91 scalar :: e -> c e 95 scalar :: e -> c e
92 scale :: e -> c e -> c e 96 scale :: e -> c e -> c e
@@ -184,3 +188,83 @@ linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e
184linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] 188linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1]
185 where s = (b-a)/fromIntegral (n-1) 189 where s = (b-a)/fromIntegral (n-1)
186 190
191----------------------------------------------------
192
193-- reference multiply
194mulH a b = fromLists [[ doth ai bj | bj <- toColumns b] | ai <- toRows a ]
195 where doth u v = sum $ zipWith (*) (toList u) (toList v)
196
197class Element t => Prod t where
198 multiply :: Matrix t -> Matrix t -> Matrix t
199 multiply = mulH
200 ctrans :: Matrix t -> Matrix t
201
202instance Prod Double where
203 multiply = multiplyR
204 ctrans = trans
205
206instance Prod (Complex Double) where
207 multiply = multiplyC
208 ctrans = conj . trans
209
210instance Prod Float where
211 multiply = multiplyF
212 ctrans = trans
213
214instance Prod (Complex Float) where
215 multiply = multiplyQ
216 ctrans = conj . trans
217
218----------------------------------------------------------
219
220-- synonym for matrix product
221mXm :: Prod t => Matrix t -> Matrix t -> Matrix t
222mXm = multiply
223
224-- matrix - vector product
225mXv :: Prod t => Matrix t -> Vector t -> Vector t
226mXv m v = flatten $ m `mXm` (asColumn v)
227
228-- vector - matrix product
229vXm :: Prod t => Vector t -> Matrix t -> Vector t
230vXm v m = flatten $ (asRow v) `mXm` m
231
232{- | Outer product of two vectors.
233
234@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3]
235(3><3)
236 [ 5.0, 2.0, 3.0
237 , 10.0, 4.0, 6.0
238 , 15.0, 6.0, 9.0 ]@
239-}
240outer :: (Prod t) => Vector t -> Vector t -> Matrix t
241outer u v = asColumn u `multiply` asRow v
242
243{- | Kronecker product of two matrices.
244
245@m1=(2><3)
246 [ 1.0, 2.0, 0.0
247 , 0.0, -1.0, 3.0 ]
248m2=(4><3)
249 [ 1.0, 2.0, 3.0
250 , 4.0, 5.0, 6.0
251 , 7.0, 8.0, 9.0
252 , 10.0, 11.0, 12.0 ]@
253
254@\> kronecker m1 m2
255(8><9)
256 [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0
257 , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0
258 , 7.0, 8.0, 9.0, 14.0, 16.0, 18.0, 0.0, 0.0, 0.0
259 , 10.0, 11.0, 12.0, 20.0, 22.0, 24.0, 0.0, 0.0, 0.0
260 , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0
261 , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0
262 , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0
263 , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]@
264-}
265kronecker :: (Prod t) => Matrix t -> Matrix t -> Matrix t
266kronecker a b = fromBlocks
267 . splitEvery (cols a)
268 . map (reshape (cols b))
269 . toRows
270 $ flatten a `outer` flatten b