diff options
Diffstat (limited to 'lib/LinearAlgebra')
-rw-r--r-- | lib/LinearAlgebra/Algorithms.hs | 2 | ||||
-rw-r--r-- | lib/LinearAlgebra/Interface.hs | 99 | ||||
-rw-r--r-- | lib/LinearAlgebra/Linear.hs | 20 |
3 files changed, 101 insertions, 20 deletions
diff --git a/lib/LinearAlgebra/Algorithms.hs b/lib/LinearAlgebra/Algorithms.hs index 192c0d6..3112ce6 100644 --- a/lib/LinearAlgebra/Algorithms.hs +++ b/lib/LinearAlgebra/Algorithms.hs | |||
@@ -14,7 +14,7 @@ Portability : uses ffi | |||
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | 15 | ||
16 | module LinearAlgebra.Algorithms ( | 16 | module LinearAlgebra.Algorithms ( |
17 | mXv, vXm, | 17 | -- mXv, vXm, |
18 | inv, | 18 | inv, |
19 | pinv, | 19 | pinv, |
20 | pinvTol, | 20 | pinvTol, |
diff --git a/lib/LinearAlgebra/Interface.hs b/lib/LinearAlgebra/Interface.hs new file mode 100644 index 0000000..7d6ff0f --- /dev/null +++ b/lib/LinearAlgebra/Interface.hs | |||
@@ -0,0 +1,99 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | ||
3 | {- | | ||
4 | Module : 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 LinearAlgebra.Interface( | ||
18 | (<>),(<.>), | ||
19 | (.*),(*/), | ||
20 | (<|>),(<->), | ||
21 | ) where | ||
22 | |||
23 | import LinearAlgebra.Linear | ||
24 | import Data.Packed.Vector | ||
25 | import Data.Packed.Matrix | ||
26 | |||
27 | class Mul a b c | a b -> c where | ||
28 | infixl 7 <> | ||
29 | -- | matrix product | ||
30 | (<>) :: Field t => a t -> b t -> c t | ||
31 | |||
32 | instance Mul Matrix Matrix Matrix where | ||
33 | (<>) = multiply | ||
34 | |||
35 | instance Mul Matrix Vector Vector where | ||
36 | (<>) m v = flatten $ m <> (asColumn v) | ||
37 | |||
38 | instance Mul Vector Matrix Vector where | ||
39 | (<>) v m = flatten $ (asRow v) <> m | ||
40 | |||
41 | --------------------------------------------------- | ||
42 | |||
43 | -- | @u \<.\> v = dot u v@ | ||
44 | (<.>) :: (Field t) => Vector t -> Vector t -> t | ||
45 | infixl 7 <.> | ||
46 | (<.>) = dot | ||
47 | |||
48 | ---------------------------------------------------- | ||
49 | |||
50 | -- | @x .* a = scale (recip x) v@ | ||
51 | (.*) :: (Linear c a) => a -> c a -> c a | ||
52 | infixl 7 .* | ||
53 | a .* x = scale a x | ||
54 | |||
55 | ---------------------------------------------------- | ||
56 | |||
57 | -- | @a *\/ x = scale (recip x) a@ | ||
58 | (*/) :: (Linear c a) => c a -> a -> c a | ||
59 | infixl 7 */ | ||
60 | v */ x = scale (recip x) v | ||
61 | |||
62 | ------------------------------------------------ | ||
63 | |||
64 | class Joinable a b where | ||
65 | joinH :: Field t => a t -> b t -> Matrix t | ||
66 | joinV :: Field t => a t -> b t -> Matrix t | ||
67 | |||
68 | instance Joinable Matrix Matrix where | ||
69 | joinH m1 m2 = fromBlocks [[m1,m2]] | ||
70 | joinV m1 m2 = fromBlocks [[m1],[m2]] | ||
71 | |||
72 | instance Joinable Matrix Vector where | ||
73 | joinH m v = joinH m (asColumn v) | ||
74 | joinV m v = joinV m (asRow v) | ||
75 | |||
76 | instance Joinable Vector Matrix where | ||
77 | joinH v m = joinH (asColumn v) m | ||
78 | joinV v m = joinV (asRow v) m | ||
79 | |||
80 | infixl 4 <|> | ||
81 | infixl 3 <-> | ||
82 | |||
83 | {- | Horizontal concatenation of matrices and vectors: | ||
84 | |||
85 | @> (ident 3 -&- 3 * ident 3) |&| fromList [1..6.0] | ||
86 | (6><4) | ||
87 | [ 1.0, 0.0, 0.0, 1.0 | ||
88 | , 0.0, 1.0, 0.0, 2.0 | ||
89 | , 0.0, 0.0, 1.0, 3.0 | ||
90 | , 3.0, 0.0, 0.0, 4.0 | ||
91 | , 0.0, 3.0, 0.0, 5.0 | ||
92 | , 0.0, 0.0, 3.0, 6.0 ]@ | ||
93 | -} | ||
94 | (<|>) :: (Field t, Joinable a b) => a t -> b t -> Matrix t | ||
95 | a <|> b = joinH a b | ||
96 | |||
97 | -- | Vertical concatenation of matrices and vectors. | ||
98 | (<->) :: (Field t, Joinable a b) => a t -> b t -> Matrix t | ||
99 | a <-> b = joinV a b | ||
diff --git a/lib/LinearAlgebra/Linear.hs b/lib/LinearAlgebra/Linear.hs index 148bbd3..c12e30b 100644 --- a/lib/LinearAlgebra/Linear.hs +++ b/lib/LinearAlgebra/Linear.hs | |||
@@ -15,8 +15,7 @@ Portability : uses ffi | |||
15 | 15 | ||
16 | module LinearAlgebra.Linear ( | 16 | module LinearAlgebra.Linear ( |
17 | Linear(..), | 17 | Linear(..), |
18 | dot, outer, | 18 | multiply, dot, outer |
19 | Mul(..) | ||
20 | ) where | 19 | ) where |
21 | 20 | ||
22 | 21 | ||
@@ -95,7 +94,6 @@ instance Linear Matrix (Complex Double) where | |||
95 | 94 | ||
96 | -------------------------------------------------- | 95 | -------------------------------------------------- |
97 | 96 | ||
98 | |||
99 | -- | euclidean inner product | 97 | -- | euclidean inner product |
100 | dot :: (Field t) => Vector t -> Vector t -> t | 98 | dot :: (Field t) => Vector t -> Vector t -> t |
101 | dot u v = dat (multiply r c) `at` 0 | 99 | dot u v = dat (multiply r c) `at` 0 |
@@ -113,19 +111,3 @@ dot u v = dat (multiply r c) `at` 0 | |||
113 | -} | 111 | -} |
114 | outer :: (Field t) => Vector t -> Vector t -> Matrix t | 112 | outer :: (Field t) => Vector t -> Vector t -> Matrix t |
115 | outer u v = asColumn u `multiply` asRow v | 113 | outer u v = asColumn u `multiply` asRow v |
116 | |||
117 | |||
118 | class Mul a b c | a b -> c where | ||
119 | infixl 7 <> | ||
120 | -- | matrix product | ||
121 | (<>) :: Field t => a t -> b t -> c t | ||
122 | |||
123 | instance Mul Matrix Matrix Matrix where | ||
124 | (<>) = multiply | ||
125 | |||
126 | instance Mul Matrix Vector Vector where | ||
127 | (<>) m v = flatten $ m <> (asColumn v) | ||
128 | |||
129 | instance Mul Vector Matrix Vector where | ||
130 | (<>) v m = flatten $ (asRow v) <> m | ||
131 | |||