diff options
-rw-r--r-- | examples/pca1.hs | 4 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 3 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 12 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 31 |
4 files changed, 44 insertions, 6 deletions
diff --git a/examples/pca1.hs b/examples/pca1.hs index 4ad8720..ab3d0b7 100644 --- a/examples/pca1.hs +++ b/examples/pca1.hs | |||
@@ -21,7 +21,7 @@ cov x = (trans xc <> xc) / fromIntegral (rows x -1) | |||
21 | -- creates the compression and decompression functions from the desired number of components | 21 | -- creates the compression and decompression functions from the desired number of components |
22 | pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec) | 22 | pca :: Int -> Mat -> (Vec -> Vec , Vec -> Vec) |
23 | pca n dataSet = (encode,decode) | 23 | pca n dataSet = (encode,decode) |
24 | where | 24 | where |
25 | encode x = vp <> (x - m) | 25 | encode x = vp <> (x - m) |
26 | decode x = x <> vp + m | 26 | decode x = x <> vp + m |
27 | m = mean dataSet | 27 | m = mean dataSet |
@@ -29,6 +29,8 @@ pca n dataSet = (encode,decode) | |||
29 | (_,v) = eigSH c | 29 | (_,v) = eigSH c |
30 | vp = takeRows n (trans v) | 30 | vp = takeRows n (trans v) |
31 | 31 | ||
32 | norm = pnorm PNorm2 | ||
33 | |||
32 | main = do | 34 | main = do |
33 | ok <- doesFileExist ("mnist.txt") | 35 | ok <- doesFileExist ("mnist.txt") |
34 | when (not ok) $ do | 36 | when (not ok) $ do |
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index 605a6f3..f63ee52 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs | |||
@@ -142,7 +142,8 @@ createMatrix order r c = do | |||
142 | p <- createVector (r*c) | 142 | p <- createVector (r*c) |
143 | return (matrixFromVector order c p) | 143 | return (matrixFromVector order c p) |
144 | 144 | ||
145 | {- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. | 145 | {- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. (GNU-Octave groups by columns. To do it you can define @reshapeF r = trans . reshape r@ |
146 | where r is the desired number of rows.) | ||
146 | 147 | ||
147 | @\> reshape 4 ('fromList' [1..12]) | 148 | @\> reshape 4 ('fromList' [1..12]) |
148 | (3><4) | 149 | (3><4) |
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 0683956..52f9b6f 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -22,7 +22,7 @@ module Numeric.LinearAlgebra.Algorithms ( | |||
22 | -- * Linear Systems | 22 | -- * Linear Systems |
23 | linearSolve, | 23 | linearSolve, |
24 | inv, pinv, | 24 | inv, pinv, |
25 | pinvTol, det, rank, | 25 | pinvTol, det, rank, rcond, |
26 | -- * Matrix factorizations | 26 | -- * Matrix factorizations |
27 | -- ** Singular value decomposition | 27 | -- ** Singular value decomposition |
28 | svd, | 28 | svd, |
@@ -244,10 +244,10 @@ pnormCM PNorm1 m = vectorMax $ constant 1 (rows m) `vXm` liftMatrix (liftVector | |||
244 | pnormCM Infinity m = vectorMax $ liftMatrix (liftVector magnitude) m `mXv` constant 1 (cols m) | 244 | pnormCM Infinity m = vectorMax $ liftMatrix (liftVector magnitude) m `mXv` constant 1 (cols m) |
245 | --pnormCM _ _ = error "p norm not yet defined" | 245 | --pnormCM _ _ = error "p norm not yet defined" |
246 | 246 | ||
247 | -- | Objects which have a p-norm. | ||
248 | -- Using it you can define convenient shortcuts: @norm2 = pnorm PNorm2@, @frobenius = norm2 . flatten@, etc. | ||
247 | class Normed t where | 249 | class Normed t where |
248 | pnorm :: NormType -> t -> Double | 250 | pnorm :: NormType -> t -> Double |
249 | norm :: t -> Double | ||
250 | norm = pnorm PNorm2 | ||
251 | 251 | ||
252 | instance Normed (Vector Double) where | 252 | instance Normed (Vector Double) where |
253 | pnorm = pnormRV | 253 | pnorm = pnormRV |
@@ -356,6 +356,12 @@ uH (pq, tau) = (p,h) | |||
356 | 356 | ||
357 | -------------------------------------------------------------------------- | 357 | -------------------------------------------------------------------------- |
358 | 358 | ||
359 | -- | Reciprocal of the 2-norm condition number of a matrix, computed from the SVD. | ||
360 | rcond :: GenMat t => Matrix t -> Double | ||
361 | rcond m = last s / head s | ||
362 | where (_,s',_) = svd m | ||
363 | s = toList s' | ||
364 | |||
359 | -- | Number of linearly independent rows or columns. | 365 | -- | Number of linearly independent rows or columns. |
360 | rank :: GenMat t => Matrix t -> Int | 366 | rank :: GenMat t => Matrix t -> Int |
361 | rank m | pnorm PNorm1 m < eps = 0 | 367 | rank m | pnorm PNorm1 m < eps = 0 |
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 | ||
17 | module Numeric.LinearAlgebra.Linear ( | 17 | module 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 | -} |
101 | outer :: (Field t) => Vector t -> Vector t -> Matrix t | 101 | outer :: (Field t) => Vector t -> Vector t -> Matrix t |
102 | outer u v = asColumn u `multiply` asRow v | 102 | outer 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 ] | ||
109 | m2=(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 | -} | ||
126 | kronecker :: (Field t) => Matrix t -> Matrix t -> Matrix t | ||
127 | kronecker a b = fromBlocks | ||
128 | . partit (cols a) | ||
129 | . map (reshape (cols b)) | ||
130 | . toRows | ||
131 | $ flatten a `outer` flatten b | ||