user2840327
user2840327

Reputation: 47

TypeError: on my python Rotation Matrix

I want to setup a rotation matrix in python where I can rotate my vectors by 5 degrees:

import math

angle = math.radians(5)
#define sine
sine = math.sin
#define cosine
cosine = math.cos
#rotation matrix
rotMatrix = array([[cosine(angle), -sine(angle)],
                  [sine(angle),  cosine(angle)]])

however when I run my program i get an error at my rotMatrix

TypeError: must be unicode character, not list

It errors on the following line:

rotMatrix = array([[cosine(angle), -sine(angle)],
                  [sine(angle),  cosine(angle)]])

I am not sure what i am doing wrong to get this error?

Upvotes: 0

Views: 389

Answers (2)

Christian Tapia
Christian Tapia

Reputation: 34146

Just change:

sin = math.sin(angle)
cos = math.cos(angle)

to:

sin = math.sin
cos = math.cos

The error was that you were trying to call sin(...) after you have declared sin as a float (math.sin(angle) returns a float)

Edit

I will recommend you using numpy.array instead of array.array because it seems that creating an array of lists is not possible.

Or simpler, use a list of lists like:

rotMatrix = [[a, b, c],
             [d, e, f],
             [g, h, i]]

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251383

You assigned the name sin to math.sin(angle). sin is now a number, the sine of 5 degrees. When you try to call sin(angle), you are trying to call a number. The same is true for cos. Change your matrix to use sin and cos instead of sin(angle) and cos(angle). Or, probably a better idea, name those variables something like sinA and cosA. It's confusing to have a function called sin and a variable called sin that is the sine of a particular angle.

Upvotes: 2

Related Questions