diff options
author | Alberto Ruiz <aruiz@um.es> | 2007-09-14 18:23:20 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2007-09-14 18:23:20 +0000 |
commit | d14515a4a50d5b5335f9c1525432b68ab67fa7c8 (patch) | |
tree | fb07b2e27b4b5cebc32a3c7ee064ef376344d7e7 | |
parent | 9e2f7fb0ca902665b430a96f77959522976a97f9 (diff) |
more refactoring
-rw-r--r-- | HSSL.cabal | 2 | ||||
-rw-r--r-- | examples/oldtests.hs | 20 | ||||
-rw-r--r-- | examples/tests.hs | 17 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 7 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 4 | ||||
-rw-r--r-- | lib/GSL/Matrix.hs | 2 | ||||
-rw-r--r-- | lib/GSLHaskell.hs | 44 | ||||
-rw-r--r-- | lib/LinearAlgebra.hs | 15 | ||||
-rw-r--r-- | lib/LinearAlgebra/Linear.hs | 40 |
9 files changed, 89 insertions, 62 deletions
@@ -14,7 +14,7 @@ tested-with: GHC ==6.6.1 | |||
14 | Build-Depends: base, haskell98 | 14 | Build-Depends: base, haskell98 |
15 | Extensions: ForeignFunctionInterface | 15 | Extensions: ForeignFunctionInterface |
16 | --ghc-options: -Wall | 16 | --ghc-options: -Wall |
17 | ghc-options: -O | 17 | ghc-options: -O0 |
18 | hs-source-dirs: lib | 18 | hs-source-dirs: lib |
19 | Exposed-modules: Data.Packed.Internal, | 19 | Exposed-modules: Data.Packed.Internal, |
20 | Data.Packed.Internal.Common, | 20 | Data.Packed.Internal.Common, |
diff --git a/examples/oldtests.hs b/examples/oldtests.hs index b01675f..f60f4e2 100644 --- a/examples/oldtests.hs +++ b/examples/oldtests.hs | |||
@@ -5,14 +5,14 @@ import System.Random(randomRs,mkStdGen) | |||
5 | realMatrix = fromLists :: [[Double]] -> Matrix Double | 5 | realMatrix = fromLists :: [[Double]] -> Matrix Double |
6 | realVector = fromList :: [Double] -> Vector Double | 6 | realVector = fromList :: [Double] -> Vector Double |
7 | 7 | ||
8 | toComplexM = uncurry $ liftMatrix2 (curry toComplex) | 8 | |
9 | 9 | ||
10 | infixl 2 =~= | 10 | infixl 2 =~= |
11 | a =~= b = pnorm 1 (flatten (a - b)) < 1E-6 | 11 | a =~= b = pnorm 1 (flatten (a - b)) < 1E-6 |
12 | 12 | ||
13 | randomMatrix seed (n,m) = reshape m $ realVector $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed | 13 | randomMatrix seed (n,m) = reshape m $ realVector $ take (n*m) $ randomRs (-100,100) $ mkStdGen seed |
14 | 14 | ||
15 | randomMatrixC seed (n,m) = toComplexM (randomMatrix seed (n,m), randomMatrix (seed+1) (n,m)) | 15 | randomMatrixC seed (n,m) = toComplex (randomMatrix seed (n,m), randomMatrix (seed+1) (n,m)) |
16 | 16 | ||
17 | besselTest = do | 17 | besselTest = do |
18 | let (r,e) = bessel_J0_e 5.0 | 18 | let (r,e) = bessel_J0_e 5.0 |
@@ -31,7 +31,7 @@ ms = realMatrix [[1,2,3] | |||
31 | 31 | ||
32 | ms' = randomMatrix 27 (50,100) | 32 | ms' = randomMatrix 27 (50,100) |
33 | 33 | ||
34 | ms'' = toComplexM (randomMatrix 100 (50,100),randomMatrix 101 (50,100)) | 34 | ms'' = toComplex (randomMatrix 100 (50,100),randomMatrix 101 (50,100)) |
35 | 35 | ||
36 | fullsvdTest method mat msg = do | 36 | fullsvdTest method mat msg = do |
37 | let (u,s,vt) = method mat | 37 | let (u,s,vt) = method mat |
@@ -43,7 +43,7 @@ full_svd_Rd = svdRdd | |||
43 | 43 | ||
44 | -------------------------------------------------------------------- | 44 | -------------------------------------------------------------------- |
45 | 45 | ||
46 | mcu = toComplexM (randomMatrix 33 (20,20),randomMatrix 34 (20,20)) | 46 | mcu = toComplex (randomMatrix 33 (20,20),randomMatrix 34 (20,20)) |
47 | 47 | ||
48 | mcur = randomMatrix 35 (40,40) | 48 | mcur = randomMatrix 35 (40,40) |
49 | 49 | ||
@@ -53,7 +53,7 @@ eigTest method m msg = do | |||
53 | assertBool msg $ m <> v =~= v <> diag s | 53 | assertBool msg $ m <> v =~= v <> diag s |
54 | 54 | ||
55 | bigmat = m + trans m where m = randomMatrix 18 (1000,1000) | 55 | bigmat = m + trans m where m = randomMatrix 18 (1000,1000) |
56 | bigmatc = mc + conjTrans mc where mc = toComplexM(m,m) | 56 | bigmatc = mc + conjTrans mc where mc = toComplex(m,m) |
57 | m = randomMatrix 19 (1000,1000) | 57 | m = randomMatrix 19 (1000,1000) |
58 | 58 | ||
59 | -------------------------------------------------------------------- | 59 | -------------------------------------------------------------------- |
@@ -62,22 +62,22 @@ invTest msg m = do | |||
62 | assertBool msg $ m <> inv m =~= ident (rows m) | 62 | assertBool msg $ m <> inv m =~= ident (rows m) |
63 | 63 | ||
64 | invComplexTest msg m = do | 64 | invComplexTest msg m = do |
65 | assertBool msg $ m <> invC m =~= ident (rows m) | 65 | assertBool msg $ m <> invC m =~= identC (rows m) |
66 | 66 | ||
67 | invC m = linearSolveC m (ident (rows m)) | 67 | invC m = linearSolveC m (identC (rows m)) |
68 | 68 | ||
69 | --identC n = toComplexM(ident n, (0::Double) <>ident n) | 69 | identC = comp . ident |
70 | 70 | ||
71 | -------------------------------------------------------------------- | 71 | -------------------------------------------------------------------- |
72 | 72 | ||
73 | pinvTest f msg m = do | 73 | pinvTest f msg m = do |
74 | assertBool msg $ m <> f m <> m =~= m | 74 | assertBool msg $ m <> f m <> m =~= m |
75 | 75 | ||
76 | pinvC m = linearSolveLSC m (ident (rows m)) | 76 | pinvC m = linearSolveLSC m (identC (rows m)) |
77 | 77 | ||
78 | pinvSVDR m = linearSolveSVDR Nothing m (ident (rows m)) | 78 | pinvSVDR m = linearSolveSVDR Nothing m (ident (rows m)) |
79 | 79 | ||
80 | pinvSVDC m = linearSolveSVDC Nothing m (ident (rows m)) | 80 | pinvSVDC m = linearSolveSVDC Nothing m (identC (rows m)) |
81 | 81 | ||
82 | -------------------------------------------------------------------- | 82 | -------------------------------------------------------------------- |
83 | 83 | ||
diff --git a/examples/tests.hs b/examples/tests.hs index 0a09ced..2f3b3d8 100644 --- a/examples/tests.hs +++ b/examples/tests.hs | |||
@@ -6,10 +6,9 @@ | |||
6 | 6 | ||
7 | ----------------------------------------------------------------------------- | 7 | ----------------------------------------------------------------------------- |
8 | 8 | ||
9 | import Data.Packed.Internal | 9 | import Data.Packed.Internal((>|<), fdat, cdat, multiply', multiplyG, MatrixOrder(..)) |
10 | import Data.Packed.Vector | 10 | import Data.Packed.Vector |
11 | import Data.Packed.Matrix | 11 | import Data.Packed.Matrix |
12 | import Data.Packed.Internal.Matrix | ||
13 | import GSL.Vector | 12 | import GSL.Vector |
14 | import GSL.Integration | 13 | import GSL.Integration |
15 | import GSL.Differentiation | 14 | import GSL.Differentiation |
@@ -95,6 +94,8 @@ asC m = (rows m >< cols m) $ toList (cdat m) | |||
95 | mulC a b = multiply' RowMajor a b | 94 | mulC a b = multiply' RowMajor a b |
96 | mulF a b = multiply' ColumnMajor a b | 95 | mulF a b = multiply' ColumnMajor a b |
97 | 96 | ||
97 | identC = comp . ident | ||
98 | |||
98 | infixl 7 <> | 99 | infixl 7 <> |
99 | a <> b = mulF a b | 100 | a <> b = mulF a b |
100 | 101 | ||
@@ -198,15 +199,15 @@ svdTestR fun m = u <> s <> trans v |~| m | |||
198 | 199 | ||
199 | 200 | ||
200 | svdTestC m = u <> s' <> (trans v) |~| m | 201 | svdTestC m = u <> s' <> (trans v) |~| m |
201 | && u <> conjTrans u |~| ident (rows m) | 202 | && u <> conjTrans u |~| identC (rows m) |
202 | && v <> conjTrans v |~| ident (cols m) | 203 | && v <> conjTrans v |~| identC (cols m) |
203 | where (u,s,v) = svdC m | 204 | where (u,s,v) = svdC m |
204 | s' = liftMatrix comp s | 205 | s' = liftMatrix comp s |
205 | 206 | ||
206 | --svdg' m = (u,s',v) where | 207 | --svdg' m = (u,s',v) where |
207 | 208 | ||
208 | eigTestC (SqM m) = (m <> v) |~| (v <> diag s) | 209 | eigTestC (SqM m) = (m <> v) |~| (v <> diag s) |
209 | && takeDiag (conjTrans v <> v) |~| constant 1 (rows m) --normalized | 210 | && takeDiag (conjTrans v <> v) |~| comp (constant 1 (rows m)) --normalized |
210 | where (s,v) = eigC m | 211 | where (s,v) = eigC m |
211 | 212 | ||
212 | eigTestR (SqM m) = (liftMatrix comp m <> v) |~| (v <> diag s) | 213 | eigTestR (SqM m) = (liftMatrix comp m <> v) |~| (v <> diag s) |
@@ -218,7 +219,7 @@ eigTestS (Sym m) = (m <> v) |~| (v <> diag s) | |||
218 | where (s,v) = eigS m | 219 | where (s,v) = eigS m |
219 | 220 | ||
220 | eigTestH (Her m) = (m <> v) |~| (v <> diag (comp s)) | 221 | eigTestH (Her m) = (m <> v) |~| (v <> diag (comp s)) |
221 | && v <> conjTrans v |~| ident (cols m) | 222 | && v <> conjTrans v |~| identC (cols m) |
222 | where (s,v) = eigH m | 223 | where (s,v) = eigH m |
223 | 224 | ||
224 | linearSolveSQTest fun singu (PairSM a b) = singu a || (a <> fun a b) |~| b | 225 | linearSolveSQTest fun singu (PairSM a b) = singu a || (a <> fun a b) |~| b |
@@ -248,11 +249,11 @@ identC n = toComplex(ident n, (0::Double) <>ident n) | |||
248 | pinvTest f m = (m <> f m <> m) |~| m | 249 | pinvTest f m = (m <> f m <> m) |~| m |
249 | 250 | ||
250 | pinvR m = linearSolveLSR m (ident (rows m)) | 251 | pinvR m = linearSolveLSR m (ident (rows m)) |
251 | pinvC m = linearSolveLSC m (ident (rows m)) | 252 | pinvC m = linearSolveLSC m (identC (rows m)) |
252 | 253 | ||
253 | pinvSVDR m = linearSolveSVDR Nothing m (ident (rows m)) | 254 | pinvSVDR m = linearSolveSVDR Nothing m (ident (rows m)) |
254 | 255 | ||
255 | pinvSVDC m = linearSolveSVDC Nothing m (ident (rows m)) | 256 | pinvSVDC m = linearSolveSVDC Nothing m (identC (rows m)) |
256 | 257 | ||
257 | -------------------------------------------------------------------- | 258 | -------------------------------------------------------------------- |
258 | 259 | ||
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 |
349 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ | 349 | 7 |> [2.0,2.0,2.0,2.0,2.0,2.0,2.0]@ |
350 | -} | 350 | -} |
351 | constant :: Field a => a -> Int -> Vector a | 351 | constant :: Double -> Int -> Vector Double |
352 | constant = constantD | 352 | constant = constantD |
353 | 353 | ||
354 | -------------------------------------------------------------------------- | 354 | -------------------------------------------------------------------------- |
@@ -361,6 +361,11 @@ conj v = asComplex $ cdat $ reshape 2 (asReal v) `multiply` diag (fromList [1,-1 | |||
361 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) | 361 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) |
362 | toComplex (r,i) = asComplex $ cdat $ fromColumns [r,i] | 362 | toComplex (r,i) = asComplex $ cdat $ fromColumns [r,i] |
363 | 363 | ||
364 | -- | the inverse of 'toComplex' | ||
365 | fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double) | ||
366 | fromComplex 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) |
365 | comp :: Vector Double -> Vector (Complex Double) | 370 | comp :: Vector Double -> Vector (Complex Double) |
366 | comp v = toComplex (v,constant 0 (dim v)) | 371 | comp 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 | ||
80 | takeDiag :: (Field t) => Matrix t -> Vector t | 80 | takeDiag :: (Field t) => Matrix t -> Vector t |
81 | takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] | 81 | takeDiag m = fromList [cdat m `at` (k*cols m+k) | k <- [0 .. min (rows m) (cols m) -1]] |
82 | 82 | ||
83 | ident :: (Num t, Field t) => Int -> Matrix t | 83 | ident :: Int -> Matrix Double |
84 | ident n = diag (constant 1 n) | 84 | ident 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 | |||
36 | import LAPACK | ||
37 | import GSL.Integration | 36 | import GSL.Integration |
38 | import GSL.Differentiation | 37 | import GSL.Differentiation |
39 | import GSL.Fourier | 38 | import GSL.Fourier |
40 | import GSL.Polynomials | 39 | import GSL.Polynomials |
41 | import GSL.Minimization | 40 | import GSL.Minimization |
42 | import GSL.Matrix | ||
43 | import Graphics.Plot | 41 | import Graphics.Plot |
44 | import Complex | 42 | import Complex |
45 | import GSL.Special(setErrorHandlerOff, | 43 | import 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) |
51 | import Data.Packed.Internal hiding (dsp) | 49 | --import Data.Packed.Internal hiding (dsp,comp) |
52 | import Data.Packed.Vector hiding (constant) | 50 | import Data.Packed.Vector |
53 | import Data.Packed.Matrix | 51 | import Data.Packed.Matrix |
54 | import Data.Packed.Matrix hiding ((><)) | 52 | import Data.Packed.Matrix hiding ((><)) |
55 | import GSL.Vector | 53 | import GSL.Vector |
56 | import LinearAlgebra.Linear | ||
57 | import qualified LinearAlgebra.Algorithms | 54 | import qualified LinearAlgebra.Algorithms |
55 | import LAPACK | ||
56 | import GSL.Matrix | ||
58 | import LinearAlgebra.Algorithms hiding (pnorm) | 57 | import LinearAlgebra.Algorithms hiding (pnorm) |
58 | import LinearAlgebra.Linear | ||
59 | import Complex | 59 | import Complex |
60 | import Numeric(showGFloat) | 60 | import Numeric(showGFloat) |
61 | import Data.List(transpose,intersperse) | 61 | import 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 | ||
71 | liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t | 71 | liftMatrix2' :: (Field t, Field a, Field b) => (Vector a -> Vector b -> Vector t) -> Matrix a -> Matrix b -> Matrix t |
72 | liftMatrix2' f m1 m2 | compat' m1 m2 = reshape (max (cols m1) (cols m2)) (f (cdat m1) (cdat m2)) | 72 | liftMatrix2' 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 | ||
75 | compat' :: Matrix a -> Matrix b -> Bool | 75 | compat' :: 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 | ||
91 | instance (Eq a, Field a) => Eq (Matrix a) where | 91 | instance (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 | ||
94 | instance (Field a, Linear Vector a) => Num (Matrix a) where | 94 | instance (Field a, Linear Vector a) => Num (Matrix a) where |
95 | (+) = liftMatrix2' (+) | 95 | (+) = liftMatrix2' (+) |
@@ -377,9 +377,6 @@ size = dim | |||
377 | gmap :: (Storable a, Storable b) => (a->b) -> Vector a -> Vector b | 377 | gmap :: (Storable a, Storable b) => (a->b) -> Vector a -> Vector b |
378 | gmap f v = liftVector f v | 378 | gmap f v = liftVector f v |
379 | 379 | ||
380 | constant :: Double -> Int -> Vector Double | ||
381 | constant = constantR | ||
382 | |||
383 | -- shows a Double with n digits after the decimal point | 380 | -- shows a Double with n digits after the decimal point |
384 | shf :: (RealFloat a) => Int -> a -> String | 381 | shf :: (RealFloat a) => Int -> a -> String |
385 | shf dec n | abs n < 1e-10 = "0." | 382 | shf 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 | ||
477 | toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double) | ||
478 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] | ||
479 | |||
480 | -- | extracts the real and imaginary parts of a complex vector | ||
481 | fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double) | ||
482 | fromComplexV m = (a,b) where [a,b] = toColumns $ reshape 2 $ asReal m | ||
483 | |||
484 | -- | creates a complex matrix from matrices with real and imaginary parts | ||
485 | toComplexM :: (Matrix Double, Matrix Double) -> Matrix (Complex Double) | ||
486 | toComplexM (r,i) = reshape (cols r) $ asComplex $ flatten $ fromColumns [flatten r, flatten i] | ||
487 | |||
488 | -- | extracts the real and imaginary parts of a complex matrix | ||
489 | fromComplexM :: Matrix (Complex Double) -> (Matrix Double, Matrix Double) | ||
490 | fromComplexM m = (reshape c a, reshape c b) | ||
491 | where c = cols m | ||
492 | [a,b] = toColumns $ reshape 2 $ asReal $ flatten m | ||
493 | |||
494 | fromComplex :: Matrix (Complex Double) -> (Matrix Double, Matrix Double) | ||
495 | fromComplex = fromComplexM | ||
496 | |||
497 | 473 | ||
498 | pnorm :: (Normed t1, Num t) => t -> t1 -> Double | 474 | pnorm :: (Normed t1, Num t) => t -> t1 -> Double |
499 | pnorm 0 = LinearAlgebra.Algorithms.pnorm Infinity | 475 | pnorm 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 | ----------------------------------------------------------------------------- |
15 | module LinearAlgebra ( | 15 | module 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 | ||
25 | import LinearAlgebra.Linear | ||
26 | import LinearAlgebra.Algorithms | ||
27 | import LAPACK | ||
28 | import GSL.Matrix | ||
29 | import Data.Packed.Matrix | ||
30 | import Data.Packed.Vector | ||
31 | import 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 | ||
16 | module LinearAlgebra.Linear ( | 16 | module 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 | ||
37 | instance Linear Vector Double where | 39 | instance 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 | ||
44 | instance Linear Vector (Complex Double) where | 50 | instance 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 | |||
61 | instance 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 | |||
74 | instance 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] |