{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# _`expression`_`.multiply`\n", "\n", "This function is overloaded, it supports these signatures:\n", "\n", "- _`expression`_`.multiply(`_`exp`_`)`\n", "\n", " The product (i.e., the concatenation) of two expressions: `a.multiply(b)` => `ab`.\n", "\n", "- _`expression`_`.multiply(`_`num`_`)`\n", "\n", " The repeated multiplication (concatenation) of an expression with itself: `a.multiply(3)` => `aaa`. Exponent `-1` denotes the infinity: the Kleene star.\n", "\n", "- _`expression`_`.multiply((`_`min`_`, `_`max`_`))`\n", "\n", " The sum of repeated multiplications of an expression: `a.multiply((2,4))` => `aa+aaa+aaaa`. When `min = -1`, it denotes `0`, when `max = -1`, it denotes the infinity.\n", "\n", "Preconditions:\n", "- _`min`_ ` <= ` _`max`_\n", "\n", "See also:\n", "- [automaton.multiply](automaton.multiply.ipynb)\n", "- [polynomial.multiply](polynomial.multiply.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Examples" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import vcsn\n", "exp = vcsn.context('law_char, q').expression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple Multiplication\n", "Instead of `a.multiply(b)`, you may write `a * b`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "${\\mathit{a}}^{*} \\, \\mathit{b} \\, \\mathit{a} \\, {\\mathit{b}}^{*}$" ], "text/plain": [ "a*bab*" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('a*b') * exp('ab*')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, trivial identities are applied." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$ \\left\\langle 6 \\right\\rangle \\,\\mathit{a}$" ], "text/plain": [ "<6>a" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('<2>a') * exp('<3>\\e')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\emptyset$" ], "text/plain": [ "\\z" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('<2>a') * exp('\\z')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the case of word labels, adjacent words are not fused: concatenation of two expressions behaves as if the expressions were parenthetized. Pay attention to the space between $a$ and $b$ below, admittedly too discreet." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "1\n", "\n", "a\n", "\n", "\n", "2\n", "\n", "b\n", "\n", "\n", "0\n", "\n", ".\n", "\n", "\n", "0->1\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(exp('a') * exp('b')).SVG() # Two one-letter words" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "0\n", "\n", "ab\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab').SVG() # One two-letter word" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "%3\n", "\n", "\n", "1\n", "\n", "a\n", "\n", "\n", "2\n", "\n", "b\n", "\n", "\n", "0\n", "\n", ".\n", "\n", "\n", "0->1\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "\n", "\n", "" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('(a)(b)').SVG() # Two one-letter words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Repeated Multiplication\n", "Instead of `a.multiply(3)`, you may write `a ** 3`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\left(\\mathit{ab}\\right)^{3}$" ], "text/plain": [ "(ab){3}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** 3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\varepsilon$" ], "text/plain": [ "\\e" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Beware that `a * 3` actually denotes `a.rweight(3)`." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$ \\left\\langle 3 \\right\\rangle \\,{\\mathit{a}}^{*}$" ], "text/plain": [ "<3>a*" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('a*') * 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the exponent -1 to mean `infinity`. Alternatively, you may invoke `a.star` instead of `a ** -1`." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\left(\\mathit{ab}\\right)^{*}$" ], "text/plain": [ "(ab)*" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** -1" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\left(\\mathit{ab}\\right)^{*}$" ], "text/plain": [ "(ab)*" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab').star()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sums of Repeated Multiplications\n", "Instead of `a.multiply((2, 4))`, you may write `a ** (2, 4)`. Again, use exponent -1 to mean infinity." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\left(\\mathit{ab}\\right)^{2}$" ], "text/plain": [ "(ab){2}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** (2, 2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\left(\\mathit{ab}\\right)^{2} \\, \\left(\\varepsilon + \\mathit{ab} + \\left(\\mathit{ab}\\right)^{2}\\right)$" ], "text/plain": [ "(ab){2}(\\e+ab+(ab){2})" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** (2, 4)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\varepsilon + \\mathit{ab} + \\left(\\mathit{ab}\\right)^{2}$" ], "text/plain": [ "\\e+ab+(ab){2}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** (-1, 2)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/latex": [ "$\\left(\\mathit{ab}\\right)^{2} \\, \\left(\\mathit{ab}\\right)^{*}$" ], "text/plain": [ "(ab){2}(ab)*" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp('ab') ** (2, -1)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.5.1" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }