本文介绍了为什么说"ValueError:函数返回的数组在两次调用之间改变了大小"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算积分,但我不理解错误输出,它是什么意思,有什么问题以及如何解决?输出为"ValueError:函数返回的数组在调用之间改变了大小",我添加了完整的输出.请帮助,谢谢

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

这是输出

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

推荐答案

我更改了

以使它们包括函数y_min和y_max而不是其vaviabes,现在它可以正常工作

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

这篇关于为什么说"ValueError:函数返回的数组在两次调用之间改变了大小"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:15