summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Data/Packed.hs54
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs14
-rw-r--r--lib/Data/Packed/Matrix.hs49
-rw-r--r--lib/Numeric/LinearAlgebra.hs3
-rw-r--r--lib/Numeric/LinearAlgebra/Algorithms.hs6
-rw-r--r--lib/Numeric/LinearAlgebra/Interface.hs4
-rw-r--r--lib/Numeric/LinearAlgebra/LAPACK.hs5
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs4
-rw-r--r--lib/Numeric/LinearAlgebra/Tests.hs6
9 files changed, 75 insertions, 70 deletions
diff --git a/lib/Data/Packed.hs b/lib/Data/Packed.hs
index 7d6d200..87695ee 100644
--- a/lib/Data/Packed.hs
+++ b/lib/Data/Packed.hs
@@ -1,4 +1,3 @@
1{-# OPTIONS_GHC -fglasgow-exts #-}
2----------------------------------------------------------------------------- 1-----------------------------------------------------------------------------
3{- | 2{- |
4Module : Data.Packed 3Module : Data.Packed
@@ -17,59 +16,12 @@ The Vector and Matrix types and some utilities.
17module Data.Packed ( 16module Data.Packed (
18 module Data.Packed.Vector, 17 module Data.Packed.Vector,
19 module Data.Packed.Matrix, 18 module Data.Packed.Matrix,
20 module Data.Complex, 19 module Data.Packed.Random,
21 Container(..) 20 module Data.Complex
22) where 21) where
23 22
24import Data.Packed.Vector 23import Data.Packed.Vector
25import Data.Packed.Matrix 24import Data.Packed.Matrix
25import Data.Packed.Random
26import Data.Complex 26import Data.Complex
27import Data.Packed.Internal(fromComplex,toComplex,conj)
28 27
29-- | conversion utilities
30class (Element e) => Container c e where
31 toComplex :: RealFloat e => (c e, c e) -> c (Complex e)
32 fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)
33 comp :: RealFloat e => c e -> c (Complex e)
34 conj :: RealFloat e => c (Complex e) -> c (Complex e)
35 real :: c Double -> c e
36 complex :: c e -> c (Complex Double)
37
38instance Container Vector Double where
39 toComplex = Data.Packed.Internal.toComplex
40 fromComplex = Data.Packed.Internal.fromComplex
41 comp = internalcomp
42 conj = Data.Packed.Internal.conj
43 real = id
44 complex = Data.Packed.comp
45
46instance Container Vector (Complex Double) where
47 toComplex = undefined -- can't match
48 fromComplex = undefined
49 comp = undefined
50 conj = undefined
51 real = Data.Packed.comp
52 complex = id
53
54instance Container Matrix Double where
55 toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex
56 fromComplex z = (reshape c r, reshape c i)
57 where (r,i) = Data.Packed.fromComplex (flatten z)
58 c = cols z
59 comp = liftMatrix internalcomp
60 conj = liftMatrix Data.Packed.Internal.conj
61 real = id
62 complex = Data.Packed.comp
63
64instance Container Matrix (Complex Double) where
65 toComplex = undefined
66 fromComplex = undefined
67 comp = undefined
68 conj = undefined
69 real = Data.Packed.comp
70 complex = id
71
72
73-- | converts a real vector into a complex representation (with zero imaginary parts)
74internalcomp :: Vector Double -> Vector (Complex Double)
75internalcomp v = Data.Packed.Internal.toComplex (v,constant 0 (dim v))
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index d879ee6..003e8ee 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -29,7 +29,7 @@ module Data.Packed.Internal.Matrix(
29 liftMatrix, liftMatrix2, 29 liftMatrix, liftMatrix2,
30 (@@>), 30 (@@>),
31 saveMatrix, 31 saveMatrix,
32 fromComplex, toComplex, conj, 32 fromComplexV, toComplexV, conjV,
33 singleton 33 singleton
34) where 34) where
35 35
@@ -368,16 +368,16 @@ subMatrix' (r0,c0) (rt,ct) m = trans $ subMatrix' (c0,r0) (ct,rt) (trans m)
368-------------------------------------------------------------------------- 368--------------------------------------------------------------------------
369 369
370-- | obtains the complex conjugate of a complex vector 370-- | obtains the complex conjugate of a complex vector
371conj :: Vector (Complex Double) -> Vector (Complex Double) 371conjV :: Vector (Complex Double) -> Vector (Complex Double)
372conj = mapVector conjugate 372conjV = mapVector conjugate
373 373
374-- | creates a complex vector from vectors with real and imaginary parts 374-- | creates a complex vector from vectors with real and imaginary parts
375toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) 375toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double)
376toComplex (r,i) = asComplex $ flatten $ fromColumns [r,i] 376toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i]
377 377
378-- | the inverse of 'toComplex' 378-- | the inverse of 'toComplex'
379fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double) 379fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double)
380fromComplex z = (r,i) where 380fromComplexV z = (r,i) where
381 [r,i] = toColumns $ reshape 2 $ asReal z 381 [r,i] = toColumns $ reshape 2 $ asReal z
382 382
383-------------------------------------------------------------------------- 383--------------------------------------------------------------------------
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 4fdd2c6..3147e13 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
1----------------------------------------------------------------------------- 2-----------------------------------------------------------------------------
2-- | 3-- |
3-- Module : Data.Packed.Matrix 4-- Module : Data.Packed.Matrix
@@ -13,7 +14,7 @@
13----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
14 15
15module Data.Packed.Matrix ( 16module Data.Packed.Matrix (
16 Element, 17 Element, Container(..),
17 Matrix,rows,cols, 18 Matrix,rows,cols,
18 (><), 19 (><),
19 trans, 20 trans,
@@ -421,3 +422,49 @@ toBlocksEvery r c m = toBlocks rs cs m where
421 (qc,rc) = cols m `divMod` c 422 (qc,rc) = cols m `divMod` c
422 rs = replicate qr r ++ if rr > 0 then [rr] else [] 423 rs = replicate qr r ++ if rr > 0 then [rr] else []
423 cs = replicate qc c ++ if rc > 0 then [rc] else [] 424 cs = replicate qc c ++ if rc > 0 then [rc] else []
425
426-------------------------------------------------------------------
427
428-- | conversion utilities
429class (Element e) => Container c e where
430 toComplex :: RealFloat e => (c e, c e) -> c (Complex e)
431 fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)
432 comp :: RealFloat e => c e -> c (Complex e)
433 conj :: RealFloat e => c (Complex e) -> c (Complex e)
434 real :: c Double -> c e
435 complex :: c e -> c (Complex Double)
436
437instance Container Vector Double where
438 toComplex = toComplexV
439 fromComplex = fromComplexV
440 comp v = toComplex (v,constant 0 (dim v))
441 conj = conjV
442 real = id
443 complex = comp
444
445instance Container Vector (Complex Double) where
446 toComplex = undefined -- can't match
447 fromComplex = undefined
448 comp = undefined
449 conj = undefined
450 real = comp
451 complex = id
452
453instance Container Matrix Double where
454 toComplex = uncurry $ liftMatrix2 $ curry toComplex
455 fromComplex z = (reshape c r, reshape c i)
456 where (r,i) = fromComplex (flatten z)
457 c = cols z
458 comp = liftMatrix comp
459 conj = liftMatrix conj
460 real = id
461 complex = comp
462
463instance Container Matrix (Complex Double) where
464 toComplex = undefined
465 fromComplex = undefined
466 comp = undefined
467 conj = undefined
468 real = comp
469 complex = id
470
diff --git a/lib/Numeric/LinearAlgebra.hs b/lib/Numeric/LinearAlgebra.hs
index 2652f2d..7664a9f 100644
--- a/lib/Numeric/LinearAlgebra.hs
+++ b/lib/Numeric/LinearAlgebra.hs
@@ -14,13 +14,10 @@ This module reexports all normally required functions for Linear Algebra applica
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15module Numeric.LinearAlgebra ( 15module Numeric.LinearAlgebra (
16 module Data.Packed, 16 module Data.Packed,
17 module Data.Packed.Random,
18 module Numeric.LinearAlgebra.Algorithms, 17 module Numeric.LinearAlgebra.Algorithms,
19 module Numeric.LinearAlgebra.Interface 18 module Numeric.LinearAlgebra.Interface
20) where 19) where
21 20
22import Data.Packed 21import Data.Packed
23import Data.Packed.Random
24import Numeric.LinearAlgebra.Algorithms 22import Numeric.LinearAlgebra.Algorithms
25import Numeric.LinearAlgebra.Instances()
26import Numeric.LinearAlgebra.Interface 23import Numeric.LinearAlgebra.Interface
diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs
index 7de9587..55398e0 100644
--- a/lib/Numeric/LinearAlgebra/Algorithms.hs
+++ b/lib/Numeric/LinearAlgebra/Algorithms.hs
@@ -76,8 +76,10 @@ module Numeric.LinearAlgebra.Algorithms (
76) where 76) where
77 77
78 78
79import Data.Packed.Internal hiding (fromComplex, toComplex, conj, (//)) 79import Data.Packed.Internal hiding ((//))
80import Data.Packed 80import Data.Packed.Vector
81import Data.Packed.Matrix
82import Data.Complex
81import Numeric.GSL.Vector 83import Numeric.GSL.Vector
82import Numeric.LinearAlgebra.LAPACK as LAPACK 84import Numeric.LinearAlgebra.LAPACK as LAPACK
83import Numeric.LinearAlgebra.Linear 85import Numeric.LinearAlgebra.Linear
diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs
index 750670b..30547d9 100644
--- a/lib/Numeric/LinearAlgebra/Interface.hs
+++ b/lib/Numeric/LinearAlgebra/Interface.hs
@@ -9,9 +9,7 @@ Maintainer : Alberto Ruiz (aruiz at um dot es)
9Stability : provisional 9Stability : provisional
10Portability : portable 10Portability : portable
11 11
12Operators for frequent operations. 12Some useful operators, and Show, Read, Eq, Num, Fractional, and Floating instances for Vector and Matrix.
13
14This module exports Show, Read, Eq, Num, Fractional, and Floating instances for Vector and Matrix.
15 13
16In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. 14In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand.
17 15
diff --git a/lib/Numeric/LinearAlgebra/LAPACK.hs b/lib/Numeric/LinearAlgebra/LAPACK.hs
index f5af8be..7f057ba 100644
--- a/lib/Numeric/LinearAlgebra/LAPACK.hs
+++ b/lib/Numeric/LinearAlgebra/LAPACK.hs
@@ -41,8 +41,9 @@ module Numeric.LinearAlgebra.LAPACK (
41 schurR, schurC 41 schurR, schurC
42) where 42) where
43 43
44import Data.Packed.Internal hiding (toComplex) 44import Data.Packed.Internal
45import Data.Packed 45import Data.Packed.Matrix
46import Data.Complex
46import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale)) 47import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale))
47import Foreign 48import Foreign
48import Foreign.C.Types (CInt) 49import Foreign.C.Types (CInt)
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
index 2568410..c802712 100644
--- a/lib/Numeric/LinearAlgebra/Linear.hs
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -18,7 +18,9 @@ module Numeric.LinearAlgebra.Linear (
18 Linear(..) 18 Linear(..)
19) where 19) where
20 20
21import Data.Packed 21import Data.Packed.Vector
22import Data.Packed.Matrix
23import Data.Complex
22import Numeric.GSL.Vector 24import Numeric.GSL.Vector
23 25
24-- | Basic element-by-element functions. 26-- | Basic element-by-element functions.
diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs
index ac26466..65b0e4c 100644
--- a/lib/Numeric/LinearAlgebra/Tests.hs
+++ b/lib/Numeric/LinearAlgebra/Tests.hs
@@ -78,6 +78,11 @@ volSphere r = 8 * quad2 (\x y -> sqrt (r*r-x*x-y*y))
78 78
79--------------------------------------------------------------------- 79---------------------------------------------------------------------
80 80
81derivTest = abs (d (\x-> x * d (\y-> x+y) 1) 1 - 1) < 1E-10
82 where d f x = fst $ derivCentral 0.01 f x
83
84---------------------------------------------------------------------
85
81-- besselTest = utest "bessel_J0_e" ( abs (r-expected) < e ) 86-- besselTest = utest "bessel_J0_e" ( abs (r-expected) < e )
82-- where (r,e) = bessel_J0_e 5.0 87-- where (r,e) = bessel_J0_e 5.0
83-- expected = -0.17759677131433830434739701 88-- expected = -0.17759677131433830434739701
@@ -319,6 +324,7 @@ runTests n = do
319-- , utest "gamma" (gamma 5 == 24.0) 324-- , utest "gamma" (gamma 5 == 24.0)
320-- , besselTest 325-- , besselTest
321-- , exponentialTest 326-- , exponentialTest
327 , utest "deriv" derivTest
322 , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5^3) < 1E-8) 328 , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5^3) < 1E-8)
323 , utest "polySolve" (polySolveProp [1,2,3,4]) 329 , utest "polySolve" (polySolveProp [1,2,3,4])
324 , minimizationTest 330 , minimizationTest