summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Util.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Util.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Util.hs39
1 files changed, 38 insertions, 1 deletions
diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs
index d92ed76..3e4f6a9 100644
--- a/lib/Numeric/LinearAlgebra/Util.hs
+++ b/lib/Numeric/LinearAlgebra/Util.hs
@@ -2,7 +2,7 @@
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3{- | 3{- |
4Module : Numeric.LinearAlgebra.Util 4Module : Numeric.LinearAlgebra.Util
5Copyright : (c) Alberto Ruiz 2012 5Copyright : (c) Alberto Ruiz 2013
6License : GPL 6License : GPL
7 7
8Maintainer : Alberto Ruiz (aruiz at um dot es) 8Maintainer : Alberto Ruiz (aruiz at um dot es)
@@ -25,6 +25,10 @@ module Numeric.LinearAlgebra.Util(
25 norm, 25 norm,
26 unitary, 26 unitary,
27 mt, 27 mt,
28 pairwiseD2,
29 rowOuters,
30 null1,
31 null1sym,
28 -- * Convolution 32 -- * Convolution
29 -- ** 1D 33 -- ** 1D
30 corr, conv, corrMin, 34 corr, conv, corrMin,
@@ -138,6 +142,39 @@ mt = trans . inv
138 142
139---------------------------------------------------------------------- 143----------------------------------------------------------------------
140 144
145-- | Matrix of pairwise squared distances of row vectors
146-- (using the matrix product trick in blog.smola.org)
147pairwiseD2 :: Matrix Double -> Matrix Double -> Matrix Double
148pairwiseD2 x y | ok = x2 `outer` oy + ox `outer` y2 - 2* x <> trans y
149 | otherwise = error $ "pairwiseD2 with different number of columns: "
150 ++ show (size x) ++ ", " ++ show (size y)
151 where
152 ox = one (rows x)
153 oy = one (rows y)
154 oc = one (cols x)
155 one k = constant 1 k
156 x2 = x * x <> oc
157 y2 = y * y <> oc
158 ok = cols x == cols y
159
160--------------------------------------------------------------------------------
161
162-- | outer products of rows
163rowOuters :: Matrix Double -> Matrix Double -> Matrix Double
164rowOuters a b = a' * b'
165 where
166 a' = kronecker a (ones 1 (cols b))
167 b' = kronecker (ones 1 (cols a)) b
168
169--------------------------------------------------------------------------------
170
171-- | solution of overconstrained homogeneous linear system
172null1 :: Matrix Double -> Vector Double
173null1 = last . toColumns . snd . rightSV
174
175-- | solution of overconstrained homogeneous symmetric linear system
176null1sym :: Matrix Double -> Vector Double
177null1sym = last . toColumns . snd . eigSH'
141 178
142-------------------------------------------------------------------------------- 179--------------------------------------------------------------------------------
143 180