你能在python中对trig函数求和吗?例如,两次迭代的cos(2x)之和。谢谢!
def function_name(phi1, phi2, distance):
"""
phi_1 - List of first angles [radians]
phi_2 - List of second angles [radians]
distance - List of all distances
Note: All inputs must be the same length
Note: All inputs must be NumPy arrays!
"""
phi_1=[0.698132, 0.872665]
phi_2=[0.872665, 0.698132]
distance
sig_phi = 1
#Setup exponent array and get squared distance
exponents= np.ones_like(distance) * 2
dist_sq= np.power(distance,exponents)
mat1=[[np.sum(np.divide((1+np.cos(2*phi1)),(2*dist_sq))),-1.0 * np.sum(np.divide((np.sin(2*phi1)),(2*dist_sq)))], [np.sum(np.divide(1-np.cos(2*phi1)),(2*dist_sq)),-1.0 * np.sum(np.divide((np.sin(2*phi1)),(2*dist_sq)))]]
mat1 *= 1/sig2_phi**2
mat2=[[np.sum(np.divide((1+np.cos(2*phi2)),(2*dist_sq))),-1.0 * np.sum(np.divide((np.sin(2*phi2)),(2*dist_sq)))], [np.sum(np.divide(1-np.cos(2*phi2)),(2*dist_sq)),-1.0 * np.sum(np.divide((np.sin(2*phi2)),(2*dist_sq)))]]
mat2 *= 1/sig2_phi**2
return mat1,mat2
print (mat1, mat2, distance)
我想将距离保持为变量 - 所以我可以看到距离最大化矩阵的行列式。当我在终端中运行代码时,我没有得到任何结果。有谁知道我应该做什么?
答案 0 :(得分:0)
我不确定我是否完全理解这个问题,但当然每个单独的部分在Python中都很容易实现。
通过将sum()
函数与math
模块中提供的三角函数一起用于列表推导,它应该相对简单。对于你给出的例子,
from math import *
iters = [1, 2]
summation = sum(cos(2*x) for x in iters)
,应该做的伎俩。
此外,我建议您将来的问题更加全面。如果没有证据表明已经尝试过这个问题,那就不好提问。