0

The following class method has been giving me trouble:

def align(self,array,a):
   aligned=np.zeros(array.shape)
   b=[0,0,1]
   v=np.cross(a,b)
   c=np.dot(a,b)
   I=np.eye(3,3)
   vx=np.zeros((3,3))
   vx=[[0,-v[2],v[1]],[v[2],0,-v[0]],[-v[1],v[0],0]]
   rotmatrix=I + vx + (vx @ vx)/(1+c)
   aligned = rotmatrix @ array.T
   return aligned

The error message I get is:

TypeError: unsupported operand type(s) for +: 'float' and 'list'

I am certain that the operation that is breaking is my matrix multiplication (vx @ vx) after investigating each component of the rotation matrix.

To generate the vx matrix, I have tried a variety of different things in numpy such as np.asarray, np.vstack, np.array with and without initialized an array of zeros. When printing out vx, depending on what I have tried it is either an array of lists, or a regular python list of lists. It never has the correct numpy array formatting. The only way I have found to get the array right is by assigning each index of the array to the correct value, which works but I have a hard time believing that is the only way to do this.

4
  • 1
    You assign vx twice, once as a (3,3) array, and again as a list. You try the same with aligned. In Python we don't 'declare' a variable type. We make an object and assign it to a variable. If we assign another object to a variable, the initial assignment is lost. Commented Feb 10, 2020 at 18:00
  • Yeah I know that initializing a variable is unneccessary in python, however I have had issues in the past that were resolved by initializing arrays/variables so that's why I tried that Commented Feb 10, 2020 at 18:40
  • You only need to initialize arrays if you are going to modify them. Initializing an array is quite different from initializing a variable. In any case, the issue here is that the 2nd vx assignment is a list, not an array. Commented Feb 10, 2020 at 18:44
  • Ah I see, that did it! Thank you, I was unaware it could create this problem Commented Feb 10, 2020 at 18:53

1 Answer 1

1

How about a classical way vx = np.array([[0,-v[2],v[1]],[v[2],0,-v[0]],[-v[1],v[0],0]])?

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to mention that was the first thing I tried. I am baffled as to why that returns an array of lists

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.