Numpy Attribute Error : Float object has no attribtue 'arctan2'

Numpy Attribute Error : Float object has no attribtue 'arctan2'

我一直在尝试实现这段代码,但由于我是 numpy 的新手,所以我不太了解这个错误。

import numpy as np
from sympy import *
import matplotlib.pyplot as plt

def asSpherical(coord):
    x=coord[0]
    y=coord[1]
    z=coord[2]
    azimuth = np.arctan2(y,x)
    elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
    r = np.sqrt(x**2 + y**2 + z**2)
    return [azimuth, elevation, r]

def ecefToEnu(lamb, phi, coord_set):
    # @param1 is lattitude
    # @param2 is longitude
    # @param3 is coordinate set in form of a list

    # R matrix of size 3x3
    # This transforms the ECEF coordinates to ENU
    trans_matrix = [
        [-np.sin(lamb), np.cos(lamb), 0],
        [-(np.cos(lamb))*np.sin(phi), -
         np.sin(lamb)*np.sin(phi), np.cos(phi)],
        [np.cos(lamb)*np.cos(phi), np.sin(lamb)*np.cos(phi), sin(phi)]
    ]
    # Performs the matrix multiplication
    enu_coords = np.matmul(trans_matrix, coord_set)
    # Returns a list of Cartesian Coordinates
    sph_coords = asSpherical(enu_coords)
    return sph_coords


coord_list = []
print("Enter the latitude")
latitude = float(input())
print("Enter the coordinate list:")
print("Enter X:")
coord_list.append(float(input()))
print("Enter Y:")
coord_list.append(float(input()))
print("Enter Z:")
coord_list.append(float(input()))
phiDegSet = np.arange(-180, 180, 1)
Nset = len(phiDegSet)
t0 = np.linspace(0, 24, Nset)
t0 = list(t0)

res = []
for i in range(0, 360):
    temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
    res.append(temp_list)
elevDegSet = []
for i in res:
    elevDegSet.append(i[1])

这给我一个错误:

    elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
AttributeError: 'Float' object has no attribute 'arctan2'

我已经将值作为浮点数传递给函数。即使导入 numpy 也能正常工作。

我在 arctan 之前添加了一个 print 语句:

def asSpherical(coord):
    x=coord[0]
    y=coord[1]
    z=coord[2]
    print(x,y,z)
    azimuth = np.arctan2(y,x)
    ...

得到这个 运行:

....
ter the latitude
45
Enter the coordinate list:
Enter X:
45
Enter Y:
45
Enter Z:
45
-14.65116910723749 -76.54620448997258 -37.062720709188 - 45.0*sin(180)
Traceback (most recent call last):
  File "stack71078990.py", line 52, in <module>
    temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
  File "stack71078990.py", line 31, in ecefToEnu
    sph_coords = asSpherical(enu_coords)
  File "stack71078990.py", line 11, in asSpherical
    elevation = np.arctan2(z,np.sqrt(x**2 + y**2))
AttributeError: 'Add' object has no attribute 'arctan2'

z-37.062720709188 - 45.0*sin(180),其中 sin(180) 一个 sympy 表达式。

我在 trans_matrix 表达式中看到 sin(phi)。由于 from sympy import *sin(相对于 np.sin)是一个 sympy 函数。

删除该导入(正如我评论的那样)会引发:

Traceback (most recent call last):
  File "stack71078990.py", line 52, in <module>
    temp_list = ecefToEnu(latitude, phiDegSet[i], coord_list)
  File "stack71078990.py", line 26, in ecefToEnu
    [np.cos(lamb)*np.cos(phi), np.sin(lamb)*np.cos(phi), sin(phi)]
NameError: name 'sin' is not defined

将其更改为 np.sin(与定义中的其他函数一样),让脚本 运行 没有错误。