From 7659d9c67f75e1f665d6b02663ee8767e97762b4 Mon Sep 17 00:00:00 2001 From: Vivian McPhail Date: Mon, 12 Jul 2010 22:23:14 +0000 Subject: refactored Data.Packed.Vector to remove any numerics --- hmatrix.cabal | 2 +- lib/Data/Packed/Matrix.hs | 17 ++++++++++- lib/Data/Packed/Random.hs | 1 + lib/Data/Packed/Vector.hs | 50 +++++---------------------------- lib/Graphics/Plot.hs | 6 ++-- lib/Numeric/LinearAlgebra.hs | 4 ++- lib/Numeric/LinearAlgebra/Algorithms.hs | 4 +-- lib/Numeric/LinearAlgebra/Interface.hs | 2 +- lib/Numeric/LinearAlgebra/Linear.hs | 33 +++++++++++++++++++++- 9 files changed, 65 insertions(+), 54 deletions(-) diff --git a/hmatrix.cabal b/hmatrix.cabal index 009219c..04c3581 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal @@ -97,6 +97,7 @@ library Numeric.LinearAlgebra, Numeric.LinearAlgebra.LAPACK, Numeric.LinearAlgebra.Interface, + Numeric.LinearAlgebra.Linear, Numeric.LinearAlgebra.Algorithms, Graphics.Plot, -- Data.Packed.Convert, @@ -108,7 +109,6 @@ library Data.Packed.Internal.Signatures, Data.Packed.Internal.Vector, Data.Packed.Internal.Matrix, - Numeric.LinearAlgebra.Linear, Numeric.LinearAlgebra.Instances, Numeric.GSL.Internal 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 ( extractRows, ident, diag, diagRect, takeDiag, liftMatrix, liftMatrix2, liftMatrix2Auto, - dispf, disps, dispcf, latexFormat, format, + dispf, disps, dispcf, vecdisp, latexFormat, format, loadMatrix, saveMatrix, fromFile, fileDimensions, readMatrix, fromArray2D ) where @@ -314,6 +314,21 @@ formatScaled dec t = "E"++show o++"\n" ++ ss o = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t fmt = '%':show (dec+3) ++ '.':show dec ++"f" +{- | Show a vector using a function for showing matrices. + +@disp = putStr . vecdisp ('dispf' 2) + +\> disp ('linspace' 10 (0,1)) +10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 +@ +-} +vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String +vecdisp f v + = ((show (dim v) ++ " |> ") ++) . (++"\n") + . unwords . lines . tail . dropWhile (not . (`elem` " \n")) + . f . trans . reshape 1 + $ v + -- | Tool to display matrices with latex syntax. latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc. -> 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 import Data.Packed.Vector import Numeric.LinearAlgebra.Algorithms import Numeric.LinearAlgebra.Interface +import Numeric.LinearAlgebra.Linear -- | Obtains a matrix whose rows are pseudorandom samples from a multivariate -- 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 ( fromList, (|>), toList, buildVector, dim, (@>), subVector, takesV, join, - constant, linspace, - vecdisp, --- moved to Numeric.LinearAlgebra.Interface typeclass +-- moved to Numeric.LinearAlgebra.Linear +-- constant, linspace, +-- moved to Data.Packed.Matrix +-- vecdisp, +-- moved to Numeric.LinearAlgebra.Linear typeclass -- vectorFMax, vectorFMin, vectorFMaxIndex, vectorFMinIndex, -- vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, @@ -30,10 +32,9 @@ module Data.Packed.Vector ( foldLoop, foldVector, foldVectorG ) where -import Data.Packed.Internal +import Data.Packed.Internal.Vector import Numeric.GSL.Vector -- import Data.Packed.ST -import Numeric.LinearAlgebra.Linear import Data.Binary import Foreign.Storable @@ -72,19 +73,6 @@ instance (Binary a, Storable a) => Binary (Vector a) where ------------------------------------------------------------------- -{- | Creates a real vector containing a range of values: - -@\> linspace 5 (-3,7) -5 |> [-3.0,-0.5,2.0,4.5,7.0]@ - -Logarithmic spacing can be defined as follows: - -@logspace n (a,b) = 10 ** linspace n (a,b)@ --} -linspace :: (Enum e, Linear Vector e, Element e) => Int -> (e, e) -> Vector e -linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] - where s = (b-a)/fromIntegral (n-1) - {- vectorFMax :: Vector Float -> Float vectorFMax = toScalarF Max @@ -114,15 +102,6 @@ vectorMinIndex :: Vector Double -> Int vectorMinIndex = round . toScalarR MinIdx -{- | creates a vector with a given number of equal components: - -@> constant 2 7 -7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ --} -constant :: Element a => a -> Int -> Vector a --- constant x n = runSTVector (newVector x n) -constant = constantD -- about 2x faster - {- | creates a Vector of the specified length using the supplied function to to map the index to the value at that index. @@ -130,26 +109,11 @@ constant = constantD -- about 2x faster 4 |> [0.0,1.0,2.0,3.0]@ -} -buildVector :: Element a => Int -> (Int -> a) -> Vector a +buildVector :: Storable a => Int -> (Int -> a) -> Vector a buildVector len f = fromList $ map f [0 .. (len - 1)] -{- | Show a vector using a function for showing matrices. - -@disp = putStr . vecdisp ('dispf' 2) - -\> disp ('linspace' 10 (0,1)) -10 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00 -@ --} -vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String -vecdisp f v - = ((show (dim v) ++ " |> ") ++) . (++"\n") - . unwords . lines . tail . dropWhile (not . (`elem` " \n")) - . f . trans . reshape 1 - $ v - -- | unzip for Vectors unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) 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( import Data.Packed import Numeric.LinearAlgebra(outer) -import Numeric.LinearAlgebra.Linear(Vectors(..)) +import Numeric.LinearAlgebra.Linear import Data.List(intersperse) import System.Process (system) @@ -153,10 +153,10 @@ matrixToPGM m = header ++ unlines (map unwords ll) where maxgray = 255.0 maxval = vectorMax $ flatten $ m minval = vectorMin $ flatten $ m - scale = if (maxval == minval) + scale' = if (maxval == minval) then 0.0 else maxgray / (maxval - minval) - f x = show ( round ( scale *(x - minval) ) :: Int ) + f x = show ( round ( scale' *(x - minval) ) :: Int ) ll = map (map f) (toLists m) -- | 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 module Numeric.LinearAlgebra ( module Data.Packed, module Numeric.LinearAlgebra.Algorithms, - module Numeric.LinearAlgebra.Interface + module Numeric.LinearAlgebra.Interface, + module Numeric.LinearAlgebra.Linear ) where import Data.Packed import Numeric.LinearAlgebra.Algorithms import Numeric.LinearAlgebra.Interface +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". module Numeric.LinearAlgebra.Algorithms ( -- * Supported types Field(), - Vectors(..), -- * Products multiply, -- dot, moved dot to typeclass outer, kronecker, @@ -67,7 +66,6 @@ module Numeric.LinearAlgebra.Algorithms ( -- * Misc ctrans, eps, i, - Linear(..), -- * Util haussholder, unpackQR, unpackHess, @@ -78,7 +76,7 @@ module Numeric.LinearAlgebra.Algorithms ( import Data.Packed.Internal hiding ((//)) -import Data.Packed.Vector +--import Data.Packed.Vector import Data.Packed.Matrix import Data.Complex 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() import Data.Packed.Vector import Data.Packed.Matrix import Numeric.LinearAlgebra.Algorithms -import Numeric.LinearAlgebra.Linear() +import Numeric.LinearAlgebra.Linear --import Numeric.GSL.Vector 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. ----------------------------------------------------------------------------- module Numeric.LinearAlgebra.Linear ( + -- * Linear Algebra Typeclasses Vectors(..), - Linear(..) + Linear(..), + -- * Creation of numeric vectors + constant, linspace ) where import Data.Packed.Internal.Vector +import Data.Packed.Internal.Matrix import Data.Packed.Matrix import Data.Complex import Numeric.GSL.Vector @@ -29,6 +33,7 @@ import Control.Monad(ap) -- | basic Vector functions class Num e => Vectors a e where + -- the C functions sumX are twice as fast as using foldVector vectorSum :: a e -> e euclidean :: a e -> e absSum :: a e -> e @@ -153,3 +158,29 @@ instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where divide = liftMatrix2 divide equal a b = cols a == cols b && flatten a `equal` flatten b scalar x = (1><1) [x] + + +---------------------------------------------------- + +{- | creates a vector with a given number of equal components: + +@> constant 2 7 +7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ +-} +constant :: Element a => a -> Int -> Vector a +-- constant x n = runSTVector (newVector x n) +constant = constantD -- about 2x faster + +{- | Creates a real vector containing a range of values: + +@\> linspace 5 (-3,7) +5 |> [-3.0,-0.5,2.0,4.5,7.0]@ + +Logarithmic spacing can be defined as follows: + +@logspace n (a,b) = 10 ** linspace n (a,b)@ +-} +linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e +linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] + where s = (b-a)/fromIntegral (n-1) + -- cgit v1.2.3