diff options
Diffstat (limited to 'examples/bool.hs')
-rw-r--r-- | examples/bool.hs | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/examples/bool.hs b/examples/bool.hs index 679b8bf..ee85523 100644 --- a/examples/bool.hs +++ b/examples/bool.hs | |||
@@ -1,17 +1,25 @@ | |||
1 | -- vectorized boolean operations defined in terms of step or cond | 1 | -- vectorized boolean operations defined in terms of step or cond |
2 | 2 | ||
3 | {-# LANGUAGE FlexibleContexts #-} | ||
4 | |||
3 | import Numeric.LinearAlgebra | 5 | import Numeric.LinearAlgebra |
4 | 6 | ||
5 | infix 4 .==., ./=., .<., .<=., .>=., .>. | 7 | infix 4 .==., ./=., .<., .<=., .>=., .>. |
6 | infixr 3 .&&. | 8 | infixr 3 .&&. |
7 | infixr 2 .||. | 9 | infixr 2 .||. |
8 | 10 | ||
9 | a .<. b = step (b-a) | 11 | -- specialized for Int result |
10 | a .<=. b = cond a b 1 1 0 | 12 | cond' |
11 | a .==. b = cond a b 0 1 0 | 13 | :: (Element t, Ord t, Container c I, Container c t) |
12 | a ./=. b = cond a b 1 0 1 | 14 | => c t -> c t -> c I -> c I -> c I -> c I |
13 | a .>=. b = cond a b 0 1 1 | 15 | cond' = cond |
14 | a .>. b = step (a-b) | 16 | |
17 | a .<. b = cond' a b 1 0 0 | ||
18 | a .<=. b = cond' a b 1 1 0 | ||
19 | a .==. b = cond' a b 0 1 0 | ||
20 | a ./=. b = cond' a b 1 0 1 | ||
21 | a .>=. b = cond' a b 0 1 1 | ||
22 | a .>. b = cond' a b 0 0 1 | ||
15 | 23 | ||
16 | a .&&. b = step (a*b) | 24 | a .&&. b = step (a*b) |
17 | a .||. b = step (a+b) | 25 | a .||. b = step (a+b) |
@@ -29,26 +37,22 @@ maxEvery a b = cond a b b b a | |||
29 | 37 | ||
30 | clip a b x = cond y b y y b where y = cond x a a x x | 38 | clip a b x = cond y b y y b where y = cond x a a x x |
31 | 39 | ||
32 | disp = putStr . dispf 3 | 40 | eye n = ident n :: Matrix R |
33 | |||
34 | eye n = ident n :: Matrix Double | ||
35 | row = asRow . fromList :: [Double] -> Matrix Double | ||
36 | col = asColumn . fromList :: [Double] -> Matrix Double | ||
37 | 41 | ||
38 | m = (3><4) [1..] :: Matrix Double | 42 | m = (3><4) [1..] :: Matrix R |
39 | 43 | ||
40 | p = row [0,0,1,1] | 44 | p = fromList [0,0,1,1] :: Vector I |
41 | q = row [0,1,0,1] | 45 | q = fromList [0,1,0,1] :: Vector I |
42 | 46 | ||
43 | main = do | 47 | main = do |
44 | print $ find (>6) m | 48 | print $ find (>6) m |
45 | disp $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] | 49 | disp 3 $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] |
46 | disp $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)] | 50 | disp 3 $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)] |
47 | disp $ m .>=. 10 .||. m .<. 4 | 51 | print $ m .>=. 10 .||. m .<. 4 |
48 | (disp . fromColumns . map flatten) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q] | 52 | (print . fromColumns) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q] |
49 | print $ taut $ (p `imp` q ) `equiv` (no q `imp` no p) | 53 | print $ taut $ (p `imp` q ) `equiv` (no q `imp` no p) |
50 | print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q) | 54 | print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q) |
51 | disp $ clip 3 8 m | 55 | disp 3 $ clip 3 8 m |
52 | disp $ col [1..7] .<=. row [1..5] | 56 | print $ col [1..7] .<=. row [1..5] |
53 | disp $ cond (col [1..3]) (row [1..4]) m 50 (3*m) | 57 | print $ cond (col [1..3]) (row [1..4]) m 50 (3*m) |
54 | 58 | ||