]> gitweb.michael.orlitzky.com - numerical-analysis.git/blobdiff - src/Vector.hs
Bump to fixed-vector-0.2.*.
[numerical-analysis.git] / src / Vector.hs
index b42932c22d2058e685e3d6aa9cc2bb25422ca3cc..1aaf78daa42e31ca0eae5e106b52aa3827276882 100644 (file)
@@ -11,17 +11,16 @@ import Data.List (intercalate)
 import Data.Vector.Fixed (
   Dim,
   Fun(..),
-  N1,
   N2,
   N3,
   N4,
   Vector(..),
-  (!),
   construct,
   inspect,
   toList,
   )
 import qualified Data.Vector.Fixed as V (
+  eq,
   foldl,
   length,
   map,
@@ -82,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
@@ -144,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).
   --
@@ -157,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)))
@@ -192,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:
@@ -217,6 +227,20 @@ angle v1 v2 =
 -- constructors, so you can pattern match out the individual
 -- components.
 
+-- | Convenient constructor for 2D vectors.
+--
+--   Examples:
+--
+--   >>> import Roots.Simple
+--   >>> let h = 0.5 :: Double
+--   >>> let g1 (Vn (Vec2D x y)) = 1.0 + h*exp(-(x^2))/(1.0 + y^2)
+--   >>> let g2 (Vn (Vec2D x y)) = 0.5 + h*atan(x^2 + y^2)
+--   >>> let g u = make2d ((g1 u), (g2 u))
+--   >>> let u0 = make2d (1.0, 1.0)
+--   >>> let eps = 1/(10^9)
+--   >>> fixed_point g eps u0
+--   (1.0728549599342185,1.0820591495686167)
+--
 data Vec2D a = Vec2D a a
 type instance Dim Vec2D = N2
 instance Vector Vec2D a where