{ "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 }