summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Linear.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs31
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
index 3017936..94f6958 100644
--- a/lib/Numeric/LinearAlgebra/Linear.hs
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -16,7 +16,7 @@ Basic optimized operations on vectors and matrices.
16 16
17module Numeric.LinearAlgebra.Linear ( 17module Numeric.LinearAlgebra.Linear (
18 Linear(..), 18 Linear(..),
19 multiply, dot, outer 19 multiply, dot, outer, kronecker
20) where 20) where
21 21
22 22
@@ -100,3 +100,32 @@ dot u v = dat (multiply r c) `at` 0
100-} 100-}
101outer :: (Field t) => Vector t -> Vector t -> Matrix t 101outer :: (Field t) => Vector t -> Vector t -> Matrix t
102outer u v = asColumn u `multiply` asRow v 102outer u v = asColumn u `multiply` asRow v
103
104{- | Kronecker product of two matrices.
105
106@m1=(2><3)
107 [ 1.0, 2.0, 0.0
108 , 0.0, -1.0, 3.0 ]
109m2=(4><3)
110 [ 1.0, 2.0, 3.0
111 , 4.0, 5.0, 6.0
112 , 7.0, 8.0, 9.0
113 , 10.0, 11.0, 12.0 ]@
114
115@\> kronecker m1 m2
116(8><9)
117 [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0
118 , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0
119 , 7.0, 8.0, 9.0, 14.0, 16.0, 18.0, 0.0, 0.0, 0.0
120 , 10.0, 11.0, 12.0, 20.0, 22.0, 24.0, 0.0, 0.0, 0.0
121 , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0
122 , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0
123 , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0
124 , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]@
125-}
126kronecker :: (Field t) => Matrix t -> Matrix t -> Matrix t
127kronecker a b = fromBlocks
128 . partit (cols a)
129 . map (reshape (cols b))
130 . toRows
131 $ flatten a `outer` flatten b