diff options
Diffstat (limited to 'lib/Numeric/LinearAlgebra/Linear.hs')
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 160 |
1 files changed, 0 insertions, 160 deletions
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs deleted file mode 100644 index 775060e..0000000 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ /dev/null | |||
@@ -1,160 +0,0 @@ | |||
1 | {-# LANGUAGE UndecidableInstances, MultiParamTypeClasses, FlexibleInstances #-} | ||
2 | {-# LANGUAGE FlexibleContexts #-} | ||
3 | {-# LANGUAGE TypeFamilies #-} | ||
4 | ----------------------------------------------------------------------------- | ||
5 | {- | | ||
6 | Module : Numeric.LinearAlgebra.Linear | ||
7 | Copyright : (c) Alberto Ruiz 2006-7 | ||
8 | License : GPL-style | ||
9 | |||
10 | Maintainer : Alberto Ruiz (aruiz at um dot es) | ||
11 | Stability : provisional | ||
12 | Portability : uses ffi | ||
13 | |||
14 | Basic optimized operations on vectors and matrices. | ||
15 | |||
16 | -} | ||
17 | ----------------------------------------------------------------------------- | ||
18 | |||
19 | module Numeric.LinearAlgebra.Linear ( | ||
20 | -- * Linear Algebra Typeclasses | ||
21 | Vectors(..), | ||
22 | -- * Products | ||
23 | Product(..), | ||
24 | mXm,mXv,vXm, | ||
25 | outer, kronecker, | ||
26 | -- * Modules | ||
27 | --module Numeric.Vector, | ||
28 | --module Numeric.Matrix, | ||
29 | module Numeric.Container | ||
30 | ) where | ||
31 | |||
32 | import Data.Packed.Internal.Common | ||
33 | import Data.Packed.Matrix | ||
34 | import Data.Packed.Vector | ||
35 | import Data.Complex | ||
36 | import Numeric.Container | ||
37 | import Numeric.Vector() | ||
38 | import Numeric.Matrix() | ||
39 | import Numeric.GSL.Vector | ||
40 | import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ) | ||
41 | |||
42 | -- | Linear algebraic properties of objects | ||
43 | class Num e => Vectors a e where | ||
44 | -- | dot (inner) product | ||
45 | dot :: a e -> a e -> e | ||
46 | -- | sum of absolute value of elements (differs in complex case from @norm1@ | ||
47 | absSum :: a e -> e | ||
48 | -- | sum of absolute value of elements | ||
49 | norm1 :: a e -> e | ||
50 | -- | euclidean norm | ||
51 | norm2 :: a e -> e | ||
52 | -- | element of maximum magnitude | ||
53 | normInf :: a e -> e | ||
54 | |||
55 | instance Vectors Vector Float where | ||
56 | norm2 = toScalarF Norm2 | ||
57 | absSum = toScalarF AbsSum | ||
58 | dot = dotF | ||
59 | norm1 = toScalarF AbsSum | ||
60 | normInf = maxElement . vectorMapF Abs | ||
61 | |||
62 | instance Vectors Vector Double where | ||
63 | norm2 = toScalarR Norm2 | ||
64 | absSum = toScalarR AbsSum | ||
65 | dot = dotR | ||
66 | norm1 = toScalarR AbsSum | ||
67 | normInf = maxElement . vectorMapR Abs | ||
68 | |||
69 | instance Vectors Vector (Complex Float) where | ||
70 | norm2 = (:+ 0) . toScalarQ Norm2 | ||
71 | absSum = (:+ 0) . toScalarQ AbsSum | ||
72 | dot = dotQ | ||
73 | norm1 = (:+ 0) . sumElements . fst . fromComplex . vectorMapQ Abs | ||
74 | normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapQ Abs | ||
75 | |||
76 | instance Vectors Vector (Complex Double) where | ||
77 | norm2 = (:+ 0) . toScalarC Norm2 | ||
78 | absSum = (:+ 0) . toScalarC AbsSum | ||
79 | dot = dotC | ||
80 | norm1 = (:+ 0) . sumElements . fst . fromComplex . vectorMapC Abs | ||
81 | normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapC Abs | ||
82 | |||
83 | ---------------------------------------------------- | ||
84 | |||
85 | class Element t => Product t where | ||
86 | multiply :: Matrix t -> Matrix t -> Matrix t | ||
87 | ctrans :: Matrix t -> Matrix t | ||
88 | |||
89 | instance Product Double where | ||
90 | multiply = multiplyR | ||
91 | ctrans = trans | ||
92 | |||
93 | instance Product (Complex Double) where | ||
94 | multiply = multiplyC | ||
95 | ctrans = conj . trans | ||
96 | |||
97 | instance Product Float where | ||
98 | multiply = multiplyF | ||
99 | ctrans = trans | ||
100 | |||
101 | instance Product (Complex Float) where | ||
102 | multiply = multiplyQ | ||
103 | ctrans = conj . trans | ||
104 | |||
105 | ---------------------------------------------------------- | ||
106 | |||
107 | -- synonym for matrix product | ||
108 | mXm :: Product t => Matrix t -> Matrix t -> Matrix t | ||
109 | mXm = multiply | ||
110 | |||
111 | -- matrix - vector product | ||
112 | mXv :: Product t => Matrix t -> Vector t -> Vector t | ||
113 | mXv m v = flatten $ m `mXm` (asColumn v) | ||
114 | |||
115 | -- vector - matrix product | ||
116 | vXm :: Product t => Vector t -> Matrix t -> Vector t | ||
117 | vXm v m = flatten $ (asRow v) `mXm` m | ||
118 | |||
119 | {- | Outer product of two vectors. | ||
120 | |||
121 | @\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3] | ||
122 | (3><3) | ||
123 | [ 5.0, 2.0, 3.0 | ||
124 | , 10.0, 4.0, 6.0 | ||
125 | , 15.0, 6.0, 9.0 ]@ | ||
126 | -} | ||
127 | outer :: (Product t) => Vector t -> Vector t -> Matrix t | ||
128 | outer u v = asColumn u `multiply` asRow v | ||
129 | |||
130 | {- | Kronecker product of two matrices. | ||
131 | |||
132 | @m1=(2><3) | ||
133 | [ 1.0, 2.0, 0.0 | ||
134 | , 0.0, -1.0, 3.0 ] | ||
135 | m2=(4><3) | ||
136 | [ 1.0, 2.0, 3.0 | ||
137 | , 4.0, 5.0, 6.0 | ||
138 | , 7.0, 8.0, 9.0 | ||
139 | , 10.0, 11.0, 12.0 ]@ | ||
140 | |||
141 | @\> kronecker m1 m2 | ||
142 | (8><9) | ||
143 | [ 1.0, 2.0, 3.0, 2.0, 4.0, 6.0, 0.0, 0.0, 0.0 | ||
144 | , 4.0, 5.0, 6.0, 8.0, 10.0, 12.0, 0.0, 0.0, 0.0 | ||
145 | , 7.0, 8.0, 9.0, 14.0, 16.0, 18.0, 0.0, 0.0, 0.0 | ||
146 | , 10.0, 11.0, 12.0, 20.0, 22.0, 24.0, 0.0, 0.0, 0.0 | ||
147 | , 0.0, 0.0, 0.0, -1.0, -2.0, -3.0, 3.0, 6.0, 9.0 | ||
148 | , 0.0, 0.0, 0.0, -4.0, -5.0, -6.0, 12.0, 15.0, 18.0 | ||
149 | , 0.0, 0.0, 0.0, -7.0, -8.0, -9.0, 21.0, 24.0, 27.0 | ||
150 | , 0.0, 0.0, 0.0, -10.0, -11.0, -12.0, 30.0, 33.0, 36.0 ]@ | ||
151 | -} | ||
152 | kronecker :: (Product t) => Matrix t -> Matrix t -> Matrix t | ||
153 | kronecker a b = fromBlocks | ||
154 | . splitEvery (cols a) | ||
155 | . map (reshape (cols b)) | ||
156 | . toRows | ||
157 | $ flatten a `outer` flatten b | ||
158 | |||
159 | |||
160 | ------------------------------------------------------------------- | ||