# [Numpy-discussion] Faster way to generate a rotation matrix?

Jonathan Taylor jonathan.taylor@utoronto...
Tue Mar 3 22:41:37 CST 2009

```Thanks,  All these things make sense and I should have known to
calculate the sins and cosines up front.  I managed a few more
"tricks" and knocked off 40% of the computation time:

def rotation(theta, R = np.zeros((3,3))):
cx,cy,cz = np.cos(theta)
sx,sy,sz = np.sin(theta)
R.flat = (cx*cz - sx*cy*sz, cx*sz + sx*cy*cz, sx*sy,
-sx*cz - cx*cy*sz, -sx*sz + cx*cy*cz,
cx*sy, sy*sz, -sy*cz, cy)
return R

Pretty evil looking ;) but still wouldn't mind somehow getting it faster.

Am I right in thinking that I wouldn't get much of a speedup by
rewriting this in C as most of the time is spent in necessary python
functions?

Thanks again,
Jon.

On Tue, Mar 3, 2009 at 8:15 PM, Chris Colbert <sccolbert@gmail.com> wrote:
> sorry, i meant you're making 12 calls, not 16...
>
> Chris
>
> On Tue, Mar 3, 2009 at 8:14 PM, Chris Colbert <sccolbert@gmail.com> wrote:
>>
>> In addition to what Robert said, you also only need to calculate six
>> transcendentals:
>>
>> cx = cos(tx)
>> sx = sin(tx)
>> cy = cos(ty)
>> sy = sin(ty)
>> cz = cos(tz)
>> sz = sin(tz)
>>
>> you, are making sixteen transcendental calls in your loop each time.
>>
>> I can also recommend Chapter 2 of Introduction to Robotics: Mechanics and
>> Controls by John J. Craig for more on more efficient transformations.
>>
>>
>>
>>
>>
>> On Tue, Mar 3, 2009 at 7:19 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>>
>>> On Tue, Mar 3, 2009 at 17:53, Jonathan Taylor
>>> <jonathan.taylor@utoronto.ca> wrote:
>>> > Sorry.. obviously having some copy and paste trouble here.  The
>>> > message should be as follows:
>>> >
>>> > Hi,
>>> >
>>> > I am doing optimization on a vector of rotation angles tx,ty and tz
>>> > using scipy.optimize.fmin.  Unfortunately the function that I am
>>> > optimizing needs the rotation matrix corresponding to this vector so
>>> > it is getting constructed once for each iteration with new values.
>>> > >From profiling I can see that the function I am using to construct
>>> > this rotation matrix is a bottleneck.  I am currently using:
>>> >
>>> > def rotation(theta):
>>> >   tx,ty,tz = theta
>>> >
>>> >   Rx = np.array([[1,0,0], [0, cos(tx), -sin(tx)], [0, sin(tx),
>>> > cos(tx)]])
>>> >   Ry = np.array([[cos(ty), 0, -sin(ty)], [0, 1, 0], [sin(ty), 0,
>>> > cos(ty)]])
>>> >   Rz = np.array([[cos(tz), -sin(tz), 0], [sin(tz), cos(tz), 0],
>>> > [0,0,1]])
>>> >
>>> >   return np.dot(Rx, np.dot(Ry, Rz))
>>> >
>>> > Is there a faster way to do this?  Perhaps I can do this faster with a
>>> > small cython module, but this might be overkill?
>>>
>>> You could look up to the full form of the rotation matrix in terms of
>>> the angles, or use sympy to do the same. The latter might be more
>>> convenient given that the reference you find might be using a
>>> different convention for the angles. James Diebel's "Representing
>>> Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors" is a
>>> nice, comprehensive reference for such formulae.
>>>
>>>
>>> http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=5F5145BE25D61F87478B25AD1493C8F4?doi=10.1.1.110.5134&rep=rep1&type=pdf&ei=QcetSefqF4GEsQPnx4jSBA&sig2=HjJILSBPFgJTfuifbvKrxw&usg=AFQjCNFbABIxusr-NEbgrinhtR6buvjaYA
>>>
>>> --
>>> Robert Kern
>>>
>>> "I have come to believe that the whole world is an enigma, a harmless
>>> enigma that is made terrible by our own mad attempt to interpret it as
>>> though it had an underlying truth."
>>>  -- Umberto Eco
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion@scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
```

More information about the Numpy-discussion mailing list