N3,
N4,
Vector(..),
- (!),
construct,
inspect,
toList,
)
import qualified Data.Vector.Fixed as V (
+ eq,
foldl,
length,
map,
-- >>> v1 == v3
-- False
--
-instance (Eq a, Vector v a, Vector v Bool) => Eq (Vn v a) where
- (Vn v1) == (Vn v2) = V.foldl (&&) True (V.zipWith (==) v1 v2)
+instance (Eq a, Vector v a) => Eq (Vn v a) where
+ (Vn v1) == (Vn v2) = v1 `V.eq` v2
-- | The use of 'Num' here is of course incorrect (otherwise, we
-- >>> norm_infty v1
-- 5
--
- norm_infty (Vn v1) = fromRational $ toRational $ V.foldl max 0 v1
+ norm_infty (Vn v1) = realToFrac $ V.foldl max 0 v1
-- | Generic p-norms. The usual norm in R^n is (norm_p 2).
--
-- 5.0
--
norm_p p (Vn v1) =
- fromRational $ toRational $ root $ V.sum $ V.map (exponentiate . abs) v1
+ realToFrac $ root $ V.sum $ V.map (exponentiate . abs) v1
where
exponentiate = (** (fromIntegral p))
root = (** (recip (fromIntegral p)))
norms = (norm v1) * (norm v2)
+-- | Unsafe indexing.
+--
+-- Examples:
+--
+-- >>> let v1 = make2d (1,2)
+-- >>> v1 ! 1
+-- 2
+--
+(!) :: (Vector v a) => v a -> Int -> a
+(!) v1 idx = (toList v1) !! idx
+
-- | Safe indexing.
--
-- Examples: