{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Logik\n", "\n", "* `and`: und\n", "* `or`: oder\n", "* `not`: negation\n", "* `^`: xor\n", "\n", "Wahr, Falsch oder [Unbekannt](http://de.wikipedia.org/wiki/Unvollständigkeitssatz)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and True" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not True or not False" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not (True or False)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True ^ False" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True ^ True" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not (False or False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Wahrheitstabelle" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fmt = lambda t : \"X\" if t else \".\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def print_table(prop):\n", " from itertools import product\n", "\n", " print(\"A B C => P\")\n", " print(\"-\" * 14)\n", "\n", " for state in product([True, False], repeat=3):\n", " args = ' '.join(fmt(s) for s in state)\n", " res = prop(*state)\n", " print(\"{} => {}\".format(args, fmt(res)))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A B C => P\n", "--------------\n", "X X X => X\n", "X X . => .\n", "X . X => X\n", "X . . => .\n", ". X X => X\n", ". X . => .\n", ". . X => .\n", ". . . => .\n" ] } ], "source": [ "prop1 = lambda a, b, c : (a or (not a and b)) and c\n", "print_table(prop1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A B C => P\n", "--------------\n", "X X X => X\n", "X X . => .\n", "X . X => X\n", "X . . => .\n", ". X X => X\n", ". X . => .\n", ". . X => .\n", ". . . => .\n" ] } ], "source": [ "prop2 = lambda a, b, c : (a or b) and c\n", "print_table(prop2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A B C => P\n", "--------------\n", "X X X => .\n", "X X . => X\n", "X . X => X\n", "X . . => .\n", ". X X => .\n", ". X . => .\n", ". . X => .\n", ". . . => .\n" ] } ], "source": [ "prop3 = lambda a, b, c : a and (b ^ c)\n", "print_table(prop3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Addition zweier Boolscher Vektoren\n", "\n", "Die zwei Vektoren werden nach den üblichen Regeln addiert." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = [True, True, False, True, True, False]\n", "b = [True, True, False, False]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def boolean_add(a, b):\n", " size = max(len(a), len(b))\n", " # padding\n", " for v in [a, b]:\n", " while len(v) < size:\n", " v.insert(0, False)\n", " \n", " result = []\n", " c = False\n", " for pos in reversed(range(size)):\n", " s = a[pos] ^ b[pos] ^ c\n", " c = (a[pos] and b[pos]) or (c and (a[pos] ^ b[pos]))\n", " result.insert(0, s)\n", " result.insert(0, c)\n", " return result" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[True, False, False, False, False, True, False]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ab = boolean_add(a, b)\n", "ab" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def boolean_to_decimal(b):\n", " result = 0\n", " for i, x in enumerate(reversed(b)):\n", " result += 2**i * x\n", " return result" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "54\n", "12\n", "66\n" ] } ], "source": [ "print boolean_to_decimal(a)\n", "print boolean_to_decimal(b)\n", "print boolean_to_decimal(ab)" ] } ], "metadata": { "kernelspec": { "display_name": "Anaconda (Python 3)", "language": "python", "name": "anaconda3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }