{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Drawing Heptagons Using Integers\n", "\n", "Would it surprise you to learn that the two figures below were completely generated by manipulating integers?\n", "\n", "\n", "\n", "OK, to be entirely accurate, at the very end of the computation, when it is necessary to provide floating point values to the graphics library for drawing, there are exactly four irrational numbers used. Up until that time, every point being manipulated is represented by a six-tuple of integers.\n", "\n", "Perhaps that does not surprise you too much. Consider these additional facts:\n", "\n", " * Every intersection point you can see in the figures can be represented in the same way, as a six-tuple of integers.\n", " * The seven-fold rotation used in the computation is encoded using only integers.\n", " * These facts would all remain true no matter many times the nested heptagons or the spiral were iterated inward (scaling down) or outward (scaling up).\n", " \n", "Now you should be surprised, unless you already know a fair bit of college-level or even graduate-level mathematics. This article, and two successive articles, will explain how it all works, and provide Python code to let you reproduce the figures or just play. The math you need to already know is *easy* -- a little middle school algebra and geometry.\n", "\n", "Along the way, you'll get a few pointers into the relevant parts of college-level mathematics, in case you want to dig deeper. I hope that, like me, you'll find some joy and beauty in the mathematics that was never really evident when you learned it in school." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 1: Heptagon Numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peter Steinbach first [described](http://sylvester.math.nthu.edu.tw/d2/imo-training-geometry/reg-poly.pdf) how each regular polygon generates a system of numbers,\n", "self-contained under arithmetic, based on the relative lengths of its diagonals (the lines crossing the interior of the polygon).\n", "I first learned about this in an excellent [paper by Kappraff](http://www.mi.sanu.ac.rs/vismath/kappraff1/).\n", "In the case of the heptagon, a regular polygon with seven sides, if we say that the edges of the heptagon (red lines below) have length 1, then we can say that the shorter (purple) diagonals have length $\\rho$, and the longer (blue) diagonals have length $\\sigma$. \n", "\n", "\n", "\n", "These diagonal-to-edge ratios are the same for all regular heptagons, of course. Let's worry about the actual *values* of $\\rho$ and $\\sigma$ later. You will find that we can accomplish quite a lot without knowing those values!\n", "\n", "How do $\\rho$ and $\\sigma$ generate a \"system of numbers\"? Take another look at the heptagon above (and its inscribed heptagrams). Notice all the parallel lines? You should be able to find an abundance of similar triangles -- triangles that have the same angles but different sizes. These similar triangles generate a set of relationships between $\\rho$ and $\\sigma$. This becomes clearer if we hide most of the diagonals:\n", "\n", "\n", "\n", "For example, see if you can convince yourself that\n", "\n", "$$\\sigma = \\rho + \\frac{1}{\\sigma}$$\n", "\n", "by looking at the large blue-red-purple-red trapezoid that runs through horizontally across the heptagon. The proof relies on constructing a proportion, based on the similar triangles involved:\n", "\n", "$$\\sigma : 1 = 1 : \\sigma-\\rho$$\n", "\n", "Corresponding exercises can fill in the table of basic quotients:\n", "\n", "$$\\frac{1}{\\sigma} = \\sigma-\\rho \\bbox[10pt]{} \\frac{1}{\\rho} = 1+\\rho-\\sigma \\bbox[10pt]{} \\frac{\\sigma}{\\rho} = \\sigma-1 \\bbox[10pt]{} \\frac{\\rho}{\\sigma} = \\rho-1$$\n", "\n", "A little bit of algebraic manipulation with those can yield the basic products, as well:\n", "\n", "$$\\rho\\sigma = \\rho+\\sigma \\bbox[10pt]{} \\rho^2 = 1+\\sigma \\bbox[10pt]{} \\sigma^2 = 1+\\rho+\\sigma$$\n", "\n", "(You could also use Steinbach's elegant **diagonal product formula** to derive the products.)\n", "\n", "Here you can start to see some of the special properties of $\\rho$ and $\\sigma$. For example, using the division and multiplication rules above, you can reduce any expression like $\\rho^m\\sigma^n$, for any integer powers $m$ and $n$ (including negative powers) to a simple expression of the form:\n", "\n", "$$a+b\\rho+c\\sigma$$\n", "\n", "for some integers $a$, $b$, and $c$. Let us try an example. The strategy is to substitute for $\\rho\\sigma$, $\\sigma^2$, and $\\rho^2$ at every opportunity, then distribute and multiply, and repeat, until we can collect all the terms.\n", "\n", "$$\\rho^3\\sigma^2 = \\rho^2\\rho\\sigma^2$$\n", "\n", "$$ = (1+\\sigma)\\rho(1+\\rho+\\sigma)$$\n", "\n", "$$ = (\\rho+\\rho\\sigma)(1+\\rho+\\sigma)$$\n", "\n", "$$= (\\rho+\\rho+\\sigma)(1+\\rho+\\sigma)$$\n", "\n", "$$ = (2\\rho+\\sigma)(1+\\rho+\\sigma)$$\n", "\n", "$$= (2\\rho+\\sigma) + (2\\rho^2+\\rho\\sigma) + (2\\rho\\sigma+\\sigma^2)$$\n", "\n", "$$= 2\\rho+\\sigma + 2+2\\sigma + \\rho+\\sigma + 2\\rho+2\\sigma + 1+\\rho+\\sigma$$\n", "\n", "$$= 3 + 6\\rho + 7\\sigma$$\n", "\n", "\n", "Thus $a+b\\rho+c\\sigma$ is the general form of any number in our new system. We will call this a **heptagon number**, in the same way we call $x+yi$ a complex number, or $u/v$ a rational number. In all such cases we need not be bothered by the fact that a number consists of more than just numerals. We care more about the fact that these numbers can be added, subtracted, multiplied, and divided, and still produce more numbers of the same form. Mathematicians use the term *field* for such a system of numbers.\n", "\n", "Given two heptagon numbers $a+b\\rho+c\\sigma$ and $d+e\\rho+f\\sigma$, it is trivial to see that addition and subtraction produce more heptagon numbers:\n", "\n", "$$(a+b\\rho+c\\sigma) + (d+e\\rho+f\\sigma) = (a+d)+(b+e)\\rho+(c+f)\\sigma$$\n", "\n", "$$(a+b\\rho+c\\sigma) - (d+e\\rho+f\\sigma) = (a-d)+(b-e)\\rho+(c-f)\\sigma$$\n", "\n", "In math-speak, the heptagon numbers are *closed* under addition and subtraction because the integers are closed under those operations: $a+d$, $b-e$, etc. are all integers.\n", "\n", "Demonstrating that the heptagon numbers are closed under multiplication is much more interesting:\n", "\n", "$$(a+b\\rho+c\\sigma) * (d+e\\rho+f\\sigma) =$$\n", "\n", "$$(ad + ae\\rho + af\\sigma) + (bd\\rho + be\\rho^2 + bf\\rho\\sigma)\n", "+ (cd\\sigma + ce\\rho\\sigma + cf\\sigma^2) =$$\n", "\n", "$$ad + ae\\rho + af\\sigma + bd\\rho + (be\\sigma+be) + (bf\\rho+bf\\sigma)\n", "+ cd\\sigma + (ce\\rho+ce\\sigma) + (cf+cf\\rho+cf\\sigma) =$$\n", "\n", "$$(ad+be+cf) +\n", "(ae+bd+bf+ce+cf)\\rho +\n", "(af+be+bf+cd+ce+cf)\\sigma$$\n", "\n", "Notice that we have derived the formulas for addition, subtraction, and multiplication while performing those demonstrations. Deriving the formula for division of heptagon numbers is considerably more difficult, requiring some linear algebra. Also, to perform division, we would require *rational* coefficients $a$, $b$, and $c$ in every heptagon number, since the integers are not closed under division. Fortunately, we don't need to do any division for our purposes here, so we can be satisfied with the comfortable ease of doing integer arithmetic." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Implementing Heptagon Numbers in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python code below is a simple implementation of the arithmetic of heptagon numbers (except for division). The code is written in a very basic fashion, because I want you to be able to understand the code.\n", "\n", "After a glance at the code, you can see that\n", "\n", " (a,b,c)\n", "\n", "is the Python representation for the heptagon number $a+b\\rho+c\\sigma$. The only Python \"trick\" I employ is the tuple unpacking assignment, which improves the readability of the code:\n", "\n", " a, b, c = h\n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 1, 1)\n", "(1, 0, 1)\n", "(1, 1, 1)\n", "(1, 0, 0)\n", "(0, -2, 1)\n" ] } ], "source": [ "zero = (0,0,0)\n", "one = (1,0,0)\n", "rho = (0,1,0)\n", "sigma = (0,0,1)\n", "rho_inv = (1,1,-1)\n", "sigma_inv = (0,-1,1)\n", "\n", "def neg( h ) :\n", " a, b, c = h # this assigment syntax \"unpacks\" the tuple\n", " return ( -a, -b, -c )\n", "\n", "def plus( h1, h2 ) :\n", " a, b, c = h1\n", " d, e, f = h2\n", " return ( a+d, b+e, c+f )\n", "\n", "def minus( h1, h2 ) :\n", " a, b, c = h1\n", " d, e, f = h2\n", " return ( a-d, b-e, c-f )\n", "\n", "def times( h1, h2 ) :\n", " a, b, c = h1\n", " d, e, f = h2\n", " return ( a*d + b*e + c*f,\n", " a*e + b*d + b*f + c*e + c*f,\n", " a*f + b*e + b*f + c*d + c*e + c*f )\n", "\n", "rho_sigma = times( rho, sigma )\n", "rho_2 = times( rho, rho )\n", "sigma_2 = times( sigma, sigma )\n", "\n", "# some quick-and-dirty unit testing\n", "\n", "print( rho_sigma ) # should be (0, 1, 1)\n", "print( rho_2 ) # should be (1, 0, 1)\n", "print( sigma_2 ) # should be (1, 1, 1)\n", "print( times( sigma, sigma_inv ) ) # should be (1, 0, 0)\n", "print( times( (1,-1,-1), (0,-1,1) ) ) # should be (0, -2, 1)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Improving Code Readability\n", "\n", "While the implementation above is simple and perfectly functional, it is a bit awkward to code arithmetic expressions using function notation. Python does have the notion of operator overloading using special function names, so we can provide an implementation that is much more natural when used, and only a little more complicated than the code above.\n", "\n", "In addition to supporting operator overloading, we can easily support mixed expressions using Python's \"duck typing\", as long as we're willing to write $\\rho+1$ rather than $1+\\rho$, for example.\n", "\n", "Finally, this implementation supports automatic conversion of heptagon numbers to strings, so output is more readable, as well." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ρρ = 1+σ\n", "ρσ = ρ+σ\n", "σσ = 1+ρ+σ\n", "(σ)(-1ρ+σ) = 1\n", "(ρ)(1+ρ+-1σ) = 1\n", "(-1+ρ)(-1+σ) = 1\n" ] } ], "source": [ "import math\n", "\n", "class HeptagonNumber(object):\n", "\n", " def __init__( self, ones=0, rhos=0, sigmas=0 ):\n", " self.ones = ones\n", " self.rhos = rhos\n", " self.sigmas = sigmas\n", "\n", " def __add__( self, rhs ): #self and rhs are HeptagonNumber objects\n", " a,b,c = self.ones, self.rhos, self.sigmas\n", " if isinstance( rhs, self.__class__ ):\n", " d,e,f = rhs.ones, rhs.rhos, rhs.sigmas\n", " return HeptagonNumber( a+d, b+e, c+f )\n", " elif isinstance( rhs, int ):\n", " return HeptagonNumber( a+rhs, b, c )\n", " else:\n", " raise TypeError(\"unsupported operand type(s) for +: '{}' and '{}'\").format(self.__class__, type(other))\n", "\n", " def __sub__( self, rhs ): #self and rhs are HeptagonNumber objects\n", " a,b,c = self.ones, self.rhos, self.sigmas\n", " if isinstance( rhs, self.__class__ ):\n", " d,e,f = rhs.ones, rhs.rhos, rhs.sigmas\n", " return HeptagonNumber( a-d, b-e, c-f )\n", " elif isinstance( rhs, int ):\n", " return HeptagonNumber( a-rhs, b, c )\n", " else:\n", " raise TypeError(\"unsupported operand type(s) for +: '{}' and '{}'\").format(self.__class__, type(other))\n", "\n", " def __neg__( self ) :\n", " return HeptagonNumber( -self.ones, -self.rhos, -self.sigmas )\n", "\n", " def __mul__( self, rhs ) :\n", " a,b,c = self.ones, self.rhos, self.sigmas\n", " if isinstance( rhs, self.__class__ ):\n", " d,e,f = rhs.ones, rhs.rhos, rhs.sigmas\n", " return HeptagonNumber( a*d + b*e + c*f,\n", " a*e + b*d + b*f + c*e + c*f,\n", " a*f + b*e + b*f + c*d + c*e + c*f )\n", " elif isinstance( rhs, int ):\n", " return HeptagonNumber( a*rhs, b*rhs, c*rhs )\n", " else:\n", " raise TypeError(\"unsupported operand type(s) for +: '{}' and '{}'\").format(self.__class__, type(other))\n", "\n", " def __str__( self ):\n", " s = u\"\"\n", " if self.ones != 0 :\n", " s = s + u\"%d\" % ( self.ones )\n", " if self.rhos != 0 :\n", " if len(s) > 0 :\n", " s = s + u\"+\"\n", " if self.rhos != 1 :\n", " s = s + u\"%d\" % ( self.rhos )\n", " s = s + u\"\\u03C1\"\n", " if self.sigmas != 0 :\n", " if len(s) > 0 :\n", " s = s + u\"+\"\n", " if self.sigmas != 1 :\n", " s = s + u\"%d\" % ( self.sigmas )\n", " s = s + u\"\\u03C3\"\n", " return s.encode('utf-8') \n", "\n", " # These values will be derived in Part 2\n", " \n", " sigma_real = 1 / (2* math.sin(math.pi/14))\n", "\n", " rho_real = 2*math.sin(5*math.pi/14)\n", "\n", " def __float__( self ) :\n", " return self.ones + self.rho_real * self.rhos + self.sigma_real * self.sigmas\n", "\n", "zero = HeptagonNumber()\n", "one = HeptagonNumber(1)\n", "rho = HeptagonNumber(0,1)\n", "sigma = HeptagonNumber(0,0,1)\n", "\n", "# Now we can use natural operators for arithmetic,\n", "# as long as we put integers on the right.\n", "sigma_inv = sigma - rho\n", "rho_inv = rho - sigma + 1\n", "rho_over_sigma = rho - 1\n", "sigma_over_rho = sigma - 1\n", "\n", "def printProd( h1, h2 ) :\n", " print str(h1) + str(h2) + \" = \" + str(h1*h2)\n", " \n", "def printProd2( h1, h2 ) :\n", " print \"(\" + str(h1) + \")(\" + str(h2) + \") = \" + str(h1*h2)\n", " \n", "printProd( rho, rho )\n", "printProd( rho, sigma )\n", "printProd( sigma, sigma )\n", "printProd2( sigma, sigma_inv )\n", "printProd2( rho, rho_inv )\n", "printProd2( rho_over_sigma, sigma_over_rho )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Summary, and a Look Ahead" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In [Part 2](https://nbviewer.org/github/vorth/ipython/blob/master/heptagons/DrawingTheHeptagon.ipynb) of this story, I will show you how to use heptagon numbers to draw regular heptagons and heptagrams. In [Part 3](https://nbviewer.org/github/vorth/ipython/blob/master/heptagons/Sevenfold%20Rotation.ipynb), I will show how we can implement a rotation operator without leaving the heptagon numbers.\n", "\n", "For now, there are many interesting explorations you can make with heptagon numbers. For example, it is possible to implement functions for \"scale by $\\rho^n$\" and \"scale by $\\sigma^n$\" using only addition (and subtraction, for negative $n$), and looping, without any multiplication. Give it a try!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "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 }