New patches: [unrevert anonymous**20100922112922] { addfile ./lib/Graphics/Rendering/Plot/HMatrix.hs hunk ./lib/Graphics/Rendering/Plot/HMatrix.hs 1 +----------------------------------------------------------------------------- +-- | +-- Module : Graphics.Rendering.Plot.HMatrix +-- Copyright : (c) A. V. H. McPhail 2010 +-- License : BSD3 +-- +-- Maintainer : haskell.vivian.mcphail gmail com +-- Stability : provisional +-- Portability : portable +-- +-- Compatability module to replace "Graphics.Plot" of the 'hmatrix' module +-- +-- Provides all functions from hmatrix's "Graphics.Plot" as well as +-- those functions appended with 'H' which return a 'PlotHandle' for +-- interactive update. +-- +-- Comments and function signatures were copied from "hmatrix:Graphics.Plot" +-- which is released under a GPL-like licence +-- +----------------------------------------------------------------------------- + +module Graphics.Rendering.Plot.HMatrix ( + -- * Plotting functions + mplot, mplotH + , plot, plotH + , parametricPlot, parametricPlotH + , imshow, greyscaleH + -- * Compatability + , matrixToPGM + -- * Undefined functions + , splot, mesh, mesh', meshdom + ) where + +----------------------------------------------------------------------------- + +import Numeric.Vector +import Numeric.Matrix + +import Graphics.Rendering.Plot.Figure + +import qualified Graphics.Rendering.Plot.Figure.Simple as S + +import Graphics.Rendering.Plot.Gtk + +----------------------------------------------------------------------------- + +nohandle m = m >> return () + +----------------------------------------------------------------------------- + +-- | plot several vectors against the first +mplot :: [Vector Double] -> IO () +mplot = nohandle . mplotH + +-- | plot several vectors against the first +mplotH :: [Vector Double] -> IO PlotHandle +mplotH [] = error "mplot': no data" +mplotH [_] = error "mplot': no ordinates" +mplotH (v:vs) = display $ S.plot (Line,v,vs) + +----------------------------------------------------------------------------- + +-- apply several functions to one object +mapf fs x = map ($ x) fs + +{- | Draws a list of functions over a desired range and with a desired number of points + +> > plot [sin, cos, sin.(3*)] (0,2*pi) 1000 + +-} +plot :: [Vector Double -> Vector Double] -> (Double,Double) -> Int -> IO () +plot fs r n = nohandle $ plotH fs r n + +{- | Draws a list of functions over a desired range and with a desired number of points + +> > plot [sin, cos, sin.(3*)] (0,2*pi) 1000 + +-} +plotH :: [Vector Double -> Vector Double] -> (Double,Double) -> Int -> IO PlotHandle +plotH fs r n = display $ do + let ts = linspace n r + S.plot (Line,ts,mapf fs ts) + +----------------------------------------------------------------------------- + +{- | Draws a parametric curve. For instance, to draw a spiral we can do something like: + +> > parametricPlot (\t->(t * sin t, t * cos t)) (0,10*pi) 1000 + +-} +parametricPlot :: (Vector Double->(Vector Double,Vector Double)) -> (Double, Double) -> Int -> IO () +parametricPlot f r n = nohandle $ parametricPlotH f r n + +{- | Draws a parametric curve. For instance, to draw a spiral we can do something like: + +> > parametricPlot (\t->(t * sin t, t * cos t)) (0,10*pi) 1000 + +-} +parametricPlotH :: (Vector Double->(Vector Double,Vector Double)) -> (Double, Double) -> Int -> IO PlotHandle +parametricPlotH f r n = display $ do + let t = linspace n r + (fx,fy) = f t + S.plot (Line,fx,[fy]) + +----------------------------------------------------------------------------- + +-- | From vectors x and y, it generates a pair of matrices to be used as x and y arguments for matrix functions. +meshdom :: Vector Double -> Vector Double -> (Matrix Double , Matrix Double) +meshdom r1 r2 = (outer r1 (constant 1 (dim r2)), outer (constant 1 (dim r1)) r2) + +{- | Draws a 3D surface representation of a real matrix. + +> > mesh (hilb 20) + +In certain versions you can interactively rotate the graphic using the mouse. + +UNDEFINED +-} +mesh :: Matrix Double -> IO () +mesh = error "3D unimplemented" + +mesh' :: Matrix Double -> IO () +mesh' = error "3D unimplemented" + +{- | Draws the surface represented by the function f in the desired ranges and number of points, internally using 'mesh'. + +> > let f x y = cos (x + y) +> > splot f (0,pi) (0,2*pi) 50 + +UNDEFINED + +-} +splot :: (Matrix Double->Matrix Double->Matrix Double) -> (Double,Double) -> (Double,Double) -> Int -> IO () +splot = error "3D unimplemented" + +----------------------------------------------------------------------------- + +-- | writes a matrix to pgm image file +-- (c) A. Ruiz, hmatrix package +matrixToPGM :: Matrix Double -> String +matrixToPGM m = header ++ unlines (map unwords ll) where + c = cols m + r = rows m + header = "P2 "++show c++" "++show r++" "++show (round maxgray :: Int)++"\n" + maxgray = 255.0 + maxval = maxElement m + minval = minElement m + scale' = if (maxval == minval) + then 0.0 + else maxgray / (maxval - minval) + f x = show ( round ( scale' *(x - minval) ) :: Int ) + ll = map (map f) (toLists m) + +----------------------------------------------------------------------------- + +-- | imshow shows a representation of a matrix as a gray level image. +imshow :: Matrix Double -> IO () +imshow = nohandle . greyscaleH + +-- | greyscaleH shows a representation of a matrix as a gray level image. +greyscaleH :: Matrix Double -> IO PlotHandle +greyscaleH d = display $ S.plot d + +----------------------------------------------------------------------------- hunk ./plot-gtk.cabal 31 mtl, glib >= 0.11 && < 0.12, gtk >= 0.11 && < 0.12, + hmatrix >= 0.10 && < 0.11, plot < 0.2 Extensions: hunk ./plot-gtk.cabal 39 hs-source-dirs: lib Exposed-Modules: Graphics.Rendering.Plot.Gtk + Graphics.Rendering.Plot.HMatrix Graphics.UI.Gtk.Plot Other-modules: } Context: [forkIO -> forkOS $ runInBoundThread Vivian McPhail **20100922094925] [shift liftIO out in plotNew Vivian McPhail **20100922055953] [figure now modifyMVar_ not putMVar, which always blocks Vivian McPhail **20100922055306] [use postGUIAsync to generate expose event Vivian McPhail **20100922032537] [change from Figure () to FigureState Vivian McPhail **20100921051123] [figure attribute and generate expose event on handle Vivian McPhail **20100920092127] [TAG initial repository Vivian McPhail **20100919074551] Patch bundle hash: 4856f6675be164e84c3a05961634478d5d8189aa