编辑:我在python 3.7中有一些代码,如下所示。这意味着代码在python 2上,但我想将其还原为Python 3.7.4。有导入其他库,我的代码包含一些类:
编辑:
import core
from numpy import *
from scipy.special import sph_jn, sph_jnyn, lpmn
from scipy.misc.common import factorial
#from IPython.Debugger import Tracer; debug_here =
Tracer()
#from scipy.misc import derivative
from numdifftools import Derivative
from doc_inherit import doc_inherit
...
class Lab:
"""Laboratory class"""
def __init__(self, particle, alpha, m1=1):
self.particle = particle.copy()
self.alpha = alpha
self.m1 = m1
self.k1 = self.k0 * m1
...
def __get_amplitude_matrix2(self, c_sca, Theta, phi):
#debug_here()
n = len(c_sca[0])
ms = len(c_sca)
P = core.get_Pmn(ms - 1, n, cos(self.alpha + Theta))
PnT = P[:, 0, 1, 1:]
l = arange(1, n + 1)
A1 = sum(1j ** (-l) * c_sca[0] * PnT, axis=1) #axes: 0-Theta, 1-l
A2 = zeros_like(A1)
sinT = sin(self.alpha + Theta)
cosT = cos(self.alpha + Theta)
for m in xrange(1, ms):
PnT = P[:, 0, m, m:]
PdnT = P[:, 1, m, m:]
l = arange(m, n + 1)
a = c_sca[m][:n - m + 1]
b = c_sca[m][n - m + 1:]
A1 -= sinT * cos(m * phi)\
* sum(1j ** (-l + 1) * (a * PnT + 1j * b * PdnT), axis=1)
#A2 += m*sin(m*phi)/sinT\
# *sum(1j**(-l)*b*Pnt, axis=1)
A2 += m * sin(m * phi) * cosT\
* sum(((l - m) * 1j ** (-l)) * b * P[:, 0, m - 1, m:], axis=1)
A2 += m * sin(m * phi)\
* sum(((l + m) * 1j ** (-l)) * b * P[:, 0, m - 1, m - 1:size(P, 3) - 1], axis=1)
# PnT/sinT = null when SinT==0
# to solve this we can use:
# sqrt(1-x**2)P^m_l(x)
# = (l-m)xP^{m-1)_l(x) - (l+m)P^{m-1}_{l-1}(x)
# Here x will be cos(theta), sqrt(1-x**2)=cos(theta)
return A1, A2
当我编译时出现此错误:
sum() takes no keyword arguments
此行代码:
A1 = sum(1j ** (-l) * c_sca[0] * PnT, axis=1) #axes: 0-Theta, 1-l
那我应该怎么做,并简要说明错误的含义?
任何帮助将不胜感激。
答案 0 :(得分:0)
使用xrange
表示这是为Py2编写的,未经修改。 sum
错误看起来像是使用Python sum
,而不是numpy
。但事实是它已经超过了先前的范围行,这表明import numpy as *
是有效的。如果可能的话,我将代码重写为Py3兼容的,并使用import numpy as np
。在上述代码中将sum
更改为np.sum
时,它会起作用。