summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs2
-rw-r--r--lib/Data/Packed/Plot.hs169
-rw-r--r--lib/Data/Packed/Vector.hs15
3 files changed, 15 insertions, 171 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index f9dd9a9..89a162a 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -171,7 +171,7 @@ compat m1 m2 = rows m1 == rows m2 && cols m1 == cols m2
171---------------------------------------------------------------- 171----------------------------------------------------------------
172 172
173-- | Optimized matrix computations are provided for elements in the Field class. 173-- | Optimized matrix computations are provided for elements in the Field class.
174class Storable a => Field a where 174class (Storable a, Num a) => Field a where
175 constantD :: a -> Int -> Vector a 175 constantD :: a -> Int -> Vector a
176 transdata :: Int -> Vector a -> Int -> Vector a 176 transdata :: Int -> Vector a -> Int -> Vector a
177 multiplyD :: Matrix a -> Matrix a -> Matrix a 177 multiplyD :: Matrix a -> Matrix a -> Matrix a
diff --git a/lib/Data/Packed/Plot.hs b/lib/Data/Packed/Plot.hs
deleted file mode 100644
index 7d63426..0000000
--- a/lib/Data/Packed/Plot.hs
+++ /dev/null
@@ -1,169 +0,0 @@
1-----------------------------------------------------------------------------
2-- |
3-- Module : Data.Packed.Plot
4-- Copyright : (c) Alberto Ruiz 2005
5-- License : GPL-style
6--
7-- Maintainer : Alberto Ruiz (aruiz at um dot es)
8-- Stability : provisional
9-- Portability : uses gnuplot and ImageMagick
10--
11-- Very basic (and provisional) drawing tools.
12--
13-----------------------------------------------------------------------------
14
15module Data.Packed.Plot(
16
17 gnuplotX, mplot,
18
19 plot, parametricPlot,
20
21 splot, mesh, mesh', meshdom,
22
23 matrixToPGM, imshow,
24
25) where
26
27import Data.Packed.Vector
28import Data.Packed.Matrix
29import LinearAlgebra.Linear(outer)
30import GSL.Vector(FunCodeS(Max,Min),toScalarR)
31import Data.List(intersperse)
32import System
33import Data.IORef
34import System.Exit
35import Foreign hiding (rotate)
36
37
38size = dim
39
40-- | Loads a real matrix from a formatted ASCII text file
41--fromFile :: FilePath -> IO Matrix
42--fromFile filename = readFile filename >>= return . readMatrix read
43
44-- | Saves a real matrix to a formatted ascii text file
45toFile :: FilePath -> Matrix Double -> IO ()
46toFile filename matrix = writeFile filename (unlines . map unwords. map (map show) . toLists $ matrix)
47
48------------------------------------------------------------------------
49
50
51-- | From vectors x and y, it generates a pair of matrices to be used as x and y arguments for matrix functions.
52meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double)
53meshdom r1 r2 = (outer r1 (constant 1 (size r2)), outer (constant 1 (size r1)) r2)
54
55gnuplotX :: String -> IO ()
56gnuplotX command = do {system cmdstr; return()} where
57 cmdstr = "echo \""++command++"\" | gnuplot -persist"
58
59datafollows = "\\\"-\\\""
60
61prep = (++"e\n\n") . unlines . map (unwords . (map show))
62
63
64{- | Draws a 3D surface representation of a real matrix.
65
66> > mesh (hilb 20)
67
68In certain versions you can interactively rotate the graphic using the mouse.
69
70-}
71mesh :: Matrix Double -> IO ()
72mesh m = gnuplotX (command++dat) where
73 command = "splot "++datafollows++" matrix with lines\n"
74 dat = prep $ toLists $ m
75
76mesh' :: Matrix Double -> IO ()
77mesh' m = do
78 writeFile "splot-gnu-command" "splot \"splot-tmp.txt\" matrix with lines; pause -1";
79 toFile "splot-tmp.txt" m
80 putStr "Press [Return] to close the graphic and continue... "
81 system "gnuplot -persist splot-gnu-command"
82 system "rm splot-tmp.txt splot-gnu-command"
83 return ()
84
85{- | Draws the surface represented by the function f in the desired ranges and number of points, internally using 'mesh'.
86
87> > let f x y = cos (x + y)
88> > splot f (0,pi) (0,2*pi) 50
89
90-}
91splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO ()
92splot f rx ry n = mesh' z where
93 (x,y) = meshdom (linspace n rx) (linspace n ry)
94 z = f x y
95
96{- | plots several vectors against the first one -}
97mplot :: [Vector Double] -> IO ()
98mplot m = gnuplotX (commands++dats) where
99 commands = if length m == 1 then command1 else commandmore
100 command1 = "plot "++datafollows++" with lines\n" ++ dat
101 commandmore = "plot " ++ plots ++ "\n"
102 plots = concat $ intersperse ", " (map cmd [2 .. length m])
103 cmd k = datafollows++" using 1:"++show k++" with lines"
104 dat = prep $ toLists $ fromColumns m
105 dats = concat (replicate (length m-1) dat)
106
107
108
109
110
111
112mplot' m = do
113 writeFile "plot-gnu-command" (commands++endcmd)
114 toFile "plot-tmp.txt" (fromColumns m)
115 putStr "Press [Return] to close the graphic and continue... "
116 system "gnuplot plot-gnu-command"
117 system "rm plot-tmp.txt plot-gnu-command"
118 return ()
119 where
120 commands = if length m == 1 then command1 else commandmore
121 command1 = "plot \"plot-tmp.txt\" with lines\n"
122 commandmore = "plot " ++ plots ++ "\n"
123 plots = concat $ intersperse ", " (map cmd [2 .. length m])
124 cmd k = "\"plot-tmp.txt\" using 1:"++show k++" with lines"
125 endcmd = "pause -1"
126
127-- apply several functions to one object
128mapf fs x = map ($ x) fs
129
130{- | Draws a list of functions over a desired range and with a desired number of points
131
132> > plot [sin, cos, sin.(3*)] (0,2*pi) 1000
133
134-}
135plot :: [Vector Double->Vector Double] -> (Double,Double) -> Int -> IO ()
136plot fs rx n = mplot (x: mapf fs x)
137 where x = linspace n rx
138
139{- | Draws a parametric curve. For instance, to draw a spiral we can do something like:
140
141> > parametricPlot (\t->(t * sin t, t * cos t)) (0,10*pi) 1000
142
143-}
144parametricPlot :: (Vector Double->(Vector Double,Vector Double)) -> (Double, Double) -> Int -> IO ()
145parametricPlot f rt n = mplot [fx, fy]
146 where t = linspace n rt
147 (fx,fy) = f t
148
149
150-- | writes a matrix to pgm image file
151matrixToPGM :: Matrix Double -> String
152matrixToPGM m = header ++ unlines (map unwords ll) where
153 c = cols m
154 r = rows m
155 header = "P2 "++show c++" "++show r++" "++show (round maxgray :: Int)++"\n"
156 maxgray = 255.0
157 maxval = toScalarR Max $ flatten $ m
158 minval = toScalarR Min $ flatten $ m
159 scale = if (maxval == minval)
160 then 0.0
161 else maxgray / (maxval - minval)
162 f x = show ( round ( scale *(x - minval) ) :: Int )
163 ll = map (map f) (toLists m)
164
165-- | imshow shows a representation of a matrix as a gray level image using ImageMagick's display.
166imshow :: Matrix Double -> IO ()
167imshow m = do
168 system $ "echo \""++ matrixToPGM m ++"\"| display -antialias -resize 300 - &"
169 return ()
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index 9818d58..3fc625c 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -18,12 +18,13 @@ module Data.Packed.Vector (
18 dim, (@>), 18 dim, (@>),
19 subVector, join, 19 subVector, join,
20 constant, linspace, 20 constant, linspace,
21 21 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex,
22 liftVector, liftVector2 22 liftVector, liftVector2
23) where 23) where
24 24
25import Data.Packed.Internal 25import Data.Packed.Internal
26import Complex 26import Complex
27import GSL.Vector
27 28
28{- | Creates a real vector containing a range of values: 29{- | Creates a real vector containing a range of values:
29 30
@@ -33,3 +34,15 @@ import Complex
33linspace :: Int -> (Double, Double) -> Vector Double 34linspace :: Int -> (Double, Double) -> Vector Double
34linspace n (a,b) = fromList [a, a+delta .. b] 35linspace n (a,b) = fromList [a, a+delta .. b]
35 where delta = (b-a)/(fromIntegral n -1) 36 where delta = (b-a)/(fromIntegral n -1)
37
38vectorMax :: Vector Double -> Double
39vectorMax = toScalarR Max
40
41vectorMin :: Vector Double -> Double
42vectorMin = toScalarR Min
43
44vectorMaxIndex :: Vector Double -> Int
45vectorMaxIndex = round . toScalarR MaxIdx
46
47vectorMinIndex :: Vector Double -> Int
48vectorMinIndex = round . toScalarR MinIdx