diff options
Diffstat (limited to 'examples/inplace.hs')
-rw-r--r-- | examples/inplace.hs | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/examples/inplace.hs b/examples/inplace.hs index dcfff56..574aa44 100644 --- a/examples/inplace.hs +++ b/examples/inplace.hs | |||
@@ -1,9 +1,8 @@ | |||
1 | -- some tests of the interface for pure | 1 | -- some tests of the interface for pure |
2 | -- computations with inplace updates | 2 | -- computations with inplace updates |
3 | 3 | ||
4 | import Numeric.LinearAlgebra | 4 | import Numeric.LinearAlgebra.HMatrix |
5 | import Data.Packed.ST | 5 | import Numeric.LinearAlgebra.Devel |
6 | import Data.Packed.Convert | ||
7 | 6 | ||
8 | import Data.Array.Unboxed | 7 | import Data.Array.Unboxed |
9 | import Data.Array.ST | 8 | import Data.Array.ST |
@@ -15,15 +14,13 @@ main = sequence_[ | |||
15 | print test2, | 14 | print test2, |
16 | print test3, | 15 | print test3, |
17 | print test4, | 16 | print test4, |
18 | test5, | 17 | -- test5, |
19 | test6, | 18 | -- test6, |
20 | print test7, | 19 | -- print test7, |
21 | test8, | 20 | test8, |
22 | test0] | 21 | test0] |
23 | 22 | ||
24 | -- helper functions | 23 | |
25 | vector l = fromList l :: Vector Double | ||
26 | norm v = pnorm PNorm2 v | ||
27 | 24 | ||
28 | -- hmatrix vector and matrix | 25 | -- hmatrix vector and matrix |
29 | v = vector [1..10] | 26 | v = vector [1..10] |
@@ -34,16 +31,16 @@ m = (5><10) [1..50::Double] | |||
34 | -- vector creation by in-place updates on a copy of the argument | 31 | -- vector creation by in-place updates on a copy of the argument |
35 | test1 = fun v | 32 | test1 = fun v |
36 | 33 | ||
37 | fun :: Element t => Vector t -> Vector t | 34 | -- fun :: (Num t, Element t, Container) => Vector t -> Vector t |
38 | fun x = runSTVector $ do | 35 | fun x = runSTVector $ do |
39 | a <- thawVector x | 36 | a <- thawVector x |
40 | mapM_ (flip (modifyVector a) (+57)) [0 .. dim x `div` 2 - 1] | 37 | mapM_ (flip (modifyVector a) (+57)) [0 .. size x `div` 2 - 1] |
41 | return a | 38 | return a |
42 | 39 | ||
43 | -- another example: creation of an antidiagonal matrix from a list | 40 | -- another example: creation of an antidiagonal matrix from a list |
44 | test2 = antiDiag 5 8 [1..] :: Matrix Double | 41 | test2 = antiDiag 5 8 [1..] :: Matrix Double |
45 | 42 | ||
46 | antiDiag :: (Element b) => Int -> Int -> [b] -> Matrix b | 43 | -- antiDiag :: (Element b) => Int -> Int -> [b] -> Matrix b |
47 | antiDiag r c l = runSTMatrix $ do | 44 | antiDiag r c l = runSTMatrix $ do |
48 | m <- newMatrix 0 r c | 45 | m <- newMatrix 0 r c |
49 | let d = min r c - 1 | 46 | let d = min r c - 1 |
@@ -55,21 +52,23 @@ test3 = g1 v | |||
55 | 52 | ||
56 | g1 x = runST $ do | 53 | g1 x = runST $ do |
57 | a <- thawVector x | 54 | a <- thawVector x |
58 | writeVector a (dim x -1) 0 | 55 | writeVector a (size x -1) 0 |
59 | b <- freezeVector a | 56 | b <- freezeVector a |
60 | return (norm b) | 57 | return (norm_2 b) |
61 | 58 | ||
62 | -- another possibility: | 59 | -- another possibility: |
63 | test4 = g2 v | 60 | test4 = g2 v |
64 | 61 | ||
65 | g2 x = runST $ do | 62 | g2 x = runST $ do |
66 | a <- thawVector x | 63 | a <- thawVector x |
67 | writeVector a (dim x -1) 0 | 64 | writeVector a (size x -1) 0 |
68 | t <- liftSTVector norm a | 65 | t <- liftSTVector norm_2 a |
69 | return t | 66 | return t |
70 | 67 | ||
71 | -------------------------------------------------------------- | 68 | -------------------------------------------------------------- |
72 | 69 | ||
70 | {- | ||
71 | |||
73 | -- haskell arrays | 72 | -- haskell arrays |
74 | hv = listArray (0,9) [1..10::Double] | 73 | hv = listArray (0,9) [1..10::Double] |
75 | hm = listArray ((0,0),(4,9)) [1..50::Double] | 74 | hm = listArray ((0,0),(4,9)) [1..50::Double] |
@@ -78,8 +77,8 @@ hm = listArray ((0,0),(4,9)) [1..50::Double] | |||
78 | 77 | ||
79 | -- conversion from standard Haskell arrays | 78 | -- conversion from standard Haskell arrays |
80 | test5 = do | 79 | test5 = do |
81 | print $ norm (vectorFromArray hv) | 80 | print $ norm_2 (vectorFromArray hv) |
82 | print $ norm v | 81 | print $ norm_2 v |
83 | print $ rcond (matrixFromArray hm) | 82 | print $ rcond (matrixFromArray hm) |
84 | print $ rcond m | 83 | print $ rcond m |
85 | 84 | ||
@@ -101,10 +100,11 @@ test7 = unitary (listArray (1,4) [3,5,7,2] :: UArray Int Double) | |||
101 | 100 | ||
102 | unitary v = runSTUArray $ do | 101 | unitary v = runSTUArray $ do |
103 | a <- thaw v | 102 | a <- thaw v |
104 | n <- norm `fmap` vectorFromMArray a | 103 | n <- norm_2 `fmap` vectorFromMArray a |
105 | b <- mapArray (/n) a | 104 | b <- mapArray (/n) a |
106 | return b | 105 | return b |
107 | 106 | ||
107 | -} | ||
108 | ------------------------------------------------- | 108 | ------------------------------------------------- |
109 | 109 | ||
110 | -- (just to check that they are not affected) | 110 | -- (just to check that they are not affected) |
@@ -150,3 +150,4 @@ test8 = do | |||
150 | print $ histoCheck2 ds | 150 | print $ histoCheck2 ds |
151 | print $ histoCheck2 ds | 151 | print $ histoCheck2 ds |
152 | putStrLn "----------------------" | 152 | putStrLn "----------------------" |
153 | |||