ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 14 Sep 2020 03:55:50 +0200Triple integrals in a specific region of spacehttps://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.
In Mathematica:
reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
![mathematica-polyhedral-region-integrate-polynomial](/upfiles/16000472797455741.png)
Is there a way to easily perform this operation even in Sage?Sat, 12 Sep 2020 12:50:48 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/Comment by rburing for <p>Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.</p>
<p>In Mathematica:</p>
<pre><code>reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
</code></pre>
<p><img alt="mathematica-polyhedral-region-integrate-polynomial" src="/upfiles/16000472797455741.png"></p>
<p>Is there a way to easily perform this operation even in Sage?</p>
https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53427#post-id-53427Are the inequalities always polynomial? Probably you can use [cylindrical algebraic decomposition](https://mathworld.wolfram.com/CylindricalAlgebraicDecomposition.html).Sat, 12 Sep 2020 21:05:14 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53427#post-id-53427Comment by rburing for <p>Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.</p>
<p>In Mathematica:</p>
<pre><code>reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
</code></pre>
<p><img alt="mathematica-polyhedral-region-integrate-polynomial" src="/upfiles/16000472797455741.png"></p>
<p>Is there a way to easily perform this operation even in Sage?</p>
https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53420#post-id-53420Numerically or symbolically?Sat, 12 Sep 2020 13:17:33 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53420#post-id-53420Comment by Teo7 for <p>Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.</p>
<p>In Mathematica:</p>
<pre><code>reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
</code></pre>
<p><img alt="mathematica-polyhedral-region-integrate-polynomial" src="/upfiles/16000472797455741.png"></p>
<p>Is there a way to easily perform this operation even in Sage?</p>
https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53426#post-id-53426SymbolicallySat, 12 Sep 2020 20:03:25 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53426#post-id-53426Comment by slelievre for <p>Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.</p>
<p>In Mathematica:</p>
<pre><code>reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
</code></pre>
<p><img alt="mathematica-polyhedral-region-integrate-polynomial" src="/upfiles/16000472797455741.png"></p>
<p>Is there a way to easily perform this operation even in Sage?</p>
https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53446#post-id-53446Note: also asked on sage-support:
- [sage-support, 2020-09, Triple integrals in a specific region of space](https://groups.google.com/d/topic/sage-support/B2QH-4gozx0/discussion)Mon, 14 Sep 2020 03:23:47 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53446#post-id-53446Comment by slelievre for <p>Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.</p>
<p>In Mathematica:</p>
<pre><code>reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
</code></pre>
<p><img alt="mathematica-polyhedral-region-integrate-polynomial" src="/upfiles/16000472797455741.png"></p>
<p>Is there a way to easily perform this operation even in Sage?</p>
https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53450#post-id-53450Related questions:
- [Ask Sage question 44636: Integration on a region defined by an inequality](https://ask.sagemath.org/question/44636)
- [Ask Sage question 10834: Compute the volume of a cube region](https://ask.sagemath.org/question/10834)
- [Ask Sage question 10288: Integrate rational function over a polyhedral domain](https://ask.sagemath.org/question/10288)Mon, 14 Sep 2020 03:55:50 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?comment=53450#post-id-53450Answer by slelievre for <p>Can I perform a triple integral in a region of space I define?
I'm trying to migrate from Mathematica to Sage,
and in Mathematica I could go and define a region of space
(with various limitations) and then perform the integral
of a function on it.</p>
<p>In Mathematica:</p>
<pre><code>reg = ImplicitRegion[x + 2 y + 3 z < 2 && -1 < x < y < z < 1, {x, y, z}];
integral[{x, y, z} in reg, (x^2 + 2 y z)]
</code></pre>
<p><img alt="mathematica-polyhedral-region-integrate-polynomial" src="/upfiles/16000472797455741.png"></p>
<p>Is there a way to easily perform this operation even in Sage?</p>
https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?answer=53445#post-id-53445## Integrate a polynomial over a polyhedron
### Overview
The question is about integrating a polynomial
over a region defined by linear inequalities.
Such a region is called a polytope or a polyhedron.
In Sage one can construct such regions using the
`Polyhedron` class.
This class provides a method `integrate` which allows,
when the `latte_int` optional package is installed,
to integrate a polynomial function over a polyhedron.
In a way, the trick is: rather than start from the function
`integrate` and somehow specify a region, we fist define
a polyhedron and then use the method `integrate` to compute
the integral of a polynomial over that polyhedron.
### Steps
The region is defined by five inequalities:
x + 2 y + 3 z < 2
-1 < x
x < y
y < z
z < 1
and the function to integrate is
x^2 + 2 y z
The five inequalities can be rewritten as
2 + (-1) * x + (-2) * y + (-3) * z > 0
1 + (+1) * x > 0
0 + (-1) * x + (+1) * y > 0
0 + (-1) * y + (+1) * z > 0
1 + (-1) * z > 0
Encode these inequalities in Sage as
sage: ieqs = [[2, -1, -2, -3],
....: [1, 1, 0, 0],
....: [0, -1, 1, 0],
....: [0, 0, -1, 1],
....: [1, 0, 0, -1], ]
Create a polyhedron from those inequalities:
sage: P = Polyhedron(ieqs=ieqs)
Define polynomial variables and a polynomial:
sage: x, y, z = polygens(QQ, names='x, y, z')
sage: f = x^2 + 2*y*z
Integrate that polynomial over the polyhedron we defined:
sage: P.integrate(f)
53833/151875
This last command requires the `latte_int` optional package
for Sage to be installed.
### The `latte_int` optional package
To install that package, if you installed Sage from source
or from binaries downloaded from the Sage website, run the
following command in a terminal:
$ sage -i latte_int
### Documentation
- [SageMath documentation: `integrate` method for the `Polyhedron` class](https://doc.sagemath.org/html/en/reference/discrete_geometry/sage/geometry/polyhedron/base.html#sage.geometry.polyhedron.base.Polyhedron_base.integrate)
### Related
- [Sage Trac ticket 22824: Add "see also" section to "integrate over a polytope"](https://trac.sagemath.org/ticket/22824)Mon, 14 Sep 2020 03:20:10 +0200https://ask.sagemath.org/question/53419/triple-integrals-in-a-specific-region-of-space/?answer=53445#post-id-53445