" numpy.ndarray是对象不可调用的#34;启动函数时出错

时间:2017-08-29 10:30:35

标签: python numpy

您好我试图将一堆变量调用到一个函数中,但是numpy.nadarray错误会一直被抛出。

有问题的功能:

def alpha(rotate_axis,angle):
    rotate_axis = (input("What the is the axis of rotation? x1, x2 or x3 "))
    angle = int(input("What is the angle of rotation in degrees?"))
    #transforming deg -> rad
    angle_r = radians(angle[0])
    cos = np.cos(angle_r)
    sin = np.sin(angle_r)

    if rotate_axis == "x1":
        rotation = np.array([[1,0,0],[0,cos,-sin],[0,sin,cos]])
    elif rotate_axis == "x2":
        rotation = np.array([[cos, 0, sin], [0, 1, 0], [-sin,0, cos]])
    elif rotate_axis == "x3":
        rotation = np.array([[cos, -sin, 0], [sin, cos, 0], [0, 0, 1]])

    #print("Direction cosines:",rotation)

    #producing alpha matrix
    #decomposing rotation consines
    a11 = rotation[0][0]
    a12 = rotation[0][1]
    a13 = rotation[0][2]
    a21 = rotation[1][0]
    a22 = rotation[1][1]
    a23 = rotation[1][2]
    a31 = rotation[2][0]
    a32 = rotation[2][1]
    a33 = rotation[2][2]
    alpha = np.array([[ a11**2, a12**2, a13**2, 2*a12*a13, 2*a13*a11, 2*a11*a12],
                  [ a21**2, a22**2, a23**2, 2*a22*a23, 2*a23*a21, 2*a21*a22],
                  [ a31**2, a32**2, a33**2, 2*a32*a33, 2*a33*a31, 2*a31*a32],
                  [ a21*a31, a22*a32, a23*a33, a22*a33 + a23*a32, a21*a33 + a23*a31, a22*a31 + a21*a32],
                  [ a31*a11, a32*a12, a33*a13, a12*a33 + a13*a32, a13*a31 + a11*a33, a11*a32 + a12*a31],
                  [ a11*a21, a12*a22, a23*a33, a12*a23 + a13*a32, a13*a21 + a11*a23, a11*a22 + a12*a21],
                ])
    return alpha

这是我调用函数(抛出错误)

alpha_110 = alpha("x3",45)

1 个答案:

答案 0 :(得分:2)

使用此代码和python 3.6没有错误: 我取出了输入,如果你在每个函数开始时通过输入擦除值,那么将rotate_axis和angle作为参数是没用的。

# -*-coding:Utf-8 -*

# Import des packages
import numpy as np
from math import radians
import os

def alpha(rotate_axis,angle):

    #transforming deg -> rad
    angle_r = radians(angle)
    cos = np.cos(angle_r)
    sin = np.sin(angle_r)

    if rotate_axis == "x1":
        rotation = np.array([[1,0,0],[0,cos,-sin],[0,sin,cos]])
    elif rotate_axis == "x2":
        rotation = np.array([[cos, 0, sin], [0, 1, 0], [-sin,0, cos]])
    elif rotate_axis == "x3":
        rotation = np.array([[cos, -sin, 0], [sin, cos, 0], [0, 0, 1]])

    #print("Direction cosines:",rotation)

    #producing alpha matrix
    #decomposing rotation consines
    a11 = rotation[0][0]
    a12 = rotation[0][1]
    a13 = rotation[0][2]
    a21 = rotation[1][0]
    a22 = rotation[1][1]
    a23 = rotation[1][2]
    a31 = rotation[2][0]
    a32 = rotation[2][1]
    a33 = rotation[2][2]
    alpha = np.array([[ a11**2, a12**2, a13**2, 2*a12*a13, 2*a13*a11, 2*a11*a12],
                  [ a21**2, a22**2, a23**2, 2*a22*a23, 2*a23*a21, 2*a21*a22],
                  [ a31**2, a32**2, a33**2, 2*a32*a33, 2*a33*a31, 2*a31*a32],
                  [ a21*a31, a22*a32, a23*a33, a22*a33 + a23*a32, a21*a33 + a23*a31, a22*a31 + a21*a32],
                  [ a31*a11, a32*a12, a33*a13, a12*a33 + a13*a32, a13*a31 + a11*a33, a11*a32 + a12*a31],
                  [ a11*a21, a12*a22, a23*a33, a12*a23 + a13*a32, a13*a21 + a11*a23, a11*a22 + a12*a21],
                ])
    return alpha

alpha_110 = alpha("x3",45)
print (alpha_110)
os.system("pause")