diff options
author | Alberto Ruiz <aruiz@um.es> | 2008-10-02 15:53:10 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2008-10-02 15:53:10 +0000 |
commit | 192ac5f4b98517862c37ecf161505396ad223cd8 (patch) | |
tree | 811312f28bca2bd18d282bc0be732a17cd8dbcd7 /lib/Numeric/LinearAlgebra/Linear.hs | |
parent | 9c6b2af0066f7608301ad685ea5e60753fc3b6ff (diff) |
alternative multiply versions
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 54 |
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 | ||
17 | module Numeric.LinearAlgebra.Linear ( | 17 | module Numeric.LinearAlgebra.Linear ( |
18 | Linear(..), | 18 | Linear(..) |
19 | multiply, dot, outer, kronecker | ||
20 | ) where | 19 | ) where |
21 | 20 | ||
22 | 21 | ||
23 | import Data.Packed.Internal(multiply,partit) | 22 | import Data.Packed.Internal(partit) |
24 | import Data.Packed | 23 | import Data.Packed |
25 | import Numeric.GSL.Vector | 24 | import Numeric.GSL.Vector |
26 | import Complex | 25 | import 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 | ||
76 | dot :: (Element t) => Vector t -> Vector t -> t | ||
77 | dot 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 | -} | ||
90 | outer :: (Element t) => Vector t -> Vector t -> Matrix t | ||
91 | outer 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 ] | ||
98 | m2=(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 | -} | ||
115 | kronecker :: (Element t) => Matrix t -> Matrix t -> Matrix t | ||
116 | kronecker a b = fromBlocks | ||
117 | . partit (cols a) | ||
118 | . map (reshape (cols b)) | ||
119 | . toRows | ||
120 | $ flatten a `outer` flatten b | ||