From 04ec1d6b547d6c48506d66298f7d09f7de22c96e Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 2 Oct 2015 20:31:28 +0200 Subject: bool --- examples/bool.hs | 46 +- examples/bool.ipynb | 1152 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1177 insertions(+), 21 deletions(-) create mode 100644 examples/bool.ipynb (limited to 'examples') 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 @@ -- vectorized boolean operations defined in terms of step or cond +{-# LANGUAGE FlexibleContexts #-} + import Numeric.LinearAlgebra infix 4 .==., ./=., .<., .<=., .>=., .>. infixr 3 .&&. infixr 2 .||. -a .<. b = step (b-a) -a .<=. b = cond a b 1 1 0 -a .==. b = cond a b 0 1 0 -a ./=. b = cond a b 1 0 1 -a .>=. b = cond a b 0 1 1 -a .>. b = step (a-b) +-- specialized for Int result +cond' + :: (Element t, Ord t, Container c I, Container c t) + => c t -> c t -> c I -> c I -> c I -> c I +cond' = cond + +a .<. b = cond' a b 1 0 0 +a .<=. b = cond' a b 1 1 0 +a .==. b = cond' a b 0 1 0 +a ./=. b = cond' a b 1 0 1 +a .>=. b = cond' a b 0 1 1 +a .>. b = cond' a b 0 0 1 a .&&. b = step (a*b) a .||. b = step (a+b) @@ -29,26 +37,22 @@ maxEvery a b = cond a b b b a clip a b x = cond y b y y b where y = cond x a a x x -disp = putStr . dispf 3 - -eye n = ident n :: Matrix Double -row = asRow . fromList :: [Double] -> Matrix Double -col = asColumn . fromList :: [Double] -> Matrix Double +eye n = ident n :: Matrix R -m = (3><4) [1..] :: Matrix Double +m = (3><4) [1..] :: Matrix R -p = row [0,0,1,1] -q = row [0,1,0,1] +p = fromList [0,0,1,1] :: Vector I +q = fromList [0,1,0,1] :: Vector I main = do print $ find (>6) m - disp $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] - disp $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)] - disp $ m .>=. 10 .||. m .<. 4 - (disp . fromColumns . map flatten) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q] + disp 3 $ assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] + disp 3 $ accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)] + print $ m .>=. 10 .||. m .<. 4 + (print . fromColumns) [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q] print $ taut $ (p `imp` q ) `equiv` (no q `imp` no p) print $ taut $ (xor p q) `equiv` (p .&&. no q .||. no p .&&. q) - disp $ clip 3 8 m - disp $ col [1..7] .<=. row [1..5] - disp $ cond (col [1..3]) (row [1..4]) m 50 (3*m) + disp 3 $ clip 3 8 m + print $ col [1..7] .<=. row [1..5] + print $ cond (col [1..3]) (row [1..4]) m 50 (3*m) diff --git a/examples/bool.ipynb b/examples/bool.ipynb new file mode 100644 index 0000000..abceeb4 --- /dev/null +++ b/examples/bool.ipynb @@ -0,0 +1,1152 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# vectorized boolean operations" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import Numeric.LinearAlgebra\n", + ":ext FlexibleContexts" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## pretty printing" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false, + "scrolled": true + }, + "outputs": [], + "source": [ + "import IHaskell.Display\n", + ":ext FlexibleInstances\n", + "\n", + "dec = 3\n", + "\n", + "dispBool = (\"\\n\"++) . format \" \" f\n", + " where\n", + " f 1 = \"\\\\top\"\n", + " f 0 = \"\\\\cdot\"\n", + "\n", + "instance IHaskellDisplay (Matrix I) where\n", + " display m = return $ Display [html (\"

$$\"++(latexFormat \"bmatrix\" . dispBool) m++\"$$

\")]\n", + "\n", + "instance IHaskellDisplay (Matrix C) where\n", + " display m = return $ Display [html (\"

$$\"++(latexFormat \"bmatrix\" . dispcf dec) m++\"$$

\")]\n", + "\n", + "instance IHaskellDisplay (Matrix R) where\n", + " display m = return $ Display [html (\"

$$\"++ (latexFormat \"bmatrix\" . dispf dec) m++\"$$

\")]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## definitions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "vectorized operators defined in terms of `step` and `cond`" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "-- specialized for Int result\n", + "cond'\n", + " :: (Element t, Ord t, Container c I, Container c t)\n", + " => c t -> c t -> c I -> c I -> c I -> c I\n", + "cond' = cond" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "infix 4 .==., ./=., .<., .<=., .>=., .>.\n", + "infixr 3 .&&.\n", + "infixr 2 .||.\n", + "\n", + "a .<. b = cond' a b 1 0 0\n", + "a .<=. b = cond' a b 1 1 0\n", + "a .==. b = cond' a b 0 1 0\n", + "a ./=. b = cond' a b 1 0 1\n", + "a .>=. b = cond' a b 0 1 1\n", + "a .>. b = cond' a b 0 0 1\n", + "\n", + "a .&&. b = step (a*b)\n", + "a .||. b = step (a+b)\n", + "no a = 1-a\n", + "xor a b = a ./=. b\n", + "equiv a b = a .==. b\n", + "imp a b = no a .||. b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "other useful functions" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "taut x = minElement x == 1\n", + "\n", + "minEvery a b = cond a b a a b\n", + "maxEvery a b = cond a b b b a\n", + "\n", + "eye n = ident n :: Matrix R\n", + "\n", + "clip a b x = cond y b y y b\n", + " where\n", + " y = cond x a a x x" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## examples" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "\\top & \\top & \\top & \\top & \\top\n", + "\\\\\n", + "\\cdot & \\top & \\top & \\top & \\top\n", + "\\\\\n", + "\\cdot & \\cdot & \\top & \\top & \\top\n", + "\\\\\n", + "\\cdot & \\cdot & \\cdot & \\top & \\top\n", + "\\\\\n", + "\\cdot & \\cdot & \\cdot & \\cdot & \\top\n", + "\\\\\n", + "\\cdot & \\cdot & \\cdot & \\cdot & \\cdot\n", + "\\\\\n", + "\\cdot & \\cdot & \\cdot & \\cdot & \\cdot\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "col [1..7] .<=. row [1..5]" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "m = (3><4) [1..] :: Matrix R" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "1 & 2 & 3 & 4\n", + "\\\\\n", + "5 & 6 & 7 & 8\n", + "\\\\\n", + "9 & 10 & 11 & 12\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "m" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "3 & 3 & 3 & 4\n", + "\\\\\n", + "5 & 6 & 7 & 8\n", + "\\\\\n", + "8 & 8 & 8 & 8\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "clip 3 8 m" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1,2),(1,3),(2,0),(2,1),(2,2),(2,3)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "find (>6) m" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "\\top & \\top & \\top & \\cdot\n", + "\\\\\n", + "\\cdot & \\cdot & \\cdot & \\cdot\n", + "\\\\\n", + "\\cdot & \\top & \\top & \\top\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(m .>=. 10) .||. (m .<. 4)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "50 & 2 & 3 & 4\n", + "\\\\\n", + "15 & 50 & 7 & 8\n", + "\\\\\n", + "27 & 30 & 50 & 12\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "cond (col [1..3]) (row [1..4]) m 50 (3*m)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(6><8)\n", + " [ 10, 7, 7, 7, 7, 7, 7, 7\n", + " , 7, 11, 7, 7, 7, 7, 7, 7\n", + " , 7, 7, 12, 7, 7, 7, 7, 7\n", + " , 7, 7, 7, 13, 7, 7, 7, 7\n", + " , 7, 7, 7, 7, 14, 7, 7, 7\n", + " , 7, 7, 7, 7, 7, 7, 7, 7 ]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "assoc (6,8) 7 $ zip (find (/=0) (eye 5)) [10..] :: Matrix Z" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "1 & 0 & 3 & 0 & 0\n", + "\\\\\n", + "0 & 2 & 0 & 0 & 0\n", + "\\\\\n", + "0 & 0 & 1 & 0 & 0\n", + "\\\\\n", + "0 & 7 & 0 & 1 & 0\n", + "\\\\\n", + "0 & 0 & 0 & 0 & 1\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "accum (eye 5) (+) [((0,2),3), ((3,1),7), ((1,1),1)]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "p = fromList [0,0,1,1] :: Vector I\n", + "q = fromList [0,1,0,1] :: Vector I" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "

$$\\begin{bmatrix}\n", + "\\cdot & \\cdot & \\cdot & \\cdot & \\cdot & \\top & \\top\n", + "\\\\\n", + "\\cdot & \\top & \\cdot & \\top & \\top & \\cdot & \\top\n", + "\\\\\n", + "\\top & \\cdot & \\cdot & \\top & \\top & \\cdot & \\cdot\n", + "\\\\\n", + "\\top & \\top & \\top & \\top & \\cdot & \\top & \\top\n", + "\\end{bmatrix}$$

" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "fromColumns [p, q, p.&&.q, p .||.q, p `xor` q, p `equiv` q, p `imp` q]" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "taut $ (p `imp` q ) `equiv` (no q `imp` no p)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "taut $ xor p q `equiv` (p .&&. no q .||. no p .&&. q)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Haskell", + "language": "haskell", + "name": "haskell" + }, + "language_info": { + "codemirror_mode": "ihaskell", + "file_extension": ".hs", + "name": "haskell", + "version": "7.10.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} -- cgit v1.2.3