diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 481d72a..1651247 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# LANGUAGE UndecidableInstances, MultiParamTypeClasses, FlexibleInstances #-} | 1 | {-# LANGUAGE UndecidableInstances, MultiParamTypeClasses, FlexibleInstances #-} |
2 | {-# LANGUAGE FlexibleContexts #-} | ||
2 | ----------------------------------------------------------------------------- | 3 | ----------------------------------------------------------------------------- |
3 | {- | | 4 | {- | |
4 | Module : Numeric.LinearAlgebra.Linear | 5 | Module : Numeric.LinearAlgebra.Linear |
@@ -15,6 +16,7 @@ Basic optimized operations on vectors and matrices. | |||
15 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
16 | 17 | ||
17 | module Numeric.LinearAlgebra.Linear ( | 18 | module Numeric.LinearAlgebra.Linear ( |
19 | Vectors(..), normalise, | ||
18 | Linear(..) | 20 | Linear(..) |
19 | ) where | 21 | ) where |
20 | 22 | ||
@@ -23,6 +25,64 @@ import Data.Packed.Matrix | |||
23 | import Data.Complex | 25 | import Data.Complex |
24 | import Numeric.GSL.Vector | 26 | import Numeric.GSL.Vector |
25 | 27 | ||
28 | -- | normalise a vector to unit length | ||
29 | normalise :: (Floating a, Vectors Vector a, | ||
30 | Linear Vector a, Fractional (Vector a)) => Vector a -> Vector a | ||
31 | normalise v = scaleRecip (vectorSum v) v | ||
32 | |||
33 | -- | basic Vector functions | ||
34 | class (Num b) => Vectors a b where | ||
35 | vectorSum :: a b -> b | ||
36 | euclidean :: a b -> b | ||
37 | absSum :: a b -> b | ||
38 | vectorMin :: a b -> b | ||
39 | vectorMax :: a b -> b | ||
40 | minIdx :: a b -> Int | ||
41 | maxIdx :: a b -> Int | ||
42 | dot :: a b -> a b -> b | ||
43 | |||
44 | instance Vectors Vector Float where | ||
45 | vectorSum = sumF | ||
46 | euclidean = toScalarF Norm2 | ||
47 | absSum = toScalarF AbsSum | ||
48 | vectorMin = toScalarF Min | ||
49 | vectorMax = toScalarF Max | ||
50 | minIdx = round . toScalarF MinIdx | ||
51 | maxIdx = round . toScalarF MaxIdx | ||
52 | dot = dotF | ||
53 | |||
54 | instance Vectors Vector Double where | ||
55 | vectorSum = sumR | ||
56 | euclidean = toScalarR Norm2 | ||
57 | absSum = toScalarR AbsSum | ||
58 | vectorMin = toScalarR Min | ||
59 | vectorMax = toScalarR Max | ||
60 | minIdx = round . toScalarR MinIdx | ||
61 | maxIdx = round . toScalarR MaxIdx | ||
62 | dot = dotR | ||
63 | |||
64 | instance Vectors Vector (Complex Float) where | ||
65 | vectorSum = sumQ | ||
66 | euclidean = undefined | ||
67 | absSum = undefined | ||
68 | vectorMin = undefined | ||
69 | vectorMax = undefined | ||
70 | minIdx = undefined | ||
71 | maxIdx = undefined | ||
72 | dot = dotQ | ||
73 | |||
74 | instance Vectors Vector (Complex Double) where | ||
75 | vectorSum = sumC | ||
76 | euclidean = undefined | ||
77 | absSum = undefined | ||
78 | vectorMin = undefined | ||
79 | vectorMax = undefined | ||
80 | minIdx = undefined | ||
81 | maxIdx = undefined | ||
82 | dot = dotC | ||
83 | |||
84 | ---------------------------------------------------- | ||
85 | |||
26 | -- | Basic element-by-element functions. | 86 | -- | Basic element-by-element functions. |
27 | class (Container c e) => Linear c e where | 87 | class (Container c e) => Linear c e where |
28 | -- | create a structure with a single element | 88 | -- | create a structure with a single element |
@@ -50,7 +110,7 @@ instance Linear Vector Float where | |||
50 | sub = vectorZipF Sub | 110 | sub = vectorZipF Sub |
51 | mul = vectorZipF Mul | 111 | mul = vectorZipF Mul |
52 | divide = vectorZipF Div | 112 | divide = vectorZipF Div |
53 | equal u v = dim u == dim v && vectorFMax (vectorMapF Abs (sub u v)) == 0.0 | 113 | equal u v = dim u == dim v && vectorMax (vectorMapF Abs (sub u v)) == 0.0 |
54 | scalar x = fromList [x] | 114 | scalar x = fromList [x] |
55 | 115 | ||
56 | instance Linear Vector Double where | 116 | instance Linear Vector Double where |