From 1a9c07dd1fffdbd7eb939fa6a781793419947c08 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 19 Oct 2007 09:39:06 +0000 Subject: some refactoring --- lib/Data/Packed/Internal/Matrix.hs | 16 +--------------- lib/Data/Packed/Internal/Vector.hs | 3 --- lib/Data/Packed/Matrix.hs | 17 +++++++++++++++-- lib/Data/Packed/Vector.hs | 2 +- lib/Graphics/Plot.hs | 2 +- lib/Numeric/GSL.hs | 2 +- lib/Numeric/GSL/Vector.hs | 2 +- lib/Numeric/LinearAlgebra/Instances.hs | 23 ++++++++++++++++++++++- lib/Numeric/LinearAlgebra/Interface.hs | 4 ++-- lib/Numeric/LinearAlgebra/Linear.hs | 2 +- 10 files changed, 45 insertions(+), 28 deletions(-) (limited to 'lib') diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index bf7f0ec..77906dc 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -22,10 +22,10 @@ import Data.Packed.Internal.Vector import Foreign hiding (xor) import Complex import Control.Monad(when) -import Data.List(transpose,intersperse) import Data.Maybe(fromJust) import Foreign.C.String import Foreign.C.Types +import Data.List(transpose) ----------------------------------------------------------------- @@ -199,20 +199,6 @@ instance Field (Complex Double) where ------------------------------------------------------------------ -instance (Show a, Field a) => (Show (Matrix a)) where - show m = (sizes++) . dsp . map (map show) . toLists $ m - where sizes = "("++show (rows m)++"><"++show (cols m)++")\n" - -dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unwords' $ transpose mtp - where - mt = transpose as - longs = map (maximum . map length) mt - mtp = zipWith (\a b -> map (pad a) b) longs mt - pad n str = replicate (n - length str) ' ' ++ str - unwords' = concat . intersperse ", " - ------------------------------------------------------------------- - (>|<) :: (Field a) => Int -> Int -> [a] -> Matrix a r >|< c = f where f l | dim v == r*c = matrixFromVector ColumnMajor c v diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 9557206..76bd4d1 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -103,9 +103,6 @@ at :: Storable a => Vector a -> Int -> a at v n | n >= 0 && n < dim v = at' v n | otherwise = error "vector index out of range" -instance (Show a, Storable a) => (Show (Vector a)) where - show v = (show (dim v))++" |> " ++ show (toList v) - {- | takes a number of consecutive elements from a Vector @> subVector 2 3 (fromList [1..10]) diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 2b93348..260c1e0 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -9,7 +9,7 @@ -- Stability : provisional -- Portability : portable -- --- Matrices +-- A Matrix representation suitable for numerical computations using LAPACK and GSL. -- ----------------------------------------------------------------------------- @@ -22,7 +22,7 @@ module Data.Packed.Matrix ( trans, conjTrans, asRow, asColumn, fromRows, toRows, fromColumns, toColumns, - fromBlocks, + fromBlocks, repmat, flipud, fliprl, subMatrix, takeRows, dropRows, takeColumns, dropColumns, extractRows, @@ -215,3 +215,16 @@ readMatrix = fromLists . map (map read). map words . filter (not.null) . lines extractRows :: Field t => [Int] -> Matrix t -> Matrix t extractRows l m = fromRows $ extract (toRows $ m) l where extract l is = [l!!i |i<-is] + +{- | creates matrix by repetition of a matrix a given number of rows and columns + +@> repmat (ident 2) 2 3 :: Matrix Double +(4><6) + [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 + , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 + , 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 + , 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 ]@ + +-} +repmat :: (Field t) => Matrix t -> Int -> Int -> Matrix t +repmat m r c = fromBlocks $ partit c $ replicate (r*c) m \ No newline at end of file diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index c893fe7..4c331ef 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs @@ -8,7 +8,7 @@ -- Stability : provisional -- Portability : portable -- --- Vectors +-- A representation of 1D arrays suitable for numeric computations using external libraries. -- ----------------------------------------------------------------------------- diff --git a/lib/Graphics/Plot.hs b/lib/Graphics/Plot.hs index 71ad0fc..c763294 100644 --- a/lib/Graphics/Plot.hs +++ b/lib/Graphics/Plot.hs @@ -8,7 +8,7 @@ -- Stability : provisional -- Portability : uses gnuplot and ImageMagick -- --- Very basic (and provisional) drawing tools. +-- Very basic (and provisional) drawing tools using gnuplot and imageMagick. -- ----------------------------------------------------------------------------- diff --git a/lib/Numeric/GSL.hs b/lib/Numeric/GSL.hs index 4bc3940..784dfbc 100644 --- a/lib/Numeric/GSL.hs +++ b/lib/Numeric/GSL.hs @@ -8,7 +8,7 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) Stability : provisional Portability : uses -fffi and -fglasgow-exts -This module reexports all the available Numeric.GSL functions (except those in "LinearAlgebra"). +This module reexports all the available GSL functions (except those in "LinearAlgebra"). -} diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs index 41efdc0..d94b377 100644 --- a/lib/Numeric/GSL/Vector.hs +++ b/lib/Numeric/GSL/Vector.hs @@ -9,7 +9,7 @@ -- Stability : provisional -- Portability : portable (uses FFI) -- --- Vector operations +-- Low level interface to vector operations. -- ----------------------------------------------------------------------------- diff --git a/lib/Numeric/LinearAlgebra/Instances.hs b/lib/Numeric/LinearAlgebra/Instances.hs index 388f3da..4ee576f 100644 --- a/lib/Numeric/LinearAlgebra/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Instances.hs @@ -9,7 +9,7 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) Stability : provisional Portability : portable -Numeric instances for Vector and Matrix. +This module exports Show, Eq, Num, Fractional, and Floating instances for Vector and Matrix. In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. @@ -24,6 +24,27 @@ import Numeric.GSL.Vector import Data.Packed.Matrix import Data.Packed.Vector import Complex +import Data.List(transpose,intersperse) +import Foreign(Storable) + +------------------------------------------------------------------ + +instance (Show a, Field a) => (Show (Matrix a)) where + show m = (sizes++) . dsp . map (map show) . toLists $ m + where sizes = "("++show (rows m)++"><"++show (cols m)++")\n" + +dsp as = (++" ]") . (" ["++) . init . drop 2 . unlines . map (" , "++) . map unwords' $ transpose mtp + where + mt = transpose as + longs = map (maximum . map length) mt + mtp = zipWith (\a b -> map (pad a) b) longs mt + pad n str = replicate (n - length str) ' ' ++ str + unwords' = concat . intersperse ", " + +instance (Show a, Storable a) => (Show (Vector a)) where + show v = (show (dim v))++" |> " ++ show (toList v) + +------------------------------------------------------------------ adaptScalar f1 f2 f3 x y | dim x == 1 = f1 (x@>0) y diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs index 5bd207a..fd076ec 100644 --- a/lib/Numeric/LinearAlgebra/Interface.hs +++ b/lib/Numeric/LinearAlgebra/Interface.hs @@ -2,14 +2,14 @@ ----------------------------------------------------------------------------- {- | Module : Numeric.LinearAlgebra.Interface -Copyright : (c) Alberto Ruiz 2006 +Copyright : (c) Alberto Ruiz 2007 License : GPL-style Maintainer : Alberto Ruiz (aruiz at um dot es) Stability : provisional Portability : portable -Operators for frequent operations. +(Very provisional) operators for frequent operations. -} ----------------------------------------------------------------------------- diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index a3a0183..3017936 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs @@ -25,7 +25,7 @@ import Data.Packed import Numeric.GSL.Vector import Complex --- | basic optimized operations +-- | A generic interface for vectors and matrices to a few element-by-element functions in Numeric.GSL.Vector. class (Container c e) => Linear c e where scale :: e -> c e -> c e addConstant :: e -> c e -> c e -- cgit v1.2.3