英文:
Integral on a polyhedral non-rectangular domain with Python
问题
You can evaluate the triple integral in Python using the scipy
library. Here's the Python code for the triple integral:
import scipy.integrate as spi
def f(x, y, z):
return x + y * z
result, error = spi.nquad(f, [[-5, 4], [-5, 3], lambda x, y: -10 + x + y, 6 - x - y])
This code uses the nquad
function from scipy.integrate
to perform the triple integral. The result
variable will contain the value of the integral, and the error
variable will contain the error estimate.
英文:
You are a Python user and you want to evaluate this triple integral:
for a certain function f
, say f(x,y,z) = x + y*z
.
In R, a possibility is to nest one-dimensional integrals:
f <- function(x,y,z) x + y*z
integrate(Vectorize(function(x) {
integrate(Vectorize(function(y) {
integrate(function(z) {
f(x, y, z)
}, -10, 6 - x - y)$value
}), -5, 3 - x)$value
}), -5, 4)
## -5358.3 with absolute error < 9.5e-11
The point I don't like with this method is that it does not take into account
the error estimates on the first two integrals.
I'm interested in the Python way to realize this method.
But I'm also interested in a Python way to get a result with a more reliable error estimate.
答案1
得分: 0
为了解决这个问题,我们将使用四个库:
```python
import numpy as np
import pypoman
from scipy.spatial import Delaunay
from pysimplicialcubature.simplicialcubature import integrateOnSimplex
积分的定义域由以下不等式集合定义:
它等价于:
这组不等式定义了一个凸多面体。
我们可以使用 pypoman 包(或者我们也可以使用 pycddlib,但这会更冗长)获得这个多面体的顶点:
A = np.array([
[-1, 0, 0], # -x
[ 1, 0, 0], # x
[ 0,-1, 0], # -y
[ 1, 1, 0], # x+y
[ 0, 0,-1], # -z
[ 1, 1, 1] # x+y+z
])
b = np.array([5, 4, 5, 3, 10, 6])
vertices = pypoman.compute_polytope_vertices(A, b)
现在我们得到了这个凸多面体的顶点,我们使用 Delaunay 算法将其分解为四面体:
dlnay = Delaunay(vertices)
tetrahedra = np.asarray(vertices)[dlnay.simplices]
四面体是一个三维单纯形。所以剩下的就是应用 pysimplicialcubature 库来计算积分:
# 要积分的函数
f = lambda x : x[0] + x[1] * x[2]
# 在四面体上的积分
I_f = integrateOnSimplex(f, tetrahedra)
print(I_f)
我们得到:
{
'integral': -5358.300000000005,
'estAbsError': 1.1879700000000003e-08,
'functionEvaluations': 330.0,
'returnCode': 0,
'message': 'OK'
}
实际上,我们的函数 f
是一个多元多项式。在这种情况下,使用 pysimplicialcubature,可以获得积分的精确值(函数 integratePolynomialOnSimplex
)。
<details>
<summary>英文:</summary>
To give a solution to this problem, we will use four libraries:
```python
import numpy as np
import pypoman
from scipy.spatial import Delaunay
from pysimplicialcubature.simplicialcubature import integrateOnSimplex
The domain of integration is defined by the set of inequalities:
which is equivalent to:
This set of inequalities defines a convex polyhedron.
We can get the vertices of this polyhedron with the pypoman package (or
we could use pycddlib but this would be more lengthy):
A = np.array([
[-1, 0, 0], # -x
[ 1, 0, 0], # x
[ 0,-1, 0], # -y
[ 1, 1, 0], # x+y
[ 0, 0,-1], # -z
[ 1, 1, 1] # x+y+z
])
b = np.array([5, 4, 5, 3, 10, 6])
vertices = pypoman.compute_polytope_vertices(A, b)
Now that we get the vertices of this convex polytope, we decompose it into
tetrahedra with the help of the Delaunay algorithm:
dlnay = Delaunay(vertices)
tetrahedra = np.asarray(vertices)[dlnay.simplices]
A tetrahedron is a three-dimensional simplex. So it remains to apply the
pysimplicialcubature library to evaluate the integral:
# function to integrate
f = lambda x : x[0] + x[1] * x[2]
# integral of f on the tetrahedra
I_f = integrateOnSimplex(f, tetrahedra)
print(I_f)
We get:
{
'integral': -5358.300000000005,
'estAbsError': 1.1879700000000003e-08,
'functionEvaluations': 330.0,
'returnCode': 0,
'message': 'OK'
}
Actually our function f
is a multivariate polynomial. In this case, with
pysimplicialcubature, it is possible to get the exact value of the integral
(function integratePolynomialOnSimplex
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论