Weird result in setting column values of a numpy array

83 views Asked by At

In the script below, I want to apply a rotation matrix to the first two columns of a (Nx3) array.

rotate_mat = lambda theta: np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta),np.cos(theta)]])

rot_mat = rotate_mat(np.deg2rad(90))

basis1 = np.array([[i+1,j+1,k+1] for k in range(3) for j in range(3) for i in range(3)])
basis2 = basis1.copy()
rot = basis2[:,0:2] @ rot_mat

print('rot','\n',rot[:3],'\n')
print('basis2','\n',basis2[:3],'\n')

basis2[:,0:2] = rot
print('basis2 after','\n',basis2[:3])

after I ran this script, I obtained this output

rot 
 [[ 1. -1.]
 [ 1. -2.]
 [ 1. -3.]] 

basis2 
 [[1 1 1]
 [2 1 1]
 [3 1 1]] 

basis2 after 
 [[ 1  0  1]
 [ 1 -2  1]
 [ 1 -3  1]]

as you can see by basis2[:,0:2] = rot, the first row of basis2 is [1,0,1], however, the first row of rot is clearly [1,-1], where does this 0 come from?

1

There are 1 answers

4
flawr On BEST ANSWER

If you take a look at the entries of rot you will see that rot[0,1] is -0.9999999999999999. Furthermore basis2.dtype == dtype('int32'). So during the assignment the new entries get converted to int32 which rounds them towards zero. You can verify that

np.int32(rot[0, 1]) == 0

and

np.int32(rot[0, 1] - 1e-16) == -1

This is due to rounding, as np.cos(np.deg2rad(90)) == 6.123233995736766e-17, when you'd probably expect it to be exactly 0.