2017-08-29 181 views
0

嗨我試圖調用一堆變量到函數中,但是numpy.nadarray錯誤一直在拋出。「numpy.ndarray is object is callable」error in initiated an function

功能的問題:

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) 
+0

也許你應該重命名你的返回變量'alpha'。 –

+0

將'return'變量改爲除函數名外的其他內容。當你返回alpha時,你不一定會返回你用'alpha = np.array(...)'創建的'alpha'數組的值,你將返回你使用'def alpha()創建的函數。 ..):' –

+0

@DanielF return語句與局部變量'alpha'處於相同的範圍。我真的不知道如何可以返回該功能。更不用說那個消息說'numpy.ndarray'不可調用。 –

回答

2

沒有錯誤,我用這個代碼和Python 3.6: 我拿出輸入,如果在每個函數開始處通過輸入擦除值,那麼將rotation_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") 
相關問題