1 | initial version |
Staying with mpmath
... Avter evaluating :
reset()
import mpmath as mp
mp.dps = 15
f = 8500
rho = 1.225
c0 = 343
Omega = 2*mp.pi*f
k = Omega/c0
Z = -426
def Integrand(x, y):
# return a one-parameter anonymous function of kx at point x, y.
# This function returns an mpc object
return lambda kx: ((2*mp.sqrt(k**2 - kx**2)*Z)/(mp.sqrt(k**2 - kx**2)*Z + Omega*rho))*((mp.exp(1j*(kx*x + mp.sqrt(k**2 - kx**2)*y)))/(mp.sqrt(k**2 - kx**2)))
def integration(kx_interval, x, y):
# builds the one-parameter integrand and integrates them.
# Return the result as a Python complex
itg = Integrand(x, y)
rp = mp.quad(lambda kx:mp.re(itg(kx)), kx_interval)
ip = mp.quad(lambda kx:mp.im(itg(kx)), kx_interval)
return complex(rp + 1j*ip)
One gets :
sage: integration([-100*k, 100*k], 0, 0)
(356.37828188993564-16.743674967114078j)
Is that what you aimed at ?
Note that you do not need to separately integrate the real and imaginary parts :
# Shorter and cleaner
def integration1(kx_interval, x, y):
# builds the one-parameter integrands and integrates them.
# Return the result as a Python complex
return complex(mp.quad(Integrand(x, y), kx_interval))
sage: integration1([-100*k, 100*k], 0, 0)
(356.37828188993564-16.743674967114078j)
HTH,
2 | No.2 Revision |
Staying with mpmath
... Avter After evaluating :
reset()
import mpmath as mp
mp.dps = 15
f = 8500
rho = 1.225
c0 = 343
Omega = 2*mp.pi*f
k = Omega/c0
Z = -426
def Integrand(x, y):
# return a one-parameter anonymous function of kx at point x, y.
# This function returns an mpc object
return lambda kx: ((2*mp.sqrt(k**2 - kx**2)*Z)/(mp.sqrt(k**2 - kx**2)*Z + Omega*rho))*((mp.exp(1j*(kx*x + mp.sqrt(k**2 - kx**2)*y)))/(mp.sqrt(k**2 - kx**2)))
def integration(kx_interval, x, y):
# builds the one-parameter integrand and integrates them.
# Return the result as a Python complex
itg = Integrand(x, y)
rp = mp.quad(lambda kx:mp.re(itg(kx)), kx_interval)
ip = mp.quad(lambda kx:mp.im(itg(kx)), kx_interval)
return complex(rp + 1j*ip)
One one gets :
sage: integration([-100*k, 100*k], 0, 0)
(356.37828188993564-16.743674967114078j)
Is that what you aimed at ?
Note that you do not need to separately integrate the real and imaginary parts :
# Shorter and cleaner
def integration1(kx_interval, x, y):
# builds the one-parameter integrands and integrates them.
# Return the result as a Python complex
return complex(mp.quad(Integrand(x, y), kx_interval))
sage: integration1([-100*k, 100*k], 0, 0)
(356.37828188993564-16.743674967114078j)
HTH,
3 | No.3 Revision |
Staying with mpmath
... After evaluating :
reset()
import mpmath as mp
mp.dps = 15
f = 8500
rho = 1.225
c0 = 343
Omega = 2*mp.pi*f
k = Omega/c0
Z = -426
def Integrand(x, y):
# return a one-parameter anonymous function of kx at point x, y.
# This function returns an mpc object
return lambda kx: ((2*mp.sqrt(k**2 - kx**2)*Z)/(mp.sqrt(k**2 - kx**2)*Z + Omega*rho))*((mp.exp(1j*(kx*x + mp.sqrt(k**2 - kx**2)*y)))/(mp.sqrt(k**2 - kx**2)))
def integration(kx_interval, x, y):
# builds the one-parameter integrand and integrates them.
# Return the result as a Python complex
itg = Integrand(x, y)
rp = mp.quad(lambda kx:mp.re(itg(kx)), kx_interval)
ip = mp.quad(lambda kx:mp.im(itg(kx)), kx_interval)
return complex(rp + 1j*ip)
one gets :
sage: integration([-100*k, 100*k], 0, 0)
(356.37828188993564-16.743674967114078j)
Is that what you aimed at ?
Note that you do not need to separately integrate the real and imaginary parts :
# Shorter and cleaner
def integration1(kx_interval, x, y):
# builds the one-parameter integrands and integrates them.
integrand and integrate it.
# Return the result as a Python complex
return complex(mp.quad(Integrand(x, y), kx_interval))
sage: integration1([-100*k, 100*k], 0, 0)
(356.37828188993564-16.743674967114078j)
HTH,