summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2007-09-14 18:23:20 +0000
committerAlberto Ruiz <aruiz@um.es>2007-09-14 18:23:20 +0000
commitd14515a4a50d5b5335f9c1525432b68ab67fa7c8 (patch)
treefb07b2e27b4b5cebc32a3c7ee064ef376344d7e7 /lib
parent9e2f7fb0ca902665b430a96f77959522976a97f9 (diff)
more refactoring
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs7
-rw-r--r--lib/Data/Packed/Matrix.hs4
-rw-r--r--lib/GSL/Matrix.hs2
-rw-r--r--lib/GSLHaskell.hs44
-rw-r--r--lib/LinearAlgebra.hs15
-rw-r--r--lib/LinearAlgebra/Linear.hs40
6 files changed, 69 insertions, 43 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 89a162a..58b325c 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -348,7 +348,7 @@ foreign import ccall safe "aux.h constantC"
348@> constant 2 7 348@> constant 2 7
3497 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ 3497 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@
350-} 350-}
351constant :: Field a => a -> Int -> Vector a 351constant :: Double -> Int -> Vector Double
352constant = constantD 352constant = constantD
353 353
354-------------------------------------------------------------------------- 354--------------------------------------------------------------------------
@@ -361,6 +361,11 @@ conj v = asComplex $ cdat $ reshape 2 (asReal v) `multiply` diag (fromList [1,-1
361toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) 361toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double)
362toComplex (r,i) = asComplex $ cdat $ fromColumns [r,i] 362toComplex (r,i) = asComplex $ cdat $ fromColumns [r,i]
363 363
364-- | the inverse of 'toComplex'
365fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double)
366fromComplex z = (r,i) where
367 [r,i] = toColumns $ reshape 2 $ asReal z
368
364-- | converts a real vector into a complex representation (with zero imaginary parts) 369-- | converts a real vector into a complex representation (with zero imaginary parts)
365comp :: Vector Double -> Vector (Complex Double) 370comp :: Vector Double -> Vector (Complex Double)
366comp v = toComplex (v,constant 0 (dim v)) 371comp v = toComplex (v,constant 0 (dim v))
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 01e8133..fc08ce4 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -75,12 +75,12 @@ diagRect s r c
75 | r == c = diag s 75 | r == c = diag s
76 | r < c = trans $ diagRect s c r 76 | r < c = trans $ diagRect s c r
77 | r > c = joinVert [diag s , zeros (r-c,c)] 77 | r > c = joinVert [diag s , zeros (r-c,c)]
78 where zeros (r,c) = reshape c $ constant 0 (r*c) 78 where zeros (r,c) = reshape c $ constantD 0 (r*c)
79 79
80takeDiag :: (Field t) => Matrix t -> Vector t 80takeDiag :: (Field t) => Matrix t -> Vector t
81takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] 81takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]]
82 82
83ident :: (Num t, Field t) => Int -> Matrix t 83ident :: Int -> Matrix Double
84ident n = diag (constant 1 n) 84ident n = diag (constant 1 n)
85 85
86------------------------------------------------------------ 86------------------------------------------------------------
diff --git a/lib/GSL/Matrix.hs b/lib/GSL/Matrix.hs
index 15710df..ec8ceea 100644
--- a/lib/GSL/Matrix.hs
+++ b/lib/GSL/Matrix.hs
@@ -289,7 +289,7 @@ luC m = (l,u,p, fromIntegral s') where
289 lu = reshape r $ subVector 0 (r*r) v 289 lu = reshape r $ subVector 0 (r*r) v
290 s':p = map (round.realPart) . toList . subVector (r*r) (r+1) $ v 290 s':p = map (round.realPart) . toList . subVector (r*r) (r+1) $ v
291 u = triang r r 0 1 `mul` lu 291 u = triang r r 0 1 `mul` lu
292 l = (triang r r 0 0 `mul` lu) `add` ident r 292 l = (triang r r 0 0 `mul` lu) `add` liftMatrix comp (ident r)
293 add = liftMatrix2 $ vectorZipC Add 293 add = liftMatrix2 $ vectorZipC Add
294 mul = liftMatrix2 $ vectorZipC Mul 294 mul = liftMatrix2 $ vectorZipC Mul
295 295
diff --git a/lib/GSLHaskell.hs b/lib/GSLHaskell.hs
index e65ff28..3158458 100644
--- a/lib/GSLHaskell.hs
+++ b/lib/GSLHaskell.hs
@@ -18,6 +18,7 @@ module GSLHaskell(
18 module Data.Packed.Vector, 18 module Data.Packed.Vector,
19 module Data.Packed.Matrix, 19 module Data.Packed.Matrix,
20 module LinearAlgebra.Algorithms, 20 module LinearAlgebra.Algorithms,
21 module LinearAlgebra.Linear,
21 module LAPACK, 22 module LAPACK,
22 module GSL.Integration, 23 module GSL.Integration,
23 module GSL.Differentiation, 24 module GSL.Differentiation,
@@ -28,18 +29,15 @@ module GSLHaskell(
28 module GSL.Special, 29 module GSL.Special,
29 module Graphics.Plot, 30 module Graphics.Plot,
30 module Complex, 31 module Complex,
31 Mul,(<>), readMatrix, size, dispR, dispC, format, gmap, Joinable, (<|>),(<->), GSLHaskell.constant, 32 Mul,(<>), readMatrix, size, dispR, dispC, format, gmap, Joinable, (<|>),(<->),
32 fromArray2D, fromComplex, toComplex, GSLHaskell.pnorm, scale, outer 33 fromArray2D, GSLHaskell.pnorm,
33) where 34) where
34 35
35
36import LAPACK
37import GSL.Integration 36import GSL.Integration
38import GSL.Differentiation 37import GSL.Differentiation
39import GSL.Fourier 38import GSL.Fourier
40import GSL.Polynomials 39import GSL.Polynomials
41import GSL.Minimization 40import GSL.Minimization
42import GSL.Matrix
43import Graphics.Plot 41import Graphics.Plot
44import Complex 42import Complex
45import GSL.Special(setErrorHandlerOff, 43import GSL.Special(setErrorHandlerOff,
@@ -48,14 +46,16 @@ import GSL.Special(setErrorHandlerOff,
48 bessel_J0_e, 46 bessel_J0_e,
49 exp_e10_e, 47 exp_e10_e,
50 gamma) 48 gamma)
51import Data.Packed.Internal hiding (dsp) 49--import Data.Packed.Internal hiding (dsp,comp)
52import Data.Packed.Vector hiding (constant) 50import Data.Packed.Vector
53import Data.Packed.Matrix 51import Data.Packed.Matrix
54import Data.Packed.Matrix hiding ((><)) 52import Data.Packed.Matrix hiding ((><))
55import GSL.Vector 53import GSL.Vector
56import LinearAlgebra.Linear
57import qualified LinearAlgebra.Algorithms 54import qualified LinearAlgebra.Algorithms
55import LAPACK
56import GSL.Matrix
58import LinearAlgebra.Algorithms hiding (pnorm) 57import LinearAlgebra.Algorithms hiding (pnorm)
58import LinearAlgebra.Linear
59import Complex 59import Complex
60import Numeric(showGFloat) 60import Numeric(showGFloat)
61import Data.List(transpose,intersperse) 61import Data.List(transpose,intersperse)
@@ -69,7 +69,7 @@ adaptScalar f1 f2 f3 x y
69 | otherwise = f2 x y 69 | otherwise = f2 x y
70 70
71liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t 71liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t
72liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (cdat m1) (cdat m2)) 72liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (flatten m1) (flatten m2))
73 | otherwise = error "nonconformant matrices in liftMatrix2'" 73 | otherwise = error "nonconformant matrices in liftMatrix2'"
74 74
75compat' :: Matrix a -> Matrix b -> Bool 75compat' :: Matrix a -> Matrix b -> Bool
@@ -89,7 +89,7 @@ instance (Linear Vector a) => Num (Vector a) where
89 fromInteger = fromList . return . fromInteger 89 fromInteger = fromList . return . fromInteger
90 90
91instance (Eq a, Field a) => Eq (Matrix a) where 91instance (Eq a, Field a) => Eq (Matrix a) where
92 a == b = rows a == rows b && cols a == cols b && cdat a == cdat b && fdat a == fdat b 92 a == b = cols a == cols b && flatten a == flatten b
93 93
94instance (Field a, Linear Vector a) => Num (Matrix a) where 94instance (Field a, Linear Vector a) => Num (Matrix a) where
95 (+) = liftMatrix2' (+) 95 (+) = liftMatrix2' (+)
@@ -377,9 +377,6 @@ size = dim
377gmap :: (Storable a, Storable b) => (a->b) -> Vector a -> Vector b 377gmap :: (Storable a, Storable b) => (a->b) -> Vector a -> Vector b
378gmap f v = liftVector f v 378gmap f v = liftVector f v
379 379
380constant :: Double -> Int -> Vector Double
381constant = constantR
382
383-- shows a Double with n digits after the decimal point 380-- shows a Double with n digits after the decimal point
384shf :: (RealFloat a) => Int -> a -> String 381shf :: (RealFloat a) => Int -> a -> String
385shf dec n | abs n < 1e-10 = "0." 382shf dec n | abs n < 1e-10 = "0."
@@ -473,27 +470,6 @@ fromArray2D m = (r><c) (elems m)
473 r = r1-r0+1 470 r = r1-r0+1
474 c = c1-c0+1 471 c = c1-c0+1
475 472
476-- | creates a complex vector from vectors with real and imaginary parts
477toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double)
478toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i]
479
480-- | extracts the real and imaginary parts of a complex vector
481fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double)
482fromComplexV m = (a,b) where [a,b] = toColumns $ reshape 2 $ asReal m
483
484-- | creates a complex matrix from matrices with real and imaginary parts
485toComplexM :: (Matrix Double, Matrix Double) -> Matrix (Complex Double)
486toComplexM (r,i) = reshape (cols r) $ asComplex $ flatten $ fromColumns [flatten r, flatten i]
487
488-- | extracts the real and imaginary parts of a complex matrix
489fromComplexM :: Matrix (Complex Double) -> (Matrix Double, Matrix Double)
490fromComplexM m = (reshape c a, reshape c b)
491 where c = cols m
492 [a,b] = toColumns $ reshape 2 $ asReal $ flatten m
493
494fromComplex :: Matrix (Complex Double) -> (Matrix Double, Matrix Double)
495fromComplex = fromComplexM
496
497 473
498pnorm :: (Normed t1, Num t) => t -> t1 -> Double 474pnorm :: (Normed t1, Num t) => t -> t1 -> Double
499pnorm 0 = LinearAlgebra.Algorithms.pnorm Infinity 475pnorm 0 = LinearAlgebra.Algorithms.pnorm Infinity
diff --git a/lib/LinearAlgebra.hs b/lib/LinearAlgebra.hs
index b0c8b9d..3b56fc4 100644
--- a/lib/LinearAlgebra.hs
+++ b/lib/LinearAlgebra.hs
@@ -13,6 +13,19 @@ Some linear algebra algorithms, implemented by means of BLAS, LAPACK or GSL.
13-} 13-}
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15module LinearAlgebra ( 15module LinearAlgebra (
16 16 module Data.Packed.Vector,
17 module Data.Packed.Matrix,
18 module LinearAlgebra.Linear,
19 module LAPACK,
20 module GSL.Matrix,
21 module LinearAlgebra.Algorithms,
22 module Complex
17) where 23) where
18 24
25import LinearAlgebra.Linear
26import LinearAlgebra.Algorithms
27import LAPACK
28import GSL.Matrix
29import Data.Packed.Matrix
30import Data.Packed.Vector
31import Complex \ No newline at end of file
diff --git a/lib/LinearAlgebra/Linear.hs b/lib/LinearAlgebra/Linear.hs
index 53e011f..a2071ed 100644
--- a/lib/LinearAlgebra/Linear.hs
+++ b/lib/LinearAlgebra/Linear.hs
@@ -15,8 +15,6 @@ Portability : uses ffi
15 15
16module LinearAlgebra.Linear ( 16module LinearAlgebra.Linear (
17 Linear(..), 17 Linear(..),
18 toComplex, comp,
19 conj,
20 multiply, dot, outer 18 multiply, dot, outer
21) where 19) where
22 20
@@ -33,6 +31,10 @@ class (Field e) => Linear c e where
33 add :: c e -> c e -> c e 31 add :: c e -> c e -> c e
34 sub :: c e -> c e -> c e 32 sub :: c e -> c e -> c e
35 mul :: c e -> c e -> c e 33 mul :: c e -> c e -> c e
34 toComplex :: RealFloat e => (c e, c e) -> c (Complex e)
35 fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)
36 comp :: RealFloat e => c e -> c (Complex e)
37 conj :: RealFloat e => c (Complex e) -> c (Complex e)
36 38
37instance Linear Vector Double where 39instance Linear Vector Double where
38 scale = vectorMapValR Scale 40 scale = vectorMapValR Scale
@@ -40,6 +42,10 @@ instance Linear Vector Double where
40 add = vectorZipR Add 42 add = vectorZipR Add
41 sub = vectorZipR Sub 43 sub = vectorZipR Sub
42 mul = vectorZipR Mul 44 mul = vectorZipR Mul
45 toComplex = Data.Packed.Internal.toComplex
46 fromComplex = Data.Packed.Internal.fromComplex
47 comp = Data.Packed.Internal.comp
48 conj = Data.Packed.Internal.conj
43 49
44instance Linear Vector (Complex Double) where 50instance Linear Vector (Complex Double) where
45 scale = vectorMapValC Scale 51 scale = vectorMapValC Scale
@@ -47,6 +53,34 @@ instance Linear Vector (Complex Double) where
47 add = vectorZipC Add 53 add = vectorZipC Add
48 sub = vectorZipC Sub 54 sub = vectorZipC Sub
49 mul = vectorZipC Mul 55 mul = vectorZipC Mul
56 toComplex = undefined -- can't match
57 fromComplex = undefined
58 comp = undefined
59 conj = undefined
60
61instance Linear Matrix Double where
62 scale x = liftMatrix (scale x)
63 addConstant x = liftMatrix (addConstant x)
64 add = liftMatrix2 add
65 sub = liftMatrix2 sub
66 mul = liftMatrix2 mul
67 toComplex = uncurry $ liftMatrix2 $ curry LinearAlgebra.Linear.toComplex
68 fromComplex z = (reshape c r, reshape c i)
69 where (r,i) = LinearAlgebra.Linear.fromComplex (cdat z)
70 c = cols z
71 comp = liftMatrix Data.Packed.Internal.comp
72 conj = liftMatrix Data.Packed.Internal.conj
73
74instance Linear Matrix (Complex Double) where
75 scale x = liftMatrix (scale x)
76 addConstant x = liftMatrix (addConstant x)
77 add = liftMatrix2 add
78 sub = liftMatrix2 sub
79 mul = liftMatrix2 mul
80 toComplex = undefined
81 fromComplex = undefined
82 comp = undefined
83 conj = undefined
50 84
51-------------------------------------------------- 85--------------------------------------------------
52 86
@@ -58,8 +92,6 @@ dot u v = dat (multiply r c) `at` 0
58 c = asColumn v 92 c = asColumn v
59 93
60 94
61
62
63{- | Outer product of two vectors. 95{- | Outer product of two vectors.
64 96
65@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3] 97@\> 'fromList' [1,2,3] \`outer\` 'fromList' [5,2,3]