{ "metadata": { "name": "house_locations.ipynb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "A Meditation on House Locations\n", "===============================\n", "\n", "Author: Aaron Watters\n", "\n", "I created a Notebook that describes how to examine, illustrate, and solve a geometric mathematical problem called \"House Location\" using Python mathematical and numeric libraries. The discussion uses symbolic computation, visualization, and numerical computations to solve the problem while exercising the NumPy, SymPy, Matplotlib, IPython and SciPy packages.\n", "\n", "I hope that this discussion will be accessible to people with a minimal background in programming and a high-school level background in algebra and analytic geometry. There is a brief mention of complex numbers, but the use of complex numbers is not important here except as \"values to be ignored\". I also hope that this discussion illustrates how to combine different mathematically oriented Python libraries and explains how to smooth out some of the rough edges between the library interfaces.\n", "\n", "\n", "\n", "\n", "The House Location Problem\n", "==========================\n", "\n", "The House Location Problem is taken from the HackerRank.com programming\n", "challenges and is described here: \n", ".\n", "The input to the problem are the coordinates of Kimberly, Bob, Jack, and Janet's houses\n", "and two ratios 'a' and 'b'. The goal of the problem is to find a location (x,y) where\n", "\n", "The distance to Kimberly's house = 'a' times the distance to Bob's house\n", "\n", "AND\n", "\n", "The distance to Janet's house = 'b' times the distance to Jack's house.\n", "\n", "The problem statement also provides one example input set with the expected output for\n", "those inputs. Let's\n", "express the example as code. Here are the inputs and the expected output for the example provided" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# coordinates of Kimberly's house\n", "(xk,yk) = (4,0)\n", "# coordinates of Bob's house\n", "(xb,yb) = (0,0)\n", "# coordinates of Jack's house\n", "(xj,yj) = (-2,-4)\n", "# coordinates of Janet's house\n", "(xn,yn) = (-2,-1)\n", "\n", "# the ratios\n", "(a,b) = (3,4)\n", "\n", "# expected solution \n", "(xsoln,ysoln) = (-2,0)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check that the solution actually solves the problem by calculating the distances between the solution point and the house locations. I will define the distance between points using the squared distance as an intermediate value because the squared distance function will be useful later." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def dist2(x0,y0,x1,y1):\n", " \"distance between (x0,y0) and (x1,y1) squared\"\n", " return (x0-x1)**2 + (y0-y1)**2\n", "\n", "# use the numpy sqrt function, because it is the most general\n", "import numpy as np\n", "\n", "def distance(x0,y0,x1,y1):\n", " \"distance between (x0,y0) and (x1,y1)\"\n", " return np.sqrt(dist2(x0,y0,x1,y1))\n", "\n", "BobDist = distance(xsoln, ysoln, xb,yb)\n", "KimDist = distance(xsoln, ysoln, xk,yk)\n", "print \"comparing Bob and Kim\", (BobDist, KimDist), \"equal?\", (BobDist*a, KimDist)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "comparing Bob and Kim (2.0, 6.0) equal? (6.0, 6.0)\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "So the distance to Bob and Kim satisfy the requested relationship." ] }, { "cell_type": "code", "collapsed": false, "input": [ "JackDist = distance(xsoln, ysoln, xj, yj)\n", "JanetDist = distance(xsoln, ysoln, xn, yn)\n", "print \"comparing Jack and Janet\", (JackDist, JanetDist), \"equal?\", (JanetDist*b, JackDist)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "comparing Jack and Janet (4.0, 1.0) equal? (4.0, 4.0)\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "... and the example solution checks out.\n", "\n", "Can we draw a diagram that illustrates the House Location Problem?\n", "How can we compute a solution location (xsoln,ysoln) from the input locations?\n", "\n", "Examining and Illustrating the problem using sympy and matplotlib\n", "=================================================================\n", "\n", "Evidently we need to find the set of points where JanetDist $\\times$ b == JackDist\n", "and intersect that set with the set\n", "of points where BobDist $\\times$ a == KimDist: the points in the intersection will\n", "solve the problem. How can we represent those sets?\n", "\n", "It turns out that we can represent each set as the set of pairs (x,y) that\n", "satisfy an algebraic equation. The symbolic mathematics library sympy can make\n", "creating and manipulating these equations easy. The following code creates\n", "an \"equation object\" representing the set of points where JackDist=JanetDist*b." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sympy as s\n", "\n", "# Let x and y represent variable symbolic values.\n", "x,y = s.symbols(\"x y\")\n", "\n", "# define symbolic expression objects for the squares of distances to Janet and Jacks house.\n", "JackDist2 = dist2(x, y, xj, yj)\n", "JanetDist2 = dist2(x, y, xn, yn)\n", "print \"JackDist2:\", JackDist2\n", "print \"JanetDist2:\", JanetDist2\n", "\n", "# Relate the squared distances in an equation (square b because the distances are squared).\n", "# JackDist2 == JanetDist2 * b**2\n", "Jack_Janet_shape = s.Eq( JackDist2, JanetDist2 * b**2 ) # Eq creates an equation object\n", "print \"Equation:\", Jack_Janet_shape" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "JackDist2: (x + 2)**2 + (y + 4)**2\n", "JanetDist2: (x + 2)**2 + (y + 1)**2\n", "Equation: (x + 2)**2 + (y + 4)**2 == 16*(x + 2)**2 + 16*(y + 1)**2\n" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "For people not used to symbolic computing these steps may be a little mind blowing.\n", "The above steps created symbolic expression objects JackDist2 and JanetDist2 \n", "which can be manipulated in Python\n", "much like numbers or arrays, and then created an equation object for expressing\n", "a relationship between the expressions. We avoid the need for square roots in the\n", "equation by using squared distances.\n", "\n", "The text representation for Jack_Janet_shape\n", "is a little hard to read. Let's use a trick to put it into more standard\n", "mathematical notation." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display, Math\n", "\n", "def showsym(expression):\n", " display(Math( s.latex(expression) ) )\n", " \n", "# format the Jack_Janet_shape equation object using standard LaTeX conventions.\n", "showsym(Jack_Janet_shape)" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\left(x + 2\\right)^{2} + \\left(y + 4\\right)^{2} = 16 \\left(x + 2\\right)^{2} + 16 \\left(y + 1\\right)^{2}$$" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sadly, by itself the Jack_Janet_shape equation does not really help me understand much about the set of\n", "points the right distance from Jack and Janet's house. \n", "I would really like to know what the set of points looks like on the XY plane.\n", "How can we plot the points?\n", "\n", "The sympy module has an automatic plot function, but I was not able to figure out how to\n", "get it to display visualizations the way I wanted (probably due to my ignorance), so I'll\n", "plot it myself.\n", "\n", "Solve and Lambdify\n", "==================\n", "\n", "First note that we can solve for x in terms of y in the Jack_Janet_shape equation using the \n", "sympy.solve\n", "function. This works well because the \n", "Jack_Janet_shape equation is not too complex. [In general the sympy.solve\n", "function may not work or may give horribly complex results for more difficult equations.]\n", "Below we use the sympy.solve function to solve the Jack_Janet_shape equation for y." ] }, { "cell_type": "code", "collapsed": false, "input": [ "ysolutions = s.solve(Jack_Janet_shape, y)\n", "ysolutions" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "[-sqrt(-25*x**2 - 100*x - 84)/5 - 4/5, sqrt(-25*x**2 - 100*x - 84)/5 - 4/5]" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "# Format the solutions using more conventional mathematical notation\n", "for soln in ysolutions:\n", " showsym(soln)" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$- \\frac{1}{5} \\sqrt{- 25 x^{2} - 100 x -84} - \\frac{4}{5}$$" ], "output_type": "display_data", "text": [ "" ] }, { "latex": [ "$$\\frac{1}{5} \\sqrt{- 25 x^{2} - 100 x -84} - \\frac{4}{5}$$" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we have 2 solutions that express y in terms of x and we would like to use these expressions to plot the (x,y) points that satisfy the Jack_Janet_shape equation.\n", "\n", "However, the symbolic\n", "solutions as shown are not suitable for actually computing y values from x values because they are symbolic expressions and not numerical functions. The sympy.lambdify function will convert a symbolic expression to a numerical function, but you have to be a bit careful with it as we will see.\n", "In particular this first attempt below doesn't work well:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# let's convert the first solution into a numeric function of x\n", "ysoln0 = ysolutions[0]\n", "yfunction0 = s.lambdify(x, ysoln0)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "# let's try out the yfunction0 on some values\n", "yfunction0(-1.5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 9, "text": [ "-1.4244997998398399" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "yfunction0(0)" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "math domain error", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0myfunction0\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/awatters/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/__init__.pyc\u001b[0m in \u001b[0;36m\u001b[0;34m(x)\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: math domain error" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "# Uh oh. Looks like the computation above tried to find the square root of a negative number.\n", "# You can't do that for real numbers. Maybe it will work for a complex number input?\n", "yfunction0(complex(0))" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can't convert complex to float", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Uh oh. Looks like the computation above tried to find the square root of a negative number.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# You can't do that for real numbers. Maybe it will work for a complex number input?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0myfunction0\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcomplex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/awatters/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/numpy/__init__.pyc\u001b[0m in \u001b[0;36m\u001b[0;34m(x)\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: can't convert complex to float" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As shown above the yfunction0 function is not defined at all values and will not work for complex\n", "numbers even though the algebraic expression ysoln0 is well defined for all complex numbers.\n", "The underlying problem is that lambdify used the standard math.sqrt function in the definition\n", "of yfunction0 and math.sqrt will not operate on complex numbers and will generate an error\n", "for negative numbers.\n", "\n", "To make things easy it is best to use a function which is defined everywhere even\n", "though we are not interested in function values that are not real numbers.\n", "\n", "We can create a different function that will work for negative arguments, \n", "complex number arguments and even array arguments too if we direct\n", "lambdify to use the numpy implementation of sqrt as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "yfunction0 = s.lambdify(x, ysoln0, modules=\"numpy\")\n", "# by using numpy.sqrt we have a yfunction0 that is defined for all reals (and all complexes)\n", "yfunction0( complex(0) )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 12, "text": [ "(-0.80000000000000004-1.8330302779823358j)" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have a well behaved function we can plot it as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Let's guess that some x values between -8 and 8 map to\n", "# interesting y values. Let's try 2000 of them.\n", "x_range = np.linspace(-8.0, 8.0, 2000)\n", "\n", "# Convert the x_range array to complex numbers (with 0 imaginary part) \n", "# so that roots of negative numbers work.\n", "x_range_complex = np.array(x_range, dtype=np.complex)\n", "\n", "# Compute the y values for the range all at once (array application)\n", "y_values = yfunction0( x_range_complex )\n", "\n", "# We only want to plot points for y_values which are \"real\"\n", "# -- that is, have imaginary part of 0.\n", "# The following finds the indices of y_values where the\n", "# imaginary part is 0.\n", "real_indices = np.where( np.imag(y_values)==0 )\n", "\n", "# Find the \"real\" y values.\n", "real_y = np.real( y_values[ real_indices ] )\n", "\n", "# Find the x values that correspond to the \"real\" y values.\n", "real_x = x_range[ real_indices ]\n", "\n", "# Plot the (x,y) pairs using matplotlib\n", "from matplotlib.pyplot import plot, text, axes, show\n", "plot(real_x, real_y)\n", "\n", "# For illustration also show where Jack and Janet's houses are by marking them J and N\n", "text(xj,yj, \"J\")\n", "text(xn,yn, \"N\")\n", "\n", "# put a reference line between the J and the N\n", "plot([xj, xn], [yj, yn])\n", "\n", "# Tell matplotlib to keep the axes equal (don't distort them to fit)\n", "axes().set_aspect(\"equal\", \"datalim\")\n", "\n", "# show the result\n", "show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEACAYAAABI5zaHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGHdJREFUeJzt3X9QlPeBx/HPA2pIK5oY42BOBL1iQIOAacCxJ91YFrF0\nJ545O96P5K7pESJXovFHcoYalYlkGpOqMV5ivGS8m6v2xkkZqV416HX1sEVBjGccrrV3UpPQTf1x\nIYhOK+lzfzwXmoYfrg/LPsuX92vmmWF3v/B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xsVEdHh3bu3Ol1pKjKzMxUdXW18vPzvY4SNYN9ASblfhNutGjLZImJiZKkzs5OdXR0KCEh\nweNE0eX3+xUXF6e4uDjNnTtXhw8f9jpSVKWnp2vKlClex4gaExZgUu5hCmfRlukqKiqUlJSkuro6\nrVixwus4ntm+fbsCgYDXMTCATFiA2ecipqFmqC/a6u34q6qqFAgEtH79elVUVKiiokJPPfWUNm7c\n6EHKgXOj45ekyspKJSYmauHChdGON+DCOX4MHpT7p9TW1vZ4/zvvvKNz584pKytLkvTee+/p3nvv\n1fHjxzVu3LhoRhxQvR3/p33uc5/TI488opKSkigkiq4bHf+OHTt04MABHTp0KEqJoiuc3/9Qcd99\n92nlypVdt8+cOaOioiIPE908pmXCcM899+iDDz7QuXPndO7cOU2YMEFNTU1GFfuNnD17VpIz575r\n1y4tWLDA40TRtX//fm3YsEE1NTVD7vWGzzLxr9bPGj16tCTnipmWlhbV1tYqLy/P41Q3h3J3YSh+\nIs+qVauUmZmpWbNmqbOz08gz976Ul5frypUrKigoUE5OjsrKyryOFFXV1dVKTk5WfX29iouLNW/e\nPK8jDbhNmzaptLRUBQUFKisrG3Qr61nEBAAG4swdAAxEuQOAgSh3ADAQ5Q4ABqLcAcBAlDsAGIhy\nBwADUe4AYKD/A+3wpqOHnDhHAAAAAElFTkSuQmCC\n", "text": [ "" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course this plot only shows half of the (x,y) pairs\n", "which satisfy the equation Jack_Janet_shape because there was another way to\n", "solve for y.\n", "\n", "But at this point we have the tools to build an illustration for the whole problem.\n", "\n", "The following functions automate the process we used to plot equation solutions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def xplot(x, xexpr, xrng):\n", " a = np.array(xrng, np.complex)\n", " f = s.lambdify(x, xexpr, modules=\"numpy\")\n", " fa = f(a)\n", " realrng = np.where(np.imag(fa)==0)\n", " realx = xrng[realrng]\n", " realy = np.real( fa[realrng] )\n", " return (realx, realy)\n", "\n", "def plot_x_solutions(x, y, equality, xrng):\n", " solns = s.solve(equality, y)\n", " for soln in solns:\n", " #print \"plot\", soln\n", " (xx,yy) = xplot(x, soln, xrng)\n", " plot(xx,yy)\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can create an equation for the points that are the right distance from Kim and Bob's house\n", "just like we did for Jack and Janet." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# define symbolic expression objects for the squares of distances to Kim and Bob's house.\n", "KimDist2 = dist2(x, y, xk, yk)\n", "BobDist2 = dist2(x, y, xb, yb)\n", "print \"KimDist2:\", KimDist2\n", "print \"BobDist2:\", BobDist2\n", "\n", "# Relate the squared distances in an equation (square 'a' because the distances are squared).\n", "# KimDist2 == BobDist2 * a**2\n", "Kim_Bob_shape = s.Eq( KimDist2, BobDist2 * a**2 ) # Eq creates an equation object\n", "showsym(Kim_Bob_shape)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "KimDist2: y**2 + (x - 4)**2\n", "BobDist2: x**2 + y**2\n" ] }, { "latex": [ "$$y^{2} + \\left(x -4\\right)^{2} = 9 x^{2} + 9 y^{2}$$" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following generates a diagram illustrating the House location problem for the example inputs." ] }, { "cell_type": "code", "collapsed": false, "input": [ "plot_x_solutions(x,y, Kim_Bob_shape, x_range)\n", "plot_x_solutions(x,y, Jack_Janet_shape, x_range)\n", "# mark the input points and put lines between them\n", "plot([xk,xb], [yk,yb])\n", "text(xk,yk, \"K\")\n", "text(xb,yb, \"B\")\n", "plot([xj,xn], [yj,yn])\n", "text(xj,yj, \"J\")\n", "text(xn,yn, \"N\")\n", "axes().set_aspect(\"equal\", \"datalim\")\n", "show()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAEAC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\n", "text": [ "" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It looks like the House Location problem is looking for the intersection of two ellipses.\n", "In fact the ellipses look like they might be circles. Are they?\n", "\n", "Also note that one of the intersections is at the solution offered for the example (x,y) = (-2, -1).\n", "\n", "Solving the problem by cheating\n", "===============================\n", "\n", "Now that we have equations for the two shapes, we can solve for the intersection points\n", "by simply letting the sympy solver magically do all the work for us." ] }, { "cell_type": "code", "collapsed": false, "input": [ "s.solve( [Kim_Bob_shape, Jack_Janet_shape], x, y)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 17, "text": [ "[(-2, 0), (-386/289, -360/289)]" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using sympy.solve works and it will work for any other set of input parameters, but\n", "it's a little disappointing because the black box solver doesn't give us any\n", "additional insight into the problem. It's also disappointing because this solution\n", "cannot be used to solve the HackerRank challenge since the HackerRank Python instances\n", "do not include the sympy package.\n", "\n", "I will leave the reader to find a solution that is acceptible to HackerRank.\n", "\n", "References\n", "==========\n", "\n", "The Enthought Canopy environment automatically includes all software used in this\n", "example. Get the free version of Canopy from Enthought here:\n", ".\n", " \n", "As mentioned above, the official statement of the House Location problem is\n", "here: .\n", " \n", "The official documentation for sympy is here: .\n", " \n", "The scipy and numpy documentation is here: ." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }