summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-05-16 13:35:35 +0200
committerAlberto Ruiz <aruiz@um.es>2014-05-16 13:35:35 +0200
commit1838c4248679b7476bb8716a76171712dc3cd335 (patch)
treee3909ac3354eaf917bb1ebc5a7366412b6ab8f0f
parenta2d99e7d0e83fcedf3a856cdb927309e28a8eddd (diff)
linear algebra moved
-rw-r--r--packages/base/hmatrix-base.cabal13
-rw-r--r--packages/base/src/Data/Packed/IO.hs141
-rw-r--r--packages/base/src/Numeric/LinearAlgebra.hs (renamed from packages/hmatrix/src/Numeric/LinearAlgebra.hs)0
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/LAPACK.hs2
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util.hs (renamed from packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs)7
-rw-r--r--packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs (renamed from packages/hmatrix/src/Numeric/LinearAlgebra/Util/Convolution.hs)1
-rw-r--r--packages/hmatrix/hmatrix.cabal5
-rw-r--r--packages/hmatrix/src/Numeric/HMatrix.hs4
-rw-r--r--packages/hmatrix/src/Numeric/IO.hs120
-rw-r--r--packages/hmatrix/src/Numeric/Random.hs3
10 files changed, 162 insertions, 134 deletions
diff --git a/packages/base/hmatrix-base.cabal b/packages/base/hmatrix-base.cabal
index 9a84b7e..3f12dad 100644
--- a/packages/base/hmatrix-base.cabal
+++ b/packages/base/hmatrix-base.cabal
@@ -38,11 +38,11 @@ library
38 Numeric.Conversion 38 Numeric.Conversion
39 Numeric.LinearAlgebra.LAPACK 39 Numeric.LinearAlgebra.LAPACK
40 Numeric.LinearAlgebra.Algorithms 40 Numeric.LinearAlgebra.Algorithms
41 Data.Packed.Numeric
42 Numeric.Vectorized
43 Numeric.Vector
44 Numeric.Matrix
45 Numeric.Container 41 Numeric.Container
42 Numeric.LinearAlgebra
43 Numeric.LinearAlgebra.Util
44 Numeric.LinearAlgebra.Util.Convolution
45 Data.Packed.IO
46 46
47 other-modules: Data.Packed.Internal, 47 other-modules: Data.Packed.Internal,
48 Data.Packed.Internal.Common, 48 Data.Packed.Internal.Common,
@@ -50,6 +50,11 @@ library
50 Data.Packed.Internal.Vector, 50 Data.Packed.Internal.Vector,
51 Data.Packed.Internal.Matrix 51 Data.Packed.Internal.Matrix
52 Numeric.Chain 52 Numeric.Chain
53 Numeric.Vectorized
54 Numeric.Vector
55 Numeric.Matrix
56 Data.Packed.Numeric
57
53 58
54 C-sources: src/C/lapack-aux.c 59 C-sources: src/C/lapack-aux.c
55 src/C/vector-aux.c 60 src/C/vector-aux.c
diff --git a/packages/base/src/Data/Packed/IO.hs b/packages/base/src/Data/Packed/IO.hs
new file mode 100644
index 0000000..dbb2943
--- /dev/null
+++ b/packages/base/src/Data/Packed/IO.hs
@@ -0,0 +1,141 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.IO
4-- Copyright : (c) Alberto Ruiz 2010
5-- License : BSD3
6--
7-- Maintainer : Alberto Ruiz
8-- Stability : provisional
9--
10-- Display, formatting and IO functions for numeric 'Vector' and 'Matrix'
11--
12-----------------------------------------------------------------------------
13{-# OPTIONS_HADDOCK hide #-}
14
15module Data.Packed.IO (
16 dispf, disps, dispcf, vecdisp, latexFormat, format,
17 readMatrix, fromArray2D
18) where
19
20import Data.Packed
21import Data.Packed.Development
22import Text.Printf(printf)
23import Data.List(intersperse)
24import Data.Complex
25
26{- | Creates a string from a matrix given a separator and a function to show each entry. Using
27this function the user can easily define any desired display function:
28
29@import Text.Printf(printf)@
30
31@disp = putStr . format \" \" (printf \"%.2f\")@
32
33-}
34format :: (Element t) => String -> (t -> String) -> Matrix t -> String
35format sep f m = table sep . map (map f) . toLists $ m
36
37{- | Show a matrix with \"autoscaling\" and a given number of decimal places.
38
39>>> putStr . disps 2 $ 120 * (3><4) [1..]
403x4 E3
41 0.12 0.24 0.36 0.48
42 0.60 0.72 0.84 0.96
43 1.08 1.20 1.32 1.44
44
45-}
46disps :: Int -> Matrix Double -> String
47disps d x = sdims x ++ " " ++ formatScaled d x
48
49{- | Show a matrix with a given number of decimal places.
50
51>>> dispf 2 (1/3 + ident 3)
52"3x3\n1.33 0.33 0.33\n0.33 1.33 0.33\n0.33 0.33 1.33\n"
53
54>>> putStr . dispf 2 $ (3><4)[1,1.5..]
553x4
561.00 1.50 2.00 2.50
573.00 3.50 4.00 4.50
585.00 5.50 6.00 6.50
59
60>>> putStr . unlines . tail . lines . dispf 2 . asRow $ linspace 10 (0,1)
610.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
62
63-}
64dispf :: Int -> Matrix Double -> String
65dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x
66
67sdims x = show (rows x) ++ "x" ++ show (cols x)
68
69formatFixed d x = format " " (printf ("%."++show d++"f")) $ x
70
71isInt = all lookslikeInt . toList . flatten
72
73formatScaled dec t = "E"++show o++"\n" ++ ss
74 where ss = format " " (printf fmt. g) t
75 g x | o >= 0 = x/10^(o::Int)
76 | otherwise = x*10^(-o)
77 o | rows t == 0 || cols t == 0 = 0
78 | otherwise = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t
79 fmt = '%':show (dec+3) ++ '.':show dec ++"f"
80
81{- | Show a vector using a function for showing matrices.
82
83>>> putStr . vecdisp (dispf 2) $ linspace 10 (0,1)
8410 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
85
86-}
87vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String
88vecdisp f v
89 = ((show (dim v) ++ " |> ") ++) . (++"\n")
90 . unwords . lines . tail . dropWhile (not . (`elem` " \n"))
91 . f . trans . reshape 1
92 $ v
93
94{- | Tool to display matrices with latex syntax.
95
96>>> latexFormat "bmatrix" (dispf 2 $ ident 2)
97"\\begin{bmatrix}\n1 & 0\n\\\\\n0 & 1\n\\end{bmatrix}"
98
99-}
100latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc.
101 -> String -- ^ Formatted matrix, with elements separated by spaces and newlines
102 -> String
103latexFormat del tab = "\\begin{"++del++"}\n" ++ f tab ++ "\\end{"++del++"}"
104 where f = unlines . intersperse "\\\\" . map unwords . map (intersperse " & " . words) . tail . lines
105
106-- | Pretty print a complex number with at most n decimal digits.
107showComplex :: Int -> Complex Double -> String
108showComplex d (a:+b)
109 | isZero a && isZero b = "0"
110 | isZero b = sa
111 | isZero a && isOne b = s2++"i"
112 | isZero a = sb++"i"
113 | isOne b = sa++s3++"i"
114 | otherwise = sa++s1++sb++"i"
115 where
116 sa = shcr d a
117 sb = shcr d b
118 s1 = if b<0 then "" else "+"
119 s2 = if b<0 then "-" else ""
120 s3 = if b<0 then "-" else "+"
121
122shcr d a | lookslikeInt a = printf "%.0f" a
123 | otherwise = printf ("%."++show d++"f") a
124
125
126lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
127 where shx = show x
128
129isZero x = show x `elem` ["0.0","-0.0"]
130isOne x = show x `elem` ["1.0","-1.0"]
131
132-- | Pretty print a complex matrix with at most n decimal digits.
133dispcf :: Int -> Matrix (Complex Double) -> String
134dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m
135
136--------------------------------------------------------------------
137
138-- | reads a matrix from a string containing a table of numbers.
139readMatrix :: String -> Matrix Double
140readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
141
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra.hs b/packages/base/src/Numeric/LinearAlgebra.hs
index 1db860c..1db860c 100644
--- a/packages/hmatrix/src/Numeric/LinearAlgebra.hs
+++ b/packages/base/src/Numeric/LinearAlgebra.hs
diff --git a/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs b/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs
index af0c744..9cb67d4 100644
--- a/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/LAPACK.hs
@@ -10,6 +10,8 @@
10-- Functional interface to selected LAPACK functions (<http://www.netlib.org/lapack>). 10-- Functional interface to selected LAPACK functions (<http://www.netlib.org/lapack>).
11-- 11--
12----------------------------------------------------------------------------- 12-----------------------------------------------------------------------------
13{-# OPTIONS_HADDOCK hide #-}
14
13 15
14module Numeric.LinearAlgebra.LAPACK ( 16module Numeric.LinearAlgebra.LAPACK (
15 -- * Matrix product 17 -- * Matrix product
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs b/packages/base/src/Numeric/LinearAlgebra/Util.hs
index e4f21b0..533c54b 100644
--- a/packages/hmatrix/src/Numeric/LinearAlgebra/Util.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Util.hs
@@ -47,22 +47,23 @@ module Numeric.LinearAlgebra.Util(
47 vech, 47 vech,
48 dup, 48 dup,
49 vtrans, 49 vtrans,
50 -- * Plot 50{- -- * Plot
51 mplot, 51 mplot,
52 plot, parametricPlot, 52 plot, parametricPlot,
53 splot, mesh, meshdom, 53 splot, mesh, meshdom,
54 matrixToPGM, imshow, 54 matrixToPGM, imshow,
55 gnuplotX, gnuplotpdf, gnuplotWin 55 gnuplotX, gnuplotpdf, gnuplotWin
56-}
56) where 57) where
57 58
58import Numeric.Container 59import Numeric.Container
59import Numeric.IO 60import Data.Packed.IO
60import Numeric.LinearAlgebra.Algorithms hiding (i) 61import Numeric.LinearAlgebra.Algorithms hiding (i)
61import Numeric.Matrix() 62import Numeric.Matrix()
62import Numeric.Vector() 63import Numeric.Vector()
63 64
64import Numeric.LinearAlgebra.Util.Convolution 65import Numeric.LinearAlgebra.Util.Convolution
65import Graphics.Plot 66--import Graphics.Plot
66 67
67 68
68{- | print a real matrix with given number of digits after the decimal point 69{- | print a real matrix with given number of digits after the decimal point
diff --git a/packages/hmatrix/src/Numeric/LinearAlgebra/Util/Convolution.hs b/packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs
index 82de476..d04c46b 100644
--- a/packages/hmatrix/src/Numeric/LinearAlgebra/Util/Convolution.hs
+++ b/packages/base/src/Numeric/LinearAlgebra/Util/Convolution.hs
@@ -10,6 +10,7 @@ Stability : provisional
10 10
11-} 11-}
12----------------------------------------------------------------------------- 12-----------------------------------------------------------------------------
13{-# OPTIONS_HADDOCK hide #-}
13 14
14module Numeric.LinearAlgebra.Util.Convolution( 15module Numeric.LinearAlgebra.Util.Convolution(
15 corr, conv, corrMin, 16 corr, conv, corrMin,
diff --git a/packages/hmatrix/hmatrix.cabal b/packages/hmatrix/hmatrix.cabal
index 9719c96..c79da8a 100644
--- a/packages/hmatrix/hmatrix.cabal
+++ b/packages/hmatrix/hmatrix.cabal
@@ -90,8 +90,6 @@ library
90 Numeric.GSL.Fitting, 90 Numeric.GSL.Fitting,
91 Numeric.GSL.ODE, 91 Numeric.GSL.ODE,
92 Numeric.GSL, 92 Numeric.GSL,
93 Numeric.LinearAlgebra,
94 Numeric.LinearAlgebra.Util,
95 Graphics.Plot, 93 Graphics.Plot,
96 Numeric.HMatrix, 94 Numeric.HMatrix,
97 Numeric.HMatrix.Data, 95 Numeric.HMatrix.Data,
@@ -99,8 +97,7 @@ library
99 other-modules: Numeric.Random, 97 other-modules: Numeric.Random,
100 Numeric.GSL.Internal, 98 Numeric.GSL.Internal,
101 Numeric.GSL.Vector, 99 Numeric.GSL.Vector,
102 Numeric.IO, 100 Numeric.IO
103 Numeric.LinearAlgebra.Util.Convolution
104 101
105 102
106 C-sources: src/Numeric/GSL/gsl-aux.c 103 C-sources: src/Numeric/GSL/gsl-aux.c
diff --git a/packages/hmatrix/src/Numeric/HMatrix.hs b/packages/hmatrix/src/Numeric/HMatrix.hs
index 36fcf70..fcd3e02 100644
--- a/packages/hmatrix/src/Numeric/HMatrix.hs
+++ b/packages/hmatrix/src/Numeric/HMatrix.hs
@@ -129,8 +129,8 @@ module Numeric.HMatrix (
129 129
130import Numeric.HMatrix.Data 130import Numeric.HMatrix.Data
131 131
132import Numeric.Matrix() 132--import Numeric.Matrix()
133import Numeric.Vector() 133--import Numeric.Vector()
134import Numeric.Container 134import Numeric.Container
135import Numeric.LinearAlgebra.Algorithms 135import Numeric.LinearAlgebra.Algorithms
136import Numeric.LinearAlgebra.Util 136import Numeric.LinearAlgebra.Util
diff --git a/packages/hmatrix/src/Numeric/IO.hs b/packages/hmatrix/src/Numeric/IO.hs
index 58fa2b4..08d812b 100644
--- a/packages/hmatrix/src/Numeric/IO.hs
+++ b/packages/hmatrix/src/Numeric/IO.hs
@@ -20,128 +20,10 @@ module Numeric.IO (
20) where 20) where
21 21
22import Data.Packed 22import Data.Packed
23import Data.Packed.Development 23import Data.Packed.IO
24import System.Process(readProcess) 24import System.Process(readProcess)
25import Text.Printf(printf)
26import Data.List(intersperse)
27import Data.Complex
28import Numeric.GSL.Vector 25import Numeric.GSL.Vector
29 26
30{- | Creates a string from a matrix given a separator and a function to show each entry. Using
31this function the user can easily define any desired display function:
32
33@import Text.Printf(printf)@
34
35@disp = putStr . format \" \" (printf \"%.2f\")@
36
37-}
38format :: (Element t) => String -> (t -> String) -> Matrix t -> String
39format sep f m = table sep . map (map f) . toLists $ m
40
41{- | Show a matrix with \"autoscaling\" and a given number of decimal places.
42
43>>> putStr . disps 2 $ 120 * (3><4) [1..]
443x4 E3
45 0.12 0.24 0.36 0.48
46 0.60 0.72 0.84 0.96
47 1.08 1.20 1.32 1.44
48
49-}
50disps :: Int -> Matrix Double -> String
51disps d x = sdims x ++ " " ++ formatScaled d x
52
53{- | Show a matrix with a given number of decimal places.
54
55>>> dispf 2 (1/3 + ident 3)
56"3x3\n1.33 0.33 0.33\n0.33 1.33 0.33\n0.33 0.33 1.33\n"
57
58>>> putStr . dispf 2 $ (3><4)[1,1.5..]
593x4
601.00 1.50 2.00 2.50
613.00 3.50 4.00 4.50
625.00 5.50 6.00 6.50
63
64>>> putStr . unlines . tail . lines . dispf 2 . asRow $ linspace 10 (0,1)
650.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
66
67-}
68dispf :: Int -> Matrix Double -> String
69dispf d x = sdims x ++ "\n" ++ formatFixed (if isInt x then 0 else d) x
70
71sdims x = show (rows x) ++ "x" ++ show (cols x)
72
73formatFixed d x = format " " (printf ("%."++show d++"f")) $ x
74
75isInt = all lookslikeInt . toList . flatten
76
77formatScaled dec t = "E"++show o++"\n" ++ ss
78 where ss = format " " (printf fmt. g) t
79 g x | o >= 0 = x/10^(o::Int)
80 | otherwise = x*10^(-o)
81 o | rows t == 0 || cols t == 0 = 0
82 | otherwise = floor $ maximum $ map (logBase 10 . abs) $ toList $ flatten t
83 fmt = '%':show (dec+3) ++ '.':show dec ++"f"
84
85{- | Show a vector using a function for showing matrices.
86
87>>> putStr . vecdisp (dispf 2) $ linspace 10 (0,1)
8810 |> 0.00 0.11 0.22 0.33 0.44 0.56 0.67 0.78 0.89 1.00
89
90-}
91vecdisp :: (Element t) => (Matrix t -> String) -> Vector t -> String
92vecdisp f v
93 = ((show (dim v) ++ " |> ") ++) . (++"\n")
94 . unwords . lines . tail . dropWhile (not . (`elem` " \n"))
95 . f . trans . reshape 1
96 $ v
97
98{- | Tool to display matrices with latex syntax.
99
100>>> latexFormat "bmatrix" (dispf 2 $ ident 2)
101"\\begin{bmatrix}\n1 & 0\n\\\\\n0 & 1\n\\end{bmatrix}"
102
103-}
104latexFormat :: String -- ^ type of braces: \"matrix\", \"bmatrix\", \"pmatrix\", etc.
105 -> String -- ^ Formatted matrix, with elements separated by spaces and newlines
106 -> String
107latexFormat del tab = "\\begin{"++del++"}\n" ++ f tab ++ "\\end{"++del++"}"
108 where f = unlines . intersperse "\\\\" . map unwords . map (intersperse " & " . words) . tail . lines
109
110-- | Pretty print a complex number with at most n decimal digits.
111showComplex :: Int -> Complex Double -> String
112showComplex d (a:+b)
113 | isZero a && isZero b = "0"
114 | isZero b = sa
115 | isZero a && isOne b = s2++"i"
116 | isZero a = sb++"i"
117 | isOne b = sa++s3++"i"
118 | otherwise = sa++s1++sb++"i"
119 where
120 sa = shcr d a
121 sb = shcr d b
122 s1 = if b<0 then "" else "+"
123 s2 = if b<0 then "-" else ""
124 s3 = if b<0 then "-" else "+"
125
126shcr d a | lookslikeInt a = printf "%.0f" a
127 | otherwise = printf ("%."++show d++"f") a
128
129
130lookslikeInt x = show (round x :: Int) ++".0" == shx || "-0.0" == shx
131 where shx = show x
132
133isZero x = show x `elem` ["0.0","-0.0"]
134isOne x = show x `elem` ["1.0","-1.0"]
135
136-- | Pretty print a complex matrix with at most n decimal digits.
137dispcf :: Int -> Matrix (Complex Double) -> String
138dispcf d m = sdims m ++ "\n" ++ format " " (showComplex d) m
139
140--------------------------------------------------------------------
141
142-- | reads a matrix from a string containing a table of numbers.
143readMatrix :: String -> Matrix Double
144readMatrix = fromLists . map (map read). map words . filter (not.null) . lines
145 27
146{- | obtains the number of rows and columns in an ASCII data file 28{- | obtains the number of rows and columns in an ASCII data file
147 (provisionally using unix's wc). 29 (provisionally using unix's wc).
diff --git a/packages/hmatrix/src/Numeric/Random.hs b/packages/hmatrix/src/Numeric/Random.hs
index 7bf9e8b..0e722fd 100644
--- a/packages/hmatrix/src/Numeric/Random.hs
+++ b/packages/hmatrix/src/Numeric/Random.hs
@@ -21,8 +21,7 @@ module Numeric.Random (
21) where 21) where
22 22
23import Numeric.GSL.Vector 23import Numeric.GSL.Vector
24import Data.Packed 24import Numeric.Container
25import Data.Packed.Numeric
26import Numeric.LinearAlgebra.Algorithms 25import Numeric.LinearAlgebra.Algorithms
27import System.Random(randomIO) 26import System.Random(randomIO)
28 27