diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-07-12 22:23:14 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-07-12 22:23:14 +0000 |
commit | 7659d9c67f75e1f665d6b02663ee8767e97762b4 (patch) | |
tree | cddaaf3156cadef9aaf564abf9d033b72f6e2e67 /lib | |
parent | 007960d648485d5c161cd366d16282bc29fac68b (diff) |
refactored Data.Packed.Vector to remove any numerics
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 17 | ||||
-rw-r--r-- | lib/Data/Packed/Random.hs | 1 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 50 | ||||
-rw-r--r-- | lib/Graphics/Plot.hs | 6 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra.hs | 4 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 4 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Interface.hs | 2 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 33 |
8 files changed, 64 insertions, 53 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 438dabc..d5a287d 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -30,7 +30,7 @@ module Data.Packed.Matrix ( | |||
30 | extractRows, | 30 | extractRows, |
31 | ident, diag, diagRect, takeDiag, | 31 | ident, diag, diagRect, takeDiag, |
32 | liftMatrix, liftMatrix2, liftMatrix2Auto, | 32 | liftMatrix, liftMatrix2, liftMatrix2Auto, |
33 | dispf, disps, dispcf, latexFormat, format, | 33 | dispf, disps, dispcf, vecdisp, latexFormat, format, |
34 | loadMatrix, saveMatrix, fromFile, fileDimensions, | 34 | loadMatrix, saveMatrix, fromFile, fileDimensions, |
35 | readMatrix, fromArray2D | 35 | readMatrix, fromArray2D |
36 | ) where | 36 | ) where |
@@ -314,6 +314,21 @@ formatScaled dec t = "E"++show o++"\n" ++ ss | |||
314 | o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t | 314 | o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t |
315 | fmt = '%':show (dec+3) ++ '.':show dec ++"f" | 315 | fmt = '%':show (dec+3) ++ '.':show dec ++"f" |
316 | 316 | ||
317 | {- | Show a vector using a function for showing matrices. | ||
318 | |||
319 | @disp = putStr . vecdisp ('dispf' 2) | ||
320 | |||
321 | \> disp ('linspace' 10 (0,1)) | ||
322 | 10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 | ||
323 | @ | ||
324 | -} | ||
325 | vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String | ||
326 | vecdisp f v | ||
327 | = ((show (dim v) ++ " |> ") ++) . (++"\n") | ||
328 | . unwords . lines . tail . dropWhile (not . (`elem` " \n")) | ||
329 | . f . trans . reshape 1 | ||
330 | $ v | ||
331 | |||
317 | -- | Tool to display matrices with latex syntax. | 332 | -- | Tool to display matrices with latex syntax. |
318 | latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc. | 333 | latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc. |
319 | -> String -- ^ Formatted matrix, with elements separated by spaces and newlines | 334 | -> String -- ^ Formatted matrix, with elements separated by spaces and newlines |
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs index e57ba6e..3b02225 100644 --- a/lib/Data/Packed/Random.hs +++ b/lib/Data/Packed/Random.hs | |||
@@ -24,6 +24,7 @@ import Data.Packed.Matrix | |||
24 | import Data.Packed.Vector | 24 | import Data.Packed.Vector |
25 | import Numeric.LinearAlgebra.Algorithms | 25 | import Numeric.LinearAlgebra.Algorithms |
26 | import Numeric.LinearAlgebra.Interface | 26 | import Numeric.LinearAlgebra.Interface |
27 | import Numeric.LinearAlgebra.Linear | ||
27 | 28 | ||
28 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate | 29 | -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate |
29 | -- Gaussian distribution. | 30 | -- Gaussian distribution. |
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index 40dd6b5..81dfa37 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs | |||
@@ -18,9 +18,11 @@ module Data.Packed.Vector ( | |||
18 | fromList, (|>), toList, buildVector, | 18 | fromList, (|>), toList, buildVector, |
19 | dim, (@>), | 19 | dim, (@>), |
20 | subVector, takesV, join, | 20 | subVector, takesV, join, |
21 | constant, linspace, | 21 | -- moved to Numeric.LinearAlgebra.Linear |
22 | vecdisp, | 22 | -- constant, linspace, |
23 | -- moved to Numeric.LinearAlgebra.Interface typeclass | 23 | -- moved to Data.Packed.Matrix |
24 | -- vecdisp, | ||
25 | -- moved to Numeric.LinearAlgebra.Linear typeclass | ||
24 | -- vectorFMax, vectorFMin, vectorFMaxIndex, vectorFMinIndex, | 26 | -- vectorFMax, vectorFMin, vectorFMaxIndex, vectorFMinIndex, |
25 | -- vectorMax, vectorMin, | 27 | -- vectorMax, vectorMin, |
26 | vectorMaxIndex, vectorMinIndex, | 28 | vectorMaxIndex, vectorMinIndex, |
@@ -30,10 +32,9 @@ module Data.Packed.Vector ( | |||
30 | foldLoop, foldVector, foldVectorG | 32 | foldLoop, foldVector, foldVectorG |
31 | ) where | 33 | ) where |
32 | 34 | ||
33 | import Data.Packed.Internal | 35 | import Data.Packed.Internal.Vector |
34 | import Numeric.GSL.Vector | 36 | import Numeric.GSL.Vector |
35 | -- import Data.Packed.ST | 37 | -- import Data.Packed.ST |
36 | import Numeric.LinearAlgebra.Linear | ||
37 | 38 | ||
38 | import Data.Binary | 39 | import Data.Binary |
39 | import Foreign.Storable | 40 | import Foreign.Storable |
@@ -72,19 +73,6 @@ instance (Binary a, Storable a) => Binary (Vector a) where | |||
72 | ------------------------------------------------------------------- | 73 | ------------------------------------------------------------------- |
73 | 74 | ||
74 | 75 | ||
75 | {- | Creates a real vector containing a range of values: | ||
76 | |||
77 | @\> linspace 5 (-3,7) | ||
78 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | ||
79 | |||
80 | Logarithmic spacing can be defined as follows: | ||
81 | |||
82 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
83 | -} | ||
84 | linspace :: (Enum e, Linear Vector e, Element e) => Int -> (e, e) -> Vector e | ||
85 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | ||
86 | where s = (b-a)/fromIntegral (n-1) | ||
87 | |||
88 | {- | 76 | {- |
89 | vectorFMax :: Vector Float -> Float | 77 | vectorFMax :: Vector Float -> Float |
90 | vectorFMax = toScalarF Max | 78 | vectorFMax = toScalarF Max |
@@ -114,15 +102,6 @@ vectorMinIndex :: Vector Double -> Int | |||
114 | vectorMinIndex = round . toScalarR MinIdx | 102 | vectorMinIndex = round . toScalarR MinIdx |
115 | 103 | ||
116 | 104 | ||
117 | {- | creates a vector with a given number of equal components: | ||
118 | |||
119 | @> constant 2 7 | ||
120 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ | ||
121 | -} | ||
122 | constant :: Element a => a -> Int -> Vector a | ||
123 | -- constant x n = runSTVector (newVector x n) | ||
124 | constant = constantD -- about 2x faster | ||
125 | |||
126 | {- | creates a Vector of the specified length using the supplied function to | 105 | {- | creates a Vector of the specified length using the supplied function to |
127 | to map the index to the value at that index. | 106 | to map the index to the value at that index. |
128 | 107 | ||
@@ -130,26 +109,11 @@ constant = constantD -- about 2x faster | |||
130 | 4 |> [0.0,1.0,2.0,3.0]@ | 109 | 4 |> [0.0,1.0,2.0,3.0]@ |
131 | 110 | ||
132 | -} | 111 | -} |
133 | buildVector :: Element a => Int -> (Int -> a) -> Vector a | 112 | buildVector :: Storable a => Int -> (Int -> a) -> Vector a |
134 | buildVector len f = | 113 | buildVector len f = |
135 | fromList $ map f [0 .. (len - 1)] | 114 | fromList $ map f [0 .. (len - 1)] |
136 | 115 | ||
137 | 116 | ||
138 | {- | Show a vector using a function for showing matrices. | ||
139 | |||
140 | @disp = putStr . vecdisp ('dispf' 2) | ||
141 | |||
142 | \> disp ('linspace' 10 (0,1)) | ||
143 | 10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 | ||
144 | @ | ||
145 | -} | ||
146 | vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String | ||
147 | vecdisp f v | ||
148 | = ((show (dim v) ++ " |> ") ++) . (++"\n") | ||
149 | . unwords . lines . tail . dropWhile (not . (`elem` " \n")) | ||
150 | . f . trans . reshape 1 | ||
151 | $ v | ||
152 | |||
153 | -- | unzip for Vectors | 117 | -- | unzip for Vectors |
154 | unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) | 118 | unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) |
155 | unzipVector = unzipVectorWith id | 119 | unzipVector = unzipVectorWith id |
diff --git a/lib/Graphics/Plot.hs b/lib/Graphics/Plot.hs index 02b0c4c..b2acc15 100644 --- a/lib/Graphics/Plot.hs +++ b/lib/Graphics/Plot.hs | |||
@@ -30,7 +30,7 @@ module Graphics.Plot( | |||
30 | 30 | ||
31 | import Data.Packed | 31 | import Data.Packed |
32 | import Numeric.LinearAlgebra(outer) | 32 | import Numeric.LinearAlgebra(outer) |
33 | import Numeric.LinearAlgebra.Linear(Vectors(..)) | 33 | import Numeric.LinearAlgebra.Linear |
34 | import Data.List(intersperse) | 34 | import Data.List(intersperse) |
35 | import System.Process (system) | 35 | import System.Process (system) |
36 | 36 | ||
@@ -153,10 +153,10 @@ matrixToPGM m = header ++ unlines (map unwords ll) where | |||
153 | maxgray = 255.0 | 153 | maxgray = 255.0 |
154 | maxval = vectorMax $ flatten $ m | 154 | maxval = vectorMax $ flatten $ m |
155 | minval = vectorMin $ flatten $ m | 155 | minval = vectorMin $ flatten $ m |
156 | scale = if (maxval == minval) | 156 | scale' = if (maxval == minval) |
157 | then 0.0 | 157 | then 0.0 |
158 | else maxgray / (maxval - minval) | 158 | else maxgray / (maxval - minval) |
159 | f x = show ( round ( scale *(x - minval) ) :: Int ) | 159 | f x = show ( round ( scale' *(x - minval) ) :: Int ) |
160 | ll = map (map f) (toLists m) | 160 | ll = map (map f) (toLists m) |
161 | 161 | ||
162 | -- | imshow shows a representation of a matrix as a gray level image using ImageMagick's display. | 162 | -- | imshow shows a representation of a matrix as a gray level image using ImageMagick's display. |
diff --git a/lib/Numeric/LinearAlgebra.hs b/lib/Numeric/LinearAlgebra.hs index 7664a9f..e8a14d6 100644 --- a/lib/Numeric/LinearAlgebra.hs +++ b/lib/Numeric/LinearAlgebra.hs | |||
@@ -15,9 +15,11 @@ This module reexports all normally required functions for Linear Algebra applica | |||
15 | module Numeric.LinearAlgebra ( | 15 | module Numeric.LinearAlgebra ( |
16 | module Data.Packed, | 16 | module Data.Packed, |
17 | module Numeric.LinearAlgebra.Algorithms, | 17 | module Numeric.LinearAlgebra.Algorithms, |
18 | module Numeric.LinearAlgebra.Interface | 18 | module Numeric.LinearAlgebra.Interface, |
19 | module Numeric.LinearAlgebra.Linear | ||
19 | ) where | 20 | ) where |
20 | 21 | ||
21 | import Data.Packed | 22 | import Data.Packed |
22 | import Numeric.LinearAlgebra.Algorithms | 23 | import Numeric.LinearAlgebra.Algorithms |
23 | import Numeric.LinearAlgebra.Interface | 24 | import Numeric.LinearAlgebra.Interface |
25 | import Numeric.LinearAlgebra.Linear | ||
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 5191480..1109296 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs | |||
@@ -21,7 +21,6 @@ imported from "Numeric.LinearAlgebra.LAPACK". | |||
21 | module Numeric.LinearAlgebra.Algorithms ( | 21 | module Numeric.LinearAlgebra.Algorithms ( |
22 | -- * Supported types | 22 | -- * Supported types |
23 | Field(), | 23 | Field(), |
24 | Vectors(..), | ||
25 | -- * Products | 24 | -- * Products |
26 | multiply, -- dot, moved dot to typeclass | 25 | multiply, -- dot, moved dot to typeclass |
27 | outer, kronecker, | 26 | outer, kronecker, |
@@ -67,7 +66,6 @@ module Numeric.LinearAlgebra.Algorithms ( | |||
67 | -- * Misc | 66 | -- * Misc |
68 | ctrans, | 67 | ctrans, |
69 | eps, i, | 68 | eps, i, |
70 | Linear(..), | ||
71 | -- * Util | 69 | -- * Util |
72 | haussholder, | 70 | haussholder, |
73 | unpackQR, unpackHess, | 71 | unpackQR, unpackHess, |
@@ -78,7 +76,7 @@ module Numeric.LinearAlgebra.Algorithms ( | |||
78 | 76 | ||
79 | 77 | ||
80 | import Data.Packed.Internal hiding ((//)) | 78 | import Data.Packed.Internal hiding ((//)) |
81 | import Data.Packed.Vector | 79 | --import Data.Packed.Vector |
82 | import Data.Packed.Matrix | 80 | import Data.Packed.Matrix |
83 | import Data.Complex | 81 | import Data.Complex |
84 | import Numeric.GSL.Vector | 82 | import Numeric.GSL.Vector |
diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs index 8d2b52a..f8917a0 100644 --- a/lib/Numeric/LinearAlgebra/Interface.hs +++ b/lib/Numeric/LinearAlgebra/Interface.hs | |||
@@ -28,7 +28,7 @@ import Numeric.LinearAlgebra.Instances() | |||
28 | import Data.Packed.Vector | 28 | import Data.Packed.Vector |
29 | import Data.Packed.Matrix | 29 | import Data.Packed.Matrix |
30 | import Numeric.LinearAlgebra.Algorithms | 30 | import Numeric.LinearAlgebra.Algorithms |
31 | import Numeric.LinearAlgebra.Linear() | 31 | import Numeric.LinearAlgebra.Linear |
32 | 32 | ||
33 | --import Numeric.GSL.Vector | 33 | --import Numeric.GSL.Vector |
34 | 34 | ||
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 8922e51..9a7e65f 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -16,11 +16,15 @@ Basic optimized operations on vectors and matrices. | |||
16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
17 | 17 | ||
18 | module Numeric.LinearAlgebra.Linear ( | 18 | module Numeric.LinearAlgebra.Linear ( |
19 | -- * Linear Algebra Typeclasses | ||
19 | Vectors(..), | 20 | Vectors(..), |
20 | Linear(..) | 21 | Linear(..), |
22 | -- * Creation of numeric vectors | ||
23 | constant, linspace | ||
21 | ) where | 24 | ) where |
22 | 25 | ||
23 | import Data.Packed.Internal.Vector | 26 | import Data.Packed.Internal.Vector |
27 | import Data.Packed.Internal.Matrix | ||
24 | import Data.Packed.Matrix | 28 | import Data.Packed.Matrix |
25 | import Data.Complex | 29 | import Data.Complex |
26 | import Numeric.GSL.Vector | 30 | import Numeric.GSL.Vector |
@@ -29,6 +33,7 @@ import Control.Monad(ap) | |||
29 | 33 | ||
30 | -- | basic Vector functions | 34 | -- | basic Vector functions |
31 | class Num e => Vectors a e where | 35 | class Num e => Vectors a e where |
36 | -- the C functions sumX are twice as fast as using foldVector | ||
32 | vectorSum :: a e -> e | 37 | vectorSum :: a e -> e |
33 | euclidean :: a e -> e | 38 | euclidean :: a e -> e |
34 | absSum :: a e -> e | 39 | absSum :: a e -> e |
@@ -153,3 +158,29 @@ instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where | |||
153 | divide = liftMatrix2 divide | 158 | divide = liftMatrix2 divide |
154 | equal a b = cols a == cols b && flatten a `equal` flatten b | 159 | equal a b = cols a == cols b && flatten a `equal` flatten b |
155 | scalar x = (1><1) [x] | 160 | scalar x = (1><1) [x] |
161 | |||
162 | |||
163 | ---------------------------------------------------- | ||
164 | |||
165 | {- | creates a vector with a given number of equal components: | ||
166 | |||
167 | @> constant 2 7 | ||
168 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ | ||
169 | -} | ||
170 | constant :: Element a => a -> Int -> Vector a | ||
171 | -- constant x n = runSTVector (newVector x n) | ||
172 | constant = constantD -- about 2x faster | ||
173 | |||
174 | {- | Creates a real vector containing a range of values: | ||
175 | |||
176 | @\> linspace 5 (-3,7) | ||
177 | 5 |> [-3.0,-0.5,2.0,4.5,7.0]@ | ||
178 | |||
179 | Logarithmic spacing can be defined as follows: | ||
180 | |||
181 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | ||
182 | -} | ||
183 | linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e | ||
184 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | ||
185 | where s = (b-a)/fromIntegral (n-1) | ||
186 | |||