summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/LinearAlgebra/Instances.hs22
-rw-r--r--lib/Numeric/LinearAlgebra/Tests/Instances.hs6
2 files changed, 18 insertions, 10 deletions
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
83instance (Eq a, Element a) => Eq (Vector a) where 83instance (Eq a, Element a) => Eq (Vector a) where
84 a == b = dim a == dim b && toList a == toList b 84 a == b = dim a == dim b && toList a == toList b
85 85
86instance (Linear Vector a) => Num (Vector a) where 86instance Num (Vector Double) where
87 (+) = adaptScalar addConstant add (flip addConstant) 87 (+) = adaptScalar addConstant add (flip addConstant)
88 negate = scale (-1) 88 negate = scale (-1)
89 (*) = adaptScalar scale mul (flip scale) 89 (*) = adaptScalar scale mul (flip scale)
90 signum = liftVector signum 90 signum = vectorMapR Sign
91 abs = liftVector abs 91 abs = vectorMapR Abs
92 fromInteger = fromList . return . fromInteger
93
94instance Num (Vector (Complex Double)) where
95 (+) = adaptScalar addConstant add (flip addConstant)
96 negate = scale (-1)
97 (*) = adaptScalar scale mul (flip scale)
98 signum = vectorMapC Sign
99 abs = vectorMapC Abs
92 fromInteger = fromList . return . fromInteger 100 fromInteger = fromList . return . fromInteger
93 101
94instance (Eq a, Element a) => Eq (Matrix a) where 102instance (Eq a, Element a) => Eq (Matrix a) where
95 a == b = cols a == cols b && flatten a == flatten b 103 a == b = cols a == cols b && flatten a == flatten b
96 104
97instance (Linear Vector a) => Num (Matrix a) where 105instance (Linear Matrix a, Num (Vector a)) => Num (Matrix a) where
98 (+) = liftMatrix2' (+) 106 (+) = liftMatrix2' (+)
99 (-) = liftMatrix2' (-) 107 (-) = liftMatrix2' (-)
100 negate = liftMatrix negate 108 negate = liftMatrix negate
@@ -105,7 +113,7 @@ instance (Linear Vector a) => Num (Matrix a) where
105 113
106--------------------------------------------------- 114---------------------------------------------------
107 115
108instance (Linear Vector a) => Fractional (Vector a) where 116instance (Linear Vector a, Num (Vector a)) => Fractional (Vector a) where
109 fromRational n = fromList [fromRational n] 117 fromRational n = fromList [fromRational n]
110 (/) = adaptScalar f divide g where 118 (/) = adaptScalar f divide g where
111 r `f` v = scaleRecip r v 119 r `f` v = scaleRecip r v
@@ -113,7 +121,7 @@ instance (Linear Vector a) => Fractional (Vector a) where
113 121
114------------------------------------------------------- 122-------------------------------------------------------
115 123
116instance (Linear Vector a, Fractional (Vector a)) => Fractional (Matrix a) where 124instance (Linear Vector a, Fractional (Vector a), Num (Matrix a)) => Fractional (Matrix a) where
117 fromRational n = (1><1) [fromRational n] 125 fromRational n = (1><1) [fromRational n]
118 (/) = liftMatrix2' (/) 126 (/) = liftMatrix2' (/)
119 127
@@ -161,7 +169,7 @@ instance Floating (Vector (Complex Double)) where
161 169
162----------------------------------------------------------- 170-----------------------------------------------------------
163 171
164instance (Linear Vector a, Floating (Vector a)) => Floating (Matrix a) where 172instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floating (Matrix a) where
165 sin = liftMatrix sin 173 sin = liftMatrix sin
166 cos = liftMatrix cos 174 cos = liftMatrix cos
167 tan = liftMatrix tan 175 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 @@
1{-# OPTIONS #-} 1{-# LANGUAGE FlexibleContexts, UndecidableInstances #-}
2----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
3{- | 3{- |
4Module : Numeric.LinearAlgebra.Tests.Instances 4Module : Numeric.LinearAlgebra.Tests.Instances
@@ -71,7 +71,7 @@ instance (Field a, Arbitrary a) => Arbitrary (Rot a) where
71 71
72-- a complex hermitian or real symmetric matrix 72-- a complex hermitian or real symmetric matrix
73newtype (Her a) = Her (Matrix a) deriving Show 73newtype (Her a) = Her (Matrix a) deriving Show
74instance (Field a, Arbitrary a) => Arbitrary (Her a) where 74instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (Her a) where
75 arbitrary = do 75 arbitrary = do
76 Sq m <- arbitrary 76 Sq m <- arbitrary
77 let m' = m/2 77 let m' = m/2
@@ -106,7 +106,7 @@ instance (Field a, Arbitrary a) => Arbitrary (SqWC a) where
106 106
107-- a positive definite square matrix (the eigenvalues are between 0 and 100) 107-- a positive definite square matrix (the eigenvalues are between 0 and 100)
108newtype (PosDef a) = PosDef (Matrix a) deriving Show 108newtype (PosDef a) = PosDef (Matrix a) deriving Show
109instance (Field a, Arbitrary a) => Arbitrary (PosDef a) where 109instance (Field a, Arbitrary a, Num (Vector a)) => Arbitrary (PosDef a) where
110 arbitrary = do 110 arbitrary = do
111 Her m <- arbitrary 111 Her m <- arbitrary
112 let (_,v) = eigSH m 112 let (_,v) = eigSH m