diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-10-01 15:04:16 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-10-01 15:04:16 +0000 |
commit | c99b8fd6e3f8a2fb365ec12baf838f864b118ece (patch) | |
tree | 11b5b8515861fe88d547253ae10c2182d5fadaf2 /lib/Numeric/LinearAlgebra/Interface.hs | |
parent | 768f08d4134a066d773d56a9c03ae688e3850352 (diff) |
LinearAlgebra and GSL moved to Numeric
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Interface.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Interface.hs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs new file mode 100644 index 0000000..5bd207a --- /dev/null +++ b/lib/Numeric/LinearAlgebra/Interface.hs | |||
@@ -0,0 +1,106 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : Numeric.LinearAlgebra.Interface | ||
5 | Copyright : (c) Alberto Ruiz 2006 | ||
6 | License : GPL-style | ||
7 | |||
8 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
9 | Stability : provisional | ||
10 | Portability : portable | ||
11 | |||
12 | Operators for frequent operations. | ||
13 | |||
14 | -} | ||
15 | ----------------------------------------------------------------------------- | ||
16 | |||
17 | module Numeric.LinearAlgebra.Interface( | ||
18 | (<>),(<.>), | ||
19 | (<\>), | ||
20 | (.*),(*/), | ||
21 | (<|>),(<->), | ||
22 | ) where | ||
23 | |||
24 | import Numeric.LinearAlgebra.Linear | ||
25 | import Data.Packed.Vector | ||
26 | import Data.Packed.Matrix | ||
27 | import Numeric.LinearAlgebra.Algorithms | ||
28 | |||
29 | class Mul a b c | a b -> c where | ||
30 | infixl 7 <> | ||
31 | -- | matrix product | ||
32 | (<>) :: Field t => a t -> b t -> c t | ||
33 | |||
34 | instance Mul Matrix Matrix Matrix where | ||
35 | (<>) = multiply | ||
36 | |||
37 | instance Mul Matrix Vector Vector where | ||
38 | (<>) m v = flatten $ m <> (asColumn v) | ||
39 | |||
40 | instance Mul Vector Matrix Vector where | ||
41 | (<>) v m = flatten $ (asRow v) <> m | ||
42 | |||
43 | --------------------------------------------------- | ||
44 | |||
45 | -- | @u \<.\> v = dot u v@ | ||
46 | (<.>) :: (Field t) => Vector t -> Vector t -> t | ||
47 | infixl 7 <.> | ||
48 | (<.>) = dot | ||
49 | |||
50 | ---------------------------------------------------- | ||
51 | |||
52 | -- | @x .* a = scale x a@ | ||
53 | (.*) :: (Linear c a) => a -> c a -> c a | ||
54 | infixl 7 .* | ||
55 | a .* x = scale a x | ||
56 | |||
57 | ---------------------------------------------------- | ||
58 | |||
59 | -- | @a *\/ x = scale (recip x) a@ | ||
60 | (*/) :: (Linear c a) => c a -> a -> c a | ||
61 | infixl 7 */ | ||
62 | v */ x = scale (recip x) v | ||
63 | |||
64 | -- | least squares solution of a linear system, similar to the \\ operator of Matlab\/Octave (based on linearSolveSVD). | ||
65 | (<\>) :: (GenMat a) => Matrix a -> Vector a -> Vector a | ||
66 | infixl 7 <\> | ||
67 | m <\> v = flatten (linearSolveSVD m (reshape 1 v)) | ||
68 | |||
69 | ------------------------------------------------ | ||
70 | |||
71 | class Joinable a b where | ||
72 | joinH :: Field t => a t -> b t -> Matrix t | ||
73 | joinV :: Field t => a t -> b t -> Matrix t | ||
74 | |||
75 | instance Joinable Matrix Matrix where | ||
76 | joinH m1 m2 = fromBlocks [[m1,m2]] | ||
77 | joinV m1 m2 = fromBlocks [[m1],[m2]] | ||
78 | |||
79 | instance Joinable Matrix Vector where | ||
80 | joinH m v = joinH m (asColumn v) | ||
81 | joinV m v = joinV m (asRow v) | ||
82 | |||
83 | instance Joinable Vector Matrix where | ||
84 | joinH v m = joinH (asColumn v) m | ||
85 | joinV v m = joinV (asRow v) m | ||
86 | |||
87 | infixl 4 <|> | ||
88 | infixl 3 <-> | ||
89 | |||
90 | {- | Horizontal concatenation of matrices and vectors: | ||
91 | |||
92 | @> (ident 3 \<-\> 3 * ident 3) \<|\> fromList [1..6.0] | ||
93 | (6><4) | ||
94 | [ 1.0, 0.0, 0.0, 1.0 | ||
95 | , 0.0, 1.0, 0.0, 2.0 | ||
96 | , 0.0, 0.0, 1.0, 3.0 | ||
97 | , 3.0, 0.0, 0.0, 4.0 | ||
98 | , 0.0, 3.0, 0.0, 5.0 | ||
99 | , 0.0, 0.0, 3.0, 6.0 ]@ | ||
100 | -} | ||
101 | (<|>) :: (Field t, Joinable a b) => a t -> b t -> Matrix t | ||
102 | a <|> b = joinH a b | ||
103 | |||
104 | -- | Vertical concatenation of matrices and vectors. | ||
105 | (<->) :: (Field t, Joinable a b) => a t -> b t -> Matrix t | ||
106 | a <-> b = joinV a b | ||