summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Real.hs19
-rw-r--r--examples/ode.hs16
2 files changed, 30 insertions, 5 deletions
diff --git a/examples/Real.hs b/examples/Real.hs
index 02eba9f..9083b87 100644
--- a/examples/Real.hs
+++ b/examples/Real.hs
@@ -11,10 +11,10 @@ module Real(
11 row, 11 row,
12 col, 12 col,
13 (#),(&), (//), blocks, 13 (#),(&), (//), blocks,
14 rand 14 rand, randn
15) where 15) where
16 16
17import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/)) 17import Numeric.LinearAlgebra hiding ((<>), (<\>))
18import System.Random(randomIO) 18import System.Random(randomIO)
19 19
20infixl 7 <> 20infixl 7 <>
@@ -44,12 +44,21 @@ infixl 7 \>
44m \> v = flatten (m <\> reshape 1 v) 44m \> v = flatten (m <\> reshape 1 v)
45 45
46-- | Pseudorandom matrix with uniform elements between 0 and 1. 46-- | Pseudorandom matrix with uniform elements between 0 and 1.
47rand :: Int -- ^ rows 47randm :: RandDist
48 -> Int -- ^ rows
48 -> Int -- ^ columns 49 -> Int -- ^ columns
49 -> IO (Matrix Double) 50 -> IO (Matrix Double)
50rand r c = do 51randm d r c = do
51 seed <- randomIO 52 seed <- randomIO
52 return (reshape c $ randomVector seed Uniform (r*c)) 53 return (reshape c $ randomVector seed d (r*c))
54
55-- | Pseudorandom matrix with uniform elements between 0 and 1.
56rand :: Int -> Int -> IO (Matrix Double)
57rand = randm Uniform
58
59-- | Pseudorandom matrix with normal elements
60randn :: Int -> Int -> IO (Matrix Double)
61randn = randm Gaussian
53 62
54-- | Real identity matrix. 63-- | Real identity matrix.
55eye :: Int -> Matrix Double 64eye :: Int -> Matrix Double
diff --git a/examples/ode.hs b/examples/ode.hs
index 082c46c..dc6e0ec 100644
--- a/examples/ode.hs
+++ b/examples/ode.hs
@@ -1,6 +1,9 @@
1{-# LANGUAGE ViewPatterns #-}
1import Numeric.GSL.ODE 2import Numeric.GSL.ODE
2import Numeric.LinearAlgebra 3import Numeric.LinearAlgebra
3import Graphics.Plot 4import Graphics.Plot
5import Debug.Trace(trace)
6debug x = trace (show x) x
4 7
5vanderpol mu = do 8vanderpol mu = do
6 let xdot mu t [x,v] = [v, -x + mu * v * (1-x^2)] 9 let xdot mu t [x,v] = [v, -x + mu * v * (1-x^2)]
@@ -32,3 +35,16 @@ main = do
32 harmonic 1 0.1 35 harmonic 1 0.1
33 kepler 0.3 60 36 kepler 0.3 60
34 kepler 0.4 70 37 kepler 0.4 70
38 vanderpol' 2
39
40-- example of odeSolveV with jacobian
41vanderpol' mu = do
42 let xdot mu t (toList->[x,v]) = fromList [v, -x + mu * v * (1-x^2)]
43 jac t (toList->[x,v]) = (2><2) [ 0 , 1
44 , -1-2*x*v*mu, mu*(1-x**2) ]
45 ts = linspace 1000 (0,50)
46 hi = (ts@>1 - ts@>0)/100
47 sol = toColumns $ odeSolveV (MSBDF jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts
48 mplot sol
49
50