Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of polynomials over 3-Polytopes #13082

Merged
merged 7 commits into from Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
234 changes: 191 additions & 43 deletions examples/notebooks/IntegrationOverPolytopes.ipynb
Expand Up @@ -20,7 +20,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -43,25 +43,30 @@
"collapsed": true
},
"source": [
"### polytope_integrate(poly, expr, **kwargs) :\n",
" Pre-processes the input data for integrating univariate/bivariate polynomials over 2-Polytopes.\n",
"### polytope_integrate(poly, expr, **kwargs)\n",
" Integrates polynomials over 2/3-Polytopes.\n",
"\n",
" This function accepts the polytope in `poly` and the function in `expr` (uni/bi/trivariate polynomials are\n",
" implemented) and returns the exact integral of `expr` over `poly`.\n",
" \n",
" poly(Polygon) : 2-Polytope\n",
" expr(SymPy expression) : uni/bi-variate polynomial\n",
" Parameters\n",
" ---------------------------------------\n",
" 1. poly(Polygon) : 2/3-Polytope\n",
" 2. expr(SymPy expression) : uni/bi-variate polynomial for 2-Polytope and uni/bi/tri-variate for 3-Polytope\n",
" \n",
" Optional Parameters\n",
" clockwise(Boolean) : If user is not sure about orientation of vertices and wants to clockwise sort the points.\n",
" max_degree(Integer) : Maximum degree of any monomial of the input polynomial.\n",
" ---------------------------------------\n",
" 1. clockwise(Boolean) : If user is not sure about orientation of vertices of the 2-Polytope and wants\n",
" to clockwise sort the points.\n",
" 2. max_degree(Integer) : Maximum degree of any monomial of the input polynomial. This would require \n",
" \n",
" #### Examples :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"triangle = Polygon(Point(0,0), Point(1,1), Point(1,0))\n",
Expand All @@ -84,14 +89,164 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### integration_reduction(facets, index, a, b, expr, dims, degree) :\n",
" facets : List of facets that decide the region enclose by 2-Polytope.\n",
" index : The index of the facet with respect to which the integral is supposed to be found.\n",
" a, b : Hyperplane parameters corresponding to facets.\n",
" expr : Uni/Bi-variate Polynomial\n",
" dims : List of symbols denoting axes\n",
" degree : Degree of the homogeneous polynoimal(expr)\n",
"### main_integrate3d(expr, facets, vertices, hp_params)\n",
" Function to translate the problem of integrating uni/bi/tri-variate\n",
" polynomials over a 3-Polytope to integrating over its faces.\n",
" This is done using Generalized Stokes's Theorem and Euler's Theorem.\n",
" \n",
" Parameters\n",
" ------------------\n",
" 1. expr : The input polynomial\n",
" 2. facets : Faces of the 3-Polytope(expressed as indices of `vertices`)\n",
" 3. vertices : Vertices that constitute the Polytope\n",
" 4. hp_params : Hyperplane Parameters of the facets\n",
" \n",
" #### Examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cube = [[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)],\n",
" [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0], [3, 1, 0, 2], [0, 4, 6, 2]]\n",
"vertices = cube[0]\n",
"faces = cube[1:]\n",
"hp_params = hyperplane_parameters(faces, vertices)\n",
"main_integrate3d(1, faces, vertices, hp_params)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### polygon_integrate(facet, index, facets, vertices, expr, degree)\n",
" Helper function to integrate the input uni/bi/trivariate polynomial\n",
" over a certain face of the 3-Polytope.\n",
" \n",
" Parameters\n",
" ------------------\n",
" facet : Particular face of the 3-Polytope over which `expr` is integrated\n",
" index : The index of `facet` in `facets`\n",
" facets : Faces of the 3-Polytope(expressed as indices of `vertices`)\n",
" vertices : Vertices that constitute the facet\n",
" expr : The input polynomial\n",
" degree : Degree of `expr`\n",
" \n",
" #### Examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cube = [[(0, 0, 0), (0, 0, 5), (0, 5, 0), (0, 5, 5), (5, 0, 0),\n",
" (5, 0, 5), (5, 5, 0), (5, 5, 5)],\n",
" [2, 6, 7, 3], [3, 7, 5, 1], [7, 6, 4, 5], [1, 5, 4, 0],\n",
" [3, 1, 0, 2], [0, 4, 6, 2]]\n",
"facet = cube[1]\n",
"facets = cube[1:]\n",
"vertices = cube[0]\n",
"print(\"Area of polygon < [(0, 5, 0), (5, 5, 0), (5, 5, 5), (0, 5, 5)] > : \", polygon_integrate(facet, 0, facets, vertices, 1, 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### distance_to_side(point, line_seg)\n",
"\n",
" Helper function to compute the distance between given 3D point and\n",
" a line segment.\n",
"\n",
" Parameters\n",
" -----------------\n",
" point : 3D Point\n",
" line_seg : Line Segment\n",
" \n",
"#### Examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"point = (0, 0, 0)\n",
"distance_to_side(point, [(0, 0, 1), (0, 1, 0)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### lineseg_integrate(polygon, index, line_seg, expr, degree)\n",
" Helper function to compute the line integral of `expr` over `line_seg`\n",
"\n",
" Parameters\n",
" -------------\n",
" polygon : Face of a 3-Polytope\n",
" index : index of line_seg in polygon\n",
" line_seg : Line Segment\n",
"#### Examples :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"polygon = [(0, 5, 0), (5, 5, 0), (5, 5, 5), (0, 5, 5)]\n",
"line_seg = [(0, 5, 0), (5, 5, 0)]\n",
"print(lineseg_integrate(polygon, 0, line_seg, 1, 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### main_integrate(expr, facets, hp_params, max_degree=None)\n",
"\n",
" Function to translate the problem of integrating univariate/bivariate\n",
" polynomials over a 2-Polytope to integrating over its boundary facets.\n",
" This is done using Generalized Stokes's Theorem and Euler's Theorem.\n",
"\n",
" Parameters\n",
" --------------------\n",
" expr : The input polynomial\n",
" facets : Facets(Line Segments) of the 2-Polytope\n",
" hp_params : Hyperplane Parameters of the facets\n",
"\n",
" Optional Parameters:\n",
" --------------------\n",
" max_degree : The maximum degree of any monomial of the input polynomial.\n",
" \n",
"#### Examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triangle = Polygon(Point(0, 3), Point(5, 3), Point(1, 1))\n",
"facets = triangle.sides\n",
"hp_params = hyperplane_parameters(triangle)\n",
"print(main_integrate(x**2 + y**2, facets, hp_params))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### integration_reduction(facets, index, a, b, expr, dims, degree)\n",
" This is a helper function for polytope_integrate. It relates the result of the integral of a polynomial over a\n",
" d-dimensional entity to the result of the same integral of that polynomial over the (d - 1)-dimensional \n",
" facet[index].\n",
Expand All @@ -103,15 +258,22 @@
" this function is a helper one and works for a facet which bounds the polytope(i.e. the intersection point with the\n",
" other facets is required), not for an independent line.\n",
" \n",
" Parameters\n",
" ------------------\n",
" facets : List of facets that decide the region enclose by 2-Polytope.\n",
" index : The index of the facet with respect to which the integral is supposed to be found.\n",
" a, b : Hyperplane parameters corresponding to facets.\n",
" expr : Uni/Bi-variate Polynomial\n",
" dims : List of symbols denoting axes\n",
" degree : Degree of the homogeneous polynoimal(expr)\n",
" \n",
" #### Examples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"facets = [Segment2D(Point(0, 0), Point(1, 1)), Segment2D(Point(1, 1), Point(1, 0)), Segment2D(Point(0, 0), Point(1, 0))]\n",
Expand All @@ -136,9 +298,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"triangle = Polygon(Point(0,0), Point(1,1), Point(1,0))\n",
Expand All @@ -164,9 +324,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"print(\"Best origin for x**3*y on x + y = 3 : \", best_origin((1,1), 3, Segment2D(Point(0, 3), Point(3, 0)), x**3*y))\n",
Expand All @@ -190,9 +348,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"print(decompose(1 + x + x**2 + x*y))\n",
Expand All @@ -216,9 +372,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"print(norm((1, 2)))\n",
Expand All @@ -243,9 +397,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"print(intersection(Segment2D(Point(0, 0), Point(2, 2)), Segment2D(Point(1, 0), Point(0, 1))))\n",
Expand All @@ -268,7 +420,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -295,9 +447,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"hexagon = Polygon(Point(0, 0), Point(-sqrt(3) / 2, 0.5),\n",
Expand Down Expand Up @@ -326,9 +476,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"expr = x**2\n",
Expand Down Expand Up @@ -364,7 +512,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.5.2"
}
},
"nbformat": 4,
Expand Down