why does it say “ValueError: The array returned by a function changed size between calls”?

流过昼夜 提交于 2020-11-30 01:43:25

问题


I'm trying to calculate an integral and I don't understand the error output, what does it mean, what's the problem and how do I fix it? the output is "ValueError: The array returned by a function changed size between calls", I added the full output. pleas help, thank you

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

Here is the output

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

回答1:


I changed the variables of

optical_depth_fixed_source_integrand

so that they include the functions y_min and y_max instead of their vaviabes and now it works

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


来源:https://stackoverflow.com/questions/64703856/why-does-it-say-valueerror-the-array-returned-by-a-function-changed-size-betwe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!