X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FVector.hs;h=1aaf78daa42e31ca0eae5e106b52aa3827276882;hb=b5bc0c0bc71915093d0883e2fd923808ada4db6c;hp=f55808b33f534283cf7ba432d667c72a6e565353;hpb=6dbbf1130a3318a1cc408f948ac15e74090710ec;p=numerical-analysis.git diff --git a/src/Vector.hs b/src/Vector.hs index f55808b..1aaf78d 100644 --- a/src/Vector.hs +++ b/src/Vector.hs @@ -15,12 +15,12 @@ import Data.Vector.Fixed ( N3, N4, Vector(..), - (!), construct, inspect, toList, ) import qualified Data.Vector.Fixed as V ( + eq, foldl, length, map, @@ -81,8 +81,8 @@ instance (Show a, Vector v a) => Show (Vn v a) where -- >>> 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 @@ -143,7 +143,7 @@ instance (RealFloat a, Ord a, Vector v a) => Normed (Vn v a) where -- >>> 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). -- @@ -156,7 +156,7 @@ instance (RealFloat a, Ord a, Vector v a) => Normed (Vn v a) where -- 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))) @@ -191,6 +191,17 @@ angle v1 v2 = 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: