diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-08 09:46:33 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-08 09:46:33 +0000 |
commit | 34380f2b5d7b048a4d68197f16a8db0e53742030 (patch) | |
tree | 444aff88cda5c247d49bac0d294d8cfb9ef7bf23 /lib/GSL | |
parent | 0c38c1b0e122a56ea98c494e60ba90afe2688664 (diff) |
type classes
Diffstat (limited to 'lib/GSL')
-rw-r--r-- | lib/GSL/Compat.hs | 7 | ||||
-rw-r--r-- | lib/GSL/Vector.hs | 31 |
2 files changed, 5 insertions, 33 deletions
diff --git a/lib/GSL/Compat.hs b/lib/GSL/Compat.hs index 560a7d8..809a1f5 100644 --- a/lib/GSL/Compat.hs +++ b/lib/GSL/Compat.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | 1 | {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-} |
2 | ----------------------------------------------------------------------------- | 2 | ----------------------------------------------------------------------------- |
3 | {- | | 3 | {- | |
4 | Module : GSL.Compat | 4 | Module : GSL.Compat |
@@ -23,6 +23,7 @@ import Data.Packed.Internal hiding (dsp) | |||
23 | import Data.Packed.Vector | 23 | import Data.Packed.Vector |
24 | import Data.Packed.Matrix | 24 | import Data.Packed.Matrix |
25 | import GSL.Vector | 25 | import GSL.Vector |
26 | import LinearAlgebra.Linear | ||
26 | import GSL.Matrix | 27 | import GSL.Matrix |
27 | import LinearAlgebra.Algorithms | 28 | import LinearAlgebra.Algorithms |
28 | import Complex | 29 | import Complex |
@@ -49,7 +50,7 @@ compat' m1 m2 = rows m1 == 1 && cols m1 == 1 | |||
49 | instance (Eq a, Field a) => Eq (Vector a) where | 50 | instance (Eq a, Field a) => Eq (Vector a) where |
50 | a == b = dim a == dim b && toList a == toList b | 51 | a == b = dim a == dim b && toList a == toList b |
51 | 52 | ||
52 | instance (Num a, Field a) => Num (Vector a) where | 53 | instance (Linear Vector a) => Num (Vector a) where |
53 | (+) = adaptScalar addConstant add (flip addConstant) | 54 | (+) = adaptScalar addConstant add (flip addConstant) |
54 | negate = scale (-1) | 55 | negate = scale (-1) |
55 | (*) = adaptScalar scale mul (flip scale) | 56 | (*) = adaptScalar scale mul (flip scale) |
@@ -60,7 +61,7 @@ instance (Num a, Field a) => Num (Vector a) where | |||
60 | instance (Eq a, Field a) => Eq (Matrix a) where | 61 | instance (Eq a, Field a) => Eq (Matrix a) where |
61 | a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b | 62 | a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b |
62 | 63 | ||
63 | instance (Num a, Field a) => Num (Matrix a) where | 64 | instance (Field a, Linear Vector a) => Num (Matrix a) where |
64 | (+) = liftMatrix2' (+) | 65 | (+) = liftMatrix2' (+) |
65 | negate = liftMatrix negate | 66 | negate = liftMatrix negate |
66 | (*) = liftMatrix2' (*) | 67 | (*) = liftMatrix2' (*) |
diff --git a/lib/GSL/Vector.hs b/lib/GSL/Vector.hs index 0b3c3a9..b405ff8 100644 --- a/lib/GSL/Vector.hs +++ b/lib/GSL/Vector.hs | |||
@@ -17,8 +17,7 @@ module GSL.Vector ( | |||
17 | FunCodeS(..), toScalarR, | 17 | FunCodeS(..), toScalarR, |
18 | FunCodeV(..), vectorMapR, vectorMapC, | 18 | FunCodeV(..), vectorMapR, vectorMapC, |
19 | FunCodeSV(..), vectorMapValR, vectorMapValC, | 19 | FunCodeSV(..), vectorMapValR, vectorMapValC, |
20 | FunCodeVV(..), vectorZipR, vectorZipC, | 20 | FunCodeVV(..), vectorZipR, vectorZipC |
21 | scale, addConstant, add, sub, mul, | ||
22 | ) where | 21 | ) where |
23 | 22 | ||
24 | import Data.Packed.Internal.Common | 23 | import Data.Packed.Internal.Common |
@@ -70,34 +69,6 @@ data FunCodeS = Norm2 | |||
70 | | Min | 69 | | Min |
71 | deriving Enum | 70 | deriving Enum |
72 | 71 | ||
73 | |||
74 | scale :: (Num a, Field a) => a -> Vector a -> Vector a | ||
75 | scale x v | isReal baseOf v = scast $ vectorMapValR Scale (scast x) (scast v) | ||
76 | | isComp baseOf v = scast $ vectorMapValC Scale (scast x) (scast v) | ||
77 | | otherwise = fromList $ map (*x) $ toList v | ||
78 | |||
79 | addConstant :: (Num a, Field a) => a -> Vector a -> Vector a | ||
80 | addConstant x v | isReal baseOf v = scast $ vectorMapValR AddConstant (scast x) (scast v) | ||
81 | | isComp baseOf v = scast $ vectorMapValC AddConstant (scast x) (scast v) | ||
82 | | otherwise = fromList $ map (*x) $ toList v | ||
83 | |||
84 | add :: (Num a, Field a) => Vector a -> Vector a -> Vector a | ||
85 | add u v | isReal baseOf v = scast $ vectorZipR Add (scast u) (scast v) | ||
86 | | isComp baseOf v = scast $ vectorZipC Add (scast u) (scast v) | ||
87 | | otherwise = fromList $ zipWith (+) (toList u) (toList v) | ||
88 | |||
89 | sub :: (Num a, Field a) => Vector a -> Vector a -> Vector a | ||
90 | sub u v | isReal baseOf v = scast $ vectorZipR Sub (scast u) (scast v) | ||
91 | | isComp baseOf v = scast $ vectorZipC Sub (scast u) (scast v) | ||
92 | | otherwise = fromList $ zipWith (-) (toList u) (toList v) | ||
93 | |||
94 | mul :: (Num a, Field a) => Vector a -> Vector a -> Vector a | ||
95 | mul u v | isReal baseOf v = scast $ vectorZipR Mul (scast u) (scast v) | ||
96 | | isComp baseOf v = scast $ vectorZipC Mul (scast u) (scast v) | ||
97 | | otherwise = fromList $ zipWith (*) (toList u) (toList v) | ||
98 | |||
99 | |||
100 | |||
101 | ------------------------------------------------------------------ | 72 | ------------------------------------------------------------------ |
102 | 73 | ||
103 | toScalarAux fun code v = unsafePerformIO $ do | 74 | toScalarAux fun code v = unsafePerformIO $ do |