X-Git-Url: http://gitweb.michael.orlitzky.com/?p=numerical-analysis.git;a=blobdiff_plain;f=src%2FLinear%2FVector.hs;h=c9307153cc65330b2e0ec8e4f5eeec85d933607b;hp=9774dcd40c8132413ddd541a2a65be8976a462e1;hb=261aa714471648c0bcbc603117a420a1fc617ba3;hpb=303c5e7bba583f08e59bc6c848be8e75c1155a3b diff --git a/src/Linear/Vector.hs b/src/Linear/Vector.hs index 9774dcd..c930715 100644 --- a/src/Linear/Vector.hs +++ b/src/Linear/Vector.hs @@ -1,80 +1,53 @@ {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE NoMonomorphismRestriction #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} -module Linear.Vector +module Linear.Vector ( + module Data.Vector.Fixed.Boxed, + Vec0, + Vec1, + (!?), + delete, + element_sum ) where -import Data.List (intercalate) +import qualified Algebra.Additive as Additive ( C ) +import qualified Algebra.Ring as Ring ( C ) import Data.Vector.Fixed ( Dim, - Fun(..), N1, - N2, - N3, - N4, + S, Vector(..), - construct, - inspect, - toList, - ) -import qualified Data.Vector.Fixed as V ( - length, - ) - -import Normed - - --- * Low-dimension vector wrappers. --- --- These wrappers are instances of 'Vector', so they inherit all of --- the userful instances defined above. But, they use fixed --- constructors, so you can pattern match out the individual --- components. - -data D1 a = D1 a -type instance Dim D1 = N1 -instance Vector D1 a where - inspect (D1 x) (Fun f) = f x - construct = Fun D1 - -data D2 a = D2 a a -type instance Dim D2 = N2 -instance Vector D2 a where - inspect (D2 x y) (Fun f) = f x y - construct = Fun D2 - -data D3 a = D3 a a a -type instance Dim D3 = N3 -instance Vector D3 a where - inspect (D3 x y z) (Fun f) = f x y z - construct = Fun D3 + Z, + fromList, + toList ) +import Data.Vector.Fixed ( + (!), + foldl, + length ) +import Data.Vector.Fixed.Boxed ( + Vec, + Vec2, + Vec3, + Vec4, + Vec5 ) +import NumericPrelude hiding ( abs, length, foldl ) -data D4 a = D4 a a a a -type instance Dim D4 = N4 -instance Vector D4 a where - inspect (D4 w x y z) (Fun f) = f w x y z - construct = Fun D4 +type Vec0 = Vec Z +type Vec1 = Vec N1 --- | Unsafe indexing. --- --- Examples: --- --- >>> let v1 = Vec2D 1 2 --- >>> v1 ! 1 --- 2 --- -(!) :: (Vector v a) => v a -> Int -> a -(!) v1 idx = (toList v1) !! idx -- | Safe indexing. -- -- Examples: -- --- >>> let v1 = Vec3D 1 2 3 +-- >>> import Data.Vector.Fixed (mk3) +-- >>> let v1 = mk3 1 2 3 :: Vec3 Int -- >>> v1 !? 2 -- Just 3 -- >>> v1 !? 3 @@ -82,56 +55,41 @@ instance Vector D4 a where -- (!?) :: (Vector v a) => v a -> Int -> Maybe a (!?) v1 idx - | idx < 0 || idx >= V.length v1 = Nothing - | otherwise = Just $ v1 ! idx + | idx < 0 || idx >= length v1 = Nothing + | otherwise = Just $ v1 ! idx - - ---instance (RealFloat a, Ord a, Vector v a) => Normed (Vn v a) where - -- | The infinity norm. We don't use V.maximum here because it - -- relies on a type constraint that the vector be non-empty and I - -- don't know how to pattern match it away. - -- - -- Examples: - -- - -- >>> let v1 = make3d (1,5,2) - -- >>> norm_infty v1 - -- 5 - -- --- norm_infty (Vn v1) = realToFrac $ V.foldl max 0 v1 - - -- | Generic p-norms. The usual norm in R^n is (norm_p 2). - -- - -- Examples: - -- - -- >>> let v1 = make2d (3,4) - -- >>> norm_p 1 v1 - -- 7.0 - -- >>> norm_p 2 v1 - -- 5.0 - -- --- norm_p p (Vn v1) = --- realToFrac $ root $ V.sum $ V.map (exponentiate . abs) v1 --- where --- exponentiate = (** (fromIntegral p)) --- root = (** (recip (fromIntegral p))) - - - - - --- | Convenient constructor for 2D vectors. +-- | Remove an element of the given vector. +-- +-- Examples: +-- +-- >>> import Data.Vector.Fixed (mk3) +-- >>> let b = mk3 1 2 3 :: Vec3 Int +-- >>> delete b 1 :: Vec2 Int +-- fromList [1,3] +-- +delete :: (Vector v a, + Vector w a, + Dim v ~ S (Dim w)) + => v a + -> Int + -> w a +delete v1 idx = + fromList (lhalf ++ rhalf') + where + (lhalf, rhalf) = splitAt idx (toList v1) + rhalf' = tail rhalf + + +-- | We provide our own sum because sum relies on a Num instance +-- from the Prelude that we don't have. -- -- 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) +-- >>> import Data.Vector.Fixed (mk3) +-- >>> let b = mk3 1 2 3 :: Vec3 Int +-- >>> element_sum b +-- 6 -- +element_sum :: (Additive.C a, Ring.C a, Vector v a) => v a -> a +element_sum = foldl (+) (fromInteger 0)