且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"只能将size-1数组转换为Python标量& quot;

更新时间:2022-04-19 03:29:53

您可以计算 y = math.sqrt(R ** 2-(x-cc)** 2) x 单个变量中,但是在您的代码中,您尝试计算表示 x array 每个元素的表达式(并获得一个数组结果).

You can compute y = math.sqrt(R**2 - (x - cc)**2) as long as x in a single variable, but in your code you attempt to compute this expression for each element of x array (and get an array of results).

为此,请按照下列步骤操作:

To to this, proceed as follows:

  1. 将表达式定义为函数:

  1. Define your expression as a function:

 def myFun(R, x, cc):
     return math.sqrt(R**2 - (x - cc)**2)

  • 定义此功能的矢量化版本:

     myFn = np.vectorize(myFun, excluded=['R', 'cc'])
    

  • y 计算为:

     y = myFn(R, x, cc)
    

  • 对于 R = 20.0 H = 30.0 x = np.linspace(-5,5,10)(较短的数组))我知道了:

    For R = 20.0, H = 30.0 and x = np.linspace(-5,5,10) (a shorter array) I got:

    array([ 8.22875656, 10.34341406, 11.99128261, 13.34639903, 14.49112624,
           15.47223243, 16.31925481, 17.05218586, 17.6852162 , 18.22875656])