1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
{-# OPTIONS_GHC -fglasgow-exts #-}
-----------------------------------------------------------------------------
{- |
Module : LinearAlgebra.Interface
Copyright : (c) Alberto Ruiz 2006
License : GPL-style
Maintainer : Alberto Ruiz (aruiz at um dot es)
Stability : provisional
Portability : portable
Operators for frequent operations.
-}
-----------------------------------------------------------------------------
module LinearAlgebra.Interface(
(<>),(<.>),
(.*),(*/),
(<|>),(<->),
) where
import LinearAlgebra.Linear
import Data.Packed.Vector
import Data.Packed.Matrix
class Mul a b c | a b -> c where
infixl 7 <>
-- | matrix product
(<>) :: Field t => a t -> b t -> c t
instance Mul Matrix Matrix Matrix where
(<>) = multiply
instance Mul Matrix Vector Vector where
(<>) m v = flatten $ m <> (asColumn v)
instance Mul Vector Matrix Vector where
(<>) v m = flatten $ (asRow v) <> m
---------------------------------------------------
-- | @u \<.\> v = dot u v@
(<.>) :: (Field t) => Vector t -> Vector t -> t
infixl 7 <.>
(<.>) = dot
----------------------------------------------------
-- | @x .* a = scale x a@
(.*) :: (Linear c a) => a -> c a -> c a
infixl 7 .*
a .* x = scale a x
----------------------------------------------------
-- | @a *\/ x = scale (recip x) a@
(*/) :: (Linear c a) => c a -> a -> c a
infixl 7 */
v */ x = scale (recip x) v
------------------------------------------------
class Joinable a b where
joinH :: Field t => a t -> b t -> Matrix t
joinV :: Field t => a t -> b t -> Matrix t
instance Joinable Matrix Matrix where
joinH m1 m2 = fromBlocks [[m1,m2]]
joinV m1 m2 = fromBlocks [[m1],[m2]]
instance Joinable Matrix Vector where
joinH m v = joinH m (asColumn v)
joinV m v = joinV m (asRow v)
instance Joinable Vector Matrix where
joinH v m = joinH (asColumn v) m
joinV v m = joinV (asRow v) m
infixl 4 <|>
infixl 3 <->
{- | Horizontal concatenation of matrices and vectors:
@> (ident 3 \<-\> 3 * ident 3) \<|\> fromList [1..6.0]
(6><4)
[ 1.0, 0.0, 0.0, 1.0
, 0.0, 1.0, 0.0, 2.0
, 0.0, 0.0, 1.0, 3.0
, 3.0, 0.0, 0.0, 4.0
, 0.0, 3.0, 0.0, 5.0
, 0.0, 0.0, 3.0, 6.0 ]@
-}
(<|>) :: (Field t, Joinable a b) => a t -> b t -> Matrix t
a <|> b = joinH a b
-- | Vertical concatenation of matrices and vectors.
(<->) :: (Field t, Joinable a b) => a t -> b t -> Matrix t
a <-> b = joinV a b
|