diff options
Diffstat (limited to 'lib/Numeric')
-rw-r--r-- | lib/Numeric/Container.hs | 49 | ||||
-rw-r--r-- | lib/Numeric/ContainerBoot.hs | 12 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Util.hs | 24 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Util/Convolution.hs | 2 |
4 files changed, 63 insertions, 24 deletions
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs index 345c1f1..ed6714f 100644 --- a/lib/Numeric/Container.hs +++ b/lib/Numeric/Container.hs | |||
@@ -8,8 +8,8 @@ | |||
8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
9 | -- | | 9 | -- | |
10 | -- Module : Numeric.Container | 10 | -- Module : Numeric.Container |
11 | -- Copyright : (c) Alberto Ruiz 2010 | 11 | -- Copyright : (c) Alberto Ruiz 2010-14 |
12 | -- License : GPL-style | 12 | -- License : GPL |
13 | -- | 13 | -- |
14 | -- Maintainer : Alberto Ruiz <aruiz@um.es> | 14 | -- Maintainer : Alberto Ruiz <aruiz@um.es> |
15 | -- Stability : provisional | 15 | -- Stability : provisional |
@@ -35,8 +35,9 @@ module Numeric.Container ( | |||
35 | Container(..), | 35 | Container(..), |
36 | -- * Matrix product | 36 | -- * Matrix product |
37 | Product(..), | 37 | Product(..), |
38 | Contraction(..), | ||
38 | optimiseMult, | 39 | optimiseMult, |
39 | mXm,mXv,vXm,(<.>),Mul(..),LSDiv(..), | 40 | mXm,mXv,vXm,Mul(..),LSDiv(..), cdot, |
40 | outer, kronecker, | 41 | outer, kronecker, |
41 | -- * Random numbers | 42 | -- * Random numbers |
42 | RandDist(..), | 43 | RandDist(..), |
@@ -95,12 +96,9 @@ linspace :: (Enum e, Container Vector e) => Int -> (e, e) -> Vector e | |||
95 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | 96 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] |
96 | where s = (b-a)/fromIntegral (n-1) | 97 | where s = (b-a)/fromIntegral (n-1) |
97 | 98 | ||
98 | -- | Dot product: @u \<.\> v = dot u v@ | 99 | -- | dot product: @cdot u v = 'udot' ('conj' u) v@ |
99 | (<.>) :: Product t => Vector t -> Vector t -> t | 100 | cdot :: (Container Vector t, Product t) => Vector t -> Vector t -> t |
100 | infixl 7 <.> | 101 | cdot u v = udot (conj u) v |
101 | (<.>) = dot | ||
102 | |||
103 | |||
104 | 102 | ||
105 | -------------------------------------------------------- | 103 | -------------------------------------------------------- |
106 | 104 | ||
@@ -143,3 +141,36 @@ meanCov x = (med,cov) where | |||
143 | xc = x `sub` meds | 141 | xc = x `sub` meds |
144 | cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc) | 142 | cov = scale (recip (fromIntegral (r-1))) (trans xc `mXm` xc) |
145 | 143 | ||
144 | -------------------------------------------------------------------------------- | ||
145 | |||
146 | -- | matrix-matrix product, matrix-vector product, unconjugated dot product, and scaling | ||
147 | class Contraction a b c | a b -> c | ||
148 | where | ||
149 | -- ^ 0x00d7 multiplication sign | ||
150 | infixl 7 × | ||
151 | (×) :: a -> b -> c | ||
152 | |||
153 | instance Product t => Contraction (Vector t) (Vector t) t where | ||
154 | (×) = udot | ||
155 | |||
156 | instance Product t => Contraction (Matrix t) (Vector t) (Vector t) where | ||
157 | (×) = mXv | ||
158 | |||
159 | instance Product t => Contraction (Vector t) (Matrix t) (Vector t) where | ||
160 | (×) = vXm | ||
161 | |||
162 | instance Product t => Contraction (Matrix t) (Matrix t) (Matrix t) where | ||
163 | (×) = mXm | ||
164 | |||
165 | instance Container Vector t => Contraction t (Vector t) (Vector t) where | ||
166 | (×) = scale | ||
167 | |||
168 | instance Container Vector t => Contraction (Vector t) t (Vector t) where | ||
169 | (×) = flip scale | ||
170 | |||
171 | instance Container Matrix t => Contraction t (Matrix t) (Matrix t) where | ||
172 | (×) = scale | ||
173 | |||
174 | instance Container Matrix t => Contraction (Matrix t) t (Matrix t) where | ||
175 | (×) = flip scale | ||
176 | |||
diff --git a/lib/Numeric/ContainerBoot.hs b/lib/Numeric/ContainerBoot.hs index dcb326c..4c5bbd0 100644 --- a/lib/Numeric/ContainerBoot.hs +++ b/lib/Numeric/ContainerBoot.hs | |||
@@ -327,8 +327,8 @@ instance (Container Vector a) => Container Matrix a where | |||
327 | class Element e => Product e where | 327 | class Element e => Product e where |
328 | -- | matrix product | 328 | -- | matrix product |
329 | multiply :: Matrix e -> Matrix e -> Matrix e | 329 | multiply :: Matrix e -> Matrix e -> Matrix e |
330 | -- | dot (inner) product | 330 | -- | (unconjugated) dot product |
331 | dot :: Vector e -> Vector e -> e | 331 | udot :: Vector e -> Vector e -> e |
332 | -- | sum of absolute value of elements (differs in complex case from @norm1@) | 332 | -- | sum of absolute value of elements (differs in complex case from @norm1@) |
333 | absSum :: Vector e -> RealOf e | 333 | absSum :: Vector e -> RealOf e |
334 | -- | sum of absolute value of elements | 334 | -- | sum of absolute value of elements |
@@ -341,7 +341,7 @@ class Element e => Product e where | |||
341 | instance Product Float where | 341 | instance Product Float where |
342 | norm2 = toScalarF Norm2 | 342 | norm2 = toScalarF Norm2 |
343 | absSum = toScalarF AbsSum | 343 | absSum = toScalarF AbsSum |
344 | dot = dotF | 344 | udot = dotF |
345 | norm1 = toScalarF AbsSum | 345 | norm1 = toScalarF AbsSum |
346 | normInf = maxElement . vectorMapF Abs | 346 | normInf = maxElement . vectorMapF Abs |
347 | multiply = multiplyF | 347 | multiply = multiplyF |
@@ -349,7 +349,7 @@ instance Product Float where | |||
349 | instance Product Double where | 349 | instance Product Double where |
350 | norm2 = toScalarR Norm2 | 350 | norm2 = toScalarR Norm2 |
351 | absSum = toScalarR AbsSum | 351 | absSum = toScalarR AbsSum |
352 | dot = dotR | 352 | udot = dotR |
353 | norm1 = toScalarR AbsSum | 353 | norm1 = toScalarR AbsSum |
354 | normInf = maxElement . vectorMapR Abs | 354 | normInf = maxElement . vectorMapR Abs |
355 | multiply = multiplyR | 355 | multiply = multiplyR |
@@ -357,7 +357,7 @@ instance Product Double where | |||
357 | instance Product (Complex Float) where | 357 | instance Product (Complex Float) where |
358 | norm2 = toScalarQ Norm2 | 358 | norm2 = toScalarQ Norm2 |
359 | absSum = toScalarQ AbsSum | 359 | absSum = toScalarQ AbsSum |
360 | dot = dotQ | 360 | udot = dotQ |
361 | norm1 = sumElements . fst . fromComplex . vectorMapQ Abs | 361 | norm1 = sumElements . fst . fromComplex . vectorMapQ Abs |
362 | normInf = maxElement . fst . fromComplex . vectorMapQ Abs | 362 | normInf = maxElement . fst . fromComplex . vectorMapQ Abs |
363 | multiply = multiplyQ | 363 | multiply = multiplyQ |
@@ -365,7 +365,7 @@ instance Product (Complex Float) where | |||
365 | instance Product (Complex Double) where | 365 | instance Product (Complex Double) where |
366 | norm2 = toScalarC Norm2 | 366 | norm2 = toScalarC Norm2 |
367 | absSum = toScalarC AbsSum | 367 | absSum = toScalarC AbsSum |
368 | dot = dotC | 368 | udot = dotC |
369 | norm1 = sumElements . fst . fromComplex . vectorMapC Abs | 369 | norm1 = sumElements . fst . fromComplex . vectorMapC Abs |
370 | normInf = maxElement . fst . fromComplex . vectorMapC Abs | 370 | normInf = maxElement . fst . fromComplex . vectorMapC Abs |
371 | multiply = multiplyC | 371 | multiply = multiplyC |
diff --git a/lib/Numeric/LinearAlgebra/Util.hs b/lib/Numeric/LinearAlgebra/Util.hs index f7c40d7..f6aa7da 100644 --- a/lib/Numeric/LinearAlgebra/Util.hs +++ b/lib/Numeric/LinearAlgebra/Util.hs | |||
@@ -19,10 +19,11 @@ module Numeric.LinearAlgebra.Util( | |||
19 | diagl, | 19 | diagl, |
20 | row, | 20 | row, |
21 | col, | 21 | col, |
22 | (&),(!), (¦), (#), | 22 | (&), (¦), (#), |
23 | (?),(¿), | 23 | (?), (¿), |
24 | rand, randn, | 24 | rand, randn, |
25 | cross, | 25 | cross, |
26 | (<.>), | ||
26 | norm, | 27 | norm, |
27 | unitary, | 28 | unitary, |
28 | mt, | 29 | mt, |
@@ -45,7 +46,13 @@ module Numeric.LinearAlgebra.Util( | |||
45 | vec, | 46 | vec, |
46 | vech, | 47 | vech, |
47 | dup, | 48 | dup, |
48 | vtrans | 49 | vtrans, |
50 | -- * Plot | ||
51 | mplot, | ||
52 | plot, parametricPlot, | ||
53 | splot, mesh, meshdom, | ||
54 | matrixToPGM, imshow, | ||
55 | gnuplotX, gnuplotpdf, gnuplotWin | ||
49 | ) where | 56 | ) where |
50 | 57 | ||
51 | import Numeric.Container | 58 | import Numeric.Container |
@@ -55,6 +62,7 @@ import Numeric.Vector() | |||
55 | 62 | ||
56 | import System.Random(randomIO) | 63 | import System.Random(randomIO) |
57 | import Numeric.LinearAlgebra.Util.Convolution | 64 | import Numeric.LinearAlgebra.Util.Convolution |
65 | import Graphics.Plot | ||
58 | 66 | ||
59 | 67 | ||
60 | disp :: Int -> Matrix Double -> IO () | 68 | disp :: Int -> Matrix Double -> IO () |
@@ -99,11 +107,6 @@ infixl 3 & | |||
99 | (&) :: Vector Double -> Vector Double -> Vector Double | 107 | (&) :: Vector Double -> Vector Double -> Vector Double |
100 | a & b = vjoin [a,b] | 108 | a & b = vjoin [a,b] |
101 | 109 | ||
102 | -- | horizontal concatenation of real matrices | ||
103 | infixl 3 ! | ||
104 | (!) :: Matrix Double -> Matrix Double -> Matrix Double | ||
105 | a ! b = fromBlocks [[a,b]] | ||
106 | |||
107 | -- | (00A6) horizontal concatenation of real matrices | 110 | -- | (00A6) horizontal concatenation of real matrices |
108 | infixl 3 ¦ | 111 | infixl 3 ¦ |
109 | (¦) :: Matrix Double -> Matrix Double -> Matrix Double | 112 | (¦) :: Matrix Double -> Matrix Double -> Matrix Double |
@@ -161,6 +164,11 @@ size m = (rows m, cols m) | |||
161 | mt :: Matrix Double -> Matrix Double | 164 | mt :: Matrix Double -> Matrix Double |
162 | mt = trans . inv | 165 | mt = trans . inv |
163 | 166 | ||
167 | -- | dot product: @u \<.\> v = 'cdot' u v@ | ||
168 | (<.>) :: (Container Vector t, Product t) => Vector t -> Vector t -> t | ||
169 | infixl 7 <.> | ||
170 | u <.> v = cdot u v | ||
171 | |||
164 | ---------------------------------------------------------------------- | 172 | ---------------------------------------------------------------------- |
165 | 173 | ||
166 | -- | Matrix of pairwise squared distances of row vectors | 174 | -- | Matrix of pairwise squared distances of row vectors |
diff --git a/lib/Numeric/LinearAlgebra/Util/Convolution.hs b/lib/Numeric/LinearAlgebra/Util/Convolution.hs index be9b1eb..1043614 100644 --- a/lib/Numeric/LinearAlgebra/Util/Convolution.hs +++ b/lib/Numeric/LinearAlgebra/Util/Convolution.hs | |||
@@ -75,7 +75,7 @@ matSS dr m = map (reshape c) [ subVector (k*c) n v | k <- [0 .. r - dr] ] | |||
75 | corr2 :: Product a => Matrix a -> Matrix a -> Matrix a | 75 | corr2 :: Product a => Matrix a -> Matrix a -> Matrix a |
76 | -- ^ 2D correlation | 76 | -- ^ 2D correlation |
77 | corr2 ker mat = dims | 77 | corr2 ker mat = dims |
78 | . concatMap (map ((<.> ker') . flatten) . matSS c . trans) | 78 | . concatMap (map (udot ker' . flatten) . matSS c . trans) |
79 | . matSS r $ mat | 79 | . matSS r $ mat |
80 | where | 80 | where |
81 | r = rows ker | 81 | r = rows ker |