0

Hi I'm trying to call a bunch of variables into a function, but the numpy.nadarray error keeps getting thrown.

Function in question :

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

This is me calling the function (which throws an error)

alpha_110 = alpha("x3",45)
4
  • maybe you should rename your returned variable alpha. Commented Aug 29, 2017 at 11:25
  • Change the return variable to something other than the name of the function. When you return alpha you're not necessarily returning the value of the alpha array you create with alpha = np.array(...), you're returning the function you created with def alpha(...): Commented Aug 29, 2017 at 13:14
  • @DanielF The return statement is in the same scope as the local variable alpha. I don't really see how that could return the function. Not to mention that the message says that numpy.ndarray is not callable. Commented Aug 29, 2017 at 13:20
  • Are you assigning alpha as an array elsewhere in your program? Commented Aug 29, 2017 at 13:25

1 Answer 1

2

No error for me with this code and python 3.6 : I took out the input, it's quite useless to have rotate_axis and angle as parameters if you erase the value by an input at every function beginning.

# -*-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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.