numerical-integration

Volume of 3d shape using numerical integration with scipy

限于喜欢 提交于 2021-02-10 08:45:36
问题 I have written a function for computing volume of intersection of a cube and a half-space and now I'm writing tests for it. I've tried computing the volume numerically like this: integral = scipy.integrate.tplquad(lambda z, y, x: int(Vector(x, y, z).dot(normal) < distance), -0.5, 0.5, lambda x: -0.5, lambda x: 0.5, lambda x, y: -0.5, lambda x, y: 0.5, epsabs=1e-5, epsrel=1e-5) ... basically I integrate over the whole cube and each point gets value 1 or 0 based on if it is inside the half

How to do numerical integration with pytorch similar to numpy's trapz function?

ε祈祈猫儿з 提交于 2020-07-07 17:37:24
问题 Title says it all. Is there a convenient function in pytorch that can do something like np.trapz(y, x) (integrating over the the points in x and y via trapezoidal rule)? 回答1: There is no built-in tool for that, but it should not be difficult to implement it yourself, especially using the numpy code as a guideline. 回答2: Since pytorch version 1.2 a native trapz function has been introduced. 来源: https://stackoverflow.com/questions/55605577/how-to-do-numerical-integration-with-pytorch-similar-to

What would be the computationally faster way to implement this 2D numerical integration?

白昼怎懂夜的黑 提交于 2020-05-29 08:37:50
问题 I am interested in doing a 2D numerical integration. Right now I am using the scipy.integrate.dblquad but it is very slow. Please see the code below. My need is to evaluate this integral 100s of times with completely different parameters. Hence I want to make the processing as fast and efficient as possible. The code is: import numpy as np from scipy import integrate from scipy.special import erf from scipy.special import j0 import time q = np.linspace(0.03, 1.0, 1000) start = time.time() def

What would be the computationally faster way to implement this 2D numerical integration?

房东的猫 提交于 2020-05-29 08:36:20
问题 I am interested in doing a 2D numerical integration. Right now I am using the scipy.integrate.dblquad but it is very slow. Please see the code below. My need is to evaluate this integral 100s of times with completely different parameters. Hence I want to make the processing as fast and efficient as possible. The code is: import numpy as np from scipy import integrate from scipy.special import erf from scipy.special import j0 import time q = np.linspace(0.03, 1.0, 1000) start = time.time() def

Problems integrating using quadpy

冷暖自知 提交于 2020-05-09 15:54:35
问题 I am trying to use quadpy as I want to do 2D numerical vector integration for 2D integrals. To see how fast quadpy works, I wanted to test it and compare it with scipy 1D vector integration. Thus I wrote a simple code: import numpy as np from scipy import integrate from scipy.special import erf from scipy.special import j0 import quadpy q = np.linspace(0.03, 1.0, 500) def f(t): return t * 0.5 * (erf((t - 40) / 3) - 1) * j0(q * t) y, _ = integrate.quad_vec(f, 0, 50) y1, _ = quadpy.quad(f, 0,

python: integrating a piecewise function

℡╲_俬逩灬. 提交于 2020-01-24 07:09:47
问题 I want to integrate a piecewise a defined function that is multiplied by the Legendre polynomials. Unfortunately, I can't find how to use the nth Legendre polynomial of x in the documentation. I want to integrate each Legendre polynomial of x when n = 1,..., 50 so I have set n = np.arange(1, 51, 1) . import numpy as np import pylab from scipy import integrate n = np.arange(1, 51, 1) def f(x): if 0 <= x <= 1: return 1 if -1 <= x <= 0: return -1 I suppose I need to define another function let's

python: integrating a piecewise function

ぃ、小莉子 提交于 2020-01-24 07:09:06
问题 I want to integrate a piecewise a defined function that is multiplied by the Legendre polynomials. Unfortunately, I can't find how to use the nth Legendre polynomial of x in the documentation. I want to integrate each Legendre polynomial of x when n = 1,..., 50 so I have set n = np.arange(1, 51, 1) . import numpy as np import pylab from scipy import integrate n = np.arange(1, 51, 1) def f(x): if 0 <= x <= 1: return 1 if -1 <= x <= 0: return -1 I suppose I need to define another function let's