首页 > 解决方案 > 为什么它说“ValueError:函数返回的数组在调用之间改变了大小”?

问题描述

我正在尝试计算积分,但我不明白错误输出,这是什么意思,有什么问题,我该如何解决?输出是“ValueError:函数返回的数组在调用之间改变了大小”,我添加了完整的输出。请帮忙,谢谢

import numpy as np
import matplotlib.pyplot as plt
import astropy.cosmology as cosmo
from astropy import units as u
from astropy import constants as const
import scipy.integrate as integrate
from scipy.optimize import fsolve
def delta_t(m_lens,z_lens,y_impact):
    return 4*const.G*const.c**-3*m_lens*const.M_sun*(1+z_lens)*(0.5*y_impact*np.sqrt(y_impact**2+4)+np.log((np.sqrt(y_impact**2+4)+y_impact)/(np.sqrt(y_impact**2+4)-y_impact)))
def y_max(r_max):
    return np.sqrt((1+r_max)/r_max**0.5-2)
def y_min(y,r_max,m_lens,z_lens,z_source,dt_min):                         # implicit function of y_min from eq (2) with delta_t_min=1ms
    y = np.linspace(0,r_max,100)
    return -dt_min*u.s+4*const.G*const.c**-3*m_lens*const.M_sun*(1+z_lens)*(0.5*y*np.sqrt(y**2+4)+np.log((np.sqrt(y**2+4)+y)/(np.sqrt(y**2+4)-y)))
def Y_min(r_max,m_lens,z_lens,z_source,dt_min):
    return fsolve(lambda y: delta_t(m_lens, z_lens, y) - dt_min*u.s, -10)[0]
print(Y_min(5,30,0.5,1,10**-3))
def cosmo_dependent_part(z_lens,z_source,f_dm):
    return 3/2*f_dm*0.24/const.c*((cosmo.WMAP9.H0*1000*u.meter/u.kilometer)**2*cosmo.WMAP9.angular_diameter_distance_z1z2(0,z_lens)*cosmo.WMAP9.angular_diameter_distance_z1z2(z_lens,z_source))/(cosmo.WMAP9.H(z_lens)*1000*u.meter/u.kilometer*cosmo.WMAP9.angular_diameter_distance_z1z2(0,z_source))
print(cosmo_dependent_part(0.5,1,1))
def optical_depth_fixed_source_integrand(z_source,z_lens,r_max,m_lens,f_dm,dt_min):
    z_lens = np.linspace(0,z_source,100)
    return cosmo_dependent_part(z_lens,z_source,f_dm)*(1+z_lens)**2*(y_max(r_max)**2-Y_min(r_max,m_lens,z_lens,z_source,dt_min)**2)
def optical_depth_fixed_source(z_source,z_lens,r_max,m_lens,f_dm,dt_min):
    z_lens = np.linspace(0,z_source,100)
    return integrate.trapz(optical_depth_fixed_source_integrand(z_source,z_lens,r_max,m_lens,f_dm,dt_min),z_lens)
print(optical_depth_fixed_source(1,0.5,5,30,1,10**-3))

这是输出

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-199-5b86909da8d5> in <module>
      5     z_lens = np.linspace(0,z_source,100)
      6     return integrate.trapz(optical_depth_fixed_source_integrand(z_source,z_lens,r_max,m_lens,f_dm,dt_min),z_lens)
----> 7 print(optical_depth_fixed_source(1,0.5,5,30,1,10**-3))             # z_source=1

<ipython-input-199-5b86909da8d5> in optical_depth_fixed_source(z_source, z_lens, r_max, m_lens, f_dm, dt_min)
      4 def optical_depth_fixed_source(z_source,z_lens,r_max,m_lens,f_dm,dt_min):
      5     z_lens = np.linspace(0,z_source,100)
----> 6     return integrate.trapz(optical_depth_fixed_source_integrand(z_source,z_lens,r_max,m_lens,f_dm,dt_min),z_lens)
      7 print(optical_depth_fixed_source(1,0.5,5,30,1,10**-3))             # z_source=1

<ipython-input-199-5b86909da8d5> in optical_depth_fixed_source_integrand(z_source, z_lens, r_max, m_lens, f_dm, dt_min)
      1 def optical_depth_fixed_source_integrand(z_source,z_lens,r_max,m_lens,f_dm,dt_min):
      2     z_lens = np.linspace(0,z_source,100)
----> 3     return cosmo_dependent_part(z_lens,z_source,f_dm)*(1+z_lens)**2*(y_max(r_max)**2-Y_min(r_max,m_lens,z_lens,z_source,dt_min)**2)
      4 def optical_depth_fixed_source(z_source,z_lens,r_max,m_lens,f_dm,dt_min):
      5     z_lens = np.linspace(0,z_source,100)

<ipython-input-197-ef9a0e8a24a2> in Y_min(r_max, m_lens, z_lens, z_source, dt_min)
     16 
     17 def Y_min(r_max,m_lens,z_lens,z_source,dt_min):
---> 18     return fsolve(lambda y: delta_t(m_lens, z_lens, y) - dt_min*u.s, -10)[0]
     19 print(Y_min(5,30,0.5,1,10**-3))

~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
    145                'diag': diag}
    146 
--> 147     res = _root_hybr(func, x0, args, jac=fprime, **options)
    148     if full_output:
    149         x = res['x']

~\anaconda3\lib\site-packages\scipy\optimize\minpack.py in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options)
    223             maxfev = 200 * (n + 1)
    224         retval = _minpack._hybrd(func, x0, args, 1, xtol, maxfev,
--> 225                                  ml, mu, epsfcn, factor, diag)
    226     else:
    227         _check_func('fsolve', 'fprime', Dfun, x0, args, n, (n, n))

ValueError: The array returned by a function changed size between calls

标签: pythonarraysintegrationastropy

解决方案


我改变了变量

光学深度固定源积分

以便它们包含函数 y_min 和 y_max 而不是它们的 vaviabes,现在它可以工作了

def optical_depth_fixed_source_integrand(z_source,z_lens,f_dm,y_min,y_max):
    z_lens = np.linspace(0,z_source,100)
    return cosmo_dependent_part(z_lens,z_source,f_dm)*(1+z_lens)**2*(y_max**2-y_min**2)

def optical_depth_fixed_source(z_source,z_lens,f_dm,y_min,y_max):
    z_lens = np.linspace(0,z_source,100)
    optical_depth_fixed_source = integrate.trapz(optical_depth_fixed_source_integrand(z_source,z_lens,f_dm,y_min,y_max),z_lens)
    return optical_depth_fixed_source
print(optical_depth_fixed_source(1,0.5,1,y_min(5,30,0.5,1e-3),y_max(5))) 

推荐阅读