summaryrefslogtreecommitdiff
path: root/examples/bool.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/bool.hs')
-rw-r--r--examples/bool.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/examples/bool.hs b/examples/bool.hs
new file mode 100644
index 0000000..679b8bf
--- /dev/null
+++ b/examples/bool.hs
@@ -0,0 +1,54 @@
1-- vectorized boolean operations defined in terms of step or cond
2
3import Numeric.LinearAlgebra
4
5infix 4 .==., ./=., .<., .<=., .>=., .>.
6infixr 3 .&&.
7infixr 2 .||.
8
9a .<. b = step (b-a)
10a .<=. b = cond a b 1 1 0
11a .==. b = cond a b 0 1 0
12a ./=. b = cond a b 1 0 1
13a .>=. b = cond a b 0 1 1
14a .>. b = step (a-b)
15
16a .&&. b = step (a*b)
17a .||. b = step (a+b)
18no a = 1-a
19xor a b = a ./=. b
20equiv a b = a .==. b
21imp a b = no a .||. b
22
23taut x = minElement x == 1
24
25minEvery a b = cond a b a a b
26maxEvery a b = cond a b b b a
27
28-- examples
29
30clip a b x = cond y b y y b where y = cond x a a x x
31
32disp = putStr . dispf 3
33
34eye n = ident n :: Matrix Double
35row = asRow . fromList :: [Double] -> Matrix Double
36col = asColumn . fromList :: [Double] -> Matrix Double
37
38m = (3><4) [1..] :: Matrix Double
39
40p = row [0,0,1,1]
41q = row [0,1,0,1]
42
43main = do
44 print $ find (>6) m
45 disp $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..]
46 disp $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)]
47 disp $ m .>=. 10 .||. m .<. 4
48 (disp . fromColumns . map flatten) [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)
50 print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q)
51 disp $ clip 3 8 m
52 disp $ col [1..7] .<=. row [1..5]
53 disp $ cond (col [1..3]) (row [1..4]) m 50 (3*m)
54