{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook we plot the [permutahedron](https://en.wikipedia.org/wiki/Permutohedron) of order 4 which is a 3-dimensional polyhedron living in a 4-dimensional space.\n", "It is defined as the convex hull of all permutations of $(0, 1, 2, 3)$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "V-representation Polyhedra.PointsHull{Int64,Array{Int64,1},Int64}:\n", "24-element iterator of Array{Int64,1}:\n", " [0, 1, 2, 3]\n", " [0, 1, 3, 2]\n", " [0, 2, 1, 3]\n", " [0, 2, 3, 1]\n", " [0, 3, 1, 2]\n", " [0, 3, 2, 1]\n", " [1, 0, 2, 3]\n", " [1, 0, 3, 2]\n", " [1, 2, 0, 3]\n", " [1, 2, 3, 0]\n", " [1, 3, 0, 2]\n", " [1, 3, 2, 0]\n", " [2, 0, 1, 3]\n", " [2, 0, 3, 1]\n", " [2, 1, 0, 3]\n", " [2, 1, 3, 0]\n", " [2, 3, 0, 1]\n", " [2, 3, 1, 0]\n", " [3, 0, 1, 2]\n", " [3, 0, 2, 1]\n", " [3, 1, 0, 2]\n", " [3, 1, 2, 0]\n", " [3, 2, 0, 1]\n", " [3, 2, 1, 0]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Combinatorics, Polyhedra\n", "v = vrep(collect(permutations([0, 1, 2, 3])))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To plot a polyhedron, we need both the H-representation and V-representation so we will need a library to do representation conversion.\n", "We choose [CDD](https://github.com/JuliaPolyhedra/CDDLib.jl) in floating point arithmetic." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Polyhedron CDDLib.Polyhedron{Float64}:\n", "24-element iterator of Array{Float64,1}:\n", " [0.0, 1.0, 2.0, 3.0]\n", " [0.0, 1.0, 3.0, 2.0]\n", " [0.0, 2.0, 1.0, 3.0]\n", " [0.0, 2.0, 3.0, 1.0]\n", " [0.0, 3.0, 1.0, 2.0]\n", " [0.0, 3.0, 2.0, 1.0]\n", " [1.0, 0.0, 2.0, 3.0]\n", " [1.0, 0.0, 3.0, 2.0]\n", " [1.0, 2.0, 0.0, 3.0]\n", " [1.0, 2.0, 3.0, 0.0]\n", " [1.0, 3.0, 0.0, 2.0]\n", " [1.0, 3.0, 2.0, 0.0]\n", " [2.0, 0.0, 1.0, 3.0]\n", " [2.0, 0.0, 3.0, 1.0]\n", " [2.0, 1.0, 0.0, 3.0]\n", " [2.0, 1.0, 3.0, 0.0]\n", " [2.0, 3.0, 0.0, 1.0]\n", " [2.0, 3.0, 1.0, 0.0]\n", " [3.0, 0.0, 1.0, 2.0]\n", " [3.0, 0.0, 2.0, 1.0]\n", " [3.0, 1.0, 0.0, 2.0]\n", " [3.0, 1.0, 2.0, 0.0]\n", " [3.0, 2.0, 0.0, 1.0]\n", " [3.0, 2.0, 1.0, 0.0]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using CDDLib\n", "p4 = polyhedron(v, CDDLib.Library())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The permutahedron lives in a 4-dimension space but is 3-dimensional as it is contained in the hyperplane $x_1 + x_2 + x_3 + x_4 = 0 + 1 + 2 + 3 = 6$.\n", "We choose an orthogonal basis of this hyperplane: $(1, -1, 0, 0)$, $(1, 1, -2, 0)$ and $(1, 1, 1, -3)$." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "v1 = [1, -1, 0, 0]\n", "v2 = [1, 1, -2, 0]\n", "v3 = [1, 1, 1, -3];" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We project the polyhedron in this basis to obtain a full dimensional 3-dimensional polyhedron living in a 3-dimensional space." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Polyhedron CDDLib.Polyhedron{Float64}:\n", "14-element iterator of HalfSpace{Float64,Array{Float64,1}}:\n", " HalfSpace([-0.0, 3.26599, -1.1547], 6.0)\n", " HalfSpace([1.41421, 0.816497, -1.1547], 3.9999999999999996)\n", " HalfSpace([-0.0, -0.0, -1.1547], 2.0)\n", " HalfSpace([-1.41421, 0.816497, -1.1547], 3.999999999999999)\n", " HalfSpace([-2.82843, -1.63299, -1.1547], 6.0)\n", " HalfSpace([-0.0, -1.63299, -1.1547], 4.000000000000001)\n", " HalfSpace([-1.41421, -0.816497, 1.1547], 4.000000000000002)\n", " HalfSpace([-0.0, -3.26599, 1.1547], 6.000000000000011)\n", " HalfSpace([-2.82843, 1.63299, 1.1547], 5.999999999999995)\n", " HalfSpace([2.82843, -1.63299, -1.1547], 6.0)\n", " HalfSpace([1.41421, -0.816497, 1.1547], 4.0000000000000036)\n", " HalfSpace([-0.0, -0.0, 1.1547], 2.0)\n", " HalfSpace([-0.0, 1.63299, 1.1547], 3.9999999999999964)\n", " HalfSpace([2.82843, 1.63299, 1.1547], 5.9999999999999964)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p3 = project(p4, [v1 v2 v3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a plottable object, we transform the polyhedron into a mesh as follows." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "m = Polyhedra.Mesh(p3);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now plot this mesh with [MeshCat](https://github.com/rdeits/MeshCat.jl) or [Makie](https://github.com/JuliaPlots/Makie.jl) as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using MeshCat\n", "vis = Visualizer()\n", "setobject!(vis, m)\n", "IJuliaCell(vis)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Makie\n", "mesh(m, color=:blue)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "using Makie\n", "wireframe(m)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.0.1", "language": "julia", "name": "julia-1.0" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.0.1" } }, "nbformat": 4, "nbformat_minor": 2 }