diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Real.hs | 19 | ||||
-rw-r--r-- | examples/ode.hs | 16 |
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 | ||
17 | import Numeric.LinearAlgebra hiding ((<>), (<|>), (<->), (<\>), (.*), (*/)) | 17 | import Numeric.LinearAlgebra hiding ((<>), (<\>)) |
18 | import System.Random(randomIO) | 18 | import System.Random(randomIO) |
19 | 19 | ||
20 | infixl 7 <> | 20 | infixl 7 <> |
@@ -44,12 +44,21 @@ infixl 7 \> | |||
44 | m \> v = flatten (m <\> reshape 1 v) | 44 | m \> 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. |
47 | rand :: Int -- ^ rows | 47 | randm :: RandDist |
48 | -> Int -- ^ rows | ||
48 | -> Int -- ^ columns | 49 | -> Int -- ^ columns |
49 | -> IO (Matrix Double) | 50 | -> IO (Matrix Double) |
50 | rand r c = do | 51 | randm 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. | ||
56 | rand :: Int -> Int -> IO (Matrix Double) | ||
57 | rand = randm Uniform | ||
58 | |||
59 | -- | Pseudorandom matrix with normal elements | ||
60 | randn :: Int -> Int -> IO (Matrix Double) | ||
61 | randn = randm Gaussian | ||
53 | 62 | ||
54 | -- | Real identity matrix. | 63 | -- | Real identity matrix. |
55 | eye :: Int -> Matrix Double | 64 | eye :: 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 #-} | ||
1 | import Numeric.GSL.ODE | 2 | import Numeric.GSL.ODE |
2 | import Numeric.LinearAlgebra | 3 | import Numeric.LinearAlgebra |
3 | import Graphics.Plot | 4 | import Graphics.Plot |
5 | import Debug.Trace(trace) | ||
6 | debug x = trace (show x) x | ||
4 | 7 | ||
5 | vanderpol mu = do | 8 | vanderpol 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 | ||
41 | vanderpol' 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 | |||