From c210311be639e8774dca25a332542d6b64ce3ca3 Mon Sep 17 00:00:00 2001 From: Tracy Wadleigh Date: Fri, 24 Apr 2009 13:58:33 +0000 Subject: More defined list-to-V/M operators: (|>|), (>|<). Provides analogs of the (|>) and (><) operators that first apply an appropriate 'take' to the given lists so that they may be safely used on lists that are too long (or infinite) -- a feature I find particularly useful from the interactive prompt. As these operators are more defined, I would ask the package maintainer to consider, rather than adding (|>|) and (>|<), just updating the definitions of (|>) and (><) with the new semantics. --- lib/Data/Packed/Internal/Vector.hs | 13 +++++++++++++ lib/Data/Packed/Matrix.hs | 21 ++++++++++++++++++++- lib/Data/Packed/Vector.hs | 2 +- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index d410c4d..0e6b3f4 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -96,6 +96,19 @@ toList v = safeRead v $ peekArray (dim v) infixl 9 |> n |> l = if length l == n then fromList l else error "|> with wrong size" +{- | Like '(|>)', but explicitly truncates the list, if it is too long. + +It may safely be used, for instance, with infinite lists. +-} +(|>|) :: (Storable a) => Int -> [a] -> Vector a +infixl 9 |>| +n |>| l = if length l' == n then + fromList l' + else + error "|>|: list too short" + where l' = take n l + + -- | access to Vector elements without range checking at' :: Storable a => Vector a -> Int -> a at' v n = safeRead v $ flip peekElemOff n diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 4fd61a1..5a73c06 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -16,7 +16,7 @@ module Data.Packed.Matrix ( Element, Matrix,rows,cols, - (><), + (><), (>|<), trans, reshape, flatten, fromLists, toLists, @@ -126,6 +126,25 @@ r >< c = f where ++show (dim v) ++" in ("++show r++"><"++show c++")" where v = fromList l +{- | Like '(><)', but explicitly truncates the list, so that it can +safely be used with lists that are too long (like infinite lists). + +Example: + +@\> (2>|<3)[1..] +(2><3) + [ 1.0, 2.0, 3.0 + , 4.0, 5.0, 6.0 ]@ + +Effectively, a more defined version of '(><)'. +-} +(>|<) :: (Element a) => Int -> Int -> [a] -> Matrix a +r >|< c = f where + f l | dim v == r*c = matrixFromVector RowMajor c v + | otherwise = error $ "inconsistent list size = " + ++show (dim v) ++" in ("++show r++"><"++show c++")" + where v = fromList $ take (r*c) l + ---------------------------------------------------------------- -- | Creates a matrix with the first n rows of another matrix diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs index e53d455..ae30c27 100644 --- a/lib/Data/Packed/Vector.hs +++ b/lib/Data/Packed/Vector.hs @@ -14,7 +14,7 @@ module Data.Packed.Vector ( Vector, - fromList, (|>), toList, + fromList, (|>), (|>|), toList, dim, (@>), subVector, join, constant, linspace, -- cgit v1.2.3