)
import qualified Data.Vector.Fixed as V (
and,
- foldl,
fromList,
head,
length,
toList,
zipWith
)
-import Data.Vector.Fixed.Boxed (Vec)
import Data.Vector.Fixed.Cont (Arity, arity)
import Linear.Vector
import Normed
ToRational.C a,
Arity m)
=> Normed (Mat (S m) N1 a) where
- -- | Generic p-norms. The usual norm in R^n is (norm_p 2). We treat
- -- all matrices as big vectors.
+ -- | Generic p-norms for vectors in R^n that are represented as nx1
+ -- matrices.
--
-- Examples:
--
--
frobenius_norm :: (Algebraic.C a, Ring.C a) => Mat m n a -> a
frobenius_norm (Mat rows) =
- sqrt $ vsum $ V.map row_sum rows
+ sqrt $ element_sum $ V.map row_sum rows
where
- -- | The \"sum\" function defined in fixed-vector requires a 'Num'
- -- constraint whereas we want to use the classes from
- -- numeric-prelude.
- vsum = V.foldl (+) (fromInteger 0)
-
-- | Square and add up the entries of a row.
- row_sum = vsum . V.map (^2)
+ row_sum = element_sum . V.map (^2)
-- Vector helpers. We want it to be easy to create low-dimension
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
+{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
-module Linear.Vector
+module Linear.Vector (
+ module Data.Vector.Fixed.Boxed,
+ Vec1,
+ (!?),
+ delete,
+ element_sum )
where
+import qualified Algebra.Additive as Additive ( C )
+import qualified Algebra.Ring as Ring ( C )
import Data.Vector.Fixed (
Dim,
N1,
- N4,
- N5,
S,
Vector(..),
fromList,
- toList,
- )
+ toList )
import qualified Data.Vector.Fixed as V (
(!),
- length,
- )
-import Data.Vector.Fixed.Boxed
+ foldl,
+ length )
+import Data.Vector.Fixed.Boxed (
+ Vec,
+ Vec2,
+ Vec3,
+ Vec4,
+ Vec5 )
+import NumericPrelude hiding ( abs )
type Vec1 = Vec N1
-type Vec4 = Vec N4
-type Vec5 = Vec N5
where
(lhalf, rhalf) = splitAt idx (toList v1)
rhalf' = tail rhalf
+
+
+-- | We provide our own sum because V.sum relies on a Num instance
+-- from the Prelude that we don't have.
+--
+-- Examples:
+--
+-- >>> 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 = V.foldl (+) (fromInteger 0)