From 212ba96a26b6590868d9724560cef8b2471697f5 Mon Sep 17 00:00:00 2001 From: Dylan Simon Date: Fri, 17 Oct 2008 16:56:02 +0000 Subject: fast signum/abs Vector instance --- lib/Numeric/LinearAlgebra/Instances.hs | 22 +++++++++++++++------- lib/Numeric/LinearAlgebra/Tests/Instances.hs | 6 +++--- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'lib/Numeric/LinearAlgebra') diff --git a/lib/Numeric/LinearAlgebra/Instances.hs b/lib/Numeric/LinearAlgebra/Instances.hs index 147537d..334ccff 100644 --- a/lib/Numeric/LinearAlgebra/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Instances.hs @@ -83,18 +83,26 @@ compat' m1 m2 = rows m1 == 1 && cols m1 == 1 instance (Eq a, Element a) => Eq (Vector a) where a == b = dim a == dim b && toList a == toList b -instance (Linear Vector a) => Num (Vector a) where +instance Num (Vector Double) where (+) = adaptScalar addConstant add (flip addConstant) negate = scale (-1) (*) = adaptScalar scale mul (flip scale) - signum = liftVector signum - abs = liftVector abs + signum = vectorMapR Sign + abs = vectorMapR Abs + fromInteger = fromList . return . fromInteger + +instance Num (Vector (Complex Double)) where + (+) = adaptScalar addConstant add (flip addConstant) + negate = scale (-1) + (*) = adaptScalar scale mul (flip scale) + signum = vectorMapC Sign + abs = vectorMapC Abs fromInteger = fromList . return . fromInteger instance (Eq a, Element a) => Eq (Matrix a) where a == b = cols a == cols b && flatten a == flatten b -instance (Linear Vector a) => Num (Matrix a) where +instance (Linear Matrix a, Num (Vector a)) => Num (Matrix a) where (+) = liftMatrix2' (+) (-) = liftMatrix2' (-) negate = liftMatrix negate @@ -105,7 +113,7 @@ instance (Linear Vector a) => Num (Matrix a) where --------------------------------------------------- -instance (Linear Vector a) => Fractional (Vector a) where +instance (Linear Vector a, Num (Vector a)) => Fractional (Vector a) where fromRational n = fromList [fromRational n] (/) = adaptScalar f divide g where r `f` v = scaleRecip r v @@ -113,7 +121,7 @@ instance (Linear Vector a) => Fractional (Vector a) where ------------------------------------------------------- -instance (Linear Vector a, Fractional (Vector a)) => Fractional (Matrix a) where +instance (Linear Vector a, Fractional (Vector a), Num (Matrix a)) => Fractional (Matrix a) where fromRational n = (1><1) [fromRational n] (/) = liftMatrix2' (/) @@ -161,7 +169,7 @@ instance Floating (Vector (Complex Double)) where ----------------------------------------------------------- -instance (Linear Vector a, Floating (Vector a)) => Floating (Matrix a) where +instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floating (Matrix a) where sin = liftMatrix sin cos = liftMatrix cos tan = liftMatrix tan diff --git a/lib/Numeric/LinearAlgebra/Tests/Instances.hs b/lib/Numeric/LinearAlgebra/Tests/Instances.hs index e7fecf2..677ad2b 100644 --- a/lib/Numeric/LinearAlgebra/Tests/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Tests/Instances.hs @@ -1,4 +1,4 @@ -{-# OPTIONS #-} +{-# LANGUAGE FlexibleContexts, UndecidableInstances #-} ----------------------------------------------------------------------------- {- | Module : Numeric.LinearAlgebra.Tests.Instances @@ -71,7 +71,7 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where -- a complex hermitian or real symmetric matrix newtype (Her a) = Her (Matrix a) deriving Show -instance (Field a, Arbitrary a) => Arbitrary (Her a) where +instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where arbitrary = do Sq m <- arbitrary let m' = m/2 @@ -106,7 +106,7 @@ instance (Field a, Arbitrary a) => Arbitrary (SqWC a) where -- a positive definite square matrix (the eigenvalues are between 0 and 100) newtype (PosDef a) = PosDef (Matrix a) deriving Show -instance (Field a, Arbitrary a) => Arbitrary (PosDef a) where +instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (PosDef a) where arbitrary = do Her m <- arbitrary let (_,v) = eigSH m -- cgit v1.2.3