Matt Brown
Matt Brown

Reputation: 309

Matrix in python containing cos and sin

I am a beginner in python. I have this matrix:

    Mat RX = (Mat_<double>(4, 4) <<
          1, 0,         0,          0,
          0, cos(roll), -sin(roll), 0,
          0, sin(roll), cos(roll),  0,
          0, 0,         0,          1);

How do I make this in python? It gives error when I try something like this:

    MatrixX = np.mat('1,2,3;cos(roll),6,7')

Upvotes: 0

Views: 1325

Answers (2)

Eric
Eric

Reputation: 97641

Don't use the string constructor to np.mat. it's primarily there to ease matlab users in, and only works in the simplest cases. It also creates a np.matrix, which is generally not recommended to use.

All you need here is:

np.array([
    [1, 2, 3],
    [4, 5, np.cos(roll)]
])

(line-wrapping optional)

Upvotes: 2

Stephen Rauch
Stephen Rauch

Reputation: 49812

If you intepret cos(), and then construct the string, this can work:

Code:

MatrixX = np.mat('1,2,3;%s,6,7' % cos(roll))

To Test:

>>> np.mat('1,2;3,%s' % math.cos(2))
matrix([[ 1.        ,  2.        ],
        [ 3.        , -0.41614684]])

Upvotes: 0

Related Questions