diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Interface.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Interface.hs | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs index 542d76e..ec08694 100644 --- a/lib/Numeric/LinearAlgebra/Interface.hs +++ b/lib/Numeric/LinearAlgebra/Interface.hs | |||
@@ -1,4 +1,5 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | 1 | {-# OPTIONS_GHC -fglasgow-exts #-} |
2 | {-# LANGUAGE UndecidableInstances #-} | ||
2 | ----------------------------------------------------------------------------- | 3 | ----------------------------------------------------------------------------- |
3 | {- | | 4 | {- | |
4 | Module : Numeric.LinearAlgebra.Interface | 5 | Module : Numeric.LinearAlgebra.Interface |
@@ -18,7 +19,7 @@ In the context of the standard numeric operators, one-component vectors and matr | |||
18 | ----------------------------------------------------------------------------- | 19 | ----------------------------------------------------------------------------- |
19 | 20 | ||
20 | module Numeric.LinearAlgebra.Interface( | 21 | module Numeric.LinearAlgebra.Interface( |
21 | (<>),(<.>), | 22 | (<>),(<.>),mulG, Adapt, adaptElements, |
22 | (<\>), | 23 | (<\>), |
23 | (.*),(*/), | 24 | (.*),(*/), |
24 | (<|>),(<->), | 25 | (<|>),(<->), |
@@ -28,22 +29,28 @@ import Data.Packed.Vector | |||
28 | import Data.Packed.Matrix | 29 | import Data.Packed.Matrix |
29 | import Numeric.LinearAlgebra.Algorithms | 30 | import Numeric.LinearAlgebra.Algorithms |
30 | import Numeric.LinearAlgebra.Linear | 31 | import Numeric.LinearAlgebra.Linear |
32 | import Data.Complex | ||
33 | import Control.Arrow((***)) | ||
31 | 34 | ||
32 | --import Numeric.GSL.Vector | 35 | --import Numeric.GSL.Vector |
33 | 36 | ||
34 | class Mul a b c | a b -> c where | 37 | class Mul a b c | a b -> c where |
35 | infixl 7 <> | 38 | infixl 7 <> |
36 | -- | Matrix-matrix, matrix-vector, and vector-matrix products. | 39 | -- | Matrix-matrix, matrix-vector, and vector-matrix products. |
37 | (<>) :: Prod t => a t -> b t -> c t | 40 | (<>) :: Product t => a t -> b t -> c t |
41 | mulG :: (Element r, Element s, Adapt r s t t, Product t) => a r -> b s -> c t | ||
38 | 42 | ||
39 | instance Mul Matrix Matrix Matrix where | 43 | instance Mul Matrix Matrix Matrix where |
40 | (<>) = multiply | 44 | (<>) = mXm |
45 | mulG a b = uncurry mXm (curry adapt a b) | ||
41 | 46 | ||
42 | instance Mul Matrix Vector Vector where | 47 | instance Mul Matrix Vector Vector where |
43 | (<>) m v = flatten $ m <> (asColumn v) | 48 | (<>) m v = flatten $ m <> (asColumn v) |
49 | mulG m v = flatten $ m `mulG` (asColumn v) | ||
44 | 50 | ||
45 | instance Mul Vector Matrix Vector where | 51 | instance Mul Vector Matrix Vector where |
46 | (<>) v m = flatten $ (asRow v) <> m | 52 | (<>) v m = flatten $ (asRow v) <> m |
53 | mulG v m = flatten $ (asRow v) `mulG` m | ||
47 | 54 | ||
48 | --------------------------------------------------- | 55 | --------------------------------------------------- |
49 | 56 | ||
@@ -120,3 +127,45 @@ a <-> b = joinV a b | |||
120 | 127 | ||
121 | ---------------------------------------------------- | 128 | ---------------------------------------------------- |
122 | 129 | ||
130 | class Adapt a b c d | a b -> c, a b -> d where | ||
131 | adapt :: Container k => (k a, k b) -> (k c, k d) | ||
132 | |||
133 | --instance Adapt a a a a where | ||
134 | -- adapt = id *** id | ||
135 | |||
136 | instance Adapt Float Float Float Float where | ||
137 | adapt = id *** id | ||
138 | |||
139 | instance Adapt Double Double Double Double where | ||
140 | adapt = id *** id | ||
141 | |||
142 | instance Adapt (Complex Float) (Complex Float) (Complex Float) (Complex Float) where | ||
143 | adapt = id *** id | ||
144 | |||
145 | instance Adapt Float Double Double Double where | ||
146 | adapt = double *** id | ||
147 | |||
148 | instance Adapt Double Float Double Double where | ||
149 | adapt = id *** double | ||
150 | |||
151 | instance Adapt Float (Complex Float) (Complex Float) (Complex Float) where | ||
152 | adapt = complex *** id | ||
153 | |||
154 | instance Adapt (Complex Float) Float (Complex Float) (Complex Float) where | ||
155 | adapt = id *** complex | ||
156 | |||
157 | instance (Convert a, Convert (DoubleOf a), ComplexOf (DoubleOf a) ~ Complex Double) => Adapt a (Complex Double) (Complex Double) (Complex Double) where | ||
158 | adapt = complex.double *** id | ||
159 | |||
160 | instance (Convert a, Convert (DoubleOf a), ComplexOf (DoubleOf a) ~ Complex Double) => Adapt (Complex Double) a (Complex Double) (Complex Double) where | ||
161 | adapt = id *** complex.double | ||
162 | |||
163 | instance Adapt Double (Complex Float) (Complex Double) (Complex Double) where | ||
164 | adapt = complex *** double | ||
165 | |||
166 | instance Adapt (Complex Float) Double (Complex Double) (Complex Double) where | ||
167 | adapt = double *** complex | ||
168 | |||
169 | adaptElements:: (Adapt a b c d, Container k) => (k a, k b) -> (k c, k d) | ||
170 | adaptElements p = adapt p | ||
171 | |||