summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-09-06 07:37:18 +0000
committerAlberto Ruiz <aruiz@um.es>2010-09-06 07:37:18 +0000
commit29099e3bfb4eec87ac3d4d675d7cfc82234c20d6 (patch)
tree82124829d5e2a51e1b272430cee2617b7e182e0e /lib
parentfa4e2233a873bbfee26939c013b56acc160bca7b (diff)
working on conversion / linear
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed/Matrix.hs2
-rw-r--r--lib/Data/Packed/Random.hs3
-rw-r--r--lib/Graphics/Plot.hs1
-rw-r--r--lib/Numeric/Container.hs51
-rw-r--r--lib/Numeric/LinearAlgebra.hs6
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs2
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs72
-rw-r--r--lib/Numeric/Matrix.hs9
-rw-r--r--lib/Numeric/Vector.hs48
9 files changed, 112 insertions, 82 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 07258f8..b860cb1 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -19,7 +19,7 @@
19----------------------------------------------------------------------------- 19-----------------------------------------------------------------------------
20 20
21module Data.Packed.Matrix ( 21module Data.Packed.Matrix (
22 Element(..), 22 Element,
23 Matrix,rows,cols, 23 Matrix,rows,cols,
24 (><), 24 (><),
25 trans, 25 trans,
diff --git a/lib/Data/Packed/Random.hs b/lib/Data/Packed/Random.hs
index 579d13c..0bc5350 100644
--- a/lib/Data/Packed/Random.hs
+++ b/lib/Data/Packed/Random.hs
@@ -21,10 +21,11 @@ module Data.Packed.Random (
21 21
22import Numeric.GSL.Vector 22import Numeric.GSL.Vector
23import Data.Packed.Matrix 23import Data.Packed.Matrix
24import Data.Packed.Vector 24import Numeric.Vector
25import Numeric.LinearAlgebra.Linear 25import Numeric.LinearAlgebra.Linear
26import Numeric.LinearAlgebra.Algorithms 26import Numeric.LinearAlgebra.Algorithms
27 27
28
28-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate 29-- | Obtains a matrix whose rows are pseudorandom samples from a multivariate
29-- Gaussian distribution. 30-- Gaussian distribution.
30gaussianSample :: Int -- ^ seed 31gaussianSample :: Int -- ^ seed
diff --git a/lib/Graphics/Plot.hs b/lib/Graphics/Plot.hs
index 478b4ad..ae88a92 100644
--- a/lib/Graphics/Plot.hs
+++ b/lib/Graphics/Plot.hs
@@ -29,6 +29,7 @@ module Graphics.Plot(
29) where 29) where
30 30
31import Data.Packed 31import Data.Packed
32import Numeric.Vector
32import Numeric.LinearAlgebra.Linear 33import Numeric.LinearAlgebra.Linear
33import Data.List(intersperse) 34import Data.List(intersperse)
34import System.Process (system) 35import System.Process (system)
diff --git a/lib/Numeric/Container.hs b/lib/Numeric/Container.hs
index 0bec2e8..010235f 100644
--- a/lib/Numeric/Container.hs
+++ b/lib/Numeric/Container.hs
@@ -19,10 +19,14 @@
19----------------------------------------------------------------------------- 19-----------------------------------------------------------------------------
20 20
21module Numeric.Container ( 21module Numeric.Container (
22 RealElement, AutoReal(..), 22 Container(..), RealElement, Precision, NumericContainer(..), comp,
23 Container(..), Linear(..), 23 Convert(..), AutoReal(..),
24 Convert(..), RealOf, ComplexOf, SingleOf, DoubleOf, ElementOf, IndexOf, 24 RealOf, ComplexOf, SingleOf, DoubleOf,
25 Precision(..), comp, 25
26-- ElementOf,
27
28 IndexOf,
29
26 module Data.Complex 30 module Data.Complex
27) where 31) where
28 32
@@ -62,7 +66,7 @@ instance RealElement Double
62instance RealElement Float 66instance RealElement Float
63 67
64-- | Conversion utilities 68-- | Conversion utilities
65class Container c where 69class NumericContainer c where
66 toComplex :: (RealElement e) => (c e, c e) -> c (Complex e) 70 toComplex :: (RealElement e) => (c e, c e) -> c (Complex e)
67 fromComplex :: (RealElement e) => c (Complex e) -> (c e, c e) 71 fromComplex :: (RealElement e) => c (Complex e) -> (c e, c e)
68 complex' :: (RealElement e) => c e -> c (Complex e) 72 complex' :: (RealElement e) => c e -> c (Complex e)
@@ -71,9 +75,11 @@ class Container c where
71 single' :: Precision a b => c b -> c a 75 single' :: Precision a b => c b -> c a
72 double' :: Precision a b => c a -> c b 76 double' :: Precision a b => c a -> c b
73 77
78-- | a synonym for "complex'"
79comp :: (NumericContainer c, RealElement e) => c e -> c (Complex e)
74comp x = complex' x 80comp x = complex' x
75 81
76instance Container Vector where 82instance NumericContainer Vector where
77 toComplex = toComplexV 83 toComplex = toComplexV
78 fromComplex = fromComplexV 84 fromComplex = fromComplexV
79 complex' v = toComplex (v,constantD 0 (dim v)) 85 complex' v = toComplex (v,constantD 0 (dim v))
@@ -82,7 +88,7 @@ instance Container Vector where
82 single' = double2FloatG 88 single' = double2FloatG
83 double' = float2DoubleG 89 double' = float2DoubleG
84 90
85instance Container Matrix where 91instance NumericContainer Matrix where
86 toComplex = uncurry $ liftMatrix2 $ curry toComplex 92 toComplex = uncurry $ liftMatrix2 $ curry toComplex
87 fromComplex z = (reshape c *** reshape c) . fromComplex . flatten $ z 93 fromComplex z = (reshape c *** reshape c) . fromComplex . flatten $ z
88 where c = cols z 94 where c = cols z
@@ -138,10 +144,10 @@ type instance IndexOf Matrix = (Int,Int)
138 144
139-- | generic conversion functions 145-- | generic conversion functions
140class Convert t where 146class Convert t where
141 real :: Container c => c (RealOf t) -> c t 147 real :: NumericContainer c => c (RealOf t) -> c t
142 complex :: Container c => c t -> c (ComplexOf t) 148 complex :: NumericContainer c => c t -> c (ComplexOf t)
143 single :: Container c => c t -> c (SingleOf t) 149 single :: NumericContainer c => c t -> c (SingleOf t)
144 double :: Container c => c t -> c (DoubleOf t) 150 double :: NumericContainer c => c t -> c (DoubleOf t)
145 151
146instance Convert Double where 152instance Convert Double where
147 real = id 153 real = id
@@ -171,8 +177,8 @@ instance Convert (Complex Float) where
171 177
172-- | to be replaced by Convert 178-- | to be replaced by Convert
173class Convert t => AutoReal t where 179class Convert t => AutoReal t where
174 real'' :: Container c => c Double -> c t 180 real'' :: NumericContainer c => c Double -> c t
175 complex'' :: Container c => c t -> c (Complex Double) 181 complex'' :: NumericContainer c => c t -> c (Complex Double)
176 182
177instance AutoReal Double where 183instance AutoReal Double where
178 real'' = real 184 real'' = real
@@ -193,23 +199,7 @@ instance AutoReal (Complex Float) where
193------------------------------------------------------------------- 199-------------------------------------------------------------------
194 200
195-- | Basic element-by-element functions. 201-- | Basic element-by-element functions.
196class (Element e, Container c) => Linear c e where 202class (Element e) => Container c e where
197 -- | create a structure with a single element
198 scalar :: e -> c e
199 scale :: e -> c e -> c e
200 -- | scale the element by element reciprocal of the object:
201 --
202 -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@
203 scaleRecip :: e -> c e -> c e
204 addConstant :: e -> c e -> c e
205 add :: c e -> c e -> c e
206 sub :: c e -> c e -> c e
207 -- | element by element multiplication
208 mul :: c e -> c e -> c e
209 -- | element by element division
210 divide :: c e -> c e -> c e
211 equal :: c e -> c e -> Bool
212 --
213 minIndex :: c e -> IndexOf c 203 minIndex :: c e -> IndexOf c
214 maxIndex :: c e -> IndexOf c 204 maxIndex :: c e -> IndexOf c
215 minElement :: c e -> e 205 minElement :: c e -> e
@@ -217,3 +207,4 @@ class (Element e, Container c) => Linear c e where
217 207
218 208
219 209
210
diff --git a/lib/Numeric/LinearAlgebra.hs b/lib/Numeric/LinearAlgebra.hs
index 3df9bd7..da4a4b5 100644
--- a/lib/Numeric/LinearAlgebra.hs
+++ b/lib/Numeric/LinearAlgebra.hs
@@ -15,9 +15,13 @@ This module reexports all normally required functions for Linear Algebra applica
15module Numeric.LinearAlgebra ( 15module Numeric.LinearAlgebra (
16 module Numeric.LinearAlgebra.Algorithms, 16 module Numeric.LinearAlgebra.Algorithms,
17 module Numeric.LinearAlgebra.Interface, 17 module Numeric.LinearAlgebra.Interface,
18 module Numeric.LinearAlgebra.Linear 18 module Numeric.LinearAlgebra.Linear,
19 module Numeric.Matrix,
20 module Numeric.Vector
19) where 21) where
20 22
21import Numeric.LinearAlgebra.Algorithms 23import Numeric.LinearAlgebra.Algorithms
22import Numeric.LinearAlgebra.Interface 24import Numeric.LinearAlgebra.Interface
23import Numeric.LinearAlgebra.Linear 25import Numeric.LinearAlgebra.Linear
26import Numeric.Matrix
27import Numeric.Vector
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index 14bf5d8..ac46847 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -81,6 +81,8 @@ import Numeric.LinearAlgebra.Linear
81import Numeric.LinearAlgebra.LAPACK as LAPACK 81import Numeric.LinearAlgebra.LAPACK as LAPACK
82import Data.List(foldl1') 82import Data.List(foldl1')
83import Data.Array 83import Data.Array
84import Numeric.Vector
85import Numeric.Matrix()
84 86
85-- | Auxiliary typeclass used to define generic computations for both real and complex matrices. 87-- | Auxiliary typeclass used to define generic computations for both real and complex matrices.
86class (Product t, Linear Vector t, Linear Matrix t) => Field t where 88class (Product t, Linear Vector t, Linear Matrix t) => Field t where
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
index 9048204..952661d 100644
--- a/lib/Numeric/LinearAlgebra/Linear.hs
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -18,24 +18,25 @@ Basic optimized operations on vectors and matrices.
18 18
19module Numeric.LinearAlgebra.Linear ( 19module Numeric.LinearAlgebra.Linear (
20 -- * Linear Algebra Typeclasses 20 -- * Linear Algebra Typeclasses
21 Vectors(..), 21 Vectors(..), Linear(..),
22 -- * Products 22 -- * Products
23 Product(..), 23 Product(..),
24 mXm,mXv,vXm, 24 mXm,mXv,vXm,
25 outer, kronecker, 25 outer, kronecker,
26 -- * Modules 26 -- * Modules
27 module Numeric.Vector, 27 --module Numeric.Vector,
28 module Numeric.Matrix, 28 --module Numeric.Matrix,
29 module Numeric.Container 29 module Numeric.Container
30) where 30) where
31 31
32import Data.Packed.Internal.Common 32import Data.Packed.Internal.Common
33import Data.Packed.Matrix 33import Data.Packed.Matrix
34import Data.Packed.Vector
34import Data.Complex 35import Data.Complex
35import Numeric.Container 36import Numeric.Container
36import Numeric.Vector 37--import Numeric.Vector
37import Numeric.Matrix 38--import Numeric.Matrix
38import Numeric.GSL.Vector 39--import Numeric.GSL.Vector
39import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ) 40import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ)
40 41
41-- | basic Vector functions 42-- | basic Vector functions
@@ -49,43 +50,6 @@ class Num e => Vectors a e where
49 norm2 :: a e -> e 50 norm2 :: a e -> e
50 normInf :: a e -> e 51 normInf :: a e -> e
51 52
52
53instance Vectors Vector Float where
54 vectorSum = sumF
55 vectorProd = prodF
56 norm2 = toScalarF Norm2
57 absSum = toScalarF AbsSum
58 dot = dotF
59 norm1 = toScalarF AbsSum
60 normInf = maxElement . vectorMapF Abs
61
62instance Vectors Vector Double where
63 vectorSum = sumR
64 vectorProd = prodR
65 norm2 = toScalarR Norm2
66 absSum = toScalarR AbsSum
67 dot = dotR
68 norm1 = toScalarR AbsSum
69 normInf = maxElement . vectorMapR Abs
70
71instance Vectors Vector (Complex Float) where
72 vectorSum = sumQ
73 vectorProd = prodQ
74 norm2 = (:+ 0) . toScalarQ Norm2
75 absSum = (:+ 0) . toScalarQ AbsSum
76 dot = dotQ
77 norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapQ Abs
78 normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapQ Abs
79
80instance Vectors Vector (Complex Double) where
81 vectorSum = sumC
82 vectorProd = prodC
83 norm2 = (:+ 0) . toScalarC Norm2
84 absSum = (:+ 0) . toScalarC AbsSum
85 dot = dotC
86 norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapC Abs
87 normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapC Abs
88
89---------------------------------------------------- 53----------------------------------------------------
90 54
91class Element t => Product t where 55class Element t => Product t where
@@ -162,4 +126,24 @@ kronecker a b = fromBlocks
162 . toRows 126 . toRows
163 $ flatten a `outer` flatten b 127 $ flatten a `outer` flatten b
164 128
165-------------------------------------------------- 129
130-------------------------------------------------------------------
131
132
133-- | Basic element-by-element functions.
134class (Element e, Container c e) => Linear c e where
135 -- | create a structure with a single element
136 scalar :: e -> c e
137 scale :: e -> c e -> c e
138 -- | scale the element by element reciprocal of the object:
139 --
140 -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@
141 scaleRecip :: e -> c e -> c e
142 addConstant :: e -> c e -> c e
143 add :: c e -> c e -> c e
144 sub :: c e -> c e -> c e
145 -- | element by element multiplication
146 mul :: c e -> c e -> c e
147 -- | element by element division
148 divide :: c e -> c e -> c e
149 equal :: c e -> c e -> Bool
diff --git a/lib/Numeric/Matrix.hs b/lib/Numeric/Matrix.hs
index 8d3764a..f240384 100644
--- a/lib/Numeric/Matrix.hs
+++ b/lib/Numeric/Matrix.hs
@@ -27,7 +27,8 @@ module Numeric.Matrix (
27import Data.Packed.Vector 27import Data.Packed.Vector
28import Data.Packed.Matrix 28import Data.Packed.Matrix
29import Numeric.Container 29import Numeric.Container
30import Numeric.Vector() 30import Numeric.LinearAlgebra.Linear
31--import Numeric.Vector
31 32
32import Control.Monad(ap) 33import Control.Monad(ap)
33 34
@@ -74,7 +75,7 @@ instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floati
74 75
75--------------------------------------------------------------- 76---------------------------------------------------------------
76 77
77instance (Linear Vector a, Container Matrix) => (Linear Matrix a) where 78instance (Linear Vector a, NumericContainer Matrix) => (Linear Matrix a) where
78 scale x = liftMatrix (scale x) 79 scale x = liftMatrix (scale x)
79 scaleRecip x = liftMatrix (scaleRecip x) 80 scaleRecip x = liftMatrix (scaleRecip x)
80 addConstant x = liftMatrix (addConstant x) 81 addConstant x = liftMatrix (addConstant x)
@@ -84,6 +85,9 @@ instance (Linear Vector a, Container Matrix) => (Linear Matrix a) where
84 divide = liftMatrix2 divide 85 divide = liftMatrix2 divide
85 equal a b = cols a == cols b && flatten a `equal` flatten b 86 equal a b = cols a == cols b && flatten a `equal` flatten b
86 scalar x = (1><1) [x] 87 scalar x = (1><1) [x]
88
89
90instance (Linear Vector a, NumericContainer Matrix) => (Container Matrix a) where
87 minIndex m = let (r,c) = (rows m,cols m) 91 minIndex m = let (r,c) = (rows m,cols m)
88 i = 1 + (minIndex $ flatten m) 92 i = 1 + (minIndex $ flatten m)
89 in (i `div` r,i `mod` r) 93 in (i `div` r,i `mod` r)
@@ -94,4 +98,3 @@ instance (Linear Vector a, Container Matrix) => (Linear Matrix a) where
94 maxElement = ap (@@>) maxIndex 98 maxElement = ap (@@>) maxIndex
95 99
96---------------------------------------------------- 100----------------------------------------------------
97
diff --git a/lib/Numeric/Vector.hs b/lib/Numeric/Vector.hs
index ced202f..d92a5e4 100644
--- a/lib/Numeric/Vector.hs
+++ b/lib/Numeric/Vector.hs
@@ -29,10 +29,11 @@ import Data.Complex
29import Control.Monad(ap) 29import Control.Monad(ap)
30 30
31import Data.Packed.Vector 31import Data.Packed.Vector
32import Data.Packed.Matrix(Element(..)) 32import Data.Packed.Internal.Matrix(Element(..))
33import Numeric.GSL.Vector 33import Numeric.GSL.Vector
34 34
35import Numeric.Container 35import Numeric.Container
36import Numeric.LinearAlgebra.Linear
36 37
37------------------------------------------------------------------- 38-------------------------------------------------------------------
38 39
@@ -263,12 +264,13 @@ instance Linear Vector Float where
263 divide = vectorZipF Div 264 divide = vectorZipF Div
264 equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 265 equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0
265 scalar x = fromList [x] 266 scalar x = fromList [x]
267
268instance Container Vector Float where
266 minIndex = round . toScalarF MinIdx 269 minIndex = round . toScalarF MinIdx
267 maxIndex = round . toScalarF MaxIdx 270 maxIndex = round . toScalarF MaxIdx
268 minElement = toScalarF Min 271 minElement = toScalarF Min
269 maxElement = toScalarF Max 272 maxElement = toScalarF Max
270 273
271
272instance Linear Vector Double where 274instance Linear Vector Double where
273 scale = vectorMapValR Scale 275 scale = vectorMapValR Scale
274 scaleRecip = vectorMapValR Recip 276 scaleRecip = vectorMapValR Recip
@@ -279,6 +281,8 @@ instance Linear Vector Double where
279 divide = vectorZipR Div 281 divide = vectorZipR Div
280 equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 282 equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0
281 scalar x = fromList [x] 283 scalar x = fromList [x]
284
285instance Container Vector Double where
282 minIndex = round . toScalarR MinIdx 286 minIndex = round . toScalarR MinIdx
283 maxIndex = round . toScalarR MaxIdx 287 maxIndex = round . toScalarR MaxIdx
284 minElement = toScalarR Min 288 minElement = toScalarR Min
@@ -294,6 +298,8 @@ instance Linear Vector (Complex Double) where
294 divide = vectorZipC Div 298 divide = vectorZipC Div
295 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 299 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0
296 scalar x = fromList [x] 300 scalar x = fromList [x]
301
302instance Container Vector (Complex Double) where
297 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) 303 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate)
298 maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) 304 maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate)
299 minElement = ap (@>) minIndex 305 minElement = ap (@>) minIndex
@@ -309,9 +315,47 @@ instance Linear Vector (Complex Float) where
309 divide = vectorZipQ Div 315 divide = vectorZipQ Div
310 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 316 equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0
311 scalar x = fromList [x] 317 scalar x = fromList [x]
318
319instance Container Vector (Complex Float) where
312 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) 320 minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate)
313 maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) 321 maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate)
314 minElement = ap (@>) minIndex 322 minElement = ap (@>) minIndex
315 maxElement = ap (@>) maxIndex 323 maxElement = ap (@>) maxIndex
316 324
317--------------------------------------------------------------- 325---------------------------------------------------------------
326
327instance Vectors Vector Float where
328 vectorSum = sumF
329 vectorProd = prodF
330 norm2 = toScalarF Norm2
331 absSum = toScalarF AbsSum
332 dot = dotF
333 norm1 = toScalarF AbsSum
334 normInf = maxElement . vectorMapF Abs
335
336instance Vectors Vector Double where
337 vectorSum = sumR
338 vectorProd = prodR
339 norm2 = toScalarR Norm2
340 absSum = toScalarR AbsSum
341 dot = dotR
342 norm1 = toScalarR AbsSum
343 normInf = maxElement . vectorMapR Abs
344
345instance Vectors Vector (Complex Float) where
346 vectorSum = sumQ
347 vectorProd = prodQ
348 norm2 = (:+ 0) . toScalarQ Norm2
349 absSum = (:+ 0) . toScalarQ AbsSum
350 dot = dotQ
351 norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapQ Abs
352 normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapQ Abs
353
354instance Vectors Vector (Complex Double) where
355 vectorSum = sumC
356 vectorProd = prodC
357 norm2 = (:+ 0) . toScalarC Norm2
358 absSum = (:+ 0) . toScalarC AbsSum
359 dot = dotC
360 norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapC Abs
361 normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapC Abs \ No newline at end of file