使用np.ndarray的函数调用说“ TypeError:缺少1个必需的位置参数:“

时间:2019-07-07 04:11:50

标签: python python-3.x numpy numpy-ndarray

我正在编写代码以在Python 3.5.2上旋转矩阵(1x3)。

要旋转矩阵,我做了一个函数。(不是类)

def rotate_rpy(posvec: Vector, rotvec: Vector) -> Vector :
  return np.dot(np.dot(np.dot (posvec, rotate_rpy(rotvec[0]) ), rotate_pitch(rotvec[1]) ), rotate_yaw(rotvec[2]))

newpose = rotate_rpy(mypose, rotateang)#enbug

出现这样的错误

Traceback (most recent call last): 
  File "rotation_matrix.py", line 50, in <module>
    newpose = rotate_rpy(mypose, rotateang)#enbug
  File "rotation_matrix.py", line 35, in rotate_rpy
    return np.dot(np.dot(np.dot (posvec, rotate_rpy(rotvec[0]) ), rotate_pitch(rotvec[1]) ), rotate_yaw(rotvec[2]))
TypeError: rotate_rpy() missing 1 required positional argument: 'rotvec'

也许是一个愚蠢的问题,但我不明白这是什么问题。

Internet上大多数问题都说明了有关实例化的内容,但这只是一个功能。不上课。无论如何,我只是尝试更改参数:

newpose = rotate_rpy(mypose, mypose, rotateang)#enbug

然后

TypeError: rotate_rpy() takes 2 positional arguments but 3 were given

newpose = rotate_rpy(mypose)#enbug

然后

TypeError: rotate_rpy() missing 1 required positional argument: 'rotvec'

我相信它很傻,但是

newpose = rotate_rpy()#enbug

然后

TypeError: rotate_rpy() missing 2 required positional arguments: 'posvec' and 'rotvec'

我被认罪了

3 arg-arg过多

2个arg-1个arg丢失

1个arg-1个arg丢失

0个arg-2个arg丢失

不对应。我不知道。请帮助我...

下面的完整代码:

#coding:utf-8

import numpy as np

def rotate_roll (th):
  _rot_vec_roll = {
    { 1.,          0. ,         0.},
    { 0.,   np.cos(th), np.sin(th)},
    { 0., - np.sin(th), np.cos(th)}
  }
  return _rot_vec_roll

def rotate_pitch (th):
  _rot_vec_pitch = {
    {  np.cos(th),0. , np.sin(th)},
    {  0.,        1.,          0.},
    {- np.sin(th),1.,  np.cos(th)}
  }
  return _rot_vec_pitch

def rotate_yaw (th):
  _rot_vec_yaw = {
    {   np.cos(th), np.sin(th), 0.},
    { - np.sin(th), np.cos(th), 0.},
    {           0.,         0., 1.}
  }
  return _rot_vec_yaw

# R2 = A * R1 
# A = roll_vec * pitch_vec * yaw_vec

Vector = np.ndarray(shape=(1,3), dtype=np.float)

def rotate_rpy(posvec: Vector, rotvec: Vector) -> Vector :
  return np.dot(np.dot(np.dot (posvec, rotate_rpy(rotvec[0]) ), rotate_pitch(rotvec[1]) ), rotate_yaw(rotvec[2]))

mypose = np.ndarray(shape=(1,3), dtype=np.float)
mypose = np.array([3.0,1.0,1.0], dtype=float)

print(mypose)

base = np.pi / 6.0

rotateang = np.ndarray(shape=(1,3), dtype=np.float)
rotateang = np.array([base, base/2.0, base], dtype=float)

print(rotateang)

newpose = np.ndarray(shape=(1,3), dtype=np.float)
newpose = rotate_rpy(mypose, rotateang)#enbug
print(newpose);

以及下面的完整错误:

[ 3.  1.  1.]
[ 0.52359878  0.26179939  0.52359878]
Traceback (most recent call last):
  File "rotation_matrix.py", line 50, in <module>
    newpose = rotate_rpy(mypose, rotateang)#enbug
  File "rotation_matrix.py", line 35, in rotate_rpy
    return np.dot(np.dot(np.dot (posvec, rotate_rpy(rotvec[0]) ), rotate_pitch(rotvec[1]) ), rotate_yaw(rotvec[2]))
TypeError: rotate_rpy() missing 1 required positional argument: 'rotvec'

1 个答案:

答案 0 :(得分:1)

函数rotate_rpy被定义为无限递归地调用自身(这是一个问题):

def rotate_rpy(posvec, rotvec):
    return np.dot(np.dot(np.dot (posvec, rotate_rpy(rotvec[0]), ....)

它也只用一个参数来调用自己。 Python解释器无法通过幸运地报告第二个错误来捕获第一个错误。