From d8dc884aa9b16c7bbd88c4c107ddd684a339a191 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 2 Feb 2014 18:46:05 -0500 Subject: [PATCH] Switch Linear.Vector to the numeric prelude and add the element_sum function to it. Use Linear.Vector.element_sum in Linear.Matrix. --- src/Linear/Matrix.hs | 15 ++++----------- src/Linear/Vector.hs | 43 +++++++++++++++++++++++++++++++++---------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/Linear/Matrix.hs b/src/Linear/Matrix.hs index 66b22f9..69a74b5 100644 --- a/src/Linear/Matrix.hs +++ b/src/Linear/Matrix.hs @@ -35,7 +35,6 @@ import Data.Vector.Fixed ( ) import qualified Data.Vector.Fixed as V ( and, - foldl, fromList, head, length, @@ -45,7 +44,6 @@ import qualified Data.Vector.Fixed as V ( toList, zipWith ) -import Data.Vector.Fixed.Boxed (Vec) import Data.Vector.Fixed.Cont (Arity, arity) import Linear.Vector import Normed @@ -430,8 +428,8 @@ instance (Algebraic.C a, 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: -- @@ -475,15 +473,10 @@ instance (Algebraic.C a, -- 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 diff --git a/src/Linear/Vector.hs b/src/Linear/Vector.hs index e4f6225..9d43bfa 100644 --- a/src/Linear/Vector.hs +++ b/src/Linear/Vector.hs @@ -1,31 +1,40 @@ {-# 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 @@ -66,3 +75,17 @@ delete v1 idx = 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) -- 2.44.2