summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTracy Wadleigh <tracy.wadleigh@gmail.com>2009-04-24 13:58:33 +0000
committerTracy Wadleigh <tracy.wadleigh@gmail.com>2009-04-24 13:58:33 +0000
commitc210311be639e8774dca25a332542d6b64ce3ca3 (patch)
tree032457af80ff755d97a3e7e1922ceaf5eaf2aa62 /lib
parent3ccfccc82aa1cf374af1b822087fb6c4fd41b3c3 (diff)
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed/Internal/Vector.hs13
-rw-r--r--lib/Data/Packed/Matrix.hs21
-rw-r--r--lib/Data/Packed/Vector.hs2
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)
96infixl 9 |> 96infixl 9 |>
97n |> l = if length l == n then fromList l else error "|> with wrong size" 97n |> l = if length l == n then fromList l else error "|> with wrong size"
98 98
99{- | Like '(|>)', but explicitly truncates the list, if it is too long.
100
101It may safely be used, for instance, with infinite lists.
102-}
103(|>|) :: (Storable a) => Int -> [a] -> Vector a
104infixl 9 |>|
105n |>| l = if length l' == n then
106 fromList l'
107 else
108 error "|>|: list too short"
109 where l' = take n l
110
111
99-- | access to Vector elements without range checking 112-- | access to Vector elements without range checking
100at' :: Storable a => Vector a -> Int -> a 113at' :: Storable a => Vector a -> Int -> a
101at' v n = safeRead v $ flip peekElemOff n 114at' 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 @@
16module Data.Packed.Matrix ( 16module Data.Packed.Matrix (
17 Element, 17 Element,
18 Matrix,rows,cols, 18 Matrix,rows,cols,
19 (><), 19 (><), (>|<),
20 trans, 20 trans,
21 reshape, flatten, 21 reshape, flatten,
22 fromLists, toLists, 22 fromLists, toLists,
@@ -126,6 +126,25 @@ r >< c = f where
126 ++show (dim v) ++" in ("++show r++"><"++show c++")" 126 ++show (dim v) ++" in ("++show r++"><"++show c++")"
127 where v = fromList l 127 where v = fromList l
128 128
129{- | Like '(><)', but explicitly truncates the list, so that it can
130safely be used with lists that are too long (like infinite lists).
131
132Example:
133
134@\> (2>|<3)[1..]
135(2><3)
136 [ 1.0, 2.0, 3.0
137 , 4.0, 5.0, 6.0 ]@
138
139Effectively, a more defined version of '(><)'.
140-}
141(>|<) :: (Element a) => Int -> Int -> [a] -> Matrix a
142r >|< c = f where
143 f l | dim v == r*c = matrixFromVector RowMajor c v
144 | otherwise = error $ "inconsistent list size = "
145 ++show (dim v) ++" in ("++show r++"><"++show c++")"
146 where v = fromList $ take (r*c) l
147
129---------------------------------------------------------------- 148----------------------------------------------------------------
130 149
131-- | Creates a matrix with the first n rows of another matrix 150-- | 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 @@
14 14
15module Data.Packed.Vector ( 15module Data.Packed.Vector (
16 Vector, 16 Vector,
17 fromList, (|>), toList, 17 fromList, (|>), (|>|), toList,
18 dim, (@>), 18 dim, (@>),
19 subVector, join, 19 subVector, join,
20 constant, linspace, 20 constant, linspace,