Python Reason for why certain elements come out as arrays and scalars

Viewed 62

I'm running a code which basically rotates an large data array (50M Lines) around 3 dimensions. However I have run into a strange problem which I have narrowed down to how the rotation matrices are evaluated. Basically, for any rotation other than around the x-axis, the python code hangs forever (can't even hotkey out of it), i.e, when I attempt to do a large matrix multiplication with np.dot using y/z rotation and data.T.

Strangely, it is absolutely fine when I use the x rotation and it computes fine. Evaluating the form of the matrices separately, it looks like under some certain unknown condition, the y and z rotation matrices return arrays instead of a scalar and I suspect this is interfering with numpy's method of np.dot.

While I can just float all the elements in the rotation matrix and it runs, but I would like to determine the source of this problem. I have attached a sample code adapted from my original code that reproduces this effect. The first set of print produces rotation matrices with scalar elements. The second set of print statements produces matrices where the x_rotation has scalar elements, but y,z rotation has an 1x1 arrays for non-zero elements

import numpy as np

def x_rot(a):
        return np.array([[1,0,0],[0,np.cos(a),-np.sin(a)],[0,np.sin(a),np.cos(a)]])

def y_rot(a):
        return np.array([[np.cos(a),0,-np.sin(a)],[0,1,0],[np.sin(a),0,np.cos(a)]])

def z_rot(a):
        return np.array([[np.cos(a),-np.sin(a),0],[np.sin(a),np.cos(a),0],[0,0,1]])

ang1 = np.random.choice([-1.,1.],1)*((np.pi/4.*np.random.random()) + np.pi/6.)

print x_rot(0.1)
print y_rot(0.1)
print z_rot(0.1)

print y_rot(ang1)
print x_rot(ang1)
print z_rot(ang1)

Thanks in advance

2 Answers
Related