在Python中对多面体非矩形域进行积分

huangapple go评论62阅读模式
英文:

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:

在Python中对多面体非矩形域进行积分

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

积分的定义域由以下不等式集合定义:

在Python中对多面体非矩形域进行积分

它等价于:

在Python中对多面体非矩形域进行积分

这组不等式定义了一个凸多面体。
我们可以使用 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:

在Python中对多面体非矩形域进行积分

which is equivalent to:

在Python中对多面体非矩形域进行积分

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:

{
 &#39;integral&#39;: -5358.300000000005, 
 &#39;estAbsError&#39;: 1.1879700000000003e-08, 
 &#39;functionEvaluations&#39;: 330.0, 
 &#39;returnCode&#39;: 0, 
 &#39;message&#39;: &#39;OK&#39;
}

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).

huangapple
  • 本文由 发表于 2023年5月11日 13:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定