-- | Convert a matrix to a nested list.
toList :: (V.Vector v (Vn w a), V.Vector w a) => Mat v w a -> [[a]]
-toList m = Prelude.map FV.toList (FV.toList m)
+toList m = Prelude.map V.toList (V.toList m)
-- | Create a matrix from a nested list.
fromList :: (V.Vector v (Vn w a), V.Vector w a) => [[a]] -> Mat v w a
-fromList vs = FV.fromList $ Prelude.map FV.fromList vs
+fromList vs = V.fromList $ Prelude.map V.fromList vs
-- | Unsafe indexing.
(!) :: (V.Vector v (Vn w a), V.Vector w a) => Mat v w a -> (Int, Int) -> a
-(!) m (i, j) = (row m i) FV.! j
+(!) m (i, j) = (row m i) V.! j
-- | Safe indexing.
(!?) :: (V.Vector v (Vn w a), V.Vector w a) => Mat v w a
-> Maybe a
(!?) m (i, j)
| i < 0 || j < 0 = Nothing
- | i > FV.length m = Nothing
- | otherwise = if j > FV.length (row m j)
+ | i > V.length m = Nothing
+ | otherwise = if j > V.length (row m j)
then Nothing
- else Just $ (row m j) FV.! j
+ else Just $ (row m j) V.! j
--- | The number of rows in the matrix. Implementation stolen from
--- Data.Vector.Fixed.Length.
+-- | The number of rows in the matrix.
nrows :: forall v w a. (V.Vector v (Vn w a), V.Vector w a) => Mat v w a -> Int
-nrows _ = arity (undefined :: V.Dim v)
+nrows = V.length
-- | The number of columns in the first row of the
--- matrix. Implementation stolen from
+-- matrix. Implementation stolen from Data.Vector.Fixed.length.
ncols :: forall v w a. (V.Vector v (Vn w a), V.Vector w a) => Mat v w a -> Int
ncols _ = arity (undefined :: V.Dim w)
row :: (V.Vector v (Vn w a), V.Vector w a) => Mat v w a
-> Int
-> Vn w a
-row m i = m FV.! i
+row m i = m V.! i
-- | Return the @j@th column of @m@. Unsafe.
-> Int
-> Vn v a
column m j =
- FV.map (element j) m
+ V.map (element j) m
where
- element = flip (FV.!)
+ element = flip (V.!)
-- | Transose @m@; switch it's columns and its rows. This is a dirty
V.Vector w a)
=> Mat v w a
-> Mat w v a
-transpose m = FV.fromList column_list
+transpose m = V.fromList column_list
where
column_list = [ column m i | i <- [0..(ncols m)-1] ]
construct lambda = rows
where
-- The arity trick is used in Data.Vector.Fixed.length.
- imax = (arity (undefined :: V.Dim w)) - 1
+ imax = (arity (undefined :: V.Dim v)) - 1
jmax = (arity (undefined :: V.Dim w)) - 1
- row' i = FV.fromList [ lambda i j | j <- [0..jmax] ]
- rows = FV.fromList [ row' i | i <- [0..imax] ]
+ row' i = V.fromList [ lambda i j | j <- [0..jmax] ]
+ rows = V.fromList [ row' i | i <- [0..imax] ]
norms = (norm v1) * (norm v2)
--- | The length of a vector.
---
--- Examples:
---
--- >>> let v1 = make2d (1,2)
--- >>> length v1
--- 2
---
-length :: (V.Vector v a) => Vn v a -> Int
-length (Vn v1) = V.length v1
-
-
--- | Unsafe indexing.
---
--- Examples:
---
--- >>> let v1 = make3d (1,2,3)
--- >>> v1 ! 2
--- 3
--- >>> v1 ! 3
--- *** Exception: Data.Vector.Fixed.!: index out of range
---
-(!) :: (V.Vector v a) => Vn v a -> Int -> a
-(!) (Vn v1) idx = v1 V.! idx
-
-
-- | Safe indexing.
--
-- Examples:
-- >>> v1 !? 3
-- Nothing
--
-(!?) :: (V.Vector v a) => Vn v a -> Int -> Maybe a
-(!?) v1@(Vn v2) idx
- | idx < 0 || idx >= V.length v2 = Nothing
- | otherwise = Just $ v1 ! idx
-
+(!?) :: (V.Vector v a) => v a -> Int -> Maybe a
+(!?) v1 idx
+ | idx < 0 || idx >= V.length v1 = Nothing
+ | otherwise = Just $ v1 V.! idx
--- | Convert vector to a list.
---
--- Examples:
---
--- >>> let v1 = make2d (1,2)
--- >>> toList v1
--- [1,2]
---
-toList :: (V.Vector v a) => Vn v a -> [a]
-toList (Vn v1) = V.toList v1
-
-
--- | Convert a list to a vector.
---
--- Examples:
---
--- >>> fromList [1,2] :: Vn Vec2D Int
--- (1,2)
---
-fromList :: (V.Vector v a) => [a] -> Vn v a
-fromList xs = Vn $ V.fromList xs
-
--- | Map a function over a vector.
---
--- Examples:
---
--- >>> let v1 = make2d (1,2)
--- >>> map (*2) v1
--- (2,4)
---
-map :: (V.Vector v a, V.Vector v b) => (a -> b) -> Vn v a -> Vn v b
-map f (Vn vs) = Vn $ V.map f vs