繁体   English   中英

初始化函数时出现“ numpy.ndarray是对象不可调用”错误

[英]“ numpy.ndarray is object is not callable ” error in initiating a function

嗨,我试图将一堆变量调用到一个函数中,但是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)

使用此代码和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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM