{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In lecture 8 of mcs 320, we consider evaluation and execution." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Addition and Multiplication Tables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For finite fields, we can tabulate the outcomes of the addition and multiplications of all elements in the field. Let us make a field with four elements. Because four is not a prime number, we cannot use the modulo four integers." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "x^2 + x + 1" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Z2 = Integers(2)\n", "P. = Z2[]\n", "q = x^2 + x + 1\n", "factor(q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a polynomial ``q`` in ``x`` of degree 2 with modulo two coefficients which is irreducible." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, a, a + 1]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Q. = Z2.extension(q)\n", "list(Q)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the addition table:\n", "[0, 1, a, a + 1]\n", "[1, 0, a + 1, a]\n", "[a, a + 1, 0, 1]\n", "[a + 1, a, 1, 0]\n" ] } ], "source": [ "print('the addition table:')\n", "for x in Q:\n", " print([x+y for y in Q])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the multiplication table:\n", "[0, 0, 0, 0]\n", "[0, 1, a, a + 1]\n", "[0, a, a + 1, 1]\n", "[0, a + 1, 1, a]\n" ] } ], "source": [ "print('the multiplication table:')\n", "for x in Q:\n", " print([x*y for y in Q])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In ``Q`` we have the following properties:\n", "\n", "1. every number has an inverse with respect to the addition,\n", "\n", "2. every nonzero number has a multiplicative inverse.\n", "\n", "These properties make that ``Q`` is a field." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Observe the addition and multiplication tables of the ``GF(4)`` the Galois field of size 4." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "+ a b c d\n", " +--------\n", "a| a b c d\n", "b| b a d c\n", "c| c d a b\n", "d| d c b a\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GF(4).addition_table()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "* a b c d\n", " +--------\n", "a| a a a a\n", "b| a b c d\n", "c| a c d b\n", "d| a d b c\n" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "GF(4).multiplication_table()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Binary Expression Trees" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A binary expression tree is a binary tree with leaves and nodes:\n", "\n", "1. The leaves are either constants or operands.\n", "\n", "2. The notes are the arithmetical operations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us make an example: $x^3 + 4 x y^3$." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "add(ipow(v_0, 3), mul(mul(4, v_0), ipow(v_1, 3)))\n" ] } ], "source": [ "from sage.ext.fast_callable import ExpressionTreeBuilder\n", "etb = ExpressionTreeBuilder(vars=['x','y'])\n", "x = etb.var('x')\n", "y = etb.var('y')\n", "p = x^3 + 4*x*y^3\n", "print(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To draw the tree, we start with the six leaves." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "L1 = LabelledBinaryTree([None, None], label='v_0')\n", "L2 = LabelledBinaryTree([None, None], label='3')\n", "L3 = LabelledBinaryTree([None, None], label='4')\n", "L4 = LabelledBinaryTree([None, None], label='v_1')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two of the leaves appear twice, we do not make duplicate leaves." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " _*_\n", " / \\\n", "v_0 3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N12 = LabelledBinaryTree([L1, L2], label=\"*\")\n", "ascii_art(N12)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " *\n", " / \\\n", "4 v_0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N31 = LabelledBinaryTree([L3, L1], label='*')\n", "ascii_art(N31)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " _^_\n", " / \\\n", "v_1 3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N42 = LabelledBinaryTree([L4, L2], label='^')\n", "ascii_art(N42)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " ___*____\n", " / \\\n", " * _^_\n", " / \\ / \\\n", "4 v_0 v_1 3" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "N3142 = LabelledBinaryTree([N31, N42], label=\"*\")\n", "ascii_art(N3142)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " _____+_____\n", " / \\\n", " _*_ ___*____\n", " / \\ / \\\n", "v_0 3 * _^_\n", " / \\ / \\\n", " 4 v_0 v_1 3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final= LabelledBinaryTree([N12, N3142], label=\"+\")\n", "ascii_art(final)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To see a representation of the program listing to evaluate ``p``, we work as follows." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('load_arg', 0),\n", " ('ipow', 3),\n", " ('load_const', 4),\n", " ('load_arg', 0),\n", " 'mul',\n", " ('load_arg', 1),\n", " ('ipow', 3),\n", " 'mul',\n", " 'add',\n", " 'return']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = fast_callable(p)\n", "f.op_list()" ] } ], "metadata": { "kernelspec": { "display_name": "SageMath 10.3", "language": "sage", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10" } }, "nbformat": 4, "nbformat_minor": 4 }