summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Linear.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2008-10-02 15:53:10 +0000
committerAlberto Ruiz <aruiz@um.es>2008-10-02 15:53:10 +0000
commit192ac5f4b98517862c37ecf161505396ad223cd8 (patch)
tree811312f28bca2bd18d282bc0be732a17cd8dbcd7 /lib/Numeric/LinearAlgebra/Linear.hs
parent9c6b2af0066f7608301ad685ea5e60753fc3b6ff (diff)
alternative multiply versions
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs54
1 files changed, 2 insertions, 52 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
index 0ddbb55..1bf8b04 100644
--- a/lib/Numeric/LinearAlgebra/Linear.hs
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -15,12 +15,11 @@ Basic optimized operations on vectors and matrices.
15----------------------------------------------------------------------------- 15-----------------------------------------------------------------------------
16 16
17module Numeric.LinearAlgebra.Linear ( 17module Numeric.LinearAlgebra.Linear (
18 Linear(..), 18 Linear(..)
19 multiply, dot, outer, kronecker
20) where 19) where
21 20
22 21
23import Data.Packed.Internal(multiply,partit) 22import Data.Packed.Internal(partit)
24import Data.Packed 23import Data.Packed
25import Numeric.GSL.Vector 24import Numeric.GSL.Vector
26import Complex 25import Complex
@@ -69,52 +68,3 @@ instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where
69 mul = liftMatrix2 mul 68 mul = liftMatrix2 mul
70 divide = liftMatrix2 divide 69 divide = liftMatrix2 divide
71 equal a b = cols a == cols b && flatten a `equal` flatten b 70 equal a b = cols a == cols b && flatten a `equal` flatten b
72
73--------------------------------------------------
74
75-- | euclidean inner product
76dot :: (Element t) => Vector t -> Vector t -> t
77dot u v = multiply r c @@> (0,0)
78 where r = asRow u
79 c = asColumn v
80
81
82{- | Outer product of two vectors.
83
84@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3]
85(3><3)
86 [ 5.0, 2.0, 3.0
87 , 10.0, 4.0, 6.0
88 , 15.0, 6.0, 9.0 ]@
89-}
90outer :: (Element t) => Vector t -> Vector t -> Matrix t
91outer u v = asColumn u `multiply` asRow v
92
93{- | Kronecker product of two matrices.
94
95@m1=(2><3)
96 [ 1.0, 2.0, 0.0
97 , 0.0, -1.0, 3.0 ]
98m2=(4><3)
99 [ 1.0, 2.0, 3.0
100 , 4.0, 5.0, 6.0
101 , 7.0, 8.0, 9.0
102 , 10.0, 11.0, 12.0 ]@
103
104@\> kronecker m1 m2
105(8><9)
106 [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0
107 , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0
108 , 7.0, 8.0, 9.0, 14.0, 16.0, 18.0, 0.0, 0.0, 0.0
109 , 10.0, 11.0, 12.0, 20.0, 22.0, 24.0, 0.0, 0.0, 0.0
110 , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0
111 , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0
112 , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0
113 , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]@
114-}
115kronecker :: (Element t) => Matrix t -> Matrix t -> Matrix t
116kronecker a b = fromBlocks
117 . partit (cols a)
118 . map (reshape (cols b))
119 . toRows
120 $ flatten a `outer` flatten b