summaryrefslogtreecommitdiff
path: root/lib/Numeric/LinearAlgebra/Interface.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Interface.hs')
-rw-r--r--lib/Numeric/LinearAlgebra/Interface.hs106
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{- |
4Module : Numeric.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 Numeric.LinearAlgebra.Interface(
18 (<>),(<.>),
19 (<\>),
20 (.*),(*/),
21 (<|>),(<->),
22) where
23
24import Numeric.LinearAlgebra.Linear
25import Data.Packed.Vector
26import Data.Packed.Matrix
27import Numeric.LinearAlgebra.Algorithms
28
29class Mul a b c | a b -> c where
30 infixl 7 <>
31 -- | matrix product
32 (<>) :: Field t => a t -> b t -> c t
33
34instance Mul Matrix Matrix Matrix where
35 (<>) = multiply
36
37instance Mul Matrix Vector Vector where
38 (<>) m v = flatten $ m <> (asColumn v)
39
40instance 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
47infixl 7 <.>
48(<.>) = dot
49
50----------------------------------------------------
51
52-- | @x .* a = scale x a@
53(.*) :: (Linear c a) => a -> c a -> c a
54infixl 7 .*
55a .* x = scale a x
56
57----------------------------------------------------
58
59-- | @a *\/ x = scale (recip x) a@
60(*/) :: (Linear c a) => c a -> a -> c a
61infixl 7 */
62v */ 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
66infixl 7 <\>
67m <\> v = flatten (linearSolveSVD m (reshape 1 v))
68
69------------------------------------------------
70
71class 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
75instance Joinable Matrix Matrix where
76 joinH m1 m2 = fromBlocks [[m1,m2]]
77 joinV m1 m2 = fromBlocks [[m1],[m2]]
78
79instance Joinable Matrix Vector where
80 joinH m v = joinH m (asColumn v)
81 joinV m v = joinV m (asRow v)
82
83instance Joinable Vector Matrix where
84 joinH v m = joinH (asColumn v) m
85 joinV v m = joinV (asRow v) m
86
87infixl 4 <|>
88infixl 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
102a <|> b = joinH a b
103
104-- | Vertical concatenation of matrices and vectors.
105(<->) :: (Field t, Joinable a b) => a t -> b t -> Matrix t
106a <-> b = joinV a b