diff options
-rw-r--r-- | CHANGES | 6 | ||||
-rw-r--r-- | examples/bool.hs | 50 | ||||
-rw-r--r-- | hmatrix.cabal | 1 | ||||
-rw-r--r-- | lib/Numeric/ContainerBoot.hs | 20 |
4 files changed, 70 insertions, 7 deletions
@@ -3,9 +3,11 @@ | |||
3 | 3 | ||
4 | - invlndet (inverse and log of determinant) | 4 | - invlndet (inverse and log of determinant) |
5 | 5 | ||
6 | - step, cond, | 6 | - step, cond |
7 | 7 | ||
8 | - find, assoc | 8 | - find |
9 | |||
10 | - assoc | ||
9 | 11 | ||
10 | 0.10.0.0 | 12 | 0.10.0.0 |
11 | ======== | 13 | ======== |
diff --git a/examples/bool.hs b/examples/bool.hs new file mode 100644 index 0000000..9301689 --- /dev/null +++ b/examples/bool.hs | |||
@@ -0,0 +1,50 @@ | |||
1 | -- vectorized boolean operations defined in terms of step or cond | ||
2 | |||
3 | import Numeric.LinearAlgebra | ||
4 | |||
5 | infix 4 .==., ./=., .<., .<=., .>=., .>. | ||
6 | infixr 3 .&&. | ||
7 | infixr 2 .||. | ||
8 | |||
9 | a .<. b = step (b-a) | ||
10 | a .<=. b = cond a b 1 1 0 | ||
11 | a .==. b = cond a b 0 1 0 | ||
12 | a ./=. b = cond a b 1 0 1 | ||
13 | a .>=. b = cond a b 0 1 1 | ||
14 | a .>. b = step (a-b) | ||
15 | |||
16 | a .&&. b = step (a*b) | ||
17 | a .||. b = step (a+b) | ||
18 | no a = 1-a | ||
19 | xor a b = a ./=. b | ||
20 | equiv a b = a .==. b | ||
21 | imp a b = no a .||. b | ||
22 | |||
23 | taut x = minElement x == 1 | ||
24 | |||
25 | -- examples | ||
26 | |||
27 | clip a b x = cond y b y y b where y = cond x a a x x | ||
28 | |||
29 | disp = putStr . dispf 3 | ||
30 | |||
31 | eye n = ident n :: Matrix Double | ||
32 | row = asRow . fromList :: [Double] -> Matrix Double | ||
33 | col = asColumn . fromList :: [Double] -> Matrix Double | ||
34 | |||
35 | m = (3><4) [1..] :: Matrix Double | ||
36 | |||
37 | p = row [0,0,1,1] | ||
38 | q = row [0,1,0,1] | ||
39 | |||
40 | main = do | ||
41 | print $ find (>6) m | ||
42 | disp $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] | ||
43 | disp $ m .>=. 10 .||. m .<. 4 | ||
44 | (disp . fromColumns . map flatten) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q] | ||
45 | print $ taut $ (p `imp` q ) `equiv` (no q `imp` no p) | ||
46 | print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q) | ||
47 | disp $ clip 3 8 m | ||
48 | disp $ col [1..7] .<=. row [1..5] | ||
49 | disp $ cond (col [1..3]) (row [1..4]) m 50 (3*m) | ||
50 | |||
diff --git a/hmatrix.cabal b/hmatrix.cabal index b8f23a1..daed1e5 100644 --- a/hmatrix.cabal +++ b/hmatrix.cabal | |||
@@ -54,6 +54,7 @@ extra-source-files: examples/tests.hs | |||
54 | examples/Real.hs | 54 | examples/Real.hs |
55 | examples/vector.hs | 55 | examples/vector.hs |
56 | examples/monadic.hs | 56 | examples/monadic.hs |
57 | examples/bool.hs | ||
57 | 58 | ||
58 | extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, | 59 | extra-source-files: lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h, |
59 | lib/Numeric/LinearAlgebra/LAPACK/clapack.h | 60 | lib/Numeric/LinearAlgebra/LAPACK/clapack.h |
diff --git a/lib/Numeric/ContainerBoot.hs b/lib/Numeric/ContainerBoot.hs index e33857a..276eaa8 100644 --- a/lib/Numeric/ContainerBoot.hs +++ b/lib/Numeric/ContainerBoot.hs | |||
@@ -121,14 +121,24 @@ class (Complexable c, Fractional e, Element e) => Container c e where | |||
121 | sumElements :: c e -> e | 121 | sumElements :: c e -> e |
122 | -- | the product of elements (faster than using @fold@) | 122 | -- | the product of elements (faster than using @fold@) |
123 | prodElements :: c e -> e | 123 | prodElements :: c e -> e |
124 | -- | map (if x_i>0 then 1.0 else 0.0) | 124 | -- | a more efficient implementation of @cmap (\x -> if x>0 then 1 else 0)@ |
125 | step :: RealFloat e => c e -> c e | 125 | step :: RealElement e => c e -> c e |
126 | -- | find index of elements which satisfy a predicate | 126 | -- | find index of elements which satisfy a predicate |
127 | find :: (e -> Bool) -> c e -> [IndexOf c] | 127 | find :: (e -> Bool) -> c e -> [IndexOf c] |
128 | -- | create a structure from an association list | 128 | -- | create a structure from an association list |
129 | assoc :: IndexOf c -> e -> [(IndexOf c, e)] -> c e | 129 | assoc :: IndexOf c -- ^ size |
130 | -- | a vectorized form of case 'compare' a_i b_i of LT -> l_i; EQ -> e_i; GT -> g_i | 130 | -> e -- ^ default value |
131 | cond :: RealFloat e => c e -> c e -> c e -> c e -> c e -> c e | 131 | -> [(IndexOf c, e)] -- ^ association list |
132 | -> c e -- ^ result | ||
133 | |||
134 | -- | element by element @case compare a b of LT -> l, EQ -> e, GT -> g@ | ||
135 | cond :: RealElement e | ||
136 | => c e -- ^ a | ||
137 | -> c e -- ^ b | ||
138 | -> c e -- ^ l | ||
139 | -> c e -- ^ e | ||
140 | -> c e -- ^ g | ||
141 | -> c e -- ^ result | ||
132 | 142 | ||
133 | -------------------------------------------------------------------------- | 143 | -------------------------------------------------------------------------- |
134 | 144 | ||