summaryrefslogtreecommitdiff
path: root/lib/LinearAlgebra/Interface.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/LinearAlgebra/Interface.hs')
-rw-r--r--lib/LinearAlgebra/Interface.hs99
1 files changed, 99 insertions, 0 deletions
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{- |
4Module : LinearAlgebra.Interface
5Copyright : (c) Alberto Ruiz 2006
6License : GPL-style
7
8Maintainer : Alberto Ruiz (aruiz at um dot es)
9Stability : provisional
10Portability : portable
11
12Operators for frequent operations.
13
14-}
15-----------------------------------------------------------------------------
16
17module LinearAlgebra.Interface(
18 (<>),(<.>),
19 (.*),(*/),
20 (<|>),(<->),
21) where
22
23import LinearAlgebra.Linear
24import Data.Packed.Vector
25import Data.Packed.Matrix
26
27class Mul a b c | a b -> c where
28 infixl 7 <>
29 -- | matrix product
30 (<>) :: Field t => a t -> b t -> c t
31
32instance Mul Matrix Matrix Matrix where
33 (<>) = multiply
34
35instance Mul Matrix Vector Vector where
36 (<>) m v = flatten $ m <> (asColumn v)
37
38instance 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
45infixl 7 <.>
46(<.>) = dot
47
48----------------------------------------------------
49
50-- | @x .* a = scale (recip x) v@
51(.*) :: (Linear c a) => a -> c a -> c a
52infixl 7 .*
53a .* x = scale a x
54
55----------------------------------------------------
56
57-- | @a *\/ x = scale (recip x) a@
58(*/) :: (Linear c a) => c a -> a -> c a
59infixl 7 */
60v */ x = scale (recip x) v
61
62------------------------------------------------
63
64class 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
68instance Joinable Matrix Matrix where
69 joinH m1 m2 = fromBlocks [[m1,m2]]
70 joinV m1 m2 = fromBlocks [[m1],[m2]]
71
72instance Joinable Matrix Vector where
73 joinH m v = joinH m (asColumn v)
74 joinV m v = joinV m (asRow v)
75
76instance Joinable Vector Matrix where
77 joinH v m = joinH (asColumn v) m
78 joinV v m = joinV (asRow v) m
79
80infixl 4 <|>
81infixl 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
95a <|> b = joinH a b
96
97-- | Vertical concatenation of matrices and vectors.
98(<->) :: (Field t, Joinable a b) => a t -> b t -> Matrix t
99a <-> b = joinV a b